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