1 /* Go language support routines for GDB, the GNU debugger.
3 Copyright (C) 2012-2013 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_assert.h"
36 #include "gdb_obstack.h"
37 #include "gdb_string.h"
43 #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 minimal_symbol
*msym
;
60 msym
= lookup_minimal_symbol (GO_MAIN_MAIN
, NULL
, 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 CHECK_TYPEDEF (type0
);
82 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 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_TAG_NAME (type
) != NULL
111 && strcmp (TYPE_TAG_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 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 /* 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. */
140 unpack_package_and_object (char *buf
,
141 const char **packagep
, const char **objectp
)
145 last_dot
= strrchr (buf
, '.');
146 gdb_assert (last_dot
!= NULL
);
147 *objectp
= last_dot
+ 1;
149 last_dot
= strrchr (buf
, '.');
150 if (last_dot
!= NULL
)
151 *packagep
= last_dot
+ 1;
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"
161 *METHOD_TYPE_PACKAGEP = "textproto"
162 *METHOD_TYPE_OBJECTP = "ProtocolError"
164 Space for the resulting strings is malloc'd in one buffer.
165 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
166 [There are a few exceptions, but the caller is still responsible for
167 freeing the resulting pointer.]
168 A pointer to this buffer is returned, or NULL if symbol isn't a
170 The caller is responsible for freeing the result.
172 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
173 the method type is a pointer.
175 There may be value in returning the outer container,
176 i.e., "net" in the above example, but for now it's not needed.
177 Plus it's currently not straightforward to compute,
178 it comes from -fgo-prefix, and there's no algorithm to compute it.
180 If we ever need to unpack the method type, this routine should work
184 unpack_mangled_go_symbol (const char *mangled_name
,
185 const char **packagep
,
186 const char **objectp
,
187 const char **method_type_packagep
,
188 const char **method_type_objectp
,
189 int *method_type_is_pointerp
)
193 int len
= strlen (mangled_name
);
194 /* Pointer to last digit in "N<digit(s)>_". */
196 /* Pointer to "N" if valid "N<digit(s)>_" found. */
198 /* Pointer to the first '.'. */
200 /* Pointer to the last '.'. */
202 /* Non-zero if we saw a pointer indicator. */
205 *packagep
= *objectp
= NULL
;
206 *method_type_packagep
= *method_type_objectp
= NULL
;
207 *method_type_is_pointerp
= 0;
209 /* main.init is mangled specially. */
210 if (strcmp (mangled_name
, "__go_init_main") == 0)
212 char *package
= xstrdup ("main");
219 /* main.main is mangled specially (missing prefix). */
220 if (strcmp (mangled_name
, "main.main") == 0)
222 char *package
= xstrdup ("main");
229 /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
230 Alas it looks exactly like "prefix.package.object."
231 To cope for now we only recognize the following prefixes:
234 libgo_.*: used by gccgo's runtime
236 Thus we don't support -fgo-prefix (except as used by the runtime). */
237 if (strncmp (mangled_name
, "go.", 3) != 0
238 && strncmp (mangled_name
, "libgo_", 6) != 0)
241 /* Quick check for whether a search may be fruitful. */
242 /* Ignore anything with @plt, etc. in it. */
243 if (strchr (mangled_name
, '@') != NULL
)
245 /* It must have at least two dots. */
246 first_dot
= strchr (mangled_name
, '.');
247 if (first_dot
== NULL
)
249 /* Treat "foo.bar" as unmangled. It can collide with lots of other
250 languages and it's not clear what the consequences are.
251 And except for main.main, all gccgo symbols are at least
252 prefix.package.object. */
253 last_dot
= strrchr (mangled_name
, '.');
254 if (last_dot
== first_dot
)
257 /* More quick checks. */
258 if (last_dot
[1] == '\0' /* foo. */
259 || last_dot
[-1] == '.') /* foo..bar */
262 /* At this point we've decided we have a mangled Go symbol. */
264 buf
= xstrdup (mangled_name
);
266 /* Search backwards looking for "N<digit(s)>". */
268 saw_digit
= method_type
= NULL
;
272 int current
= *(const unsigned char *) --p
;
273 int current_is_digit
= isdigit (current
);
277 if (current_is_digit
)
280 && ((p
> buf
&& p
[-1] == '.')
281 || (p
> buf
+ 1 && p
[-1] == 'p' && p
[-2] == '.')))
283 if (atoi (p
+ 1) == strlen (saw_digit
+ 2))
289 gdb_assert (p
[-1] == 'p');
296 /* Not what we're looking for, reset and keep looking. */
301 if (current_is_digit
&& p
[1] == '_')
303 /* Possible start of method "this" [sic] type. */
309 if (method_type
!= NULL
310 /* Ensure not something like "..foo". */
311 && (method_type
> buf
&& method_type
[-1] != '.'))
313 unpack_package_and_object (saw_digit
+ 2,
314 method_type_packagep
, method_type_objectp
);
316 *method_type_is_pointerp
= saw_pointer
;
319 unpack_package_and_object (buf
, packagep
, objectp
);
323 /* Implements the la_demangle language_defn routine for language Go.
325 N.B. This may get passed *any* symbol, including symbols from other
326 languages and including symbols that are already demangled.
327 Both of these situations are kinda unfortunate, but that's how things
330 N.B. This currently only supports gccgo's mangling.
332 N.B. gccgo's mangling needs, I think, changing.
333 This demangler can't work in all situations,
334 thus not too much effort is currently put into it. */
337 go_demangle (const char *mangled_name
, int options
)
339 struct obstack tempbuf
;
342 const char *package_name
;
343 const char *object_name
;
344 const char *method_type_package_name
;
345 const char *method_type_object_name
;
346 int method_type_is_pointer
;
348 if (mangled_name
== NULL
)
351 name_buf
= unpack_mangled_go_symbol (mangled_name
,
352 &package_name
, &object_name
,
353 &method_type_package_name
,
354 &method_type_object_name
,
355 &method_type_is_pointer
);
356 if (name_buf
== NULL
)
359 obstack_init (&tempbuf
);
361 /* Print methods as they appear in "method expressions". */
362 if (method_type_package_name
!= NULL
)
364 /* FIXME: Seems like we should include package_name here somewhere. */
365 if (method_type_is_pointer
)
366 obstack_grow_str (&tempbuf
, "(*");
367 obstack_grow_str (&tempbuf
, method_type_package_name
);
368 obstack_grow_str (&tempbuf
, ".");
369 obstack_grow_str (&tempbuf
, method_type_object_name
);
370 if (method_type_is_pointer
)
371 obstack_grow_str (&tempbuf
, ")");
372 obstack_grow_str (&tempbuf
, ".");
373 obstack_grow_str (&tempbuf
, object_name
);
377 obstack_grow_str (&tempbuf
, package_name
);
378 obstack_grow_str (&tempbuf
, ".");
379 obstack_grow_str (&tempbuf
, object_name
);
381 obstack_grow_str0 (&tempbuf
, "");
383 result
= xstrdup (obstack_finish (&tempbuf
));
384 obstack_free (&tempbuf
, NULL
);
389 /* Given a Go symbol, return its package or NULL if unknown.
390 Space for the result is malloc'd, caller must free. */
393 go_symbol_package_name (const struct symbol
*sym
)
395 const char *mangled_name
= SYMBOL_LINKAGE_NAME (sym
);
396 const char *package_name
;
397 const char *object_name
;
398 const char *method_type_package_name
;
399 const char *method_type_object_name
;
400 int method_type_is_pointer
;
404 gdb_assert (SYMBOL_LANGUAGE (sym
) == language_go
);
405 name_buf
= unpack_mangled_go_symbol (mangled_name
,
406 &package_name
, &object_name
,
407 &method_type_package_name
,
408 &method_type_object_name
,
409 &method_type_is_pointer
);
410 /* Some Go symbols don't have mangled form we interpret (yet). */
411 if (name_buf
== NULL
)
413 result
= xstrdup (package_name
);
418 /* Return the package that BLOCK is in, or NULL if there isn't one.
419 Space for the result is malloc'd, caller must free. */
422 go_block_package_name (const struct block
*block
)
424 while (block
!= NULL
)
426 struct symbol
*function
= BLOCK_FUNCTION (block
);
428 if (function
!= NULL
)
430 char *package_name
= go_symbol_package_name (function
);
432 if (package_name
!= NULL
)
435 /* Stop looking if we find a function without a package name.
436 We're most likely outside of Go and thus the concept of the
437 "current" package is gone. */
441 block
= BLOCK_SUPERBLOCK (block
);
447 /* Table mapping opcodes into strings for printing operators
448 and precedences of the operators.
451 static const struct op_print go_op_print_tab
[] =
453 {",", BINOP_COMMA
, PREC_COMMA
, 0},
454 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
455 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
456 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
457 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
458 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
459 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
460 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
461 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
462 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
463 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
464 {">", BINOP_GTR
, PREC_ORDER
, 0},
465 {"<", BINOP_LESS
, PREC_ORDER
, 0},
466 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
467 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
468 {"+", BINOP_ADD
, PREC_ADD
, 0},
469 {"-", BINOP_SUB
, PREC_ADD
, 0},
470 {"*", BINOP_MUL
, PREC_MUL
, 0},
471 {"/", BINOP_DIV
, PREC_MUL
, 0},
472 {"%", BINOP_REM
, PREC_MUL
, 0},
473 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
474 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
475 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
476 {"^", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
477 {"*", UNOP_IND
, PREC_PREFIX
, 0},
478 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
479 {"unsafe.Sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
480 {"++", UNOP_POSTINCREMENT
, PREC_SUFFIX
, 0},
481 {"--", UNOP_POSTDECREMENT
, PREC_SUFFIX
, 0},
485 enum go_primitive_types
{
486 go_primitive_type_void
,
487 go_primitive_type_char
,
488 go_primitive_type_bool
,
489 go_primitive_type_int
,
490 go_primitive_type_uint
,
491 go_primitive_type_uintptr
,
492 go_primitive_type_int8
,
493 go_primitive_type_int16
,
494 go_primitive_type_int32
,
495 go_primitive_type_int64
,
496 go_primitive_type_uint8
,
497 go_primitive_type_uint16
,
498 go_primitive_type_uint32
,
499 go_primitive_type_uint64
,
500 go_primitive_type_float32
,
501 go_primitive_type_float64
,
502 go_primitive_type_complex64
,
503 go_primitive_type_complex128
,
504 nr_go_primitive_types
508 go_language_arch_info (struct gdbarch
*gdbarch
,
509 struct language_arch_info
*lai
)
511 const struct builtin_go_type
*builtin
= builtin_go_type (gdbarch
);
513 lai
->string_char_type
= builtin
->builtin_char
;
515 lai
->primitive_type_vector
516 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_go_primitive_types
+ 1,
519 lai
->primitive_type_vector
[go_primitive_type_void
]
520 = builtin
->builtin_void
;
521 lai
->primitive_type_vector
[go_primitive_type_char
]
522 = builtin
->builtin_char
;
523 lai
->primitive_type_vector
[go_primitive_type_bool
]
524 = builtin
->builtin_bool
;
525 lai
->primitive_type_vector
[go_primitive_type_int
]
526 = builtin
->builtin_int
;
527 lai
->primitive_type_vector
[go_primitive_type_uint
]
528 = builtin
->builtin_uint
;
529 lai
->primitive_type_vector
[go_primitive_type_uintptr
]
530 = builtin
->builtin_uintptr
;
531 lai
->primitive_type_vector
[go_primitive_type_int8
]
532 = builtin
->builtin_int8
;
533 lai
->primitive_type_vector
[go_primitive_type_int16
]
534 = builtin
->builtin_int16
;
535 lai
->primitive_type_vector
[go_primitive_type_int32
]
536 = builtin
->builtin_int32
;
537 lai
->primitive_type_vector
[go_primitive_type_int64
]
538 = builtin
->builtin_int64
;
539 lai
->primitive_type_vector
[go_primitive_type_uint8
]
540 = builtin
->builtin_uint8
;
541 lai
->primitive_type_vector
[go_primitive_type_uint16
]
542 = builtin
->builtin_uint16
;
543 lai
->primitive_type_vector
[go_primitive_type_uint32
]
544 = builtin
->builtin_uint32
;
545 lai
->primitive_type_vector
[go_primitive_type_uint64
]
546 = builtin
->builtin_uint64
;
547 lai
->primitive_type_vector
[go_primitive_type_float32
]
548 = builtin
->builtin_float32
;
549 lai
->primitive_type_vector
[go_primitive_type_float64
]
550 = builtin
->builtin_float64
;
551 lai
->primitive_type_vector
[go_primitive_type_complex64
]
552 = builtin
->builtin_complex64
;
553 lai
->primitive_type_vector
[go_primitive_type_complex128
]
554 = builtin
->builtin_complex128
;
556 lai
->bool_type_symbol
= "bool";
557 lai
->bool_type_default
= builtin
->builtin_bool
;
560 static const struct language_defn go_language_defn
=
572 c_printchar
, /* Print a character constant. */
573 c_printstr
, /* Function to print string constant. */
574 c_emit_char
, /* Print a single char. */
575 go_print_type
, /* Print a type using appropriate syntax. */
576 c_print_typedef
, /* Print a typedef using appropriate
578 go_val_print
, /* Print a value using appropriate syntax. */
579 c_value_print
, /* Print a top-level value. */
580 default_read_var_value
, /* la_read_var_value */
581 NULL
, /* Language specific skip_trampoline. */
582 NULL
, /* name_of_this */
583 basic_lookup_symbol_nonlocal
,
584 basic_lookup_transparent_type
,
585 go_demangle
, /* Language specific symbol demangler. */
586 NULL
, /* Language specific
587 class_name_from_physname. */
588 go_op_print_tab
, /* Expression operators for printing. */
589 1, /* C-style arrays. */
590 0, /* String lower bound. */
591 default_word_break_characters
,
592 default_make_symbol_completion_list
,
593 go_language_arch_info
,
594 default_print_array_index
,
595 default_pass_by_reference
,
598 iterate_over_symbols
,
603 build_go_types (struct gdbarch
*gdbarch
)
605 struct builtin_go_type
*builtin_go_type
606 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_go_type
);
608 builtin_go_type
->builtin_void
609 = arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
610 builtin_go_type
->builtin_char
611 = arch_character_type (gdbarch
, 8, 1, "char");
612 builtin_go_type
->builtin_bool
613 = arch_boolean_type (gdbarch
, 8, 0, "bool");
614 builtin_go_type
->builtin_int
615 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 0, "int");
616 builtin_go_type
->builtin_uint
617 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 1, "uint");
618 builtin_go_type
->builtin_uintptr
619 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr");
620 builtin_go_type
->builtin_int8
621 = arch_integer_type (gdbarch
, 8, 0, "int8");
622 builtin_go_type
->builtin_int16
623 = arch_integer_type (gdbarch
, 16, 0, "int16");
624 builtin_go_type
->builtin_int32
625 = arch_integer_type (gdbarch
, 32, 0, "int32");
626 builtin_go_type
->builtin_int64
627 = arch_integer_type (gdbarch
, 64, 0, "int64");
628 builtin_go_type
->builtin_uint8
629 = arch_integer_type (gdbarch
, 8, 1, "uint8");
630 builtin_go_type
->builtin_uint16
631 = arch_integer_type (gdbarch
, 16, 1, "uint16");
632 builtin_go_type
->builtin_uint32
633 = arch_integer_type (gdbarch
, 32, 1, "uint32");
634 builtin_go_type
->builtin_uint64
635 = arch_integer_type (gdbarch
, 64, 1, "uint64");
636 builtin_go_type
->builtin_float32
637 = arch_float_type (gdbarch
, 32, "float32", NULL
);
638 builtin_go_type
->builtin_float64
639 = arch_float_type (gdbarch
, 64, "float64", NULL
);
640 builtin_go_type
->builtin_complex64
641 = arch_complex_type (gdbarch
, "complex64",
642 builtin_go_type
->builtin_float32
);
643 builtin_go_type
->builtin_complex128
644 = arch_complex_type (gdbarch
, "complex128",
645 builtin_go_type
->builtin_float64
);
647 return builtin_go_type
;
650 static struct gdbarch_data
*go_type_data
;
652 const struct builtin_go_type
*
653 builtin_go_type (struct gdbarch
*gdbarch
)
655 return gdbarch_data (gdbarch
, go_type_data
);
658 extern initialize_file_ftype _initialize_go_language
;
661 _initialize_go_language (void)
663 go_type_data
= gdbarch_data_register_post_init (build_go_types
);
665 add_language (&go_language_defn
);