]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/go-lang.c
gdb: fix --args handling when inferior argument have dash
[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 43
a766d390
DE
44
45/* The main function in the main package. */
46static const char GO_MAIN_MAIN[] = "main.main";
47
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. */
52
53const char *
54go_main_name (void)
55{
4144d36a
SM
56 bound_minimal_symbol msym
57 = lookup_minimal_symbol (current_program_space, GO_MAIN_MAIN);
3b7344d5 58 if (msym.minsym != NULL)
a766d390
DE
59 return GO_MAIN_MAIN;
60
61 /* No known entry procedure found, the main program is probably not Go. */
62 return NULL;
63}
64
65/* Return non-zero if TYPE is a gccgo string.
66 We assume CHECK_TYPEDEF has already been done. */
67
68static int
69gccgo_string_p (struct type *type)
70{
71 /* gccgo strings don't necessarily have a name we can use. */
72
1f704f76 73 if (type->num_fields () == 2)
a766d390 74 {
940da03e
SM
75 struct type *type0 = type->field (0).type ();
76 struct type *type1 = type->field (1).type ();
a766d390 77
f168693b
SM
78 type0 = check_typedef (type0);
79 type1 = check_typedef (type1);
a766d390 80
78134374 81 if (type0->code () == TYPE_CODE_PTR
33d16dd9 82 && strcmp (type->field (0).name (), "__data") == 0
78134374 83 && type1->code () == TYPE_CODE_INT
33d16dd9 84 && strcmp (type->field (1).name (), "__length") == 0)
a766d390 85 {
27710edb 86 struct type *target_type = type0->target_type ();
a766d390 87
f168693b 88 target_type = check_typedef (target_type);
a766d390 89
78134374 90 if (target_type->code () == TYPE_CODE_INT
df86565b 91 && target_type->length () == 1
7d93a1e0 92 && strcmp (target_type->name (), "uint8") == 0)
a766d390
DE
93 return 1;
94 }
95 }
96
97 return 0;
98}
99
100/* Return non-zero if TYPE is a 6g string.
101 We assume CHECK_TYPEDEF has already been done. */
102
103static int
104sixg_string_p (struct type *type)
105{
1f704f76 106 if (type->num_fields () == 2
7d93a1e0
SM
107 && type->name () != NULL
108 && strcmp (type->name (), "string") == 0)
a766d390
DE
109 return 1;
110
111 return 0;
112}
113
114/* Classify the kind of Go object that TYPE is.
115 TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
116
117enum go_type
118go_classify_struct_type (struct type *type)
119{
f168693b 120 type = check_typedef (type);
a766d390
DE
121
122 /* Recognize strings as they're useful to be able to print without
123 pretty-printers. */
124 if (gccgo_string_p (type)
125 || sixg_string_p (type))
126 return GO_TYPE_STRING;
127
128 return GO_TYPE_NONE;
129}
130
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. */
135
136static void
137unpack_package_and_object (char *buf,
138 const char **packagep, const char **objectp)
139{
140 char *last_dot;
141
142 last_dot = strrchr (buf, '.');
143 gdb_assert (last_dot != NULL);
144 *objectp = last_dot + 1;
145 *last_dot = '\0';
146 last_dot = strrchr (buf, '.');
147 if (last_dot != NULL)
148 *packagep = last_dot + 1;
149 else
150 *packagep = buf;
151}
152
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"
157 *OBJECTP = "String"
158 *METHOD_TYPE_PACKAGEP = "textproto"
159 *METHOD_TYPE_OBJECTP = "ProtocolError"
160
161 Space for the resulting strings is malloc'd in one buffer.
162 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
a766d390
DE
163 A pointer to this buffer is returned, or NULL if symbol isn't a
164 mangled Go symbol.
a766d390
DE
165
166 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
167 the method type is a pointer.
168
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.
173
174 If we ever need to unpack the method type, this routine should work
175 for that too. */
176
be643e07 177static gdb::unique_xmalloc_ptr<char>
a766d390
DE
178unpack_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)
184{
185 char *buf;
186 char *p;
187 int len = strlen (mangled_name);
188 /* Pointer to last digit in "N<digit(s)>_". */
189 char *saw_digit;
190 /* Pointer to "N" if valid "N<digit(s)>_" found. */
191 char *method_type;
192 /* Pointer to the first '.'. */
e6a959d6 193 const char *first_dot;
a766d390 194 /* Pointer to the last '.'. */
e6a959d6 195 const char *last_dot;
a766d390
DE
196 /* Non-zero if we saw a pointer indicator. */
197 int saw_pointer;
198
199 *packagep = *objectp = NULL;
200 *method_type_packagep = *method_type_objectp = NULL;
201 *method_type_is_pointerp = 0;
202
203 /* main.init is mangled specially. */
204 if (strcmp (mangled_name, "__go_init_main") == 0)
205 {
be643e07
TT
206 gdb::unique_xmalloc_ptr<char> package
207 = make_unique_xstrdup ("main");
a766d390 208
be643e07 209 *packagep = package.get ();
a766d390
DE
210 *objectp = "init";
211 return package;
212 }
213
214 /* main.main is mangled specially (missing prefix). */
215 if (strcmp (mangled_name, "main.main") == 0)
216 {
be643e07
TT
217 gdb::unique_xmalloc_ptr<char> package
218 = make_unique_xstrdup ("main");
a766d390 219
be643e07 220 *packagep = package.get ();
a766d390
DE
221 *objectp = "main";
222 return package;
223 }
224
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:
228
229 go: the default
230 libgo_.*: used by gccgo's runtime
231
232 Thus we don't support -fgo-prefix (except as used by the runtime). */
3f577261
TV
233 bool v3;
234 if (startswith (mangled_name, "go_0"))
235 /* V3 mangling detected, see
236 https://go-review.googlesource.com/c/gofrontend/+/271726 . */
237 v3 = true;
238 else if (startswith (mangled_name, "go.")
239 || startswith (mangled_name, "libgo_"))
240 v3 = false;
241 else
a766d390
DE
242 return NULL;
243
244 /* Quick check for whether a search may be fruitful. */
245 /* Ignore anything with @plt, etc. in it. */
246 if (strchr (mangled_name, '@') != NULL)
247 return NULL;
3f577261 248
a766d390 249 /* It must have at least two dots. */
3f577261
TV
250 if (v3)
251 first_dot = strchr (mangled_name, '0');
252 else
253 first_dot = strchr (mangled_name, '.');
254
a766d390
DE
255 if (first_dot == NULL)
256 return 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)
263 return NULL;
264
265 /* More quick checks. */
266 if (last_dot[1] == '\0' /* foo. */
267 || last_dot[-1] == '.') /* foo..bar */
268 return NULL;
269
270 /* At this point we've decided we have a mangled Go symbol. */
271
be643e07
TT
272 gdb::unique_xmalloc_ptr<char> result = make_unique_xstrdup (mangled_name);
273 buf = result.get ();
a766d390 274
3f577261
TV
275 if (v3)
276 {
277 /* Replace "go_0" with "\0go.". */
278 buf[0] = '\0';
279 buf[1] = 'g';
280 buf[2] = 'o';
281 buf[3] = '.';
282
283 /* Skip the '\0'. */
284 buf++;
285 }
286
a766d390
DE
287 /* Search backwards looking for "N<digit(s)>". */
288 p = buf + len;
289 saw_digit = method_type = NULL;
290 saw_pointer = 0;
291 while (p > buf)
292 {
293 int current = *(const unsigned char *) --p;
37194720 294 int current_is_digit = c_isdigit (current);
a766d390
DE
295
296 if (saw_digit)
297 {
298 if (current_is_digit)
299 continue;
300 if (current == 'N'
301 && ((p > buf && p[-1] == '.')
302 || (p > buf + 1 && p[-1] == 'p' && p[-2] == '.')))
303 {
304 if (atoi (p + 1) == strlen (saw_digit + 2))
305 {
306 if (p[-1] == '.')
307 method_type = p - 1;
308 else
309 {
310 gdb_assert (p[-1] == 'p');
311 saw_pointer = 1;
312 method_type = p - 2;
313 }
314 break;
315 }
316 }
317 /* Not what we're looking for, reset and keep looking. */
318 saw_digit = NULL;
319 saw_pointer = 0;
320 continue;
321 }
322 if (current_is_digit && p[1] == '_')
323 {
324 /* Possible start of method "this" [sic] type. */
325 saw_digit = p;
326 continue;
327 }
328 }
329
330 if (method_type != NULL
331 /* Ensure not something like "..foo". */
332 && (method_type > buf && method_type[-1] != '.'))
333 {
334 unpack_package_and_object (saw_digit + 2,
335 method_type_packagep, method_type_objectp);
336 *method_type = '\0';
337 *method_type_is_pointerp = saw_pointer;
338 }
339
340 unpack_package_and_object (buf, packagep, objectp);
be643e07 341 return result;
a766d390
DE
342}
343
344/* Implements the la_demangle language_defn routine for language Go.
345
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
349 are today.
350
351 N.B. This currently only supports gccgo's mangling.
352
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. */
356
3456e70c 357gdb::unique_xmalloc_ptr<char>
82fc57fd 358go_language::demangle_symbol (const char *mangled_name, int options) const
a766d390 359{
a766d390
DE
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;
365
366 if (mangled_name == NULL)
367 return NULL;
368
3456e70c
TT
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));
a766d390
DE
375 if (name_buf == NULL)
376 return NULL;
377
3456e70c 378 auto_obstack tempbuf;
a766d390
DE
379
380 /* Print methods as they appear in "method expressions". */
381 if (method_type_package_name != NULL)
382 {
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);
393 }
394 else
395 {
396 obstack_grow_str (&tempbuf, package_name);
397 obstack_grow_str (&tempbuf, ".");
398 obstack_grow_str (&tempbuf, object_name);
399 }
400 obstack_grow_str0 (&tempbuf, "");
401
3456e70c 402 return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
a766d390
DE
403}
404
be643e07 405/* See go-lang.h. */
a766d390 406
be643e07 407gdb::unique_xmalloc_ptr<char>
a766d390
DE
408go_symbol_package_name (const struct symbol *sym)
409{
987012b8 410 const char *mangled_name = sym->linkage_name ();
a766d390
DE
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;
be643e07 416 gdb::unique_xmalloc_ptr<char> name_buf;
a766d390 417
e8eca7a6
TT
418 if (sym->language () != language_go)
419 return nullptr;
a766d390
DE
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)
427 return NULL;
be643e07 428 return make_unique_xstrdup (package_name);
a766d390
DE
429}
430
be643e07 431/* See go-lang.h. */
a766d390 432
be643e07 433gdb::unique_xmalloc_ptr<char>
a766d390
DE
434go_block_package_name (const struct block *block)
435{
436 while (block != NULL)
437 {
6c00f721 438 struct symbol *function = block->function ();
a766d390
DE
439
440 if (function != NULL)
441 {
be643e07
TT
442 gdb::unique_xmalloc_ptr<char> package_name
443 = go_symbol_package_name (function);
a766d390
DE
444
445 if (package_name != NULL)
446 return package_name;
447
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. */
451 return NULL;
452 }
453
f135fe72 454 block = block->superblock ();
a766d390
DE
455 }
456
457 return NULL;
458}
459
82fc57fd 460/* See language.h. */
0874fd07 461
82fc57fd
AB
462void
463go_language::language_arch_info (struct gdbarch *gdbarch,
464 struct language_arch_info *lai) const
0874fd07 465{
82fc57fd 466 const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
0a50df5d 467
82fc57fd
AB
468 /* Helper function to allow shorter lines below. */
469 auto add = [&] (struct type * t) -> struct type *
fbfb0a46 470 {
82fc57fd
AB
471 lai->add_primitive_type (t);
472 return t;
473 };
474
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);
493
494 lai->set_string_char_type (builtin->builtin_char);
495 lai->set_bool_type (builtin->builtin_bool, "bool");
496}
0874fd07
AB
497
498/* Single instance of the Go language class. */
499
500static go_language go_language_defn;
501
cb275538 502static struct builtin_go_type *
a766d390
DE
503build_go_types (struct gdbarch *gdbarch)
504{
cb275538 505 struct builtin_go_type *builtin_go_type = new struct builtin_go_type;
a766d390 506
2d39ccd3 507 type_allocator alloc (gdbarch);
95751990 508 builtin_go_type->builtin_void = builtin_type (gdbarch)->builtin_void;
a766d390 509 builtin_go_type->builtin_char
f50b437c 510 = init_character_type (alloc, 8, 1, "char");
a766d390 511 builtin_go_type->builtin_bool
46c04ea3 512 = init_boolean_type (alloc, 8, 0, "bool");
a766d390 513 builtin_go_type->builtin_int
2d39ccd3 514 = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 0, "int");
a766d390 515 builtin_go_type->builtin_uint
2d39ccd3 516 = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 1, "uint");
a766d390 517 builtin_go_type->builtin_uintptr
2d39ccd3 518 = init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
a766d390 519 builtin_go_type->builtin_int8
2d39ccd3 520 = init_integer_type (alloc, 8, 0, "int8");
a766d390 521 builtin_go_type->builtin_int16
2d39ccd3 522 = init_integer_type (alloc, 16, 0, "int16");
a766d390 523 builtin_go_type->builtin_int32
2d39ccd3 524 = init_integer_type (alloc, 32, 0, "int32");
a766d390 525 builtin_go_type->builtin_int64
2d39ccd3 526 = init_integer_type (alloc, 64, 0, "int64");
a766d390 527 builtin_go_type->builtin_uint8
2d39ccd3 528 = init_integer_type (alloc, 8, 1, "uint8");
a766d390 529 builtin_go_type->builtin_uint16
2d39ccd3 530 = init_integer_type (alloc, 16, 1, "uint16");
a766d390 531 builtin_go_type->builtin_uint32
2d39ccd3 532 = init_integer_type (alloc, 32, 1, "uint32");
a766d390 533 builtin_go_type->builtin_uint64
2d39ccd3 534 = init_integer_type (alloc, 64, 1, "uint64");
a766d390 535 builtin_go_type->builtin_float32
77c5f496 536 = init_float_type (alloc, 32, "float32", floatformats_ieee_single);
a766d390 537 builtin_go_type->builtin_float64
77c5f496 538 = init_float_type (alloc, 64, "float64", floatformats_ieee_double);
a766d390 539 builtin_go_type->builtin_complex64
5b930b45 540 = init_complex_type ("complex64", builtin_go_type->builtin_float32);
a766d390 541 builtin_go_type->builtin_complex128
5b930b45 542 = init_complex_type ("complex128", builtin_go_type->builtin_float64);
a766d390
DE
543
544 return builtin_go_type;
545}
546
cb275538 547static const registry<gdbarch>::key<struct builtin_go_type> go_type_data;
a766d390
DE
548
549const struct builtin_go_type *
550builtin_go_type (struct gdbarch *gdbarch)
551{
cb275538
TT
552 struct builtin_go_type *result = go_type_data.get (gdbarch);
553 if (result == nullptr)
554 {
555 result = build_go_types (gdbarch);
556 go_type_data.set (gdbarch, result);
557 }
a766d390 558
cb275538 559 return result;
a766d390 560}