testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / go-lang.c
blob18d3fc1a008e6454a4320494c5f50dcf1d96a13e
1 /* Go language support routines for GDB, the GNU debugger.
3 Copyright (C) 2012-2024 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/>. */
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
34 #include "gdbsupport/gdb_obstack.h"
35 #include "block.h"
36 #include "symtab.h"
37 #include "language.h"
38 #include "varobj.h"
39 #include "go-lang.h"
40 #include "c-lang.h"
41 #include "parser-defs.h"
42 #include "gdbarch.h"
44 #include <ctype.h>
46 /* The main function in the main package. */
47 static const char GO_MAIN_MAIN[] = "main.main";
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. */
54 const char *
55 go_main_name (void)
57 bound_minimal_symbol msym
58 = lookup_minimal_symbol (current_program_space, GO_MAIN_MAIN);
59 if (msym.minsym != NULL)
60 return GO_MAIN_MAIN;
62 /* No known entry procedure found, the main program is probably not Go. */
63 return NULL;
66 /* Return non-zero if TYPE is a gccgo string.
67 We assume CHECK_TYPEDEF has already been done. */
69 static int
70 gccgo_string_p (struct type *type)
72 /* gccgo strings don't necessarily have a name we can use. */
74 if (type->num_fields () == 2)
76 struct type *type0 = type->field (0).type ();
77 struct type *type1 = type->field (1).type ();
79 type0 = check_typedef (type0);
80 type1 = check_typedef (type1);
82 if (type0->code () == TYPE_CODE_PTR
83 && strcmp (type->field (0).name (), "__data") == 0
84 && type1->code () == TYPE_CODE_INT
85 && strcmp (type->field (1).name (), "__length") == 0)
87 struct type *target_type = type0->target_type ();
89 target_type = check_typedef (target_type);
91 if (target_type->code () == TYPE_CODE_INT
92 && target_type->length () == 1
93 && strcmp (target_type->name (), "uint8") == 0)
94 return 1;
98 return 0;
101 /* Return non-zero if TYPE is a 6g string.
102 We assume CHECK_TYPEDEF has already been done. */
104 static int
105 sixg_string_p (struct type *type)
107 if (type->num_fields () == 2
108 && type->name () != NULL
109 && strcmp (type->name (), "string") == 0)
110 return 1;
112 return 0;
115 /* Classify the kind of Go object that TYPE is.
116 TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
118 enum go_type
119 go_classify_struct_type (struct type *type)
121 type = check_typedef (type);
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;
129 return GO_TYPE_NONE;
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. */
137 static void
138 unpack_package_and_object (char *buf,
139 const char **packagep, const char **objectp)
141 char *last_dot;
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;
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"
162 Space for the resulting strings is malloc'd in one buffer.
163 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
164 A pointer to this buffer is returned, or NULL if symbol isn't a
165 mangled Go symbol.
167 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
168 the method type is a pointer.
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.
175 If we ever need to unpack the method type, this routine should work
176 for that too. */
178 static gdb::unique_xmalloc_ptr<char>
179 unpack_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)
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 '.'. */
194 const char *first_dot;
195 /* Pointer to the last '.'. */
196 const char *last_dot;
197 /* Non-zero if we saw a pointer indicator. */
198 int saw_pointer;
200 *packagep = *objectp = NULL;
201 *method_type_packagep = *method_type_objectp = NULL;
202 *method_type_is_pointerp = 0;
204 /* main.init is mangled specially. */
205 if (strcmp (mangled_name, "__go_init_main") == 0)
207 gdb::unique_xmalloc_ptr<char> package
208 = make_unique_xstrdup ("main");
210 *packagep = package.get ();
211 *objectp = "init";
212 return package;
215 /* main.main is mangled specially (missing prefix). */
216 if (strcmp (mangled_name, "main.main") == 0)
218 gdb::unique_xmalloc_ptr<char> package
219 = make_unique_xstrdup ("main");
221 *packagep = package.get ();
222 *objectp = "main";
223 return package;
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:
230 go: the default
231 libgo_.*: used by gccgo's runtime
233 Thus we don't support -fgo-prefix (except as used by the runtime). */
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
243 return NULL;
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;
250 /* It must have at least two dots. */
251 if (v3)
252 first_dot = strchr (mangled_name, '0');
253 else
254 first_dot = strchr (mangled_name, '.');
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;
266 /* More quick checks. */
267 if (last_dot[1] == '\0' /* foo. */
268 || last_dot[-1] == '.') /* foo..bar */
269 return NULL;
271 /* At this point we've decided we have a mangled Go symbol. */
273 gdb::unique_xmalloc_ptr<char> result = make_unique_xstrdup (mangled_name);
274 buf = result.get ();
276 if (v3)
278 /* Replace "go_0" with "\0go.". */
279 buf[0] = '\0';
280 buf[1] = 'g';
281 buf[2] = 'o';
282 buf[3] = '.';
284 /* Skip the '\0'. */
285 buf++;
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)
294 int current = *(const unsigned char *) --p;
295 int current_is_digit = isdigit (current);
297 if (saw_digit)
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] == '.')))
305 if (atoi (p + 1) == strlen (saw_digit + 2))
307 if (p[-1] == '.')
308 method_type = p - 1;
309 else
311 gdb_assert (p[-1] == 'p');
312 saw_pointer = 1;
313 method_type = p - 2;
315 break;
318 /* Not what we're looking for, reset and keep looking. */
319 saw_digit = NULL;
320 saw_pointer = 0;
321 continue;
323 if (current_is_digit && p[1] == '_')
325 /* Possible start of method "this" [sic] type. */
326 saw_digit = p;
327 continue;
331 if (method_type != NULL
332 /* Ensure not something like "..foo". */
333 && (method_type > buf && method_type[-1] != '.'))
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;
341 unpack_package_and_object (buf, packagep, objectp);
342 return result;
345 /* Implements the la_demangle language_defn routine for language Go.
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.
352 N.B. This currently only supports gccgo's mangling.
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. */
358 gdb::unique_xmalloc_ptr<char>
359 go_language::demangle_symbol (const char *mangled_name, int options) const
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;
367 if (mangled_name == NULL)
368 return NULL;
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));
376 if (name_buf == NULL)
377 return NULL;
379 auto_obstack tempbuf;
381 /* Print methods as they appear in "method expressions". */
382 if (method_type_package_name != NULL)
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);
395 else
397 obstack_grow_str (&tempbuf, package_name);
398 obstack_grow_str (&tempbuf, ".");
399 obstack_grow_str (&tempbuf, object_name);
401 obstack_grow_str0 (&tempbuf, "");
403 return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
406 /* See go-lang.h. */
408 gdb::unique_xmalloc_ptr<char>
409 go_symbol_package_name (const struct symbol *sym)
411 const char *mangled_name = sym->linkage_name ();
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;
417 gdb::unique_xmalloc_ptr<char> name_buf;
419 if (sym->language () != language_go)
420 return nullptr;
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;
429 return make_unique_xstrdup (package_name);
432 /* See go-lang.h. */
434 gdb::unique_xmalloc_ptr<char>
435 go_block_package_name (const struct block *block)
437 while (block != NULL)
439 struct symbol *function = block->function ();
441 if (function != NULL)
443 gdb::unique_xmalloc_ptr<char> package_name
444 = go_symbol_package_name (function);
446 if (package_name != NULL)
447 return package_name;
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;
455 block = block->superblock ();
458 return NULL;
461 /* See language.h. */
463 void
464 go_language::language_arch_info (struct gdbarch *gdbarch,
465 struct language_arch_info *lai) const
467 const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
469 /* Helper function to allow shorter lines below. */
470 auto add = [&] (struct type * t) -> struct type *
472 lai->add_primitive_type (t);
473 return t;
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);
495 lai->set_string_char_type (builtin->builtin_char);
496 lai->set_bool_type (builtin->builtin_bool, "bool");
499 /* Single instance of the Go language class. */
501 static go_language go_language_defn;
503 static struct builtin_go_type *
504 build_go_types (struct gdbarch *gdbarch)
506 struct builtin_go_type *builtin_go_type = new struct builtin_go_type;
508 type_allocator alloc (gdbarch);
509 builtin_go_type->builtin_void = builtin_type (gdbarch)->builtin_void;
510 builtin_go_type->builtin_char
511 = init_character_type (alloc, 8, 1, "char");
512 builtin_go_type->builtin_bool
513 = init_boolean_type (alloc, 8, 0, "bool");
514 builtin_go_type->builtin_int
515 = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 0, "int");
516 builtin_go_type->builtin_uint
517 = init_integer_type (alloc, gdbarch_int_bit (gdbarch), 1, "uint");
518 builtin_go_type->builtin_uintptr
519 = init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
520 builtin_go_type->builtin_int8
521 = init_integer_type (alloc, 8, 0, "int8");
522 builtin_go_type->builtin_int16
523 = init_integer_type (alloc, 16, 0, "int16");
524 builtin_go_type->builtin_int32
525 = init_integer_type (alloc, 32, 0, "int32");
526 builtin_go_type->builtin_int64
527 = init_integer_type (alloc, 64, 0, "int64");
528 builtin_go_type->builtin_uint8
529 = init_integer_type (alloc, 8, 1, "uint8");
530 builtin_go_type->builtin_uint16
531 = init_integer_type (alloc, 16, 1, "uint16");
532 builtin_go_type->builtin_uint32
533 = init_integer_type (alloc, 32, 1, "uint32");
534 builtin_go_type->builtin_uint64
535 = init_integer_type (alloc, 64, 1, "uint64");
536 builtin_go_type->builtin_float32
537 = init_float_type (alloc, 32, "float32", floatformats_ieee_single);
538 builtin_go_type->builtin_float64
539 = init_float_type (alloc, 64, "float64", floatformats_ieee_double);
540 builtin_go_type->builtin_complex64
541 = init_complex_type ("complex64", builtin_go_type->builtin_float32);
542 builtin_go_type->builtin_complex128
543 = init_complex_type ("complex128", builtin_go_type->builtin_float64);
545 return builtin_go_type;
548 static const registry<gdbarch>::key<struct builtin_go_type> go_type_data;
550 const struct builtin_go_type *
551 builtin_go_type (struct gdbarch *gdbarch)
553 struct builtin_go_type *result = go_type_data.get (gdbarch);
554 if (result == nullptr)
556 result = build_go_types (gdbarch);
557 go_type_data.set (gdbarch, result);
560 return result;