0

So I wanted to print all the network dump I log as readable data. I have successfully mapped the logged dump into their respective structures (using memcpy) and now wish for the same class values to be printed (without defining a print() function per se).

Using the Qt debugger (which uses gdb if i remember correctly), I can use breakpoints to see and even copy the values into a file. Considering the application can take some time to process and convert the dump into readable data, is it possible for me to automate the system of copying the debug info into a file?

I have gone through the gdb documentation and couldn't locate any function for the same. Most of the solutions online include creating a print() function for each class.

How the question is different from How to get all properties/variables of a class at runtime/dynamically in C++: The question asks how to get variable data type @ runtime. I am on the other hand defining the data type during compilation (in a *.h header file containing all the structure info and even mapping all the values to the variable). The problem is largely revolving around the fact that the data can be viewed for printing via a debugger; I wish to automate the display and copying of the same onto a file / console.

Community
  • 1
  • 1
Ankit
  • 23
  • 1
  • 9
  • Possible duplicate of [How to get all properties/variables of a class at runtime/dynamically in C++](http://stackoverflow.com/questions/4017708/how-to-get-all-properties-variables-of-a-class-at-runtime-dynamically-in-c) – jpo38 Feb 18 '17 at 07:53
  • See linked post above. As there is no way to get list of attributes from a class, there will be no way to print them! – jpo38 Feb 18 '17 at 07:54
  • You are looking for serialisation. Have a look at http://www.boost.org/doc/libs/1_63_0/libs/serialization/doc/. Be warned that this is not at all comparable to C# or Java, which have reflection built into the language. – Christian Hackl Feb 18 '17 at 12:54
  • boost::serialization requires a dedicated function which is equivalent (in capacity) to a print function, which the OP doesn't want. – Kunal Tyagi Feb 19 '17 at 06:14
  • @ChristianHackl thanks for the answer. Seems something like what I was looking for (I haven't tried implementing it yet). – Ankit Feb 20 '17 at 04:54

1 Answers1

-1

Dumping the raw object is as easy as getting a char pointer to the object and printing the data as long as the size of the object. It won't obviously be able to access data stored in pointers, only their address.

Since you are ready to take memcpy output as the format, here's some code for the same:

struct large_object fi;
...
char* pt = (char*)&fi;

for (size_t i = 0; i < sizeof(fi); ++i)
{
    printf("%1x", *(pt + i));
}
printf("\n");

If required, you can get the bit-by-bit output also, on adding another loop:

    for (int bit_to_print = sizeof(char)-1; bit_to_print < 0; --bit_to_print)
    {
        printf("%d", *(pt + i) & (1 << bit_to_print);
    }

PS: The inner loop would not be the same on a small-endian and big-endian systems.

Kunal Tyagi
  • 549
  • 4
  • 12