title: Dumping memory in gdb - how to choose the file name at run time
body: <p>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)</p>
  
  <p>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:</p>
  
  <p>To get the file name to an environment variable:</p>
  
  <ul>
  <li>break HereIsTheFileName</li>
  <li>commands 1</li>
  <li>set $filename = malloc(strlen(filename) + 1) </li>
  <li>call memcpy($filename, filename, strlen(filename) + 1) </li>
  <li>end</li>
  </ul>
  
  <p>Then to dump the memory to the filename I saved earlier:</p>
  
  <ul>
  <li>break DumpThisMemoryRegion</li>
  <li>commands 2</li>
  <li>append binary memory "%s.memory"%$filename startAddr endAddr</li>
  <li>end</li>
  </ul>
  
  <p>(I would even settle for the filename as it is, without formatting, if that turns out to be the difficult part)</p>
  
  <p>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?</p>
  
  <p>Thanks!</p>