Gdb, dump memory, saving formatted output to a file

I have buggy (leaked memory) software. As evidence, I have 1 GB of core.dump file. The heap size is 900 MB, so obviously something allocates but does not free memory.

So, I have a memory area so that is the case.

(gdb) x/50000s 0x200000000 

However, it is difficult to guess only with the naked eye which object or structure is not released. My idea is to keep track of: "Save the output in gdb format to a file and run the template to see which magic line works best." So here is my question:

How to save the output of the following command to a text file so that I can write an analyzer?

 (gdb) x/10000000s 0x20000000 <-- I need this output into a file 

Thanks for any help.

+7
source share
3 answers

You can use the dump function for gdb, see https://sourceware.org/gdb/onlinedocs/gdb/Dump_002fRestore-Files.html

In your example:

 dump binary memory result.bin 0x200000000 0x20000c350 

This will give you a simple binary file dump int result.bin . You can also use the following to reset it in hexadecimal format:

 dump ihex memory result.bin 0x200000000 0x20000c350 

Using the dump command is much clearer than using gdb logging hack (which didn't even work for me in any way).

+16
source

How to save the output of the following command to a text file so that I can write an analyzer?

  (gdb) x/10000000s 0x20000000 

This is actually quite simple:

 (gdb) set height 0 # prevent GDB from stopping every screenfull (gdb) set logging on # GDB output is now also copied into gdb.txt (gdb) x/10000000s 0x20000000 (gdb) quit 

Voila, enjoy your gdb.txt to gdb.txt .

I have a buggy (leaked memory) software .... "Save gdb formatted output to a file and run pattern matching to see which magic string is best suited."

This idea is unlikely to give satisfactory results. Consider:

 void some_function() { std::vector<string> *v = new std::vector<string>(); // code to insert and use 1000s of strings into "v". return; // Oops: forgot to delete "v". } 

Even if you can effectively β€œsee the magic line that fits best”, you will find that you leak all the lines; but this is not a problem, the problem "v" is a problem.

So, you really want to build a graph in which the selected regions point to other selected areas, and find the "root" of this graph. This is almost impossible to do manually.

So, which is more likely to help you find a memory leak? Fortunately, there are many tools that can solve this problem for you:

+7
source

you can write simple lkm, it will do

 lkm: #include <linux/kernel.h> #include <linux/module.h> int *ptr=(int*)0Xc18251c0; //the address you want to read from kernel space int module_i(void) { printk("%d\n",*ptr); } module_init(module_i); 

and the data will be displayed in the log so write

 enter code here dmesg 
0
source

All Articles