This is the mail archive of the gdb-patches@sources.redhat.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]
Other format: [Raw text]

Proposed gdb patch



Dear GDBers -

     Here is a patch to gdb 5.1.1 that adds functionality to the gdb command
language.  It implements the "edit" command featured in Sun's dbx, which
invokes your favorite file editor on the active line of the program being
debugged.  ChangeLog entry and patches follow.

                                       George

----------

2002-04-26  George Helffrich <george@gly.bris.ac.uk>

        * cli/cli-cmds.c, defs.h, source.c, gdb.1: Add edit command.  This
	command mimics the behavior of Sun dbx, and invokes an editor at
	the current line number.  Editor comes from EDITOR environment
	variable, or defaults to /bin/ex.

----------

Index: gdb-5.1.1/src/gdb/ChangeLog
===================================================================
RCS file: /usr/src/local/cvsroot/gdb-5.1.1/src/gdb/ChangeLog,v
retrieving revision 1.5
retrieving revision 1.6
diff -c -r1.5 -r1.6
*** gdb-5.1.1/src/gdb/ChangeLog	2002/05/25 20:27:13	1.5
--- gdb-5.1.1/src/gdb/ChangeLog	2002/05/26 12:12:25	1.6
***************
*** 1,3 ****
--- 1,10 ----
+ 2002-04-26  George Helffrich <george@gly.bris.ac.uk>
+ 
+         * cli/cli-cmds.c, defs.h, source.c, gdb.1: Add edit command.  This
+ 	command mimics the behavior of Sun dbx, and invokes an editor at
+ 	the current line number.  Editor comes from EDITOR environment
+ 	variable, or defaults to /bin/ex.
+ 
  2002-04-25  George Helffrich <george@gly.bris.ac.uk>
  
          * source.c, symfile.c, symtab.c, symtab.h: Define mechanism to set
Index: gdb-5.1.1/src/gdb/defs.h
===================================================================
RCS file: /usr/src/local/cvsroot/gdb-5.1.1/src/gdb/defs.h,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -c -r1.1.1.1 -r1.2
*** gdb-5.1.1/src/gdb/defs.h	2002/02/02 11:34:18	1.1.1.1
--- gdb-5.1.1/src/gdb/defs.h	2002/05/26 12:12:26	1.2
***************
*** 751,756 ****
--- 751,760 ----
  
  extern void print_address (CORE_ADDR, struct ui_file *);
  
+ /* From command.c */
+ 
+ extern void shell_escape (char *, int);
+ 
  /* From source.c */
  
  extern int openp (const char *, int, const char *, int, int, char **);
Index: gdb-5.1.1/src/gdb/gdb.1
===================================================================
RCS file: /usr/src/local/cvsroot/gdb-5.1.1/src/gdb/gdb.1,v
retrieving revision 1.2
retrieving revision 1.3
diff -c -r1.2 -r1.3
*** gdb-5.1.1/src/gdb/gdb.1	2002/05/22 12:25:15	1.2
--- gdb-5.1.1/src/gdb/gdb.1	2002/05/26 12:16:34	1.3
***************
*** 1,6 ****
  .\" Copyright 1991, 1999 Free Software Foundation, Inc.
  .\" See section COPYING for conditions for redistribution
! .\" $Id: gdb.1,v 1.2 2002/05/22 12:25:15 george Exp $
  .TH gdb 1 "22may2002" "GNU Tools" "GNU Tools"
  .SH NAME
  gdb \- The GNU Debugger
--- 1,6 ----
  .\" Copyright 1991, 1999 Free Software Foundation, Inc.
  .\" See section COPYING for conditions for redistribution
! .\" $Id: gdb.1,v 1.3 2002/05/26 12:16:34 george Exp $
  .TH gdb 1 "22may2002" "GNU Tools" "GNU Tools"
  .SH NAME
  gdb \- The GNU Debugger
***************
*** 148,153 ****
--- 148,156 ----
  .I over\c
  \& any
  function calls in the line.
+ .TP
+ .B edit \fR[\|\fIfile\fB:\fR\|]\fIfunction
+ look at the program line where it is presently stopped.
  .TP
  .B list \fR[\|\fIfile\fB:\fR\|]\fIfunction
  type the text of the program in the vicinity of where it is presently stopped.
Index: gdb-5.1.1/src/gdb/source.c
===================================================================
RCS file: /usr/src/local/cvsroot/gdb-5.1.1/src/gdb/source.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -c -r1.2 -r1.3
*** gdb-5.1.1/src/gdb/source.c	2002/05/25 20:27:13	1.2
--- gdb-5.1.1/src/gdb/source.c	2002/05/26 12:12:26	1.3
***************
*** 82,87 ****
--- 82,91 ----
  
  static void list_command (char *, int);
  
+ static void edit_command (char *, int);
+ 
+ static void do_edit (struct symtab *, int, int);
+ 
  static void ambiguous_line_spec (struct symtabs_and_lines *);
  
  static void source_info (char *, int);
***************
*** 1443,1448 ****
--- 1447,1563 ----
    xfree (sals.sals);
  }
  
