p-lang.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Pascal language support routines for GDB, the GNU debugger.
  2. Copyright (C) 2000-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. /* This file is derived from c-lang.c */
  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "parser-defs.h"
  20. #include "language.h"
  21. #include "varobj.h"
  22. #include "p-lang.h"
  23. #include "valprint.h"
  24. #include "value.h"
  25. #include <ctype.h>
  26. #include "c-lang.h"
  27. #include "gdbarch.h"
  28. #include "cli/cli-style.h"
  29. /* All GPC versions until now (2007-09-27) also define a symbol called
  30. '_p_initialize'. Check for the presence of this symbol first. */
  31. static const char GPC_P_INITIALIZE[] = "_p_initialize";
  32. /* The name of the symbol that GPC uses as the name of the main
  33. procedure (since version 20050212). */
  34. static const char GPC_MAIN_PROGRAM_NAME_1[] = "_p__M0_main_program";
  35. /* Older versions of GPC (versions older than 20050212) were using
  36. a different name for the main procedure. */
  37. static const char GPC_MAIN_PROGRAM_NAME_2[] = "pascal_main_program";
  38. /* Function returning the special symbol name used
  39. by GPC for the main procedure in the main program
  40. if it is found in minimal symbol list.
  41. This function tries to find minimal symbols generated by GPC
  42. so that it finds the even if the program was compiled
  43. without debugging information.
  44. According to information supplied by Waldeck Hebisch,
  45. this should work for all versions posterior to June 2000. */
  46. const char *
  47. pascal_main_name (void)
  48. {
  49. struct bound_minimal_symbol msym;
  50. msym = lookup_minimal_symbol (GPC_P_INITIALIZE, NULL, NULL);
  51. /* If '_p_initialize' was not found, the main program is likely not
  52. written in Pascal. */
  53. if (msym.minsym == NULL)
  54. return NULL;
  55. msym = lookup_minimal_symbol (GPC_MAIN_PROGRAM_NAME_1, NULL, NULL);
  56. if (msym.minsym != NULL)
  57. {
  58. return GPC_MAIN_PROGRAM_NAME_1;
  59. }
  60. msym = lookup_minimal_symbol (GPC_MAIN_PROGRAM_NAME_2, NULL, NULL);
  61. if (msym.minsym != NULL)
  62. {
  63. return GPC_MAIN_PROGRAM_NAME_2;
  64. }
  65. /* No known entry procedure found, the main program is probably
  66. not compiled with GPC. */
  67. return NULL;
  68. }
  69. /* See p-lang.h. */
  70. int
  71. pascal_is_string_type (struct type *type,int *length_pos, int *length_size,
  72. int *string_pos, struct type **char_type,
  73. const char **arrayname)
  74. {
  75. if (type != NULL && type->code () == TYPE_CODE_STRUCT)
  76. {
  77. /* Old Borland type pascal strings from Free Pascal Compiler. */
  78. /* Two fields: length and st. */
  79. if (type->num_fields () == 2
  80. && type->field (0).name ()
  81. && strcmp (type->field (0).name (), "length") == 0
  82. && type->field (1).name ()
  83. && strcmp (type->field (1).name (), "st") == 0)
  84. {
  85. if (length_pos)
  86. *length_pos = type->field (0).loc_bitpos () / TARGET_CHAR_BIT;
  87. if (length_size)
  88. *length_size = TYPE_LENGTH (type->field (0).type ());
  89. if (string_pos)
  90. *string_pos = type->field (1).loc_bitpos () / TARGET_CHAR_BIT;
  91. if (char_type)
  92. *char_type = TYPE_TARGET_TYPE (type->field (1).type ());
  93. if (arrayname)
  94. *arrayname = type->field (1).name ();
  95. return 2;
  96. };
  97. /* GNU pascal strings. */
  98. /* Three fields: Capacity, length and schema$ or _p_schema. */
  99. if (type->num_fields () == 3
  100. && type->field (0).name ()
  101. && strcmp (type->field (0).name (), "Capacity") == 0
  102. && type->field (1).name ()
  103. && strcmp (type->field (1).name (), "length") == 0)
  104. {
  105. if (length_pos)
  106. *length_pos = type->field (1).loc_bitpos () / TARGET_CHAR_BIT;
  107. if (length_size)
  108. *length_size = TYPE_LENGTH (type->field (1).type ());
  109. if (string_pos)
  110. *string_pos = type->field (2).loc_bitpos () / TARGET_CHAR_BIT;
  111. /* FIXME: how can I detect wide chars in GPC ?? */
  112. if (char_type)
  113. {
  114. *char_type = TYPE_TARGET_TYPE (type->field (2).type ());
  115. if ((*char_type)->code () == TYPE_CODE_ARRAY)
  116. *char_type = TYPE_TARGET_TYPE (*char_type);
  117. }
  118. if (arrayname)
  119. *arrayname = type->field (2).name ();
  120. return 3;
  121. };
  122. }
  123. return 0;
  124. }
  125. /* See p-lang.h. */
  126. void
  127. pascal_language::print_one_char (int c, struct ui_file *stream,
  128. int *in_quotes) const
  129. {
  130. if (c == '\'' || ((unsigned int) c <= 0xff && (PRINT_LITERAL_FORM (c))))
  131. {
  132. if (!(*in_quotes))
  133. gdb_puts ("'", stream);
  134. *in_quotes = 1;
  135. if (c == '\'')
  136. {
  137. gdb_puts ("''", stream);
  138. }
  139. else
  140. gdb_printf (stream, "%c", c);
  141. }
  142. else
  143. {
  144. if (*in_quotes)
  145. gdb_puts ("'", stream);
  146. *in_quotes = 0;
  147. gdb_printf (stream, "#%d", (unsigned int) c);
  148. }
  149. }
  150. /* See language.h. */
  151. void
  152. pascal_language::printchar (int c, struct type *type,
  153. struct ui_file *stream) const
  154. {
  155. int in_quotes = 0;
  156. print_one_char (c, stream, &in_quotes);
  157. if (in_quotes)
  158. gdb_puts ("'", stream);
  159. }
  160. /* See language.h. */
  161. void pascal_language::language_arch_info
  162. (struct gdbarch *gdbarch, struct language_arch_info *lai) const
  163. {
  164. const struct builtin_type *builtin = builtin_type (gdbarch);
  165. /* Helper function to allow shorter lines below. */
  166. auto add = [&] (struct type * t)
  167. {
  168. lai->add_primitive_type (t);
  169. };
  170. add (builtin->builtin_int);
  171. add (builtin->builtin_long);
  172. add (builtin->builtin_short);
  173. add (builtin->builtin_char);
  174. add (builtin->builtin_float);
  175. add (builtin->builtin_double);
  176. add (builtin->builtin_void);
  177. add (builtin->builtin_long_long);
  178. add (builtin->builtin_signed_char);
  179. add (builtin->builtin_unsigned_char);
  180. add (builtin->builtin_unsigned_short);
  181. add (builtin->builtin_unsigned_int);
  182. add (builtin->builtin_unsigned_long);
  183. add (builtin->builtin_unsigned_long_long);
  184. add (builtin->builtin_long_double);
  185. add (builtin->builtin_complex);
  186. add (builtin->builtin_double_complex);
  187. lai->set_string_char_type (builtin->builtin_char);
  188. lai->set_bool_type (builtin->builtin_bool, "boolean");
  189. }
  190. /* See language.h. */
  191. void
  192. pascal_language::printstr (struct ui_file *stream, struct type *elttype,
  193. const gdb_byte *string, unsigned int length,
  194. const char *encoding, int force_ellipses,
  195. const struct value_print_options *options) const
  196. {
  197. enum bfd_endian byte_order = type_byte_order (elttype);
  198. unsigned int i;
  199. unsigned int things_printed = 0;
  200. int in_quotes = 0;
  201. int need_comma = 0;
  202. int width;
  203. /* Preserve ELTTYPE's original type, just set its LENGTH. */
  204. check_typedef (elttype);
  205. width = TYPE_LENGTH (elttype);
  206. /* If the string was not truncated due to `set print elements', and
  207. the last byte of it is a null, we don't print that, in traditional C
  208. style. */
  209. if ((!force_ellipses) && length > 0
  210. && extract_unsigned_integer (string + (length - 1) * width, width,
  211. byte_order) == 0)
  212. length--;
  213. if (length == 0)
  214. {
  215. gdb_puts ("''", stream);
  216. return;
  217. }
  218. for (i = 0; i < length && things_printed < options->print_max; ++i)
  219. {
  220. /* Position of the character we are examining
  221. to see whether it is repeated. */
  222. unsigned int rep1;
  223. /* Number of repetitions we have detected so far. */
  224. unsigned int reps;
  225. unsigned long int current_char;
  226. QUIT;
  227. if (need_comma)
  228. {
  229. gdb_puts (", ", stream);
  230. need_comma = 0;
  231. }
  232. current_char = extract_unsigned_integer (string + i * width, width,
  233. byte_order);
  234. rep1 = i + 1;
  235. reps = 1;
  236. while (rep1 < length
  237. && extract_unsigned_integer (string + rep1 * width, width,
  238. byte_order) == current_char)
  239. {
  240. ++rep1;
  241. ++reps;
  242. }
  243. if (reps > options->repeat_count_threshold)
  244. {
  245. if (in_quotes)
  246. {
  247. gdb_puts ("', ", stream);
  248. in_quotes = 0;
  249. }
  250. printchar (current_char, elttype, stream);
  251. gdb_printf (stream, " %p[<repeats %u times>%p]",
  252. metadata_style.style ().ptr (),
  253. reps, nullptr);
  254. i = rep1 - 1;
  255. things_printed += options->repeat_count_threshold;
  256. need_comma = 1;
  257. }
  258. else
  259. {
  260. if ((!in_quotes) && (PRINT_LITERAL_FORM (current_char)))
  261. {
  262. gdb_puts ("'", stream);
  263. in_quotes = 1;
  264. }
  265. print_one_char (current_char, stream, &in_quotes);
  266. ++things_printed;
  267. }
  268. }
  269. /* Terminate the quotes if necessary. */
  270. if (in_quotes)
  271. gdb_puts ("'", stream);
  272. if (force_ellipses || i < length)
  273. gdb_puts ("...", stream);
  274. }
  275. /* Single instance of the Pascal language class. */
  276. static pascal_language pascal_language_defn;