]>
Commit | Line | Data |
---|---|---|
1 | /* Go language support routines for GDB, the GNU debugger. | |
2 | ||
3 | Copyright (C) 2012-2025 Free Software Foundation, Inc. | |
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 "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" | |
43 | ||
44 | ||
45 | /* The main function in the main package. */ | |
46 | static 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 | ||
53 | const char * | |
54 | go_main_name (void) | |
55 | { | |
56 | bound_minimal_symbol msym | |
57 | = lookup_minimal_symbol (current_program_space, GO_MAIN_MAIN); | |
58 | if (msym.minsym != NULL) | |
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 | ||
68 | static int | |
69 | gccgo_string_p (struct type *type) | |
70 | { | |
71 | /* gccgo strings don't necessarily have a name we can use. */ | |
72 | ||
73 | if (type->num_fields () == 2) | |
74 | { | |
75 | struct type *type0 = type->field (0).type (); | |
76 | struct type *type1 = type->field (1).type (); | |
77 | ||
78 | type0 = check_typedef (type0); | |
79 | type1 = check_typedef (type1); | |
80 | ||
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) | |
85 | { | |
86 | struct type *target_type = type0->target_type (); | |
87 | ||
88 | target_type = check_typedef (target_type); | |
89 | ||
90 | if (target_type->code () == TYPE_CODE_INT | |
91 | && target_type->length () == 1 | |
92 | && strcmp (target_type->name (), "uint8") == 0) | |
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 | ||
103 | static int | |
104 | sixg_string_p (struct type *type) | |
105 | { | |
106 | if (type->num_fields () == 2 | |
107 | && type->name () != NULL | |
108 | && strcmp (type->name (), "string") == 0) | |
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 | ||
117 | enum go_type | |
118 | go_classify_struct_type (struct type *type) | |
119 | { | |
120 | type = check_typedef (type); | |
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 | ||
136 | static void | |
137 | unpack_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. | |
163 | A pointer to this buffer is returned, or NULL if symbol isn't a | |
164 | mangled Go symbol. | |
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 | ||
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) | |
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 '.'. */ | |
193 | const char *first_dot; | |
194 | /* Pointer to the last '.'. */ | |
195 | const char *last_dot; | |
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 | { | |
206 | gdb::unique_xmalloc_ptr<char> package | |
207 | = make_unique_xstrdup ("main"); | |
208 | ||
209 | *packagep = package.get (); | |
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 | { | |
217 | gdb::unique_xmalloc_ptr<char> package | |
218 | = make_unique_xstrdup ("main"); | |
219 | ||
220 | *packagep = package.get (); | |
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). */ | |
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 | |
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; | |
248 | ||
249 | /* It must have at least two dots. */ | |
250 | if (v3) | |
251 | first_dot = strchr (mangled_name, '0'); | |
252 | else | |
253 | first_dot = strchr (mangled_name, '.'); | |
254 | ||
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 | ||
272 | gdb::unique_xmalloc_ptr<char> result = make_unique_xstrdup (mangled_name); | |
273 | buf = result.get (); | |
274 | ||
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 | ||
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; | |
294 | int current_is_digit = c_isdigit (current); | |
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); | |
341 | return result; | |
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 | ||
357 | gdb::unique_xmalloc_ptr<char> | |
358 | go_language::demangle_symbol (const char *mangled_name, int options) const | |
359 | { | |
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 | ||
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) | |
376 | return NULL; | |
377 | ||
378 | auto_obstack tempbuf; | |
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 | ||
402 | return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf)); | |
403 | } | |
404 | ||
405 | /* See go-lang.h. */ | |
406 | ||
407 | gdb::unique_xmalloc_ptr<char> | |
408 | go_symbol_package_name (const struct symbol *sym) | |
409 | { | |
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; | |
417 | ||
418 | if (sym->language () != language_go) | |
419 | return nullptr; | |
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; | |
428 | return make_unique_xstrdup (package_name); | |
429 | } | |
430 | ||
431 | /* See go-lang.h. */ | |
432 | ||
433 | gdb::unique_xmalloc_ptr<char> | |
434 | go_block_package_name (const struct block *block) | |
435 | { | |
436 | while (block != NULL) | |
437 | { | |
438 | struct symbol *function = block->function (); | |
439 | ||
440 | if (function != NULL) | |
441 | { | |
442 | gdb::unique_xmalloc_ptr<char> package_name | |
443 | = go_symbol_package_name (function); | |
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 | ||
454 | block = block->superblock (); | |
455 | } | |
456 | ||
457 | return NULL; | |
458 | } | |
459 | ||
460 | /* See language.h. */ | |
461 | ||
462 | void | |
463 | go_language::language_arch_info (struct gdbarch *gdbarch, | |
464 | struct language_arch_info *lai) const | |
465 | { | |
466 | const struct builtin_go_type *builtin = builtin_go_type (gdbarch); | |
467 | ||
468 | /* Helper function to allow shorter lines below. */ | |
469 | auto add = [&] (struct type * t) -> struct type * | |
470 | { | |
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 | } | |
497 | ||
498 | /* Single instance of the Go language class. */ | |
499 | ||
500 | static go_language go_language_defn; | |
501 | ||
502 | static struct builtin_go_type * | |
503 | build_go_types (struct gdbarch *gdbarch) | |
504 | { | |
505 | struct builtin_go_type *builtin_go_type = new struct builtin_go_type; | |
506 | ||
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); | |
543 | ||
544 | return builtin_go_type; | |
545 | } | |
546 | ||
547 | static const registry<gdbarch>::key<struct builtin_go_type> go_type_data; | |
548 | ||
549 | const struct builtin_go_type * | |
550 | builtin_go_type (struct gdbarch *gdbarch) | |
551 | { | |
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 | } | |
558 | ||
559 | return result; | |
560 | } |