tags:

views:

700

answers:

2

Hi,

I'm running a program that does processing on a file. I want to be able to supply the program with several files, and by attaching to it with gdb, I want to get a memory dump at a certain point in the code for each of the files. I want the dump for each file to go to a file with the same filename as the input file (maybe after formatting it a little, say adding a suffix)

So suppose I have a function called HereIsTheFileName(char* filename), and another function called DumpThisMemoryRegion(void* startAddr, void* endAddr), I want to do something like the following:

To get the file name to an environment variable:

  • break HereIsTheFileName
  • commands 1
  • set $filename = malloc(strlen(filename) + 1)
  • call memcpy($filename, filename, strlen(filename) + 1)
  • end

Then to dump the memory to the filename I saved earlier:

  • break DumpThisMemoryRegion
  • commands 2
  • append binary memory "%s.memory"%$filename startAddr endAddr
  • end

(I would even settle for the filename as it is, without formatting, if that turns out to be the difficult part)

However, I couldn't get gdb to accept anything except an exlicit file name for the append/dump commands. when I ran "append binary memory $filename ..." I got the output in the file "/workdir/$filename". Is there any way to make gdb choose the file name at runtime?

Thanks!

A: 

I don't know how to make append accept a runtime filename, but you can always cheat a bit by writing the whole thing to a file and then sourcing that file, using logging.

By putting this in your ~/.gdbinit

define reallyappend
  printf "using gdbtmp.log to dump memory to file %s\n", $arg0
  set logging file gdbtmp.log
  set logging overwrite on
  set logging redirect on
  set logging on
  printf "append binary memory %s 0x%x 0x%x", $arg0, $arg1, $arg2
  set logging off
  set logging redirect off
  set logging overwrite off
  source gdbtmp.log
end

you can use the function reallyappend instead, for example with

(gdb) set $filename = "somethingruntimegenerated"
(gdb) reallyappend $filename startAddr endAddr

I don't know if logging works ok in an "commands" environment, but you can give it a shot at least.

Olof
A: 

Yeah, you can't use a variable here for the filename argument. The best suggestion I can offer is to write a script that will set all the breakpoints and set up the "append" commands, and use text editing or awk and sed to set up the filenames in the script.

Michael Snyder