1 /* Go language support routines for GDB, the GNU debugger.
3 Copyright (C) 2012-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 - printing of native types
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
35 #include "gdbsupport/gdb_obstack.h"
42 #include "parser-defs.h"
47 /* The main function in the main package. */
48 static const char GO_MAIN_MAIN
[] = "main.main";
50 /* Function returning the special symbol name used by Go for the main
51 procedure in the main program if it is found in minimal symbol list.
52 This function tries to find minimal symbols so that it finds them even
53 if the program was compiled without debugging information. */
58 struct bound_minimal_symbol msym
;
60 msym
= lookup_minimal_symbol (GO_MAIN_MAIN
, NULL
, NULL
);
61 if (msym
.minsym
!= NULL
)
64 /* No known entry procedure found, the main program is probably not Go. */
68 /* Return non-zero if TYPE is a gccgo string.
69 We assume CHECK_TYPEDEF has already been done. */
72 gccgo_string_p (struct type
*type
)
74 /* gccgo strings don't necessarily have a name we can use. */
76 if (type
->num_fields () == 2)
78 struct type
*type0
= type
->field (0).type ();
79 struct type
*type1
= type
->field (1).type ();
81 type0
= check_typedef (type0
);
82 type1
= check_typedef (type1
);
84 if (type0
->code () == TYPE_CODE_PTR
85 && strcmp (type
->field (0).name (), "__data") == 0
86 && type1
->code () == TYPE_CODE_INT
87 && strcmp (type
->field (1).name (), "__length") == 0)
89 struct type
*target_type
= type0
->target_type ();
91 target_type
= check_typedef (target_type
);
93 if (target_type
->code () == TYPE_CODE_INT
94 && target_type
->length () == 1
95 && strcmp (target_type
->name (), "uint8") == 0)
103 /* Return non-zero if TYPE is a 6g string.
104 We assume CHECK_TYPEDEF has already been done. */
107 sixg_string_p (struct type
*type
)
109 if (type
->num_fields () == 2
110 && type
->name () != NULL
111 && strcmp (type
->name (), "string") == 0)
117 /* Classify the kind of Go object that TYPE is.
118 TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
121 go_classify_struct_type (struct type
*type
)
123 type
= check_typedef (type
);
125 /* Recognize strings as they're useful to be able to print without
127 if (gccgo_string_p (type
)
128 || sixg_string_p (type
))
129 return GO_TYPE_STRING
;
134 /* Subroutine of unpack_mangled_go_symbol to simplify it.
135 Given "[foo.]bar.baz", store "bar" in *PACKAGEP and "baz" in *OBJECTP.
136 We stomp on the last '.' to nul-terminate "bar".
137 The caller is responsible for memory management. */
140 unpack_package_and_object (char *buf
,
141 const char **packagep
, const char **objectp
)
145 last_dot
= strrchr (buf
, '.');
146 gdb_assert (last_dot
!= NULL
);
147 *objectp
= last_dot
+ 1;
149 last_dot
= strrchr (buf
, '.');
150 if (last_dot
!= NULL
)
151 *packagep
= last_dot
+ 1;
156 /* Given a mangled Go symbol, find its package name, object name, and
157 method type (if present).
158 E.g., for "libgo_net.textproto.String.N33_libgo_net.textproto.ProtocolError"
159 *PACKAGEP = "textproto"
161 *METHOD_TYPE_PACKAGEP = "textproto"
162 *METHOD_TYPE_OBJECTP = "ProtocolError"
164 Space for the resulting strings is malloc'd in one buffer.
165 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
166 [There are a few exceptions, but the caller is still responsible for
167 freeing the resulting pointer.]
168 A pointer to this buffer is returned, or NULL if symbol isn't a
170 The caller is responsible for freeing the result.
172 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
173 the method type is a pointer.
175 There may be value in returning the outer container,
176 i.e., "net" in the above example, but for now it's not needed.
177 Plus it's currently not straightforward to compute,
178 it comes from -fgo-prefix, and there's no algorithm to compute it.
180 If we ever need to unpack the method type, this routine should work
184 unpack_mangled_go_symbol (const char *mangled_name
,
185 const char **packagep
,
186 const char **objectp
,
187 const char **method_type_packagep
,
188 const char **method_type_objectp
,
189 int *method_type_is_pointerp
)
193 int len
= strlen (mangled_name
);
194 /* Pointer to last digit in "N<digit(s)>_". */
196 /* Pointer to "N" if valid "N<digit(s)>_" found. */
198 /* Pointer to the first '.'. */
199 const char *first_dot
;
200 /* Pointer to the last '.'. */
201 const char *last_dot
;
202 /* Non-zero if we saw a pointer indicator. */
205 *packagep
= *objectp
= NULL
;
206 *method_type_packagep
= *method_type_objectp
= NULL
;
207 *method_type_is_pointerp
= 0;
209 /* main.init is mangled specially. */
210 if (strcmp (mangled_name
, "__go_init_main") == 0)
212 char *package
= xstrdup ("main");
219 /* main.main is mangled specially (missing prefix). */
220 if (strcmp (mangled_name
, "main.main") == 0)
222 char *package
= xstrdup ("main");
229 /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
230 Alas it looks exactly like "prefix.package.object."
231 To cope for now we only recognize the following prefixes:
234 libgo_.*: used by gccgo's runtime
236 Thus we don't support -fgo-prefix (except as used by the runtime). */
237 if (!startswith (mangled_name
, "go.")
238 && !startswith (mangled_name
, "libgo_"))
241 /* Quick check for whether a search may be fruitful. */
242 /* Ignore anything with @plt, etc. in it. */
243 if (strchr (mangled_name
, '@') != NULL
)
245 /* It must have at least two dots. */
246 first_dot
= strchr (mangled_name
, '.');
247 if (first_dot
== NULL
)
249 /* Treat "foo.bar" as unmangled. It can collide with lots of other
250 languages and it's not clear what the consequences are.
251 And except for main.main, all gccgo symbols are at least
252 prefix.package.object. */
253 last_dot
= strrchr (mangled_name
, '.');
254 if (last_dot
== first_dot
)
257 /* More quick checks. */
258 if (last_dot
[1] == '\0' /* foo. */
259 || last_dot
[-1] == '.') /* foo..bar */
262 /* At this point we've decided we have a mangled Go symbol. */
264 buf
= xstrdup (mangled_name
);
266 /* Search backwards looking for "N<digit(s)>". */
268 saw_digit
= method_type
= NULL
;
272 int current
= *(const unsigned char *) --p
;
273 int current_is_digit
= isdigit (current
);
277 if (current_is_digit
)
280 && ((p
> buf
&& p
[-1] == '.')
281 || (p
> buf
+ 1 && p
[-1] == 'p' && p
[-2] == '.')))
283 if (atoi (p
+ 1) == strlen (saw_digit
+ 2))
289 gdb_assert (p
[-1] == 'p');
296 /* Not what we're looking for, reset and keep looking. */
301 if (current_is_digit
&& p
[1] == '_')
303 /* Possible start of method "this" [sic] type. */
309 if (method_type
!= NULL
310 /* Ensure not something like "..foo". */
311 && (method_type
> buf
&& method_type
[-1] != '.'))
313 unpack_package_and_object (saw_digit
+ 2,
314 method_type_packagep
, method_type_objectp
);
316 *method_type_is_pointerp
= saw_pointer
;
319 unpack_package_and_object (buf
, packagep
, objectp
);
323 /* Implements the la_demangle language_defn routine for language Go.
325 N.B. This may get passed *any* symbol, including symbols from other
326 languages and including symbols that are already demangled.
327 Both of these situations are kinda unfortunate, but that's how things
330 N.B. This currently only supports gccgo's mangling.
332 N.B. gccgo's mangling needs, I think, changing.
333 This demangler can't work in all situations,
334 thus not too much effort is currently put into it. */
336 gdb::unique_xmalloc_ptr
<char>
337 go_language::demangle_symbol (const char *mangled_name
, int options
) const
339 const char *package_name
;
340 const char *object_name
;
341 const char *method_type_package_name
;
342 const char *method_type_object_name
;
343 int method_type_is_pointer
;
345 if (mangled_name
== NULL
)
348 gdb::unique_xmalloc_ptr
<char> name_buf
349 (unpack_mangled_go_symbol (mangled_name
,
350 &package_name
, &object_name
,
351 &method_type_package_name
,
352 &method_type_object_name
,
353 &method_type_is_pointer
));
354 if (name_buf
== NULL
)
357 auto_obstack tempbuf
;
359 /* Print methods as they appear in "method expressions". */
360 if (method_type_package_name
!= NULL
)
362 /* FIXME: Seems like we should include package_name here somewhere. */
363 if (method_type_is_pointer
)
364 obstack_grow_str (&tempbuf
, "(*");
365 obstack_grow_str (&tempbuf
, method_type_package_name
);
366 obstack_grow_str (&tempbuf
, ".");
367 obstack_grow_str (&tempbuf
, method_type_object_name
);
368 if (method_type_is_pointer
)
369 obstack_grow_str (&tempbuf
, ")");
370 obstack_grow_str (&tempbuf
, ".");
371 obstack_grow_str (&tempbuf
, object_name
);
375 obstack_grow_str (&tempbuf
, package_name
);
376 obstack_grow_str (&tempbuf
, ".");
377 obstack_grow_str (&tempbuf
, object_name
);
379 obstack_grow_str0 (&tempbuf
, "");
381 return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf
));
384 /* Given a Go symbol, return its package or NULL if unknown.
385 Space for the result is malloc'd, caller must free. */
388 go_symbol_package_name (const struct symbol
*sym
)
390 const char *mangled_name
= sym
->linkage_name ();
391 const char *package_name
;
392 const char *object_name
;
393 const char *method_type_package_name
;
394 const char *method_type_object_name
;
395 int method_type_is_pointer
;
399 gdb_assert (sym
->language () == language_go
);
400 name_buf
= unpack_mangled_go_symbol (mangled_name
,
401 &package_name
, &object_name
,
402 &method_type_package_name
,
403 &method_type_object_name
,
404 &method_type_is_pointer
);
405 /* Some Go symbols don't have mangled form we interpret (yet). */
406 if (name_buf
== NULL
)
408 result
= xstrdup (package_name
);
413 /* Return the package that BLOCK is in, or NULL if there isn't one.
414 Space for the result is malloc'd, caller must free. */
417 go_block_package_name (const struct block
*block
)
419 while (block
!= NULL
)
421 struct symbol
*function
= block
->function ();
423 if (function
!= NULL
)
425 char *package_name
= go_symbol_package_name (function
);
427 if (package_name
!= NULL
)
430 /* Stop looking if we find a function without a package name.
431 We're most likely outside of Go and thus the concept of the
432 "current" package is gone. */
436 block
= block
->superblock ();
442 /* See language.h. */
445 go_language::language_arch_info (struct gdbarch
*gdbarch
,
446 struct language_arch_info
*lai
) const
448 const struct builtin_go_type
*builtin
= builtin_go_type (gdbarch
);
450 /* Helper function to allow shorter lines below. */
451 auto add
= [&] (struct type
* t
) -> struct type
*
453 lai
->add_primitive_type (t
);
457 add (builtin
->builtin_void
);
458 add (builtin
->builtin_char
);
459 add (builtin
->builtin_bool
);
460 add (builtin
->builtin_int
);
461 add (builtin
->builtin_uint
);
462 add (builtin
->builtin_uintptr
);
463 add (builtin
->builtin_int8
);
464 add (builtin
->builtin_int16
);
465 add (builtin
->builtin_int32
);
466 add (builtin
->builtin_int64
);
467 add (builtin
->builtin_uint8
);
468 add (builtin
->builtin_uint16
);
469 add (builtin
->builtin_uint32
);
470 add (builtin
->builtin_uint64
);
471 add (builtin
->builtin_float32
);
472 add (builtin
->builtin_float64
);
473 add (builtin
->builtin_complex64
);
474 add (builtin
->builtin_complex128
);
476 lai
->set_string_char_type (builtin
->builtin_char
);
477 lai
->set_bool_type (builtin
->builtin_bool
, "bool");
480 /* Single instance of the Go language class. */
482 static go_language go_language_defn
;
484 static struct builtin_go_type
*
485 build_go_types (struct gdbarch
*gdbarch
)
487 struct builtin_go_type
*builtin_go_type
= new struct builtin_go_type
;
489 builtin_go_type
->builtin_void
490 = arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
491 builtin_go_type
->builtin_char
492 = arch_character_type (gdbarch
, 8, 1, "char");
493 builtin_go_type
->builtin_bool
494 = arch_boolean_type (gdbarch
, 8, 0, "bool");
495 builtin_go_type
->builtin_int
496 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 0, "int");
497 builtin_go_type
->builtin_uint
498 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 1, "uint");
499 builtin_go_type
->builtin_uintptr
500 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr");
501 builtin_go_type
->builtin_int8
502 = arch_integer_type (gdbarch
, 8, 0, "int8");
503 builtin_go_type
->builtin_int16
504 = arch_integer_type (gdbarch
, 16, 0, "int16");
505 builtin_go_type
->builtin_int32
506 = arch_integer_type (gdbarch
, 32, 0, "int32");
507 builtin_go_type
->builtin_int64
508 = arch_integer_type (gdbarch
, 64, 0, "int64");
509 builtin_go_type
->builtin_uint8
510 = arch_integer_type (gdbarch
, 8, 1, "uint8");
511 builtin_go_type
->builtin_uint16
512 = arch_integer_type (gdbarch
, 16, 1, "uint16");
513 builtin_go_type
->builtin_uint32
514 = arch_integer_type (gdbarch
, 32, 1, "uint32");
515 builtin_go_type
->builtin_uint64
516 = arch_integer_type (gdbarch
, 64, 1, "uint64");
517 builtin_go_type
->builtin_float32
518 = arch_float_type (gdbarch
, 32, "float32", floatformats_ieee_single
);
519 builtin_go_type
->builtin_float64
520 = arch_float_type (gdbarch
, 64, "float64", floatformats_ieee_double
);
521 builtin_go_type
->builtin_complex64
522 = init_complex_type ("complex64", builtin_go_type
->builtin_float32
);
523 builtin_go_type
->builtin_complex128
524 = init_complex_type ("complex128", builtin_go_type
->builtin_float64
);
526 return builtin_go_type
;
529 static const registry
<gdbarch
>::key
<struct builtin_go_type
> go_type_data
;
531 const struct builtin_go_type
*
532 builtin_go_type (struct gdbarch
*gdbarch
)
534 struct builtin_go_type
*result
= go_type_data
.get (gdbarch
);
535 if (result
== nullptr)
537 result
= build_go_types (gdbarch
);
538 go_type_data
.set (gdbarch
, result
);