go-lang.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* Go language support routines for GDB, the GNU debugger.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* TODO:
  15. - split stacks
  16. - printing of native types
  17. - goroutines
  18. - lots more
  19. - gccgo mangling needs redoing
  20. It's too hard, for example, to know whether one is looking at a mangled
  21. Go symbol or not, and their are ambiguities, e.g., the demangler may
  22. get passed *any* symbol, including symbols from other languages
  23. and including symbols that are already demangled.
  24. One thought is to at least add an _G prefix.
  25. - 6g mangling isn't supported yet
  26. */
  27. #include "defs.h"
  28. #include "gdbsupport/gdb_obstack.h"
  29. #include "block.h"
  30. #include "symtab.h"
  31. #include "language.h"
  32. #include "varobj.h"
  33. #include "go-lang.h"
  34. #include "c-lang.h"
  35. #include "parser-defs.h"
  36. #include "gdbarch.h"
  37. #include <ctype.h>
  38. /* The main function in the main package. */
  39. static const char GO_MAIN_MAIN[] = "main.main";
  40. /* Function returning the special symbol name used by Go for the main
  41. procedure in the main program if it is found in minimal symbol list.
  42. This function tries to find minimal symbols so that it finds them even
  43. if the program was compiled without debugging information. */
  44. const char *
  45. go_main_name (void)
  46. {
  47. struct bound_minimal_symbol msym;
  48. msym = lookup_minimal_symbol (GO_MAIN_MAIN, NULL, NULL);
  49. if (msym.minsym != NULL)
  50. return GO_MAIN_MAIN;
  51. /* No known entry procedure found, the main program is probably not Go. */
  52. return NULL;
  53. }
  54. /* Return non-zero if TYPE is a gccgo string.
  55. We assume CHECK_TYPEDEF has already been done. */
  56. static int
  57. gccgo_string_p (struct type *type)
  58. {
  59. /* gccgo strings don't necessarily have a name we can use. */
  60. if (type->num_fields () == 2)
  61. {
  62. struct type *type0 = type->field (0).type ();
  63. struct type *type1 = type->field (1).type ();
  64. type0 = check_typedef (type0);
  65. type1 = check_typedef (type1);
  66. if (type0->code () == TYPE_CODE_PTR
  67. && strcmp (type->field (0).name (), "__data") == 0
  68. && type1->code () == TYPE_CODE_INT
  69. && strcmp (type->field (1).name (), "__length") == 0)
  70. {
  71. struct type *target_type = TYPE_TARGET_TYPE (type0);
  72. target_type = check_typedef (target_type);
  73. if (target_type->code () == TYPE_CODE_INT
  74. && TYPE_LENGTH (target_type) == 1
  75. && strcmp (target_type->name (), "uint8") == 0)
  76. return 1;
  77. }
  78. }
  79. return 0;
  80. }
  81. /* Return non-zero if TYPE is a 6g string.
  82. We assume CHECK_TYPEDEF has already been done. */
  83. static int
  84. sixg_string_p (struct type *type)
  85. {
  86. if (type->num_fields () == 2
  87. && type->name () != NULL
  88. && strcmp (type->name (), "string") == 0)
  89. return 1;
  90. return 0;
  91. }
  92. /* Classify the kind of Go object that TYPE is.
  93. TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
  94. enum go_type
  95. go_classify_struct_type (struct type *type)
  96. {
  97. type = check_typedef (type);
  98. /* Recognize strings as they're useful to be able to print without
  99. pretty-printers. */
  100. if (gccgo_string_p (type)
  101. || sixg_string_p (type))
  102. return GO_TYPE_STRING;
  103. return GO_TYPE_NONE;
  104. }
  105. /* Subroutine of unpack_mangled_go_symbol to simplify it.
  106. Given "[foo.]bar.baz", store "bar" in *PACKAGEP and "baz" in *OBJECTP.
  107. We stomp on the last '.' to nul-terminate "bar".
  108. The caller is responsible for memory management. */
  109. static void
  110. unpack_package_and_object (char *buf,
  111. const char **packagep, const char **objectp)
  112. {
  113. char *last_dot;
  114. last_dot = strrchr (buf, '.');
  115. gdb_assert (last_dot != NULL);
  116. *objectp = last_dot + 1;
  117. *last_dot = '\0';
  118. last_dot = strrchr (buf, '.');
  119. if (last_dot != NULL)
  120. *packagep = last_dot + 1;
  121. else
  122. *packagep = buf;
  123. }
  124. /* Given a mangled Go symbol, find its package name, object name, and
  125. method type (if present).
  126. E.g., for "libgo_net.textproto.String.N33_libgo_net.textproto.ProtocolError"
  127. *PACKAGEP = "textproto"
  128. *OBJECTP = "String"
  129. *METHOD_TYPE_PACKAGEP = "textproto"
  130. *METHOD_TYPE_OBJECTP = "ProtocolError"
  131. Space for the resulting strings is malloc'd in one buffer.
  132. PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
  133. [There are a few exceptions, but the caller is still responsible for
  134. freeing the resulting pointer.]
  135. A pointer to this buffer is returned, or NULL if symbol isn't a
  136. mangled Go symbol.
  137. The caller is responsible for freeing the result.
  138. *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
  139. the method type is a pointer.
  140. There may be value in returning the outer container,
  141. i.e., "net" in the above example, but for now it's not needed.
  142. Plus it's currently not straightforward to compute,
  143. it comes from -fgo-prefix, and there's no algorithm to compute it.
  144. If we ever need to unpack the method type, this routine should work
  145. for that too. */
  146. static char *
  147. unpack_mangled_go_symbol (const char *mangled_name,
  148. const char **packagep,
  149. const char **objectp,
  150. const char **method_type_packagep,
  151. const char **method_type_objectp,
  152. int *method_type_is_pointerp)
  153. {
  154. char *buf;
  155. char *p;
  156. int len = strlen (mangled_name);
  157. /* Pointer to last digit in "N<digit(s)>_". */
  158. char *saw_digit;
  159. /* Pointer to "N" if valid "N<digit(s)>_" found. */
  160. char *method_type;
  161. /* Pointer to the first '.'. */
  162. const char *first_dot;
  163. /* Pointer to the last '.'. */
  164. const char *last_dot;
  165. /* Non-zero if we saw a pointer indicator. */
  166. int saw_pointer;
  167. *packagep = *objectp = NULL;
  168. *method_type_packagep = *method_type_objectp = NULL;
  169. *method_type_is_pointerp = 0;
  170. /* main.init is mangled specially. */
  171. if (strcmp (mangled_name, "__go_init_main") == 0)
  172. {
  173. char *package = xstrdup ("main");
  174. *packagep = package;
  175. *objectp = "init";
  176. return package;
  177. }
  178. /* main.main is mangled specially (missing prefix). */
  179. if (strcmp (mangled_name, "main.main") == 0)
  180. {
  181. char *package = xstrdup ("main");
  182. *packagep = package;
  183. *objectp = "main";
  184. return package;
  185. }
  186. /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
  187. Alas it looks exactly like "prefix.package.object."
  188. To cope for now we only recognize the following prefixes:
  189. go: the default
  190. libgo_.*: used by gccgo's runtime
  191. Thus we don't support -fgo-prefix (except as used by the runtime). */
  192. if (!startswith (mangled_name, "go.")
  193. && !startswith (mangled_name, "libgo_"))
  194. return NULL;
  195. /* Quick check for whether a search may be fruitful. */
  196. /* Ignore anything with @plt, etc. in it. */
  197. if (strchr (mangled_name, '@') != NULL)
  198. return NULL;
  199. /* It must have at least two dots. */
  200. first_dot = strchr (mangled_name, '.');
  201. if (first_dot == NULL)
  202. return NULL;
  203. /* Treat "foo.bar" as unmangled. It can collide with lots of other
  204. languages and it's not clear what the consequences are.
  205. And except for main.main, all gccgo symbols are at least
  206. prefix.package.object. */
  207. last_dot = strrchr (mangled_name, '.');
  208. if (last_dot == first_dot)
  209. return NULL;
  210. /* More quick checks. */
  211. if (last_dot[1] == '\0' /* foo. */
  212. || last_dot[-1] == '.') /* foo..bar */
  213. return NULL;
  214. /* At this point we've decided we have a mangled Go symbol. */
  215. buf = xstrdup (mangled_name);
  216. /* Search backwards looking for "N<digit(s)>". */
  217. p = buf + len;
  218. saw_digit = method_type = NULL;
  219. saw_pointer = 0;
  220. while (p > buf)
  221. {
  222. int current = *(const unsigned char *) --p;
  223. int current_is_digit = isdigit (current);
  224. if (saw_digit)
  225. {
  226. if (current_is_digit)
  227. continue;
  228. if (current == 'N'
  229. && ((p > buf && p[-1] == '.')
  230. || (p > buf + 1 && p[-1] == 'p' && p[-2] == '.')))
  231. {
  232. if (atoi (p + 1) == strlen (saw_digit + 2))
  233. {
  234. if (p[-1] == '.')
  235. method_type = p - 1;
  236. else
  237. {
  238. gdb_assert (p[-1] == 'p');
  239. saw_pointer = 1;
  240. method_type = p - 2;
  241. }
  242. break;
  243. }
  244. }
  245. /* Not what we're looking for, reset and keep looking. */
  246. saw_digit = NULL;
  247. saw_pointer = 0;
  248. continue;
  249. }
  250. if (current_is_digit && p[1] == '_')
  251. {
  252. /* Possible start of method "this" [sic] type. */
  253. saw_digit = p;
  254. continue;
  255. }
  256. }
  257. if (method_type != NULL
  258. /* Ensure not something like "..foo". */
  259. && (method_type > buf && method_type[-1] != '.'))
  260. {
  261. unpack_package_and_object (saw_digit + 2,
  262. method_type_packagep, method_type_objectp);
  263. *method_type = '\0';
  264. *method_type_is_pointerp = saw_pointer;
  265. }
  266. unpack_package_and_object (buf, packagep, objectp);
  267. return buf;
  268. }
  269. /* Implements the la_demangle language_defn routine for language Go.
  270. N.B. This may get passed *any* symbol, including symbols from other
  271. languages and including symbols that are already demangled.
  272. Both of these situations are kinda unfortunate, but that's how things
  273. are today.
  274. N.B. This currently only supports gccgo's mangling.
  275. N.B. gccgo's mangling needs, I think, changing.
  276. This demangler can't work in all situations,
  277. thus not too much effort is currently put into it. */
  278. gdb::unique_xmalloc_ptr<char>
  279. go_language::demangle_symbol (const char *mangled_name, int options) const
  280. {
  281. const char *package_name;
  282. const char *object_name;
  283. const char *method_type_package_name;
  284. const char *method_type_object_name;
  285. int method_type_is_pointer;
  286. if (mangled_name == NULL)
  287. return NULL;
  288. gdb::unique_xmalloc_ptr<char> name_buf
  289. (unpack_mangled_go_symbol (mangled_name,
  290. &package_name, &object_name,
  291. &method_type_package_name,
  292. &method_type_object_name,
  293. &method_type_is_pointer));
  294. if (name_buf == NULL)
  295. return NULL;
  296. auto_obstack tempbuf;
  297. /* Print methods as they appear in "method expressions". */
  298. if (method_type_package_name != NULL)
  299. {
  300. /* FIXME: Seems like we should include package_name here somewhere. */
  301. if (method_type_is_pointer)
  302. obstack_grow_str (&tempbuf, "(*");
  303. obstack_grow_str (&tempbuf, method_type_package_name);
  304. obstack_grow_str (&tempbuf, ".");
  305. obstack_grow_str (&tempbuf, method_type_object_name);
  306. if (method_type_is_pointer)
  307. obstack_grow_str (&tempbuf, ")");
  308. obstack_grow_str (&tempbuf, ".");
  309. obstack_grow_str (&tempbuf, object_name);
  310. }
  311. else
  312. {
  313. obstack_grow_str (&tempbuf, package_name);
  314. obstack_grow_str (&tempbuf, ".");
  315. obstack_grow_str (&tempbuf, object_name);
  316. }
  317. obstack_grow_str0 (&tempbuf, "");
  318. return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
  319. }
  320. /* Given a Go symbol, return its package or NULL if unknown.
  321. Space for the result is malloc'd, caller must free. */
  322. char *
  323. go_symbol_package_name (const struct symbol *sym)
  324. {
  325. const char *mangled_name = sym->linkage_name ();
  326. const char *package_name;
  327. const char *object_name;
  328. const char *method_type_package_name;
  329. const char *method_type_object_name;
  330. int method_type_is_pointer;
  331. char *name_buf;
  332. char *result;
  333. gdb_assert (sym->language () == language_go);
  334. name_buf = unpack_mangled_go_symbol (mangled_name,
  335. &package_name, &object_name,
  336. &method_type_package_name,
  337. &method_type_object_name,
  338. &method_type_is_pointer);
  339. /* Some Go symbols don't have mangled form we interpret (yet). */
  340. if (name_buf == NULL)
  341. return NULL;
  342. result = xstrdup (package_name);
  343. xfree (name_buf);
  344. return result;
  345. }
  346. /* Return the package that BLOCK is in, or NULL if there isn't one.
  347. Space for the result is malloc'd, caller must free. */
  348. char *
  349. go_block_package_name (const struct block *block)
  350. {
  351. while (block != NULL)
  352. {
  353. struct symbol *function = BLOCK_FUNCTION (block);
  354. if (function != NULL)
  355. {
  356. char *package_name = go_symbol_package_name (function);
  357. if (package_name != NULL)
  358. return package_name;
  359. /* Stop looking if we find a function without a package name.
  360. We're most likely outside of Go and thus the concept of the
  361. "current" package is gone. */
  362. return NULL;
  363. }
  364. block = BLOCK_SUPERBLOCK (block);
  365. }
  366. return NULL;
  367. }
  368. /* See language.h. */
  369. void
  370. go_language::language_arch_info (struct gdbarch *gdbarch,
  371. struct language_arch_info *lai) const
  372. {
  373. const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
  374. /* Helper function to allow shorter lines below. */
  375. auto add = [&] (struct type * t) -> struct type *
  376. {
  377. lai->add_primitive_type (t);
  378. return t;
  379. };
  380. add (builtin->builtin_void);
  381. add (builtin->builtin_char);
  382. add (builtin->builtin_bool);
  383. add (builtin->builtin_int);
  384. add (builtin->builtin_uint);
  385. add (builtin->builtin_uintptr);
  386. add (builtin->builtin_int8);
  387. add (builtin->builtin_int16);
  388. add (builtin->builtin_int32);
  389. add (builtin->builtin_int64);
  390. add (builtin->builtin_uint8);
  391. add (builtin->builtin_uint16);
  392. add (builtin->builtin_uint32);
  393. add (builtin->builtin_uint64);
  394. add (builtin->builtin_float32);
  395. add (builtin->builtin_float64);
  396. add (builtin->builtin_complex64);
  397. add (builtin->builtin_complex128);
  398. lai->set_string_char_type (builtin->builtin_char);
  399. lai->set_bool_type (builtin->builtin_bool, "bool");
  400. }
  401. /* Single instance of the Go language class. */
  402. static go_language go_language_defn;
  403. static void *
  404. build_go_types (struct gdbarch *gdbarch)
  405. {
  406. struct builtin_go_type *builtin_go_type
  407. = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_go_type);
  408. builtin_go_type->builtin_void
  409. = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
  410. builtin_go_type->builtin_char
  411. = arch_character_type (gdbarch, 8, 1, "char");
  412. builtin_go_type->builtin_bool
  413. = arch_boolean_type (gdbarch, 8, 0, "bool");
  414. builtin_go_type->builtin_int
  415. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "int");
  416. builtin_go_type->builtin_uint
  417. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "uint");
  418. builtin_go_type->builtin_uintptr
  419. = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
  420. builtin_go_type->builtin_int8
  421. = arch_integer_type (gdbarch, 8, 0, "int8");
  422. builtin_go_type->builtin_int16
  423. = arch_integer_type (gdbarch, 16, 0, "int16");
  424. builtin_go_type->builtin_int32
  425. = arch_integer_type (gdbarch, 32, 0, "int32");
  426. builtin_go_type->builtin_int64
  427. = arch_integer_type (gdbarch, 64, 0, "int64");
  428. builtin_go_type->builtin_uint8
  429. = arch_integer_type (gdbarch, 8, 1, "uint8");
  430. builtin_go_type->builtin_uint16
  431. = arch_integer_type (gdbarch, 16, 1, "uint16");
  432. builtin_go_type->builtin_uint32
  433. = arch_integer_type (gdbarch, 32, 1, "uint32");
  434. builtin_go_type->builtin_uint64
  435. = arch_integer_type (gdbarch, 64, 1, "uint64");
  436. builtin_go_type->builtin_float32
  437. = arch_float_type (gdbarch, 32, "float32", floatformats_ieee_single);
  438. builtin_go_type->builtin_float64
  439. = arch_float_type (gdbarch, 64, "float64", floatformats_ieee_double);
  440. builtin_go_type->builtin_complex64
  441. = init_complex_type ("complex64", builtin_go_type->builtin_float32);
  442. builtin_go_type->builtin_complex128
  443. = init_complex_type ("complex128", builtin_go_type->builtin_float64);
  444. return builtin_go_type;
  445. }
  446. static struct gdbarch_data *go_type_data;
  447. const struct builtin_go_type *
  448. builtin_go_type (struct gdbarch *gdbarch)
  449. {
  450. return (const struct builtin_go_type *) gdbarch_data (gdbarch, go_type_data);
  451. }
  452. void _initialize_go_language ();
  453. void
  454. _initialize_go_language ()
  455. {
  456. go_type_data = gdbarch_data_register_post_init (build_go_types);
  457. }