]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/go-lang.c
RISC-V: [gprofng] Allow building gprofng without asm/hwprobe.h
[thirdparty/binutils-gdb.git] / gdb / go-lang.c
CommitLineData
a766d390
DE
1/* Go language support routines for GDB, the GNU debugger.
2
d01e8234 3 Copyright (C) 2012-2025 Free Software Foundation, Inc.
a766d390
DE
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
bf31fd38 34#include "gdbsupport/gdb_obstack.h"
a766d390
DE
35#include "block.h"
36#include "symtab.h"
37#include "language.h"
a53b64ea 38#include "varobj.h"
a766d390
DE
39#include "go-lang.h"
40#include "c-lang.h"
41#include "parser-defs.h"
0d12e84c 42#include "gdbarch.h"
a766d390
DE
43
44#include <ctype.h>
45
46/* The main function in the main package. */
47static 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
54const char *
55go_main_name (void)
56{
4144d36a
SM
57 bound_minimal_symbol msym
58 = lookup_minimal_symbol (current_program_space, GO_MAIN_MAIN);
3b7344d5 59 if (msym.minsym != NULL)
a766d390
DE
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
69static int
70gccgo_string_p (struct type *type)
71{
72 /* gccgo strings don't necessarily have a name we can use. */
73
1f704f76 74 if (type->num_fields () == 2)
a766d390 75 {
940da03e
SM
76 struct type *type0 = type->field (0).type ();
77 struct type *type1 = type->field (1).type ();
a766d390 78
f168693b
SM
79 type0 = check_typedef (type0);
80 type1 = check_typedef (type1);
a766d390 81
78134374 82 if (type0->code () == TYPE_CODE_PTR
33d16dd9 83 && strcmp (type->field (0).name (), "__data") == 0
78134374 84 && type1->code () == TYPE_CODE_INT
33d16dd9 85 && strcmp (type->field (1).name (), "__length") == 0)
a766d390 86 {
27710edb 87 struct type *target_type = type0->target_type ();
a766d390 88
f168693b 89 target_type = check_typedef (target_type);
a766d390 90
78134374 91 if (target_type->code () == TYPE_CODE_INT
df86565b 92 && target_type->length () == 1
7d93a1e0 93 && strcmp (target_type->name (), "uint8") == 0)
a766d390
DE
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
104static int
105sixg_string_p (struct type *type)
106{
1f704f76 107 if (type->num_fields () == 2
7d93a1e0
SM
108 && type->name () != NULL
109 && strcmp (type->name (), "string") == 0)
a766d390
DE
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
118enum go_type
119go_classify_struct_type (struct type *type)
120{
f168693b 121 type = check_typedef (type);
a766d390
DE
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
137static void
138unpack_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.
a766d390
DE
164 A pointer to this buffer is returned, or NULL if symbol isn't a
165 mangled Go symbol.
a766d390
DE
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
be643e07 178static gdb::unique_xmalloc_ptr<char>
a766d390
DE
179unpack_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 '.'. */
e6a959d6 194 const char *first_dot;
a766d390 195 /* Pointer to the last '.'. */
e6a959d6 196 const char *last_dot;
a766d390
DE
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 {
be643e07
TT
207 gdb::unique_xmalloc_ptr<char> package
208 = make_unique_xstrdup ("main");
a766d390 209
be643e07 210 *packagep = package.get ();
a766d390
DE
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 {
be643e07
TT
218 gdb::unique_xmalloc_ptr<char> package
219 = make_unique_xstrdup ("main");
a766d390 220
be643e07 221 *packagep = package.get ();
a766d390
DE
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). */
3f577261
TV
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
a766d390
DE
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;
3f577261 249
a766d390 250 /* It must have at least two dots. */
3f577261
TV
251 if (v3)
252 first_dot = strchr (mangled_name, '0');
253 else
254 first_dot = strchr (mangled_name, '.');
255
a766d390
DE
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
be643e07
TT
273 gdb::unique_xmalloc_ptr<char> result = make_unique_xstrdup (mangled_name);
274 buf = result.get ();
a766d390 275
3f577261
TV
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
a766d390
DE
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 (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);
be643e07 342 return result;
a766d390
DE
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
3456e70c 358gdb::unique_xmalloc_ptr<char>
82fc57fd 359go_language::demangle_symbol (const char *mangled_name, int options) const
a766d390 360{
a766d390
DE
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
3456e70c
TT
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));
a766d390
DE
376 if (name_buf == NULL)
377 return NULL;
378
3456e70c 379 auto_obstack tempbuf;
a766d390
DE
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
3456e70c 403 return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
a766d390
DE
404}
405
be643e07 406/* See go-lang.h. */
a766d390 407
be643e07 408gdb::unique_xmalloc_ptr<char>
a766d390
DE
409go_symbol_package_name (const struct symbol *sym)
410{
987012b8 411 const char *mangled_name = sym->linkage_name ();
a766d390
DE
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;
be643e07 417 gdb::unique_xmalloc_ptr<char> name_buf;
a766d390 418
e8eca7a6
TT
419 if (sym->language () != language_go)
420 return nullptr;
a766d390
DE
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;
be643e07 429 return make_unique_xstrdup (package_name);
a766d390
DE
430}
431
be643e07 432/* See go-lang.h. */
a766d390 433
be643e07 434gdb::unique_xmalloc_ptr<char>
a766d390
DE
435go_block_package_name (const struct block *block)
436{
437 while (block != NULL)
438 {
6c00f721 439 struct symbol *function = block->function ();
a766d390
DE
440
441 if (function != NULL)
442 {
be643e07
TT
443 gdb::unique_xmalloc_ptr<char> package_name
444 = go_symbol_package_name (function);
a766d390
DE
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
f135fe72 455 block = block->superblock ();
a766d390
DE
456 }
457
458 return NULL;
459}
460
82fc57fd 461/* See language.h. */
0874fd07 462
82fc57fd
AB
463void
464go_language::language_arch_info (struct gdbarch *gdbarch,
465 struct language_arch_info *lai) const
0874fd07 466{
82fc57fd 467 const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
0a50df5d 468
82fc57fd
AB
469 /* Helper function to allow shorter lines below. */
470 auto add = [&] (struct type * t) -> struct type *
fbfb0a46 471 {
82fc57fd
AB
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}
0874fd07
AB
498
499/* Single instance of the Go language class. */
500
501static go_language go_language_defn;
502
cb275538 503static struct builtin_go_type *
a766d390
DE
504build_go_types (struct gdbarch *gdbarch)
505{
cb275538 506 struct builtin_go_type *builtin_go_type = new struct builtin_go_type;
a766d390 507
2d39ccd3 508 type_allocator alloc (gdbarch);
95751990 509 builtin_go_type->builtin_void = builtin_type (gdbarch)->builtin_void;
a766d390 510 builtin_go_type->builtin_char
f50b437c 511 = init_character_type (alloc, 8, 1, "char");
a766d390 512 builtin_go_type->builtin_bool
46c04ea3 513 = init_boolean_type (alloc, 8, 0, "bool");
a766d390 514 builtin_go_type->builtin_int
2d39ccd3 515 = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 0, "int");
a766d390 516 builtin_go_type->builtin_uint
2d39ccd3 517 = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 1, "uint");
a766d390 518 builtin_go_type->builtin_uintptr
2d39ccd3 519 = init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
a766d390 520 builtin_go_type->builtin_int8
2d39ccd3 521 = init_integer_type (alloc, 8, 0, "int8");
a766d390 522 builtin_go_type->builtin_int16
2d39ccd3 523 = init_integer_type (alloc, 16, 0, "int16");
a766d390 524 builtin_go_type->builtin_int32
2d39ccd3 525 = init_integer_type (alloc, 32, 0, "int32");
a766d390 526 builtin_go_type->builtin_int64
2d39ccd3 527 = init_integer_type (alloc, 64, 0, "int64");
a766d390 528 builtin_go_type->builtin_uint8
2d39ccd3 529 = init_integer_type (alloc, 8, 1, "uint8");
a766d390 530 builtin_go_type->builtin_uint16
2d39ccd3 531 = init_integer_type (alloc, 16, 1, "uint16");
a766d390 532 builtin_go_type->builtin_uint32
2d39ccd3 533 = init_integer_type (alloc, 32, 1, "uint32");
a766d390 534 builtin_go_type->builtin_uint64
2d39ccd3 535 = init_integer_type (alloc, 64, 1, "uint64");
a766d390 536 builtin_go_type->builtin_float32
77c5f496 537 = init_float_type (alloc, 32, "float32", floatformats_ieee_single);
a766d390 538 builtin_go_type->builtin_float64
77c5f496 539 = init_float_type (alloc, 64, "float64", floatformats_ieee_double);
a766d390 540 builtin_go_type->builtin_complex64
5b930b45 541 = init_complex_type ("complex64", builtin_go_type->builtin_float32);
a766d390 542 builtin_go_type->builtin_complex128
5b930b45 543 = init_complex_type ("complex128", builtin_go_type->builtin_float64);
a766d390
DE
544
545 return builtin_go_type;
546}
547
cb275538 548static const registry<gdbarch>::key<struct builtin_go_type> go_type_data;
a766d390
DE
549
550const struct builtin_go_type *
551builtin_go_type (struct gdbarch *gdbarch)
552{
cb275538
TT
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 }
a766d390 559
cb275538 560 return result;
a766d390 561}