1 /* Go language support routines for GDB, the GNU debugger.
3 Copyright (C) 2012-2025 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
34 #include "gdbsupport/gdb_obstack.h"
41 #include "parser-defs.h"
45 /* The main function in the main package. */
46 static const char GO_MAIN_MAIN
[] = "main.main";
48 /* Function returning the special symbol name used by Go for the main
49 procedure in the main program if it is found in minimal symbol list.
50 This function tries to find minimal symbols so that it finds them even
51 if the program was compiled without debugging information. */
56 bound_minimal_symbol msym
57 = lookup_minimal_symbol (current_program_space
, GO_MAIN_MAIN
);
58 if (msym
.minsym
!= NULL
)
61 /* No known entry procedure found, the main program is probably not Go. */
65 /* Return non-zero if TYPE is a gccgo string.
66 We assume CHECK_TYPEDEF has already been done. */
69 gccgo_string_p (struct type
*type
)
71 /* gccgo strings don't necessarily have a name we can use. */
73 if (type
->num_fields () == 2)
75 struct type
*type0
= type
->field (0).type ();
76 struct type
*type1
= type
->field (1).type ();
78 type0
= check_typedef (type0
);
79 type1
= check_typedef (type1
);
81 if (type0
->code () == TYPE_CODE_PTR
82 && strcmp (type
->field (0).name (), "__data") == 0
83 && type1
->code () == TYPE_CODE_INT
84 && strcmp (type
->field (1).name (), "__length") == 0)
86 struct type
*target_type
= type0
->target_type ();
88 target_type
= check_typedef (target_type
);
90 if (target_type
->code () == TYPE_CODE_INT
91 && target_type
->length () == 1
92 && strcmp (target_type
->name (), "uint8") == 0)
100 /* Return non-zero if TYPE is a 6g string.
101 We assume CHECK_TYPEDEF has already been done. */
104 sixg_string_p (struct type
*type
)
106 if (type
->num_fields () == 2
107 && type
->name () != NULL
108 && strcmp (type
->name (), "string") == 0)
114 /* Classify the kind of Go object that TYPE is.
115 TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
118 go_classify_struct_type (struct type
*type
)
120 type
= check_typedef (type
);
122 /* Recognize strings as they're useful to be able to print without
124 if (gccgo_string_p (type
)
125 || sixg_string_p (type
))
126 return GO_TYPE_STRING
;
131 /* Subroutine of unpack_mangled_go_symbol to simplify it.
132 Given "[foo.]bar.baz", store "bar" in *PACKAGEP and "baz" in *OBJECTP.
133 We stomp on the last '.' to nul-terminate "bar".
134 The caller is responsible for memory management. */
137 unpack_package_and_object (char *buf
,
138 const char **packagep
, const char **objectp
)
142 last_dot
= strrchr (buf
, '.');
143 gdb_assert (last_dot
!= NULL
);
144 *objectp
= last_dot
+ 1;
146 last_dot
= strrchr (buf
, '.');
147 if (last_dot
!= NULL
)
148 *packagep
= last_dot
+ 1;
153 /* Given a mangled Go symbol, find its package name, object name, and
154 method type (if present).
155 E.g., for "libgo_net.textproto.String.N33_libgo_net.textproto.ProtocolError"
156 *PACKAGEP = "textproto"
158 *METHOD_TYPE_PACKAGEP = "textproto"
159 *METHOD_TYPE_OBJECTP = "ProtocolError"
161 Space for the resulting strings is malloc'd in one buffer.
162 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
163 A pointer to this buffer is returned, or NULL if symbol isn't a
166 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
167 the method type is a pointer.
169 There may be value in returning the outer container,
170 i.e., "net" in the above example, but for now it's not needed.
171 Plus it's currently not straightforward to compute,
172 it comes from -fgo-prefix, and there's no algorithm to compute it.
174 If we ever need to unpack the method type, this routine should work
177 static gdb::unique_xmalloc_ptr
<char>
178 unpack_mangled_go_symbol (const char *mangled_name
,
179 const char **packagep
,
180 const char **objectp
,
181 const char **method_type_packagep
,
182 const char **method_type_objectp
,
183 int *method_type_is_pointerp
)
187 int len
= strlen (mangled_name
);
188 /* Pointer to last digit in "N<digit(s)>_". */
190 /* Pointer to "N" if valid "N<digit(s)>_" found. */
192 /* Pointer to the first '.'. */
193 const char *first_dot
;
194 /* Pointer to the last '.'. */
195 const char *last_dot
;
196 /* Non-zero if we saw a pointer indicator. */
199 *packagep
= *objectp
= NULL
;
200 *method_type_packagep
= *method_type_objectp
= NULL
;
201 *method_type_is_pointerp
= 0;
203 /* main.init is mangled specially. */
204 if (strcmp (mangled_name
, "__go_init_main") == 0)
206 gdb::unique_xmalloc_ptr
<char> package
207 = make_unique_xstrdup ("main");
209 *packagep
= package
.get ();
214 /* main.main is mangled specially (missing prefix). */
215 if (strcmp (mangled_name
, "main.main") == 0)
217 gdb::unique_xmalloc_ptr
<char> package
218 = make_unique_xstrdup ("main");
220 *packagep
= package
.get ();
225 /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
226 Alas it looks exactly like "prefix.package.object."
227 To cope for now we only recognize the following prefixes:
230 libgo_.*: used by gccgo's runtime
232 Thus we don't support -fgo-prefix (except as used by the runtime). */
234 if (startswith (mangled_name
, "go_0"))
235 /* V3 mangling detected, see
236 https://go-review.googlesource.com/c/gofrontend/+/271726 . */
238 else if (startswith (mangled_name
, "go.")
239 || startswith (mangled_name
, "libgo_"))
244 /* Quick check for whether a search may be fruitful. */
245 /* Ignore anything with @plt, etc. in it. */
246 if (strchr (mangled_name
, '@') != NULL
)
249 /* It must have at least two dots. */
251 first_dot
= strchr (mangled_name
, '0');
253 first_dot
= strchr (mangled_name
, '.');
255 if (first_dot
== NULL
)
257 /* Treat "foo.bar" as unmangled. It can collide with lots of other
258 languages and it's not clear what the consequences are.
259 And except for main.main, all gccgo symbols are at least
260 prefix.package.object. */
261 last_dot
= strrchr (mangled_name
, '.');
262 if (last_dot
== first_dot
)
265 /* More quick checks. */
266 if (last_dot
[1] == '\0' /* foo. */
267 || last_dot
[-1] == '.') /* foo..bar */
270 /* At this point we've decided we have a mangled Go symbol. */
272 gdb::unique_xmalloc_ptr
<char> result
= make_unique_xstrdup (mangled_name
);
277 /* Replace "go_0" with "\0go.". */
287 /* Search backwards looking for "N<digit(s)>". */
289 saw_digit
= method_type
= NULL
;
293 int current
= *(const unsigned char *) --p
;
294 int current_is_digit
= c_isdigit (current
);
298 if (current_is_digit
)
301 && ((p
> buf
&& p
[-1] == '.')
302 || (p
> buf
+ 1 && p
[-1] == 'p' && p
[-2] == '.')))
304 if (atoi (p
+ 1) == strlen (saw_digit
+ 2))
310 gdb_assert (p
[-1] == 'p');
317 /* Not what we're looking for, reset and keep looking. */
322 if (current_is_digit
&& p
[1] == '_')
324 /* Possible start of method "this" [sic] type. */
330 if (method_type
!= NULL
331 /* Ensure not something like "..foo". */
332 && (method_type
> buf
&& method_type
[-1] != '.'))
334 unpack_package_and_object (saw_digit
+ 2,
335 method_type_packagep
, method_type_objectp
);
337 *method_type_is_pointerp
= saw_pointer
;
340 unpack_package_and_object (buf
, packagep
, objectp
);
344 /* Implements the la_demangle language_defn routine for language Go.
346 N.B. This may get passed *any* symbol, including symbols from other
347 languages and including symbols that are already demangled.
348 Both of these situations are kinda unfortunate, but that's how things
351 N.B. This currently only supports gccgo's mangling.
353 N.B. gccgo's mangling needs, I think, changing.
354 This demangler can't work in all situations,
355 thus not too much effort is currently put into it. */
357 gdb::unique_xmalloc_ptr
<char>
358 go_language::demangle_symbol (const char *mangled_name
, int options
) const
360 const char *package_name
;
361 const char *object_name
;
362 const char *method_type_package_name
;
363 const char *method_type_object_name
;
364 int method_type_is_pointer
;
366 if (mangled_name
== NULL
)
369 gdb::unique_xmalloc_ptr
<char> name_buf
370 (unpack_mangled_go_symbol (mangled_name
,
371 &package_name
, &object_name
,
372 &method_type_package_name
,
373 &method_type_object_name
,
374 &method_type_is_pointer
));
375 if (name_buf
== NULL
)
378 auto_obstack tempbuf
;
380 /* Print methods as they appear in "method expressions". */
381 if (method_type_package_name
!= NULL
)
383 /* FIXME: Seems like we should include package_name here somewhere. */
384 if (method_type_is_pointer
)
385 obstack_grow_str (&tempbuf
, "(*");
386 obstack_grow_str (&tempbuf
, method_type_package_name
);
387 obstack_grow_str (&tempbuf
, ".");
388 obstack_grow_str (&tempbuf
, method_type_object_name
);
389 if (method_type_is_pointer
)
390 obstack_grow_str (&tempbuf
, ")");
391 obstack_grow_str (&tempbuf
, ".");
392 obstack_grow_str (&tempbuf
, object_name
);
396 obstack_grow_str (&tempbuf
, package_name
);
397 obstack_grow_str (&tempbuf
, ".");
398 obstack_grow_str (&tempbuf
, object_name
);
400 obstack_grow_str0 (&tempbuf
, "");
402 return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf
));
407 gdb::unique_xmalloc_ptr
<char>
408 go_symbol_package_name (const struct symbol
*sym
)
410 const char *mangled_name
= sym
->linkage_name ();
411 const char *package_name
;
412 const char *object_name
;
413 const char *method_type_package_name
;
414 const char *method_type_object_name
;
415 int method_type_is_pointer
;
416 gdb::unique_xmalloc_ptr
<char> name_buf
;
418 if (sym
->language () != language_go
)
420 name_buf
= unpack_mangled_go_symbol (mangled_name
,
421 &package_name
, &object_name
,
422 &method_type_package_name
,
423 &method_type_object_name
,
424 &method_type_is_pointer
);
425 /* Some Go symbols don't have mangled form we interpret (yet). */
426 if (name_buf
== NULL
)
428 return make_unique_xstrdup (package_name
);
433 gdb::unique_xmalloc_ptr
<char>
434 go_block_package_name (const struct block
*block
)
436 while (block
!= NULL
)
438 struct symbol
*function
= block
->function ();
440 if (function
!= NULL
)
442 gdb::unique_xmalloc_ptr
<char> package_name
443 = go_symbol_package_name (function
);
445 if (package_name
!= NULL
)
448 /* Stop looking if we find a function without a package name.
449 We're most likely outside of Go and thus the concept of the
450 "current" package is gone. */
454 block
= block
->superblock ();
460 /* See language.h. */
463 go_language::language_arch_info (struct gdbarch
*gdbarch
,
464 struct language_arch_info
*lai
) const
466 const struct builtin_go_type
*builtin
= builtin_go_type (gdbarch
);
468 /* Helper function to allow shorter lines below. */
469 auto add
= [&] (struct type
* t
) -> struct type
*
471 lai
->add_primitive_type (t
);
475 add (builtin
->builtin_void
);
476 add (builtin
->builtin_char
);
477 add (builtin
->builtin_bool
);
478 add (builtin
->builtin_int
);
479 add (builtin
->builtin_uint
);
480 add (builtin
->builtin_uintptr
);
481 add (builtin
->builtin_int8
);
482 add (builtin
->builtin_int16
);
483 add (builtin
->builtin_int32
);
484 add (builtin
->builtin_int64
);
485 add (builtin
->builtin_uint8
);
486 add (builtin
->builtin_uint16
);
487 add (builtin
->builtin_uint32
);
488 add (builtin
->builtin_uint64
);
489 add (builtin
->builtin_float32
);
490 add (builtin
->builtin_float64
);
491 add (builtin
->builtin_complex64
);
492 add (builtin
->builtin_complex128
);
494 lai
->set_string_char_type (builtin
->builtin_char
);
495 lai
->set_bool_type (builtin
->builtin_bool
, "bool");
498 /* Single instance of the Go language class. */
500 static go_language go_language_defn
;
502 static struct builtin_go_type
*
503 build_go_types (struct gdbarch
*gdbarch
)
505 struct builtin_go_type
*builtin_go_type
= new struct builtin_go_type
;
507 type_allocator
alloc (gdbarch
);
508 builtin_go_type
->builtin_void
= builtin_type (gdbarch
)->builtin_void
;
509 builtin_go_type
->builtin_char
510 = init_character_type (alloc
, 8, 1, "char");
511 builtin_go_type
->builtin_bool
512 = init_boolean_type (alloc
, 8, 0, "bool");
513 builtin_go_type
->builtin_int
514 = init_integer_type (alloc
, gdbarch_int_bit (gdbarch
), 0, "int");
515 builtin_go_type
->builtin_uint
516 = init_integer_type (alloc
, gdbarch_int_bit (gdbarch
), 1, "uint");
517 builtin_go_type
->builtin_uintptr
518 = init_integer_type (alloc
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr");
519 builtin_go_type
->builtin_int8
520 = init_integer_type (alloc
, 8, 0, "int8");
521 builtin_go_type
->builtin_int16
522 = init_integer_type (alloc
, 16, 0, "int16");
523 builtin_go_type
->builtin_int32
524 = init_integer_type (alloc
, 32, 0, "int32");
525 builtin_go_type
->builtin_int64
526 = init_integer_type (alloc
, 64, 0, "int64");
527 builtin_go_type
->builtin_uint8
528 = init_integer_type (alloc
, 8, 1, "uint8");
529 builtin_go_type
->builtin_uint16
530 = init_integer_type (alloc
, 16, 1, "uint16");
531 builtin_go_type
->builtin_uint32
532 = init_integer_type (alloc
, 32, 1, "uint32");
533 builtin_go_type
->builtin_uint64
534 = init_integer_type (alloc
, 64, 1, "uint64");
535 builtin_go_type
->builtin_float32
536 = init_float_type (alloc
, 32, "float32", floatformats_ieee_single
);
537 builtin_go_type
->builtin_float64
538 = init_float_type (alloc
, 64, "float64", floatformats_ieee_double
);
539 builtin_go_type
->builtin_complex64
540 = init_complex_type ("complex64", builtin_go_type
->builtin_float32
);
541 builtin_go_type
->builtin_complex128
542 = init_complex_type ("complex128", builtin_go_type
->builtin_float64
);
544 return builtin_go_type
;
547 static const registry
<gdbarch
>::key
<struct builtin_go_type
> go_type_data
;
549 const struct builtin_go_type
*
550 builtin_go_type (struct gdbarch
*gdbarch
)
552 struct builtin_go_type
*result
= go_type_data
.get (gdbarch
);
553 if (result
== nullptr)
555 result
= build_go_types (gdbarch
);
556 go_type_data
.set (gdbarch
, result
);