This is the mail archive of the gdb@sourceware.cygnus.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Dumping memory to file


Hello again,

I solved it by adding a command, savemem, which does just that. It's
most likely not a very good solution (it's my fist gdb hack), but I'm
attaching the diff anyway, in case someone is interested.

/Dan

Dan Hovang wrote:
> 
> I've got an embedded system running eCos which I'm debugging using
> gdb over a serial line. I'm looking for ways to save blocks of
> memory to disk in the good old brutal fashion, for example, after
> hitting some breakpoing typing:
> (gdb) save *0x0400 *0x07e8 "screendump"

--- ../printcmd.c	Wed Oct  6 08:01:40 1999
+++ printcmd.c	Sat Jan 15 00:33:20 2000
@@ -132,6 +132,8 @@
 
 static void disassemble_command PARAMS ((char *, int));
 
+static void savemem_command PARAMS ((char *, int));
+
 static void printf_command PARAMS ((char *, int));
 
 static void print_frame_nameless_args PARAMS ((struct frame_info *,
long,
@@ -1977,6 +1979,94 @@
     }
 }
 
+/* Save a chunk of memory to file */
+static void savemem_command(arg, from_tty)
+	 char *arg;
+	 int from_tty;
+{
+  const int fname_buf_size = 128;
+  int i;
+  char *s = arg;
+  char *s_;
+  char fname[fname_buf_size];
+  CORE_ADDR from, to;
+  FILE *f;
+
+  if(!s)
+	error("need arguments.");
+
+  /* Fetch the from address */
+
+  while(*s == ' ')
+	s++;
+  s_ = s;
+  while(*s != ' ' && s)
+	s++;
+  if(!(*s))
+	error("need more arguments.");
+  *s++ = 0;
+  from = parse_and_eval_address(s_);
+
+  /* Fetch the to address */
+
+  while(*s == ' ')
+	s++;
+  s_ = s;
+  while(*s != ' ' && s)
+	s++;
+  if(!(*s))
+	error("need more arguments.");
+  *s++ = 0;
+  to = parse_and_eval_address(s_);
+
+  /* Fetch the filename */
+
+  while(*s == ' ')
+	s++;
+
+  if(*s != '"')
+	error("filename must be enclosed in \"\"");
+  s++;
+
+  i = 0;
+  while(*s != '"' && i < fname_buf_size && s)
+	fname[i++] = *s++;
+
+  if(*s != '"')
+	error("filename must be enclosed in \"\"");
+  fname[i] = 0; /* Null-terminate filename */
+
+  printf_filtered("save from ");
+  print_address_numeric(from, 1, gdb_stdout);
+  printf_filtered(" to ");
+  print_address_numeric(to, 1, gdb_stdout);
+  printf_filtered(" in file %s\n", fname);
+
+  if(f = fopen(fname, "wb")) {
+	int so_far = 0, left, l;
+	if(to < from) { /* Swap to/from if in wrong order */
+	  CORE_ADDR t;
+	  t = from;
+	  from = to;
+	  to = t;
+	}
+	left = to - from;
+
+	while(so_far < (to - from)) {
+	  if(left > fname_buf_size)
+		l = fname_buf_size;
+	  else
+		l = left;
+	  read_memory(from + so_far, fname, l);
+	  fwrite(fname, l, 1, f);
+	  so_far += l;
+	}
+	fclose(f);
+  } else
+	error("failed to open output file.");
+
+}
+
 /* ARGSUSED */
 static void
 printf_command (arg, from_tty)
@@ -2548,6 +2638,10 @@
   add_com ("inspect", class_vars, inspect_command,
 "Same as \"print\" command, except that if you are running in the
epoch\n\
 environment, the value is printed in its own window.");
+
+  add_com ("savemem", class_vars, savemem_command,
+"Save a chunk of memory to file.\nsavemem from to \"filename\".");
+
 
   add_show_from_set (
       add_set_cmd ("max-symbolic-offset", no_class, var_uinteger,

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]