1 /* Go language support routines for GDB, the GNU debugger.
3 Copyright (C) 2012-2019 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/>. */
22 - printing of native types
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
35 #include "gdb_obstack.h"
42 #include "parser-defs.h"
47 /* The main function in the main package. */
48 static const char GO_MAIN_MAIN[] = "main.main";
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. */
58 struct bound_minimal_symbol msym;
60 msym = lookup_minimal_symbol (GO_MAIN_MAIN, NULL, NULL);
61 if (msym.minsym != NULL)
64 /* No known entry procedure found, the main program is probably not Go. */
68 /* Return non-zero if TYPE is a gccgo string.
69 We assume CHECK_TYPEDEF has already been done. */
72 gccgo_string_p (struct type *type)
74 /* gccgo strings don't necessarily have a name we can use. */
76 if (TYPE_NFIELDS (type) == 2)
78 struct type *type0 = TYPE_FIELD_TYPE (type, 0);
79 struct type *type1 = TYPE_FIELD_TYPE (type, 1);
81 type0 = check_typedef (type0);
82 type1 = check_typedef (type1);
84 if (TYPE_CODE (type0) == TYPE_CODE_PTR
85 && strcmp (TYPE_FIELD_NAME (type, 0), "__data") == 0
86 && TYPE_CODE (type1) == TYPE_CODE_INT
87 && strcmp (TYPE_FIELD_NAME (type, 1), "__length") == 0)
89 struct type *target_type = TYPE_TARGET_TYPE (type0);
91 target_type = check_typedef (target_type);
93 if (TYPE_CODE (target_type) == TYPE_CODE_INT
94 && TYPE_LENGTH (target_type) == 1
95 && strcmp (TYPE_NAME (target_type), "uint8") == 0)
103 /* Return non-zero if TYPE is a 6g string.
104 We assume CHECK_TYPEDEF has already been done. */
107 sixg_string_p (struct type *type)
109 if (TYPE_NFIELDS (type) == 2
110 && TYPE_NAME (type) != NULL
111 && strcmp (TYPE_NAME (type), "string") == 0)
117 /* Classify the kind of Go object that TYPE is.
118 TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
121 go_classify_struct_type (struct type *type)
123 type = check_typedef (type);
125 /* Recognize strings as they're useful to be able to print without
127 if (gccgo_string_p (type)
128 || sixg_string_p (type))
129 return GO_TYPE_STRING;
134 /* Return true if TYPE is a string. */
137 go_is_string_type_p (struct type *type)
139 type = check_typedef (type);
140 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
141 && go_classify_struct_type (type) == GO_TYPE_STRING);
144 /* Subroutine of unpack_mangled_go_symbol to simplify it.
145 Given "[foo.]bar.baz", store "bar" in *PACKAGEP and "baz" in *OBJECTP.
146 We stomp on the last '.' to nul-terminate "bar".
147 The caller is responsible for memory management. */
150 unpack_package_and_object (char *buf,
151 const char **packagep, const char **objectp)
155 last_dot = strrchr (buf, '.');
156 gdb_assert (last_dot != NULL);
157 *objectp = last_dot + 1;
159 last_dot = strrchr (buf, '.');
160 if (last_dot != NULL)
161 *packagep = last_dot + 1;
166 /* Given a mangled Go symbol, find its package name, object name, and
167 method type (if present).
168 E.g., for "libgo_net.textproto.String.N33_libgo_net.textproto.ProtocolError"
169 *PACKAGEP = "textproto"
171 *METHOD_TYPE_PACKAGEP = "textproto"
172 *METHOD_TYPE_OBJECTP = "ProtocolError"
174 Space for the resulting strings is malloc'd in one buffer.
175 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
176 [There are a few exceptions, but the caller is still responsible for
177 freeing the resulting pointer.]
178 A pointer to this buffer is returned, or NULL if symbol isn't a
180 The caller is responsible for freeing the result.
182 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
183 the method type is a pointer.
185 There may be value in returning the outer container,
186 i.e., "net" in the above example, but for now it's not needed.
187 Plus it's currently not straightforward to compute,
188 it comes from -fgo-prefix, and there's no algorithm to compute it.
190 If we ever need to unpack the method type, this routine should work
194 unpack_mangled_go_symbol (const char *mangled_name,
195 const char **packagep,
196 const char **objectp,
197 const char **method_type_packagep,
198 const char **method_type_objectp,
199 int *method_type_is_pointerp)
203 int len = strlen (mangled_name);
204 /* Pointer to last digit in "N<digit(s)>_". */
206 /* Pointer to "N" if valid "N<digit(s)>_" found. */
208 /* Pointer to the first '.'. */
209 const char *first_dot;
210 /* Pointer to the last '.'. */
211 const char *last_dot;
212 /* Non-zero if we saw a pointer indicator. */
215 *packagep = *objectp = NULL;
216 *method_type_packagep = *method_type_objectp = NULL;
217 *method_type_is_pointerp = 0;
219 /* main.init is mangled specially. */
220 if (strcmp (mangled_name, "__go_init_main") == 0)
222 char *package = xstrdup ("main");
229 /* main.main is mangled specially (missing prefix). */
230 if (strcmp (mangled_name, "main.main") == 0)
232 char *package = xstrdup ("main");
239 /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
240 Alas it looks exactly like "prefix.package.object."
241 To cope for now we only recognize the following prefixes:
244 libgo_.*: used by gccgo's runtime
246 Thus we don't support -fgo-prefix (except as used by the runtime). */
247 if (!startswith (mangled_name, "go.")
248 && !startswith (mangled_name, "libgo_"))
251 /* Quick check for whether a search may be fruitful. */
252 /* Ignore anything with @plt, etc. in it. */
253 if (strchr (mangled_name, '@') != NULL)
255 /* It must have at least two dots. */
256 first_dot = strchr (mangled_name, '.');
257 if (first_dot == NULL)
259 /* Treat "foo.bar" as unmangled. It can collide with lots of other
260 languages and it's not clear what the consequences are.
261 And except for main.main, all gccgo symbols are at least
262 prefix.package.object. */
263 last_dot = strrchr (mangled_name, '.');
264 if (last_dot == first_dot)
267 /* More quick checks. */
268 if (last_dot[1] == '\0' /* foo. */
269 || last_dot[-1] == '.') /* foo..bar */
272 /* At this point we've decided we have a mangled Go symbol. */
274 buf = xstrdup (mangled_name);
276 /* Search backwards looking for "N<digit(s)>". */
278 saw_digit = method_type = NULL;
282 int current = *(const unsigned char *) --p;
283 int current_is_digit = isdigit (current);
287 if (current_is_digit)
290 && ((p > buf && p[-1] == '.')
291 || (p > buf + 1 && p[-1] == 'p' && p[-2] == '.')))
293 if (atoi (p + 1) == strlen (saw_digit + 2))
299 gdb_assert (p[-1] == 'p');
306 /* Not what we're looking for, reset and keep looking. */
311 if (current_is_digit && p[1] == '_')
313 /* Possible start of method "this" [sic] type. */
319 if (method_type != NULL
320 /* Ensure not something like "..foo". */
321 && (method_type > buf && method_type[-1] != '.'))
323 unpack_package_and_object (saw_digit + 2,
324 method_type_packagep, method_type_objectp);
326 *method_type_is_pointerp = saw_pointer;
329 unpack_package_and_object (buf, packagep, objectp);
333 /* Implements the la_demangle language_defn routine for language Go.
335 N.B. This may get passed *any* symbol, including symbols from other
336 languages and including symbols that are already demangled.
337 Both of these situations are kinda unfortunate, but that's how things
340 N.B. This currently only supports gccgo's mangling.
342 N.B. gccgo's mangling needs, I think, changing.
343 This demangler can't work in all situations,
344 thus not too much effort is currently put into it. */
347 go_demangle (const char *mangled_name, int options)
349 struct obstack tempbuf;
352 const char *package_name;
353 const char *object_name;
354 const char *method_type_package_name;
355 const char *method_type_object_name;
356 int method_type_is_pointer;
358 if (mangled_name == NULL)
361 name_buf = unpack_mangled_go_symbol (mangled_name,
362 &package_name, &object_name,
363 &method_type_package_name,
364 &method_type_object_name,
365 &method_type_is_pointer);
366 if (name_buf == NULL)
369 obstack_init (&tempbuf);
371 /* Print methods as they appear in "method expressions". */
372 if (method_type_package_name != NULL)
374 /* FIXME: Seems like we should include package_name here somewhere. */
375 if (method_type_is_pointer)
376 obstack_grow_str (&tempbuf, "(*");
377 obstack_grow_str (&tempbuf, method_type_package_name);
378 obstack_grow_str (&tempbuf, ".");
379 obstack_grow_str (&tempbuf, method_type_object_name);
380 if (method_type_is_pointer)
381 obstack_grow_str (&tempbuf, ")");
382 obstack_grow_str (&tempbuf, ".");
383 obstack_grow_str (&tempbuf, object_name);
387 obstack_grow_str (&tempbuf, package_name);
388 obstack_grow_str (&tempbuf, ".");
389 obstack_grow_str (&tempbuf, object_name);
391 obstack_grow_str0 (&tempbuf, "");
393 result = xstrdup ((const char *) obstack_finish (&tempbuf));
394 obstack_free (&tempbuf, NULL);
399 /* la_sniff_from_mangled_name for Go. */
402 go_sniff_from_mangled_name (const char *mangled, char **demangled)
404 *demangled = go_demangle (mangled, 0);
405 return *demangled != NULL;
408 /* Given a Go symbol, return its package or NULL if unknown.
409 Space for the result is malloc'd, caller must free. */
412 go_symbol_package_name (const struct symbol *sym)
414 const char *mangled_name = SYMBOL_LINKAGE_NAME (sym);
415 const char *package_name;
416 const char *object_name;
417 const char *method_type_package_name;
418 const char *method_type_object_name;
419 int method_type_is_pointer;
423 gdb_assert (SYMBOL_LANGUAGE (sym) == language_go);
424 name_buf = unpack_mangled_go_symbol (mangled_name,
425 &package_name, &object_name,
426 &method_type_package_name,
427 &method_type_object_name,
428 &method_type_is_pointer);
429 /* Some Go symbols don't have mangled form we interpret (yet). */
430 if (name_buf == NULL)
432 result = xstrdup (package_name);
437 /* Return the package that BLOCK is in, or NULL if there isn't one.
438 Space for the result is malloc'd, caller must free. */
441 go_block_package_name (const struct block *block)
443 while (block != NULL)
445 struct symbol *function = BLOCK_FUNCTION (block);
447 if (function != NULL)
449 char *package_name = go_symbol_package_name (function);
451 if (package_name != NULL)
454 /* Stop looking if we find a function without a package name.
455 We're most likely outside of Go and thus the concept of the
456 "current" package is gone. */
460 block = BLOCK_SUPERBLOCK (block);
466 /* Table mapping opcodes into strings for printing operators
467 and precedences of the operators.
470 static const struct op_print go_op_print_tab[] =
472 {",", BINOP_COMMA, PREC_COMMA, 0},
473 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
474 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
475 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
476 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
477 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
478 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
479 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
480 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
481 {"<=", BINOP_LEQ, PREC_ORDER, 0},
482 {">=", BINOP_GEQ, PREC_ORDER, 0},
483 {">", BINOP_GTR, PREC_ORDER, 0},
484 {"<", BINOP_LESS, PREC_ORDER, 0},
485 {">>", BINOP_RSH, PREC_SHIFT, 0},
486 {"<<", BINOP_LSH, PREC_SHIFT, 0},
487 {"+", BINOP_ADD, PREC_ADD, 0},
488 {"-", BINOP_SUB, PREC_ADD, 0},
489 {"*", BINOP_MUL, PREC_MUL, 0},
490 {"/", BINOP_DIV, PREC_MUL, 0},
491 {"%", BINOP_REM, PREC_MUL, 0},
492 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
493 {"-", UNOP_NEG, PREC_PREFIX, 0},
494 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
495 {"^", UNOP_COMPLEMENT, PREC_PREFIX, 0},
496 {"*", UNOP_IND, PREC_PREFIX, 0},
497 {"&", UNOP_ADDR, PREC_PREFIX, 0},
498 {"unsafe.Sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
499 {"++", UNOP_POSTINCREMENT, PREC_SUFFIX, 0},
500 {"--", UNOP_POSTDECREMENT, PREC_SUFFIX, 0},
501 {NULL, OP_NULL, PREC_SUFFIX, 0}
504 enum go_primitive_types {
505 go_primitive_type_void,
506 go_primitive_type_char,
507 go_primitive_type_bool,
508 go_primitive_type_int,
509 go_primitive_type_uint,
510 go_primitive_type_uintptr,
511 go_primitive_type_int8,
512 go_primitive_type_int16,
513 go_primitive_type_int32,
514 go_primitive_type_int64,
515 go_primitive_type_uint8,
516 go_primitive_type_uint16,
517 go_primitive_type_uint32,
518 go_primitive_type_uint64,
519 go_primitive_type_float32,
520 go_primitive_type_float64,
521 go_primitive_type_complex64,
522 go_primitive_type_complex128,
523 nr_go_primitive_types
527 go_language_arch_info (struct gdbarch *gdbarch,
528 struct language_arch_info *lai)
530 const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
532 lai->string_char_type = builtin->builtin_char;
534 lai->primitive_type_vector
535 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_go_primitive_types + 1,
538 lai->primitive_type_vector [go_primitive_type_void]
539 = builtin->builtin_void;
540 lai->primitive_type_vector [go_primitive_type_char]
541 = builtin->builtin_char;
542 lai->primitive_type_vector [go_primitive_type_bool]
543 = builtin->builtin_bool;
544 lai->primitive_type_vector [go_primitive_type_int]
545 = builtin->builtin_int;
546 lai->primitive_type_vector [go_primitive_type_uint]
547 = builtin->builtin_uint;
548 lai->primitive_type_vector [go_primitive_type_uintptr]
549 = builtin->builtin_uintptr;
550 lai->primitive_type_vector [go_primitive_type_int8]
551 = builtin->builtin_int8;
552 lai->primitive_type_vector [go_primitive_type_int16]
553 = builtin->builtin_int16;
554 lai->primitive_type_vector [go_primitive_type_int32]
555 = builtin->builtin_int32;
556 lai->primitive_type_vector [go_primitive_type_int64]
557 = builtin->builtin_int64;
558 lai->primitive_type_vector [go_primitive_type_uint8]
559 = builtin->builtin_uint8;
560 lai->primitive_type_vector [go_primitive_type_uint16]
561 = builtin->builtin_uint16;
562 lai->primitive_type_vector [go_primitive_type_uint32]
563 = builtin->builtin_uint32;
564 lai->primitive_type_vector [go_primitive_type_uint64]
565 = builtin->builtin_uint64;
566 lai->primitive_type_vector [go_primitive_type_float32]
567 = builtin->builtin_float32;
568 lai->primitive_type_vector [go_primitive_type_float64]
569 = builtin->builtin_float64;
570 lai->primitive_type_vector [go_primitive_type_complex64]
571 = builtin->builtin_complex64;
572 lai->primitive_type_vector [go_primitive_type_complex128]
573 = builtin->builtin_complex128;
575 lai->bool_type_symbol = "bool";
576 lai->bool_type_default = builtin->builtin_bool;
579 extern const struct language_defn go_language_defn =
592 c_printchar, /* Print a character constant. */
593 c_printstr, /* Function to print string constant. */
594 c_emit_char, /* Print a single char. */
595 go_print_type, /* Print a type using appropriate syntax. */
596 c_print_typedef, /* Print a typedef using appropriate
598 go_val_print, /* Print a value using appropriate syntax. */
599 c_value_print, /* Print a top-level value. */
600 default_read_var_value, /* la_read_var_value */
601 NULL, /* Language specific skip_trampoline. */
602 NULL, /* name_of_this */
603 false, /* la_store_sym_names_in_linkage_form_p */
604 basic_lookup_symbol_nonlocal,
605 basic_lookup_transparent_type,
606 go_demangle, /* Language specific symbol demangler. */
607 go_sniff_from_mangled_name,
608 NULL, /* Language specific
609 class_name_from_physname. */
610 go_op_print_tab, /* Expression operators for printing. */
611 1, /* C-style arrays. */
612 0, /* String lower bound. */
613 default_word_break_characters,
614 default_collect_symbol_completion_matches,
615 go_language_arch_info,
616 default_print_array_index,
617 default_pass_by_reference,
619 c_watch_location_expression,
620 NULL, /* la_get_symbol_name_matcher */
621 iterate_over_symbols,
622 default_search_name_hash,
627 "{...}" /* la_struct_too_deep_ellipsis */
631 build_go_types (struct gdbarch *gdbarch)
633 struct builtin_go_type *builtin_go_type
634 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_go_type);
636 builtin_go_type->builtin_void
637 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
638 builtin_go_type->builtin_char
639 = arch_character_type (gdbarch, 8, 1, "char");
640 builtin_go_type->builtin_bool
641 = arch_boolean_type (gdbarch, 8, 0, "bool");
642 builtin_go_type->builtin_int
643 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "int");
644 builtin_go_type->builtin_uint
645 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "uint");
646 builtin_go_type->builtin_uintptr
647 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
648 builtin_go_type->builtin_int8
649 = arch_integer_type (gdbarch, 8, 0, "int8");
650 builtin_go_type->builtin_int16
651 = arch_integer_type (gdbarch, 16, 0, "int16");
652 builtin_go_type->builtin_int32
653 = arch_integer_type (gdbarch, 32, 0, "int32");
654 builtin_go_type->builtin_int64
655 = arch_integer_type (gdbarch, 64, 0, "int64");
656 builtin_go_type->builtin_uint8
657 = arch_integer_type (gdbarch, 8, 1, "uint8");
658 builtin_go_type->builtin_uint16
659 = arch_integer_type (gdbarch, 16, 1, "uint16");
660 builtin_go_type->builtin_uint32
661 = arch_integer_type (gdbarch, 32, 1, "uint32");
662 builtin_go_type->builtin_uint64
663 = arch_integer_type (gdbarch, 64, 1, "uint64");
664 builtin_go_type->builtin_float32
665 = arch_float_type (gdbarch, 32, "float32", floatformats_ieee_single);
666 builtin_go_type->builtin_float64
667 = arch_float_type (gdbarch, 64, "float64", floatformats_ieee_double);
668 builtin_go_type->builtin_complex64
669 = arch_complex_type (gdbarch, "complex64",
670 builtin_go_type->builtin_float32);
671 builtin_go_type->builtin_complex128
672 = arch_complex_type (gdbarch, "complex128",
673 builtin_go_type->builtin_float64);
675 return builtin_go_type;
678 static struct gdbarch_data *go_type_data;
680 const struct builtin_go_type *
681 builtin_go_type (struct gdbarch *gdbarch)
683 return (const struct builtin_go_type *) gdbarch_data (gdbarch, go_type_data);
687 _initialize_go_language (void)
689 go_type_data = gdbarch_data_register_post_init (build_go_types);