+ static void
+ edit_command (char *arg, int from_tty)
+ {
+   struct symtabs_and_lines sals, sals_end;
+   struct symtab_and_line sal, sal_end;
+   struct symbol *sym;
+   char *arg1;
+ 
+   if (!have_full_symbols () && !have_partial_symbols())
+     error ("No symbol table is loaded.  Use the \"file\" command.");
+ 
+   /* Pull in a current source symtab if necessary */
+   if (current_source_symtab == 0 &&
+       (arg == 0 || arg[0] == '+' || arg[0] == '-'))
+     select_source_symtab (0);
+ 
+   /* bare "edit" edits file with present line.  */
+ 
+   if (arg == 0)
+     {
+       if (current_source_symtab == 0)
+ 	error ("No default source file yet.  Do \"help edit\".");
+       do_edit (current_source_symtab,
+                current_source_line + lines_to_list/2, from_tty);
+       return;
+     }
+ 
+   /* Now should only be one argument -- decode it in SAL */
+ 
+   arg1 = arg;
+   sals = decode_line_1 (&arg1, 0, 0, 0, 0);
+ 
+   if (! sals.nelts) return;  /*  C++  */
+   if (sals.nelts > 1) {
+     ambiguous_line_spec (&sals);
+     free (sals.sals);
+     return;
+   }
+ 
+   sal = sals.sals[0];
+   free (sals.sals);
+ 
+   if (*arg1)
+     error ("Junk at end of line specification.");
+ 
+   /* if line was specified by address,
+      first print exactly which line, and which file.
+      In this case, sal.symtab == 0 means address is outside
+      of all known source files, not that user failed to give a filename.  */
+   if (*arg == '*')
+     {
+       if (sal.symtab == 0)
+ 	/* FIXME-32x64--assumes sal.pc fits in long.  */
+ 	error ("No source file for address %s.",
+ 		local_hex_string((unsigned long) sal.pc));
+       sym = find_pc_function (sal.pc);
+       if (sym)
+ 	{
+ 	  print_address_numeric (sal.pc, 1, gdb_stdout);
+ 	  printf_filtered (" is in ");
+ 	  fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
+ 	  printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
+ 	}
+       else
+ 	{
+ 	  print_address_numeric (sal.pc, 1, gdb_stdout);
+ 	  printf_filtered (" is at %s:%d.\n",
+ 			   sal.symtab->filename, sal.line);
+ 	}
+     }
+ 
+   /* If what was given does not imply a symtab, it must be an undebuggable
+      symbol which means no source code.  */
+ 
+   if (sal.symtab == 0)
+     error ("No line number known for %s.", arg);
+ 
+   do_edit(sal.symtab, sal.line, from_tty);
+ }
+ 
+ static void
+ do_edit (struct symtab *symtb, int line, int from_tty)
+ {
+   int cmdlen, log10;
+   unsigned m;
+   char *editor;
+   char *p;
+ 
+   if ((editor = (char *) getenv ("EDITOR")) == NULL)
+       editor = "/bin/ex";
+   
+   /* Approximate base-10 log of line to 1 unit for digit count */
+   for(log10=32, m=0x80000000; !(line & m) && log10>0; log10--, m=m>>1);
+   log10 = 1 + (int)((log10 + (0 == (m-1)&line))/3.32192809);
+ 
+   cmdlen = strlen(editor) + 1
+          + (NULL == symtb->dirname ? 0 : strlen(symtb->dirname) + 1)
+ 	 + (NULL == symtb->filename? 0 : strlen(symtb->filename)+ 1)
+ 	 + log10 + 2;
+   
+   p = xmalloc(cmdlen);
+   sprintf(p,"%s +%d %s%s",editor,line,
+      (NULL == symtb->dirname ? "./" :
+         (NULL != symtb->filename && *(symtb->filename) != '/') ?
+ 	   symtb->dirname : ""),
+      (NULL == symtb->filename ? "unknown" : symtb->filename)
+   );
+   shell_escape(p, from_tty);
+   free(p);
+ }
+ 
  /* Commands to search the source file for a regexp.  */
  
  /* ARGSUSED */
***************
*** 1727,1732 ****
--- 1842,1858 ----
  
    if (dbx_commands)
      add_com_alias ("file", "list", class_files, 1);
+ 
+   add_com ("edit", class_files, edit_command,
+            concat ("Edit specified file or function.\n\
+ With no argument, edits file containing most recent line listed.\n\
+ ", "\
+ Editing targets can be specified in these ways:\n\
+   FILE:LINENUM, to edit at that line in that file,\n\
+   FUNCTION, to edit at the beginning of that function,\n\
+   FILE:FUNCTION, to distinguish among like-named static functions.\n\
+   *ADDRESS, to edit at the line containing that address.\n\
+ Uses EDITOR environment variable contents as editor (or ex as default).",NULL));
  
    add_show_from_set
      (add_set_cmd ("listsize", class_support, var_uinteger,
Index: gdb-5.1.1/src/gdb/cli/cli-cmds.c
===================================================================
RCS file: /usr/src/local/cvsroot/gdb-5.1.1/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -c -r1.1.1.1 -r1.2
*** gdb-5.1.1/src/gdb/cli/cli-cmds.c	2002/02/02 11:34:57	1.1.1.1
--- gdb-5.1.1/src/gdb/cli/cli-cmds.c	2002/05/26 12:13:00	1.2
***************
*** 77,84 ****
  
  static void make_command (char *, int);
  
- static void shell_escape (char *, int);
- 
  void apropos_command (char *, int);
  
  /* Define all cmd_list_elements.  */
--- 77,82 ----
***************
*** 427,433 ****
  }
  
  /* ARGSUSED */
! static void
  shell_escape (char *arg, int from_tty)
  {
  #ifdef CANT_FORK
--- 425,431 ----
  }
  
  /* ARGSUSED */
! void
  shell_escape (char *arg, int from_tty)
  {
  #ifdef CANT_FORK


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