Home | History | Annotate | Line # | Download | only in gdb
go-lang.c revision 1.12
      1 /* Go language support routines for GDB, the GNU debugger.
      2 
      3    Copyright (C) 2012-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 /* TODO:
     21    - split stacks
     22    - printing of native types
     23    - goroutines
     24    - lots more
     25    - gccgo mangling needs redoing
     26      It's too hard, for example, to know whether one is looking at a mangled
     27      Go symbol or not, and their are ambiguities, e.g., the demangler may
     28      get passed *any* symbol, including symbols from other languages
     29      and including symbols that are already demangled.
     30      One thought is to at least add an _G prefix.
     31    - 6g mangling isn't supported yet
     32 */
     33 
     34 #include "gdbsupport/gdb_obstack.h"
     35 #include "block.h"
     36 #include "symtab.h"
     37 #include "language.h"
     38 #include "varobj.h"
     39 #include "go-lang.h"
     40 #include "c-lang.h"
     41 #include "parser-defs.h"
     42 #include "gdbarch.h"
     43 
     44 #include <ctype.h>
     45 
     46 /* The main function in the main package.  */
     47 static const char GO_MAIN_MAIN[] = "main.main";
     48 
     49 /* Function returning the special symbol name used by Go for the main
     50    procedure in the main program if it is found in minimal symbol list.
     51    This function tries to find minimal symbols so that it finds them even
     52    if the program was compiled without debugging information.  */
     53 
     54 const char *
     55 go_main_name (void)
     56 {
     57   bound_minimal_symbol msym
     58     = lookup_minimal_symbol (current_program_space, GO_MAIN_MAIN);
     59   if (msym.minsym != NULL)
     60     return GO_MAIN_MAIN;
     61 
     62   /* No known entry procedure found, the main program is probably not Go.  */
     63   return NULL;
     64 }
     65 
     66 /* Return non-zero if TYPE is a gccgo string.
     67    We assume CHECK_TYPEDEF has already been done.  */
     68 
     69 static int
     70 gccgo_string_p (struct type *type)
     71 {
     72   /* gccgo strings don't necessarily have a name we can use.  */
     73 
     74   if (type->num_fields () == 2)
     75     {
     76       struct type *type0 = type->field (0).type ();
     77       struct type *type1 = type->field (1).type ();
     78 
     79       type0 = check_typedef (type0);
     80       type1 = check_typedef (type1);
     81 
     82       if (type0->code () == TYPE_CODE_PTR
     83 	  && strcmp (type->field (0).name (), "__data") == 0
     84 	  && type1->code () == TYPE_CODE_INT
     85 	  && strcmp (type->field (1).name (), "__length") == 0)
     86 	{
     87 	  struct type *target_type = type0->target_type ();
     88 
     89 	  target_type = check_typedef (target_type);
     90 
     91 	  if (target_type->code () == TYPE_CODE_INT
     92 	      && target_type->length () == 1
     93 	      && strcmp (target_type->name (), "uint8") == 0)
     94 	    return 1;
     95 	}
     96     }
     97 
     98   return 0;
     99 }
    100 
    101 /* Return non-zero if TYPE is a 6g string.
    102    We assume CHECK_TYPEDEF has already been done.  */
    103 
    104 static int
    105 sixg_string_p (struct type *type)
    106 {
    107   if (type->num_fields () == 2
    108       && type->name () != NULL
    109       && strcmp (type->name (), "string") == 0)
    110     return 1;
    111 
    112   return 0;
    113 }
    114 
    115 /* Classify the kind of Go object that TYPE is.
    116    TYPE is a TYPE_CODE_STRUCT, used to represent a Go object.  */
    117 
    118 enum go_type
    119 go_classify_struct_type (struct type *type)
    120 {
    121   type = check_typedef (type);
    122 
    123   /* Recognize strings as they're useful to be able to print without
    124      pretty-printers.  */
    125   if (gccgo_string_p (type)
    126       || sixg_string_p (type))
    127     return GO_TYPE_STRING;
    128 
    129   return GO_TYPE_NONE;
    130 }
    131 
    132 /* Subroutine of unpack_mangled_go_symbol to simplify it.
    133    Given "[foo.]bar.baz", store "bar" in *PACKAGEP and "baz" in *OBJECTP.
    134    We stomp on the last '.' to nul-terminate "bar".
    135    The caller is responsible for memory management.  */
    136 
    137 static void
    138 unpack_package_and_object (char *buf,
    139 			   const char **packagep, const char **objectp)
    140 {
    141   char *last_dot;
    142 
    143   last_dot = strrchr (buf, '.');
    144   gdb_assert (last_dot != NULL);
    145   *objectp = last_dot + 1;
    146   *last_dot = '\0';
    147   last_dot = strrchr (buf, '.');
    148   if (last_dot != NULL)
    149     *packagep = last_dot + 1;
    150   else
    151     *packagep = buf;
    152 }
    153 
    154 /* Given a mangled Go symbol, find its package name, object name, and
    155    method type (if present).
    156    E.g., for "libgo_net.textproto.String.N33_libgo_net.textproto.ProtocolError"
    157    *PACKAGEP = "textproto"
    158    *OBJECTP = "String"
    159    *METHOD_TYPE_PACKAGEP = "textproto"
    160    *METHOD_TYPE_OBJECTP = "ProtocolError"
    161 
    162    Space for the resulting strings is malloc'd in one buffer.
    163    PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
    164    A pointer to this buffer is returned, or NULL if symbol isn't a
    165    mangled Go symbol.
    166 
    167    *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
    168    the method type is a pointer.
    169 
    170    There may be value in returning the outer container,
    171    i.e., "net" in the above example, but for now it's not needed.
    172    Plus it's currently not straightforward to compute,
    173    it comes from -fgo-prefix, and there's no algorithm to compute it.
    174 
    175    If we ever need to unpack the method type, this routine should work
    176    for that too.  */
    177 
    178 static gdb::unique_xmalloc_ptr<char>
    179 unpack_mangled_go_symbol (const char *mangled_name,
    180 			  const char **packagep,
    181 			  const char **objectp,
    182 			  const char **method_type_packagep,
    183 			  const char **method_type_objectp,
    184 			  int *method_type_is_pointerp)
    185 {
    186   char *buf;
    187   char *p;
    188   int len = strlen (mangled_name);
    189   /* Pointer to last digit in "N<digit(s)>_".  */
    190   char *saw_digit;
    191   /* Pointer to "N" if valid "N<digit(s)>_" found.  */
    192   char *method_type;
    193   /* Pointer to the first '.'.  */
    194   const char *first_dot;
    195   /* Pointer to the last '.'.  */
    196   const char *last_dot;
    197   /* Non-zero if we saw a pointer indicator.  */
    198   int saw_pointer;
    199 
    200   *packagep = *objectp = NULL;
    201   *method_type_packagep = *method_type_objectp = NULL;
    202   *method_type_is_pointerp = 0;
    203 
    204   /* main.init is mangled specially.  */
    205   if (strcmp (mangled_name, "__go_init_main") == 0)
    206     {
    207       gdb::unique_xmalloc_ptr<char> package
    208 	= make_unique_xstrdup ("main");
    209 
    210       *packagep = package.get ();
    211       *objectp = "init";
    212       return package;
    213     }
    214 
    215   /* main.main is mangled specially (missing prefix).  */
    216   if (strcmp (mangled_name, "main.main") == 0)
    217     {
    218       gdb::unique_xmalloc_ptr<char> package
    219 	= make_unique_xstrdup ("main");
    220 
    221       *packagep = package.get ();
    222       *objectp = "main";
    223       return package;
    224     }
    225 
    226   /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
    227      Alas it looks exactly like "prefix.package.object."
    228      To cope for now we only recognize the following prefixes:
    229 
    230      go: the default
    231      libgo_.*: used by gccgo's runtime
    232 
    233      Thus we don't support -fgo-prefix (except as used by the runtime).  */
    234   bool v3;
    235   if (startswith (mangled_name, "go_0"))
    236     /* V3 mangling detected, see
    237        https://go-review.googlesource.com/c/gofrontend/+/271726 .  */
    238     v3 = true;
    239   else if (startswith (mangled_name, "go.")
    240 	   || startswith (mangled_name, "libgo_"))
    241     v3 = false;
    242   else
    243     return NULL;
    244 
    245   /* Quick check for whether a search may be fruitful.  */
    246   /* Ignore anything with @plt, etc. in it.  */
    247   if (strchr (mangled_name, '@') != NULL)
    248     return NULL;
    249 
    250   /* It must have at least two dots.  */
    251   if (v3)
    252     first_dot = strchr (mangled_name, '0');
    253   else
    254     first_dot = strchr (mangled_name, '.');
    255 
    256   if (first_dot == NULL)
    257     return NULL;
    258   /* Treat "foo.bar" as unmangled.  It can collide with lots of other
    259      languages and it's not clear what the consequences are.
    260      And except for main.main, all gccgo symbols are at least
    261      prefix.package.object.  */
    262   last_dot = strrchr (mangled_name, '.');
    263   if (last_dot == first_dot)
    264     return NULL;
    265 
    266   /* More quick checks.  */
    267   if (last_dot[1] == '\0' /* foo. */
    268       || last_dot[-1] == '.') /* foo..bar */
    269     return NULL;
    270 
    271   /* At this point we've decided we have a mangled Go symbol.  */
    272 
    273   gdb::unique_xmalloc_ptr<char> result = make_unique_xstrdup (mangled_name);
    274   buf = result.get ();
    275 
    276   if (v3)
    277     {
    278       /* Replace "go_0" with "\0go.".  */
    279       buf[0] = '\0';
    280       buf[1] = 'g';
    281       buf[2] = 'o';
    282       buf[3] = '.';
    283 
    284       /* Skip the '\0'.  */
    285       buf++;
    286     }
    287 
    288   /* Search backwards looking for "N<digit(s)>".  */
    289   p = buf + len;
    290   saw_digit = method_type = NULL;
    291   saw_pointer = 0;
    292   while (p > buf)
    293     {
    294       int current = *(const unsigned char *) --p;
    295       int current_is_digit = isdigit ((unsigned char)current);
    296 
    297       if (saw_digit)
    298 	{
    299 	  if (current_is_digit)
    300 	    continue;
    301 	  if (current == 'N'
    302 	      && ((p > buf && p[-1] == '.')
    303 		  || (p > buf + 1 && p[-1] == 'p' && p[-2] == '.')))
    304 	    {
    305 	      if (atoi (p + 1) == strlen (saw_digit + 2))
    306 		{
    307 		  if (p[-1] == '.')
    308 		    method_type = p - 1;
    309 		  else
    310 		    {
    311 		      gdb_assert (p[-1] == 'p');
    312 		      saw_pointer = 1;
    313 		      method_type = p - 2;
    314 		    }
    315 		  break;
    316 		}
    317 	    }
    318 	  /* Not what we're looking for, reset and keep looking.  */
    319 	  saw_digit = NULL;
    320 	  saw_pointer = 0;
    321 	  continue;
    322 	}
    323       if (current_is_digit && p[1] == '_')
    324 	{
    325 	  /* Possible start of method "this" [sic] type.  */
    326 	  saw_digit = p;
    327 	  continue;
    328 	}
    329     }
    330 
    331   if (method_type != NULL
    332       /* Ensure not something like "..foo".  */
    333       && (method_type > buf && method_type[-1] != '.'))
    334     {
    335       unpack_package_and_object (saw_digit + 2,
    336 				 method_type_packagep, method_type_objectp);
    337       *method_type = '\0';
    338       *method_type_is_pointerp = saw_pointer;
    339     }
    340 
    341   unpack_package_and_object (buf, packagep, objectp);
    342   return result;
    343 }
    344 
    345 /* Implements the la_demangle language_defn routine for language Go.
    346 
    347    N.B. This may get passed *any* symbol, including symbols from other
    348    languages and including symbols that are already demangled.
    349    Both of these situations are kinda unfortunate, but that's how things
    350    are today.
    351 
    352    N.B. This currently only supports gccgo's mangling.
    353 
    354    N.B. gccgo's mangling needs, I think, changing.
    355    This demangler can't work in all situations,
    356    thus not too much effort is currently put into it.  */
    357 
    358 gdb::unique_xmalloc_ptr<char>
    359 go_language::demangle_symbol (const char *mangled_name, int options) const
    360 {
    361   const char *package_name;
    362   const char *object_name;
    363   const char *method_type_package_name;
    364   const char *method_type_object_name;
    365   int method_type_is_pointer;
    366 
    367   if (mangled_name == NULL)
    368     return NULL;
    369 
    370   gdb::unique_xmalloc_ptr<char> name_buf
    371     (unpack_mangled_go_symbol (mangled_name,
    372 			       &package_name, &object_name,
    373 			       &method_type_package_name,
    374 			       &method_type_object_name,
    375 			       &method_type_is_pointer));
    376   if (name_buf == NULL)
    377     return NULL;
    378 
    379   auto_obstack tempbuf;
    380 
    381   /* Print methods as they appear in "method expressions".  */
    382   if (method_type_package_name != NULL)
    383     {
    384       /* FIXME: Seems like we should include package_name here somewhere.  */
    385       if (method_type_is_pointer)
    386 	  obstack_grow_str (&tempbuf, "(*");
    387       obstack_grow_str (&tempbuf, method_type_package_name);
    388       obstack_grow_str (&tempbuf, ".");
    389       obstack_grow_str (&tempbuf, method_type_object_name);
    390       if (method_type_is_pointer)
    391 	obstack_grow_str (&tempbuf, ")");
    392       obstack_grow_str (&tempbuf, ".");
    393       obstack_grow_str (&tempbuf, object_name);
    394     }
    395   else
    396     {
    397       obstack_grow_str (&tempbuf, package_name);
    398       obstack_grow_str (&tempbuf, ".");
    399       obstack_grow_str (&tempbuf, object_name);
    400     }
    401   obstack_grow_str0 (&tempbuf, "");
    402 
    403   return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
    404 }
    405 
    406 /* See go-lang.h.  */
    407 
    408 gdb::unique_xmalloc_ptr<char>
    409 go_symbol_package_name (const struct symbol *sym)
    410 {
    411   const char *mangled_name = sym->linkage_name ();
    412   const char *package_name;
    413   const char *object_name;
    414   const char *method_type_package_name;
    415   const char *method_type_object_name;
    416   int method_type_is_pointer;
    417   gdb::unique_xmalloc_ptr<char> name_buf;
    418 
    419   if (sym->language () != language_go)
    420     return nullptr;
    421   name_buf = unpack_mangled_go_symbol (mangled_name,
    422 				       &package_name, &object_name,
    423 				       &method_type_package_name,
    424 				       &method_type_object_name,
    425 				       &method_type_is_pointer);
    426   /* Some Go symbols don't have mangled form we interpret (yet).  */
    427   if (name_buf == NULL)
    428     return NULL;
    429   return make_unique_xstrdup (package_name);
    430 }
    431 
    432 /* See go-lang.h.  */
    433 
    434 gdb::unique_xmalloc_ptr<char>
    435 go_block_package_name (const struct block *block)
    436 {
    437   while (block != NULL)
    438     {
    439       struct symbol *function = block->function ();
    440 
    441       if (function != NULL)
    442 	{
    443 	  gdb::unique_xmalloc_ptr<char> package_name
    444 	    = go_symbol_package_name (function);
    445 
    446 	  if (package_name != NULL)
    447 	    return package_name;
    448 
    449 	  /* Stop looking if we find a function without a package name.
    450 	     We're most likely outside of Go and thus the concept of the
    451 	     "current" package is gone.  */
    452 	  return NULL;
    453 	}
    454 
    455       block = block->superblock ();
    456     }
    457 
    458   return NULL;
    459 }
    460 
    461 /* See language.h.  */
    462 
    463 void
    464 go_language::language_arch_info (struct gdbarch *gdbarch,
    465 				 struct language_arch_info *lai) const
    466 {
    467   const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
    468 
    469   /* Helper function to allow shorter lines below.  */
    470   auto add  = [&] (struct type * t) -> struct type *
    471   {
    472     lai->add_primitive_type (t);
    473     return t;
    474   };
    475 
    476   add (builtin->builtin_void);
    477   add (builtin->builtin_char);
    478   add (builtin->builtin_bool);
    479   add (builtin->builtin_int);
    480   add (builtin->builtin_uint);
    481   add (builtin->builtin_uintptr);
    482   add (builtin->builtin_int8);
    483   add (builtin->builtin_int16);
    484   add (builtin->builtin_int32);
    485   add (builtin->builtin_int64);
    486   add (builtin->builtin_uint8);
    487   add (builtin->builtin_uint16);
    488   add (builtin->builtin_uint32);
    489   add (builtin->builtin_uint64);
    490   add (builtin->builtin_float32);
    491   add (builtin->builtin_float64);
    492   add (builtin->builtin_complex64);
    493   add (builtin->builtin_complex128);
    494 
    495   lai->set_string_char_type (builtin->builtin_char);
    496   lai->set_bool_type (builtin->builtin_bool, "bool");
    497 }
    498 
    499 /* Single instance of the Go language class.  */
    500 
    501 static go_language go_language_defn;
    502 
    503 static struct builtin_go_type *
    504 build_go_types (struct gdbarch *gdbarch)
    505 {
    506   struct builtin_go_type *builtin_go_type = new struct builtin_go_type;
    507 
    508   type_allocator alloc (gdbarch);
    509   builtin_go_type->builtin_void = builtin_type (gdbarch)->builtin_void;
    510   builtin_go_type->builtin_char
    511     = init_character_type (alloc, 8, 1, "char");
    512   builtin_go_type->builtin_bool
    513     = init_boolean_type (alloc, 8, 0, "bool");
    514   builtin_go_type->builtin_int
    515     = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 0, "int");
    516   builtin_go_type->builtin_uint
    517     = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 1, "uint");
    518   builtin_go_type->builtin_uintptr
    519     = init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
    520   builtin_go_type->builtin_int8
    521     = init_integer_type (alloc, 8, 0, "int8");
    522   builtin_go_type->builtin_int16
    523     = init_integer_type (alloc, 16, 0, "int16");
    524   builtin_go_type->builtin_int32
    525     = init_integer_type (alloc, 32, 0, "int32");
    526   builtin_go_type->builtin_int64
    527     = init_integer_type (alloc, 64, 0, "int64");
    528   builtin_go_type->builtin_uint8
    529     = init_integer_type (alloc, 8, 1, "uint8");
    530   builtin_go_type->builtin_uint16
    531     = init_integer_type (alloc, 16, 1, "uint16");
    532   builtin_go_type->builtin_uint32
    533     = init_integer_type (alloc, 32, 1, "uint32");
    534   builtin_go_type->builtin_uint64
    535     = init_integer_type (alloc, 64, 1, "uint64");
    536   builtin_go_type->builtin_float32
    537     = init_float_type (alloc, 32, "float32", floatformats_ieee_single);
    538   builtin_go_type->builtin_float64
    539     = init_float_type (alloc, 64, "float64", floatformats_ieee_double);
    540   builtin_go_type->builtin_complex64
    541     = init_complex_type ("complex64", builtin_go_type->builtin_float32);
    542   builtin_go_type->builtin_complex128
    543     = init_complex_type ("complex128", builtin_go_type->builtin_float64);
    544 
    545   return builtin_go_type;
    546 }
    547 
    548 static const registry<gdbarch>::key<struct builtin_go_type> go_type_data;
    549 
    550 const struct builtin_go_type *
    551 builtin_go_type (struct gdbarch *gdbarch)
    552 {
    553   struct builtin_go_type *result = go_type_data.get (gdbarch);
    554   if (result == nullptr)
    555     {
    556       result = build_go_types (gdbarch);
    557       go_type_data.set (gdbarch, result);
    558     }
    559 
    560   return result;
    561 }
    562