🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gdb.bug
Article #40

Help with cfront demangling in gdb 4.7

#40
From: Robert Mecklenburg
Date: Tue, 03 Nov 1992 10:45
308 lines
9651 bytes
I am trying to get cfront style demangling working the way I think it
should :-) and have reached the end of my rope.  Basically, I would
like cfront demangling to generate demangled functions (already
works), as well as demangled arguments, local variables, and structure
(class) members.  I have managed to get much of what I want, but broke
gdb in the process. :-( The following are my diffs and an example of
how I broke gdb.  Comments and suggestions are welcome.

Thanks,
Robert Mecklenburg
University of Utah

Example file:

74 curve:~/dec/gdb-4.7-hacked$ cat junk.c
struct A
{
    int i;
    float f;
    A() {}
    ~A() {}
    virtual void set_i( int _i );
};

void
A::set_i( int _i )
{
    i = _i;
    i += 3;
}

main()
{
    int k;
    struct A ana;

    ana.i = 5;
    k = 5;
    ana.set_i( 17 );
    ana.set_i( -1 );
}

Example breakage:

76 curve:~/dec/gdb-4.7-hacked$ gdb/gdb junk
GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.7, Copyright 1992 Free Software Foundation, Inc...
(gdb) b 24
Breakpoint 2 at 0x4001e0: file junk.c, line 24.
(gdb) run
Starting program: /net_mnt/csfs/a1/gr_u/prof/mecklen/dec/gdb-4.7-hacked/junk

Breakpoint 2, main () at junk.c:24
24          ana.set_i( 17 );
(gdb) i lo
k = 5	    	    	    	    	// Works!
ana = {
  i = 5,
  f = 0,
  A::__vptr = 0x10000000
}
(gdb) p k   	    	    	    	// Works!
$1 = 5
(gdb) p ana.i   	    	    	// Works!
$2 = 5
(gdb) s

Breakpoint 1, set_i (this=0x7fffb488, _i=17) at junk.c:13
13          i = _i;
(gdb) p this
$3 = (struct junk.c *) 0x7fffb488
(gdb) p *this	    	    	    	///////// FAILS!
$4 = {
}
(gdb) p this->i
There is no member named i.
(gdb) p this->i__1A
There is no member named i__1A.
(gdb) quit
The program is running.  Quit anyway? (y or n) y


My diffs:

diff -c -r gdb-4.7/gdb/c-exp.y gdb-4.7-hacked/gdb/c-exp.y
*** gdb-4.7/gdb/c-exp.y	Mon Oct 19 15:33:03 1992
--- gdb-4.7-hacked/gdb/c-exp.y	Tue Nov  3 17:22:48 1992
***************
*** 41,46 ****
--- 41,47 ----
  #include "bfd.h"
  #include "symfile.h"
  #include "objfiles.h"
+ #include "demangle.h"
  
  /* These MUST be included in any grammar file!!!! Please choose unique names!
     Note that this are a combined list of variables that can be produced
***************
*** 1472,1477 ****
--- 1473,1485 ----
  	    return NAME_OR_UINT;
  	  }
        }
+ 
+     /* Mangle C++ data member names. */
+     if (!sym && CFRONT_DEMANGLING && (tmp = expensive_mangler(tmp)))
+     {
+ 	yylval.sval.ptr = tmp;
+ 	yylval.sval.length = strlen( tmp );
+     }
  
      /* Any other kind of symbol */
      yylval.ssym.sym = sym;
diff -c -r gdb-4.7/gdb/defs.h gdb-4.7-hacked/gdb/defs.h
*** gdb-4.7/gdb/defs.h	Mon Oct  5 18:41:41 1992
--- gdb-4.7-hacked/gdb/defs.h	Wed Oct 28 08:28:15 1992
***************
*** 92,97 ****
--- 92,102 ----
  extern int
  inside_main_func PARAMS ((CORE_ADDR pc));
  
+ /* From symtab.c */
+ 
+ char *
+ expensive_mangler PARAMS ((const char *));
+ 
  /* From cplus-dem.c */
  
  extern char *
diff -c -r gdb-4.7/gdb/symtab.c gdb-4.7-hacked/gdb/symtab.c
*** gdb-4.7/gdb/symtab.c	Fri Oct  9 10:44:31 1992
--- gdb-4.7-hacked/gdb/symtab.c	Tue Nov  3 17:28:27 1992
***************
*** 45,53 ****
  
  /* Prototypes for local functions */
  
- static char *
- expensive_mangler PARAMS ((const char *));
- 
  extern int
  find_methods PARAMS ((struct type *, char *, char **, struct symbol **));
  
--- 45,50 ----
***************
*** 441,447 ****
    register struct objfile *objfile;
    register struct block *b;
    register struct minimal_symbol *msymbol;
!   char *temp;
    extern char *gdb_completer_word_break_characters;
  
    /* If NAME contains any characters from gdb_completer_word_break_characters
--- 438,444 ----
    register struct objfile *objfile;
    register struct block *b;
    register struct minimal_symbol *msymbol;
!   char *temp, *demangled_name;
    extern char *gdb_completer_word_break_characters;
  
    /* If NAME contains any characters from gdb_completer_word_break_characters
***************
*** 448,459 ****
       then it is probably from a quoted name string.  So check to see if it
       has a C++ mangled equivalent, and if so, use the mangled equivalent. */
  
!   if (strpbrk (name, gdb_completer_word_break_characters) != NULL)
      {
!       if ((temp = expensive_mangler (name)) != NULL)
! 	{
! 	  name = temp;
! 	}
      }
  
    /* Search specified block and its superiors.  */
--- 445,456 ----
       then it is probably from a quoted name string.  So check to see if it
       has a C++ mangled equivalent, and if so, use the mangled equivalent. */
  
!   demangled_name = name;
!   if (strpbrk (name, gdb_completer_word_break_characters) != NULL ||
!       CFRONT_DEMANGLING)
      {
! 	if ((temp = expensive_mangler (name)) != NULL)
! 	    name = temp;
      }
  
    /* Search specified block and its superiors.  */
***************
*** 503,509 ****
  	  if (BLOCK_START (b) <= BLOCK_START (block)
  	      && BLOCK_END (b) > BLOCK_START (block))
  	    {
! 	      sym = lookup_demangled_block_symbol (b, name);
  	      if (sym)
  		{
  		  block_found = b;
--- 500,506 ----
  	  if (BLOCK_START (b) <= BLOCK_START (block)
  	      && BLOCK_END (b) > BLOCK_START (block))
  	    {
! 	      sym = lookup_demangled_block_symbol (b, demangled_name);
  	      if (sym)
  		{
  		  block_found = b;
***************
*** 675,681 ****
  	{
  	  bv = BLOCKVECTOR (s);
  	  block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
! 	  sym = lookup_demangled_block_symbol (block, name);
  	  if (sym) 
  	    {
  	      block_found = block;
--- 672,678 ----
  	{
  	  bv = BLOCKVECTOR (s);
  	  block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
! 	  sym = lookup_demangled_block_symbol (block, demangled_name);
  	  if (sym) 
  	    {
  	      block_found = block;
***************
*** 692,698 ****
  	      s = PSYMTAB_TO_SYMTAB(ps);
  	      bv = BLOCKVECTOR (s);
  	      block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
! 	      sym = lookup_demangled_block_symbol (block, name);
  	      if (!sym)
  		error ("Internal: mangled static symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
  	      if (symtab != NULL)
--- 689,695 ----
  	      s = PSYMTAB_TO_SYMTAB(ps);
  	      bv = BLOCKVECTOR (s);
  	      block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
! 	      sym = lookup_demangled_block_symbol (block, demangled_name);
  	      if (!sym)
  		error ("Internal: mangled static symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
  	      if (symtab != NULL)
***************
*** 2673,2679 ****
     Basically we go munging through available symbols, demangling each one,
     looking for a match on the demangled result. */
  
! static char *
  expensive_mangler (lookfor)
       const char *lookfor;
  {
--- 2670,2676 ----
     Basically we go munging through available symbols, demangling each one,
     looking for a match on the demangled result. */
  
! char *
  expensive_mangler (lookfor)
       const char *lookfor;
  {
diff -c -r gdb-4.7/libiberty/cplus-dem.c gdb-4.7-hacked/libiberty/cplus-dem.c
*** gdb-4.7/libiberty/cplus-dem.c	Mon Oct  5 18:42:45 1992
--- gdb-4.7-hacked/libiberty/cplus-dem.c	Wed Oct 28 10:51:59 1992
***************
*** 993,998 ****
--- 993,1008 ----
  	  success = 0;
  	}
      }
+   else if (CFRONT_DEMANGLING && (scan == *mangled) && (isdigit (scan[2])))
+     {
+       /* A cfront local variable starts with "__<digit>". */
+       *mangled = scan + 2;
+       while ( isdigit (**mangled) )
+ 	  (*mangled)++;
+       string_append( declp, *mangled );
+       while ( **mangled )
+ 	  (*mangled)++; 
+     }
    else if ((scan == *mangled) && (isdigit (scan[2]) || (scan[2] == 'Q')))
      {
        /* A GNU style constructor starts with "__<digit>" or "__Q". */
***************
*** 1028,1034 ****
        /* Mangled name does not start with "__" but does have one somewhere
  	 in there with non empty stuff after it.  Looks like a global
  	 function name. */
!       demangle_function_name (work, mangled, declp, scan);
      }
    else
      {
--- 1038,1054 ----
        /* Mangled name does not start with "__" but does have one somewhere
  	 in there with non empty stuff after it.  Looks like a global
  	 function name. */
!       if ( CFRONT_DEMANGLING && scan[2] != 'F' )
!         {
! 	  /* Not a function since it doesn't start with F, probably
! 	     a data member name. */
! 	  string_appendn( declp, *mangled, scan - *mangled );
! 	  *scan = '\0';
! 	  /* while ( **mangled )
! 	    (*mangled)++; */
!         }
!       else
! 	demangle_function_name (work, mangled, declp, scan);
      }
    else
      {
Technical Details
Message-ID: <9211040045.AA08798@hellgate.utah.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!umn.edu!spool.mu.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!oops.cs.utah.edu!mecklen

Show Full Headers
Complete Article Headers
Newsgroups: gnu.gdb.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!umn.edu!spool.mu.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!oops.cs.utah.edu!mecklen
From: mecklen@oops.cs.utah.edu (Robert Mecklenburg)
Subject: Help with cfront demangling in gdb 4.7
Message-ID: <9211040045.AA08798@hellgate.utah.edu>
Sender: gnulists@ai.mit.edu
Organization: University of Utah CS Dept
Distribution: gnu
Date: Tue, 3 Nov 1992 10:45:37 GMT
Approved: bug-gdb@prep.ai.mit.edu
Lines: 309