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