printcmd.c revision 1.12 1 1.1 christos /* Print values for GNU debugger GDB.
2 1.1 christos
3 1.11 christos Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.11 christos #include "event-top.h"
21 1.11 christos #include "extract-store-integer.h"
22 1.1 christos #include "frame.h"
23 1.1 christos #include "symtab.h"
24 1.1 christos #include "gdbtypes.h"
25 1.11 christos #include "top.h"
26 1.1 christos #include "value.h"
27 1.1 christos #include "language.h"
28 1.9 christos #include "c-lang.h"
29 1.1 christos #include "expression.h"
30 1.1 christos #include "gdbcore.h"
31 1.11 christos #include "cli/cli-cmds.h"
32 1.1 christos #include "target.h"
33 1.1 christos #include "breakpoint.h"
34 1.1 christos #include "demangle.h"
35 1.1 christos #include "gdb-demangle.h"
36 1.1 christos #include "valprint.h"
37 1.1 christos #include "annotate.h"
38 1.11 christos #include "symfile.h"
39 1.11 christos #include "objfiles.h"
40 1.11 christos #include "completer.h"
41 1.1 christos #include "ui-out.h"
42 1.1 christos #include "block.h"
43 1.1 christos #include "disasm.h"
44 1.8 christos #include "target-float.h"
45 1.8 christos #include "observable.h"
46 1.1 christos #include "solist.h"
47 1.1 christos #include "parser-defs.h"
48 1.1 christos #include "charset.h"
49 1.1 christos #include "arch-utils.h"
50 1.1 christos #include "cli/cli-utils.h"
51 1.9 christos #include "cli/cli-option.h"
52 1.7 christos #include "cli/cli-script.h"
53 1.8 christos #include "cli/cli-style.h"
54 1.9 christos #include "gdbsupport/format.h"
55 1.1 christos #include "source.h"
56 1.9 christos #include "gdbsupport/byte-vector.h"
57 1.11 christos #include <optional>
58 1.11 christos #include "gdbsupport/gdb-safe-ctype.h"
59 1.10 christos #include "gdbsupport/rsp-low.h"
60 1.11 christos #include "inferior.h"
61 1.10 christos
62 1.10 christos /* Chain containing all defined memory-tag subcommands. */
63 1.10 christos
64 1.10 christos static struct cmd_list_element *memory_tag_list;
65 1.1 christos
66 1.1 christos /* Last specified output format. */
67 1.1 christos
68 1.1 christos static char last_format = 0;
69 1.1 christos
70 1.1 christos /* Last specified examination size. 'b', 'h', 'w' or `q'. */
71 1.1 christos
72 1.1 christos static char last_size = 'w';
73 1.1 christos
74 1.8 christos /* Last specified count for the 'x' command. */
75 1.8 christos
76 1.8 christos static int last_count;
77 1.8 christos
78 1.10 christos /* Last specified tag-printing option. */
79 1.10 christos
80 1.10 christos static bool last_print_tags = false;
81 1.10 christos
82 1.1 christos /* Default address to examine next, and associated architecture. */
83 1.1 christos
84 1.1 christos static struct gdbarch *next_gdbarch;
85 1.1 christos static CORE_ADDR next_address;
86 1.1 christos
87 1.1 christos /* Number of delay instructions following current disassembled insn. */
88 1.1 christos
89 1.1 christos static int branch_delay_insns;
90 1.1 christos
91 1.1 christos /* Last address examined. */
92 1.1 christos
93 1.1 christos static CORE_ADDR last_examine_address;
94 1.1 christos
95 1.1 christos /* Contents of last address examined.
96 1.1 christos This is not valid past the end of the `x' command! */
97 1.1 christos
98 1.8 christos static value_ref_ptr last_examine_value;
99 1.1 christos
100 1.1 christos /* Largest offset between a symbolic value and an address, that will be
101 1.1 christos printed as `0x1234 <symbol+offset>'. */
102 1.1 christos
103 1.1 christos static unsigned int max_symbolic_offset = UINT_MAX;
104 1.1 christos static void
105 1.1 christos show_max_symbolic_offset (struct ui_file *file, int from_tty,
106 1.1 christos struct cmd_list_element *c, const char *value)
107 1.1 christos {
108 1.10 christos gdb_printf (file,
109 1.10 christos _("The largest offset that will be "
110 1.10 christos "printed in <symbol+1234> form is %s.\n"),
111 1.10 christos value);
112 1.1 christos }
113 1.1 christos
114 1.1 christos /* Append the source filename and linenumber of the symbol when
115 1.1 christos printing a symbolic value as `<symbol at filename:linenum>' if set. */
116 1.9 christos static bool print_symbol_filename = false;
117 1.1 christos static void
118 1.1 christos show_print_symbol_filename (struct ui_file *file, int from_tty,
119 1.1 christos struct cmd_list_element *c, const char *value)
120 1.1 christos {
121 1.10 christos gdb_printf (file, _("Printing of source filename and "
122 1.10 christos "line number with <symbol> is %s.\n"),
123 1.10 christos value);
124 1.1 christos }
125 1.1 christos
126 1.1 christos /* Number of auto-display expression currently being displayed.
127 1.1 christos So that we can disable it if we get a signal within it.
128 1.1 christos -1 when not doing one. */
129 1.1 christos
130 1.1 christos static int current_display_number;
131 1.1 christos
132 1.9 christos /* Last allocated display number. */
133 1.9 christos
134 1.9 christos static int display_number;
135 1.9 christos
136 1.1 christos struct display
137 1.1 christos {
138 1.9 christos display (const char *exp_string_, expression_up &&exp_,
139 1.9 christos const struct format_data &format_, struct program_space *pspace_,
140 1.9 christos const struct block *block_)
141 1.9 christos : exp_string (exp_string_),
142 1.9 christos exp (std::move (exp_)),
143 1.9 christos number (++display_number),
144 1.9 christos format (format_),
145 1.9 christos pspace (pspace_),
146 1.9 christos block (block_),
147 1.9 christos enabled_p (true)
148 1.9 christos {
149 1.9 christos }
150 1.1 christos
151 1.1 christos /* The expression as the user typed it. */
152 1.9 christos std::string exp_string;
153 1.1 christos
154 1.1 christos /* Expression to be evaluated and displayed. */
155 1.7 christos expression_up exp;
156 1.1 christos
157 1.1 christos /* Item number of this auto-display item. */
158 1.1 christos int number;
159 1.1 christos
160 1.1 christos /* Display format specified. */
161 1.1 christos struct format_data format;
162 1.1 christos
163 1.1 christos /* Program space associated with `block'. */
164 1.1 christos struct program_space *pspace;
165 1.1 christos
166 1.1 christos /* Innermost block required by this expression when evaluated. */
167 1.1 christos const struct block *block;
168 1.1 christos
169 1.1 christos /* Status of this display (enabled or disabled). */
170 1.9 christos bool enabled_p;
171 1.1 christos };
172 1.1 christos
173 1.9 christos /* Expressions whose values should be displayed automatically each
174 1.9 christos time the program stops. */
175 1.1 christos
176 1.9 christos static std::vector<std::unique_ptr<struct display>> all_displays;
177 1.1 christos
178 1.1 christos /* Prototypes for local functions. */
179 1.1 christos
180 1.1 christos static void do_one_display (struct display *);
181 1.1 christos
182 1.1 christos
184 1.1 christos /* Decode a format specification. *STRING_PTR should point to it.
185 1.1 christos OFORMAT and OSIZE are used as defaults for the format and size
186 1.1 christos if none are given in the format specification.
187 1.1 christos If OSIZE is zero, then the size field of the returned value
188 1.1 christos should be set only if a size is explicitly specified by the
189 1.1 christos user.
190 1.1 christos The structure returned describes all the data
191 1.1 christos found in the specification. In addition, *STRING_PTR is advanced
192 1.1 christos past the specification and past all whitespace following it. */
193 1.1 christos
194 1.1 christos static struct format_data
195 1.1 christos decode_format (const char **string_ptr, int oformat, int osize)
196 1.1 christos {
197 1.1 christos struct format_data val;
198 1.1 christos const char *p = *string_ptr;
199 1.1 christos
200 1.1 christos val.format = '?';
201 1.1 christos val.size = '?';
202 1.1 christos val.count = 1;
203 1.10 christos val.raw = 0;
204 1.1 christos val.print_tags = false;
205 1.6 christos
206 1.6 christos if (*p == '-')
207 1.6 christos {
208 1.6 christos val.count = -1;
209 1.6 christos p++;
210 1.1 christos }
211 1.6 christos if (*p >= '0' && *p <= '9')
212 1.1 christos val.count *= atoi (p);
213 1.1 christos while (*p >= '0' && *p <= '9')
214 1.1 christos p++;
215 1.1 christos
216 1.1 christos /* Now process size or format letters that follow. */
217 1.1 christos
218 1.1 christos while (1)
219 1.1 christos {
220 1.1 christos if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
221 1.1 christos val.size = *p++;
222 1.1 christos else if (*p == 'r')
223 1.1 christos {
224 1.1 christos val.raw = 1;
225 1.1 christos p++;
226 1.10 christos }
227 1.10 christos else if (*p == 'm')
228 1.10 christos {
229 1.10 christos val.print_tags = true;
230 1.10 christos p++;
231 1.1 christos }
232 1.1 christos else if (*p >= 'a' && *p <= 'z')
233 1.1 christos val.format = *p++;
234 1.1 christos else
235 1.1 christos break;
236 1.1 christos }
237 1.8 christos
238 1.1 christos *string_ptr = skip_spaces (p);
239 1.1 christos
240 1.1 christos /* Set defaults for format and size if not specified. */
241 1.1 christos if (val.format == '?')
242 1.1 christos {
243 1.1 christos if (val.size == '?')
244 1.1 christos {
245 1.1 christos /* Neither has been specified. */
246 1.1 christos val.format = oformat;
247 1.1 christos val.size = osize;
248 1.1 christos }
249 1.1 christos else
250 1.1 christos /* If a size is specified, any format makes a reasonable
251 1.1 christos default except 'i'. */
252 1.1 christos val.format = oformat == 'i' ? 'x' : oformat;
253 1.1 christos }
254 1.1 christos else if (val.size == '?')
255 1.1 christos switch (val.format)
256 1.1 christos {
257 1.1 christos case 'a':
258 1.1 christos /* Pick the appropriate size for an address. This is deferred
259 1.1 christos until do_examine when we know the actual architecture to use.
260 1.1 christos A special size value of 'a' is used to indicate this case. */
261 1.1 christos val.size = osize ? 'a' : osize;
262 1.1 christos break;
263 1.1 christos case 'f':
264 1.1 christos /* Floating point has to be word or giantword. */
265 1.1 christos if (osize == 'w' || osize == 'g')
266 1.1 christos val.size = osize;
267 1.1 christos else
268 1.1 christos /* Default it to giantword if the last used size is not
269 1.1 christos appropriate. */
270 1.1 christos val.size = osize ? 'g' : osize;
271 1.1 christos break;
272 1.1 christos case 'c':
273 1.1 christos /* Characters default to one byte. */
274 1.1 christos val.size = osize ? 'b' : osize;
275 1.1 christos break;
276 1.1 christos case 's':
277 1.1 christos /* Display strings with byte size chars unless explicitly
278 1.1 christos specified. */
279 1.1 christos val.size = '\0';
280 1.1 christos break;
281 1.1 christos
282 1.1 christos default:
283 1.1 christos /* The default is the size most recently specified. */
284 1.1 christos val.size = osize;
285 1.1 christos }
286 1.1 christos
287 1.1 christos return val;
288 1.1 christos }
289 1.1 christos
290 1.1 christos /* Print value VAL on stream according to OPTIONS.
292 1.1 christos Do not end with a newline.
293 1.1 christos SIZE is the letter for the size of datum being printed.
294 1.1 christos This is used to pad hex numbers so they line up. SIZE is 0
295 1.1 christos for print / output and set for examine. */
296 1.1 christos
297 1.1 christos static void
298 1.1 christos print_formatted (struct value *val, int size,
299 1.1 christos const struct value_print_options *options,
300 1.11 christos struct ui_file *stream)
301 1.10 christos {
302 1.1 christos struct type *type = check_typedef (val->type ());
303 1.11 christos int len = type->length ();
304 1.11 christos
305 1.1 christos if (val->lval () == lval_memory)
306 1.1 christos next_address = val->address () + len;
307 1.1 christos
308 1.1 christos if (size)
309 1.1 christos {
310 1.1 christos switch (options->format)
311 1.1 christos {
312 1.11 christos case 's':
313 1.1 christos {
314 1.11 christos struct type *elttype = val->type ();
315 1.1 christos
316 1.11 christos next_address = (val->address ()
317 1.1 christos + val_print_string (elttype, NULL,
318 1.1 christos val->address (), -1,
319 1.1 christos stream, options) * len);
320 1.1 christos }
321 1.1 christos return;
322 1.1 christos
323 1.10 christos case 'i':
324 1.11 christos /* We often wrap here if there are long symbolic names. */
325 1.10 christos stream->wrap_here (4);
326 1.11 christos next_address = (val->address ()
327 1.1 christos + gdb_print_insn (type->arch (),
328 1.1 christos val->address (), stream,
329 1.1 christos &branch_delay_insns));
330 1.1 christos return;
331 1.1 christos }
332 1.1 christos }
333 1.10 christos
334 1.9 christos if (options->format == 0 || options->format == 's'
335 1.9 christos || type->code () == TYPE_CODE_VOID
336 1.9 christos || type->code () == TYPE_CODE_REF
337 1.9 christos || type->code () == TYPE_CODE_ARRAY
338 1.9 christos || type->code () == TYPE_CODE_STRING
339 1.9 christos || type->code () == TYPE_CODE_STRUCT
340 1.1 christos || type->code () == TYPE_CODE_UNION
341 1.1 christos || type->code () == TYPE_CODE_NAMESPACE)
342 1.1 christos value_print (val, stream, options);
343 1.1 christos else
344 1.9 christos /* User specified format, so don't look to the type to tell us
345 1.1 christos what to do. */
346 1.1 christos value_print_scalar_formatted (val, options, size, stream);
347 1.1 christos }
348 1.1 christos
349 1.1 christos /* Return builtin floating point type of same length as TYPE.
350 1.1 christos If no such type is found, return TYPE itself. */
351 1.1 christos static struct type *
352 1.10 christos float_type_from_length (struct type *type)
353 1.1 christos {
354 1.1 christos struct gdbarch *gdbarch = type->arch ();
355 1.11 christos const struct builtin_type *builtin = builtin_type (gdbarch);
356 1.11 christos
357 1.11 christos if (type->length () == builtin->builtin_half->length ())
358 1.1 christos type = builtin->builtin_half;
359 1.10 christos else if (type->length () == builtin->builtin_float->length ())
360 1.1 christos type = builtin->builtin_float;
361 1.10 christos else if (type->length () == builtin->builtin_double->length ())
362 1.1 christos type = builtin->builtin_double;
363 1.1 christos else if (type->length () == builtin->builtin_long_double->length ())
364 1.1 christos type = builtin->builtin_long_double;
365 1.1 christos
366 1.1 christos return type;
367 1.1 christos }
368 1.1 christos
369 1.1 christos /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
370 1.1 christos according to OPTIONS and SIZE on STREAM. Formats s and i are not
371 1.1 christos supported at this level. */
372 1.6 christos
373 1.1 christos void
374 1.1 christos print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
375 1.1 christos const struct value_print_options *options,
376 1.10 christos int size, struct ui_file *stream)
377 1.10 christos {
378 1.9 christos struct gdbarch *gdbarch = type->arch ();
379 1.1 christos unsigned int len = type->length ();
380 1.1 christos enum bfd_endian byte_order = type_byte_order (type);
381 1.1 christos
382 1.1 christos /* String printing should go through val_print_scalar_formatted. */
383 1.1 christos gdb_assert (options->format != 's');
384 1.1 christos
385 1.10 christos /* If the value is a pointer, and pointers and addresses are not the
386 1.9 christos same, then at this point, the value's length (in target bytes) is
387 1.1 christos gdbarch_addr_bit/TARGET_CHAR_BIT, not type->length (). */
388 1.1 christos if (type->code () == TYPE_CODE_PTR)
389 1.1 christos len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
390 1.1 christos
391 1.1 christos /* If we are printing it as unsigned, truncate it in case it is actually
392 1.8 christos a negative signed value (e.g. "print/u (short)-1" should print 65535
393 1.10 christos (if shorts are 16 bits) instead of 4294967295). */
394 1.10 christos if (options->format != 'c'
395 1.10 christos && (options->format != 'd' || type->is_unsigned ()))
396 1.10 christos {
397 1.10 christos if (len < type->length () && byte_order == BFD_ENDIAN_BIG)
398 1.10 christos valaddr += type->length () - len;
399 1.10 christos }
400 1.10 christos
401 1.10 christos /* Allow LEN == 0, and in this case, don't assume that VALADDR is
402 1.10 christos valid. */
403 1.1 christos const gdb_byte zero = 0;
404 1.10 christos if (len == 0)
405 1.10 christos {
406 1.1 christos len = 1;
407 1.1 christos valaddr = &zero;
408 1.8 christos }
409 1.1 christos
410 1.8 christos if (size != 0 && (options->format == 'x' || options->format == 't'))
411 1.8 christos {
412 1.8 christos /* Truncate to fit. */
413 1.1 christos unsigned newlen;
414 1.8 christos switch (size)
415 1.8 christos {
416 1.8 christos case 'b':
417 1.8 christos newlen = 1;
418 1.8 christos break;
419 1.8 christos case 'h':
420 1.8 christos newlen = 2;
421 1.8 christos break;
422 1.8 christos case 'w':
423 1.8 christos newlen = 4;
424 1.8 christos break;
425 1.8 christos case 'g':
426 1.8 christos newlen = 8;
427 1.8 christos break;
428 1.1 christos default:
429 1.8 christos error (_("Undefined output size \"%c\"."), size);
430 1.8 christos }
431 1.8 christos if (newlen < len && byte_order == BFD_ENDIAN_BIG)
432 1.8 christos valaddr += len - newlen;
433 1.8 christos len = newlen;
434 1.10 christos }
435 1.10 christos
436 1.9 christos /* Biased range types and sub-word scalar types must be handled
437 1.9 christos here; the value is correctly computed by unpack_long. */
438 1.9 christos gdb::byte_vector converted_bytes;
439 1.9 christos /* Some cases below will unpack the value again. In the biased
440 1.11 christos range case, we want to avoid this, so we store the unpacked value
441 1.10 christos here for possible use later. */
442 1.9 christos std::optional<LONGEST> val_long;
443 1.9 christos if ((is_fixed_point_type (type)
444 1.9 christos && (options->format == 'o'
445 1.9 christos || options->format == 'x'
446 1.9 christos || options->format == 't'
447 1.9 christos || options->format == 'z'
448 1.10 christos || options->format == 'd'
449 1.10 christos || options->format == 'u'))
450 1.9 christos || (type->code () == TYPE_CODE_RANGE && type->bounds ()->bias != 0)
451 1.9 christos || type->bit_size_differs_p ())
452 1.10 christos {
453 1.10 christos val_long.emplace (unpack_long (type, valaddr));
454 1.9 christos converted_bytes.resize (type->length ());
455 1.9 christos store_signed_integer (converted_bytes.data (), type->length (),
456 1.8 christos byte_order, *val_long);
457 1.8 christos valaddr = converted_bytes.data ();
458 1.8 christos }
459 1.8 christos
460 1.8 christos /* Printing a non-float type as 'f' will interpret the data as if it were
461 1.8 christos of a floating-point type of the same length, if that exists. Otherwise,
462 1.9 christos the data is printed as integer. */
463 1.8 christos char format = options->format;
464 1.8 christos if (format == 'f' && type->code () != TYPE_CODE_FLT)
465 1.9 christos {
466 1.10 christos type = float_type_from_length (type);
467 1.8 christos if (type->code () != TYPE_CODE_FLT)
468 1.8 christos format = 0;
469 1.8 christos }
470 1.8 christos
471 1.8 christos switch (format)
472 1.8 christos {
473 1.1 christos case 'o':
474 1.1 christos print_octal_chars (stream, valaddr, len, byte_order);
475 1.8 christos break;
476 1.1 christos case 'd':
477 1.1 christos print_decimal_chars (stream, valaddr, len, true, byte_order);
478 1.8 christos break;
479 1.8 christos case 'u':
480 1.8 christos print_decimal_chars (stream, valaddr, len, false, byte_order);
481 1.9 christos break;
482 1.8 christos case 0:
483 1.10 christos if (type->code () != TYPE_CODE_FLT)
484 1.8 christos {
485 1.8 christos print_decimal_chars (stream, valaddr, len, !type->is_unsigned (),
486 1.8 christos byte_order);
487 1.11 christos break;
488 1.8 christos }
489 1.8 christos [[fallthrough]];
490 1.1 christos case 'f':
491 1.1 christos print_floating (valaddr, type, stream);
492 1.8 christos break;
493 1.10 christos
494 1.8 christos case 't':
495 1.8 christos print_binary_chars (stream, valaddr, len, byte_order, size > 0, options);
496 1.8 christos break;
497 1.1 christos case 'x':
498 1.8 christos print_hex_chars (stream, valaddr, len, byte_order, size > 0);
499 1.8 christos break;
500 1.1 christos case 'z':
501 1.1 christos print_hex_chars (stream, valaddr, len, byte_order, true);
502 1.1 christos break;
503 1.1 christos case 'c':
504 1.1 christos {
505 1.9 christos struct value_print_options opts = *options;
506 1.9 christos
507 1.8 christos if (!val_long.has_value ())
508 1.1 christos val_long.emplace (unpack_long (type, valaddr));
509 1.10 christos
510 1.1 christos opts.format = 0;
511 1.10 christos if (type->is_unsigned ())
512 1.1 christos type = builtin_type (gdbarch)->builtin_true_unsigned_char;
513 1.1 christos else
514 1.9 christos type = builtin_type (gdbarch)->builtin_true_char;
515 1.1 christos
516 1.1 christos value_print (value_from_longest (type, *val_long), stream, &opts);
517 1.1 christos }
518 1.8 christos break;
519 1.1 christos
520 1.9 christos case 'a':
521 1.9 christos {
522 1.9 christos if (!val_long.has_value ())
523 1.1 christos val_long.emplace (unpack_long (type, valaddr));
524 1.1 christos print_address (gdbarch, *val_long, stream);
525 1.1 christos }
526 1.1 christos break;
527 1.8 christos
528 1.1 christos default:
529 1.1 christos error (_("Undefined output format \"%c\"."), format);
530 1.1 christos }
531 1.1 christos }
532 1.1 christos
533 1.1 christos /* Specify default address for `x' command.
534 1.1 christos The `info lines' command uses this. */
535 1.1 christos
536 1.1 christos void
537 1.1 christos set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
538 1.1 christos {
539 1.1 christos struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
540 1.1 christos
541 1.1 christos next_gdbarch = gdbarch;
542 1.1 christos next_address = addr;
543 1.1 christos
544 1.1 christos /* Make address available to the user as $_. */
545 1.1 christos set_internalvar (lookup_internalvar ("_"),
546 1.1 christos value_from_pointer (ptr_type, addr));
547 1.1 christos }
548 1.1 christos
549 1.1 christos /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
550 1.1 christos after LEADIN. Print nothing if no symbolic name is found nearby.
551 1.1 christos Optionally also print source file and line number, if available.
552 1.1 christos DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
553 1.1 christos or to interpret it as a possible C++ name and convert it back to source
554 1.1 christos form. However note that DO_DEMANGLE can be overridden by the specific
555 1.1 christos settings of the demangle and asm_demangle variables. Returns
556 1.1 christos non-zero if anything was printed; zero otherwise. */
557 1.1 christos
558 1.1 christos int
559 1.7 christos print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
560 1.1 christos struct ui_file *stream,
561 1.8 christos int do_demangle, const char *leadin)
562 1.1 christos {
563 1.1 christos std::string name, filename;
564 1.1 christos int unmapped = 0;
565 1.1 christos int offset = 0;
566 1.9 christos int line = 0;
567 1.10 christos
568 1.8 christos if (build_address_symbolic (gdbarch, addr, do_demangle, false, &name,
569 1.1 christos &offset, &filename, &line, &unmapped))
570 1.10 christos return 0;
571 1.1 christos
572 1.10 christos gdb_puts (leadin, stream);
573 1.1 christos if (unmapped)
574 1.10 christos gdb_puts ("<*", stream);
575 1.8 christos else
576 1.1 christos gdb_puts ("<", stream);
577 1.10 christos fputs_styled (name.c_str (), function_name_style.style (), stream);
578 1.1 christos if (offset != 0)
579 1.1 christos gdb_printf (stream, "%+d", offset);
580 1.1 christos
581 1.8 christos /* Append source filename and line number if desired. Give specific
582 1.1 christos line # of this addr, if we have it; else line # of the nearest symbol. */
583 1.10 christos if (print_symbol_filename && !filename.empty ())
584 1.8 christos {
585 1.1 christos gdb_puts (line == -1 ? " in " : " at ", stream);
586 1.10 christos fputs_styled (filename.c_str (), file_name_style.style (), stream);
587 1.1 christos if (line != -1)
588 1.1 christos gdb_printf (stream, ":%d", line);
589 1.10 christos }
590 1.1 christos if (unmapped)
591 1.10 christos gdb_puts ("*>", stream);
592 1.1 christos else
593 1.1 christos gdb_puts (">", stream);
594 1.1 christos
595 1.1 christos return 1;
596 1.8 christos }
597 1.8 christos
598 1.1 christos /* See valprint.h. */
599 1.1 christos
600 1.1 christos int
601 1.9 christos build_address_symbolic (struct gdbarch *gdbarch,
602 1.9 christos CORE_ADDR addr, /* IN */
603 1.8 christos bool do_demangle, /* IN */
604 1.1 christos bool prefer_sym_over_minsym, /* IN */
605 1.8 christos std::string *name, /* OUT */
606 1.1 christos int *offset, /* OUT */
607 1.1 christos std::string *filename, /* OUT */
608 1.1 christos int *line, /* OUT */
609 1.1 christos int *unmapped) /* OUT */
610 1.1 christos {
611 1.1 christos struct symbol *symbol;
612 1.1 christos CORE_ADDR name_location = 0;
613 1.1 christos struct obj_section *section = NULL;
614 1.1 christos const char *name_temp = "";
615 1.1 christos
616 1.1 christos /* Let's say it is mapped (not unmapped). */
617 1.1 christos *unmapped = 0;
618 1.1 christos
619 1.1 christos /* Determine if the address is in an overlay, and whether it is
620 1.1 christos mapped. */
621 1.1 christos if (overlay_debugging)
622 1.1 christos {
623 1.1 christos section = find_pc_overlay (addr);
624 1.1 christos if (pc_in_unmapped_range (addr, section))
625 1.1 christos {
626 1.1 christos *unmapped = 1;
627 1.1 christos addr = overlay_mapped_address (addr, section);
628 1.1 christos }
629 1.9 christos }
630 1.9 christos
631 1.9 christos /* Try to find the address in both the symbol table and the minsyms.
632 1.9 christos In most cases, we'll prefer to use the symbol instead of the
633 1.1 christos minsym. However, there are cases (see below) where we'll choose
634 1.1 christos to use the minsym instead. */
635 1.1 christos
636 1.1 christos /* This is defective in the sense that it only finds text symbols. So
637 1.1 christos really this is kind of pointless--we should make sure that the
638 1.1 christos minimal symbols have everything we need (by changing that we could
639 1.1 christos save some memory, but for many debug format--ELF/DWARF or
640 1.12 christos anything/stabs--it would be inconvenient to eliminate those minimal
641 1.12 christos symbols anyway). */
642 1.1 christos bound_minimal_symbol msymbol
643 1.1 christos = lookup_minimal_symbol_by_pc_section (addr, section);
644 1.1 christos symbol = find_pc_sect_function (addr, section);
645 1.1 christos
646 1.1 christos if (symbol)
647 1.1 christos {
648 1.1 christos /* If this is a function (i.e. a code address), strip out any
649 1.1 christos non-address bits. For instance, display a pointer to the
650 1.1 christos first instruction of a Thumb function as <function>; the
651 1.1 christos second instruction will be <function+2>, even though the
652 1.1 christos pointer is <function+3>. This matches the ISA behavior. */
653 1.10 christos addr = gdbarch_addr_bits_remove (gdbarch, addr);
654 1.1 christos
655 1.9 christos name_location = symbol->value_block ()->entry_pc ();
656 1.1 christos if (do_demangle || asm_demangle)
657 1.9 christos name_temp = symbol->print_name ();
658 1.1 christos else
659 1.1 christos name_temp = symbol->linkage_name ();
660 1.3 christos }
661 1.10 christos
662 1.10 christos if (msymbol.minsym != NULL
663 1.10 christos && msymbol.minsym->has_size ()
664 1.10 christos && msymbol.minsym->size () == 0
665 1.10 christos && msymbol.minsym->type () != mst_text
666 1.3 christos && msymbol.minsym->type () != mst_text_gnu_ifunc
667 1.1 christos && msymbol.minsym->type () != mst_file_text)
668 1.3 christos msymbol.minsym = NULL;
669 1.1 christos
670 1.9 christos if (msymbol.minsym != NULL)
671 1.9 christos {
672 1.9 christos /* Use the minsym if no symbol is found.
673 1.9 christos
674 1.9 christos Additionally, use the minsym instead of a (found) symbol if
675 1.9 christos the following conditions all hold:
676 1.9 christos 1) The prefer_sym_over_minsym flag is false.
677 1.9 christos 2) The minsym address is identical to that of the address under
678 1.9 christos consideration.
679 1.9 christos 3) The symbol address is not identical to that of the address
680 1.10 christos under consideration. */
681 1.10 christos if (symbol == NULL ||
682 1.9 christos (!prefer_sym_over_minsym
683 1.1 christos && msymbol.value_address () == addr
684 1.1 christos && name_location != addr))
685 1.1 christos {
686 1.1 christos /* If this is a function (i.e. a code address), strip out any
687 1.1 christos non-address bits. For instance, display a pointer to the
688 1.1 christos first instruction of a Thumb function as <function>; the
689 1.10 christos second instruction will be <function+2>, even though the
690 1.10 christos pointer is <function+3>. This matches the ISA behavior. */
691 1.10 christos if (msymbol.minsym->type () == mst_text
692 1.10 christos || msymbol.minsym->type () == mst_text_gnu_ifunc
693 1.1 christos || msymbol.minsym->type () == mst_file_text
694 1.1 christos || msymbol.minsym->type () == mst_solib_trampoline)
695 1.1 christos addr = gdbarch_addr_bits_remove (gdbarch, addr);
696 1.10 christos
697 1.1 christos symbol = 0;
698 1.9 christos name_location = msymbol.value_address ();
699 1.1 christos if (do_demangle || asm_demangle)
700 1.9 christos name_temp = msymbol.minsym->print_name ();
701 1.1 christos else
702 1.1 christos name_temp = msymbol.minsym->linkage_name ();
703 1.3 christos }
704 1.1 christos }
705 1.1 christos if (symbol == NULL && msymbol.minsym == NULL)
706 1.1 christos return 1;
707 1.1 christos
708 1.1 christos /* If the nearest symbol is too far away, don't print anything symbolic. */
709 1.1 christos
710 1.1 christos /* For when CORE_ADDR is larger than unsigned int, we do math in
711 1.1 christos CORE_ADDR. But when we detect unsigned wraparound in the
712 1.1 christos CORE_ADDR math, we ignore this test and print the offset,
713 1.1 christos because addr+max_symbolic_offset has wrapped through the end
714 1.1 christos of the address space back to the beginning, giving bogus comparison. */
715 1.1 christos if (addr > name_location + max_symbolic_offset
716 1.1 christos && name_location + max_symbolic_offset > name_location)
717 1.9 christos return 1;
718 1.1 christos
719 1.8 christos *offset = (LONGEST) addr - name_location;
720 1.1 christos
721 1.1 christos *name = name_temp;
722 1.1 christos
723 1.1 christos if (print_symbol_filename)
724 1.1 christos {
725 1.1 christos struct symtab_and_line sal;
726 1.1 christos
727 1.1 christos sal = find_pc_sect_line (addr, section, 0);
728 1.1 christos
729 1.8 christos if (sal.symtab)
730 1.1 christos {
731 1.1 christos *filename = symtab_to_filename_for_display (sal.symtab);
732 1.1 christos *line = sal.line;
733 1.1 christos }
734 1.1 christos }
735 1.1 christos return 0;
736 1.1 christos }
737 1.1 christos
738 1.1 christos
739 1.1 christos /* Print address ADDR symbolically on STREAM.
740 1.1 christos First print it as a number. Then perhaps print
741 1.1 christos <SYMBOL + OFFSET> after the number. */
742 1.1 christos
743 1.1 christos void
744 1.1 christos print_address (struct gdbarch *gdbarch,
745 1.8 christos CORE_ADDR addr, struct ui_file *stream)
746 1.1 christos {
747 1.1 christos fputs_styled (paddress (gdbarch, addr), address_style.style (), stream);
748 1.1 christos print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
749 1.1 christos }
750 1.1 christos
751 1.1 christos /* Return a prefix for instruction address:
752 1.1 christos "=> " for current instruction, else " ". */
753 1.1 christos
754 1.1 christos const char *
755 1.1 christos pc_prefix (CORE_ADDR addr)
756 1.1 christos {
757 1.10 christos if (has_stack_frames ())
758 1.1 christos {
759 1.1 christos frame_info_ptr frame;
760 1.1 christos CORE_ADDR pc;
761 1.1 christos
762 1.1 christos frame = get_selected_frame (NULL);
763 1.1 christos if (get_frame_pc_if_available (frame, &pc) && pc == addr)
764 1.1 christos return "=> ";
765 1.1 christos }
766 1.1 christos return " ";
767 1.1 christos }
768 1.1 christos
769 1.1 christos /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
770 1.1 christos controls whether to print the symbolic name "raw" or demangled.
771 1.1 christos Return non-zero if anything was printed; zero otherwise. */
772 1.1 christos
773 1.1 christos int
774 1.1 christos print_address_demangle (const struct value_print_options *opts,
775 1.1 christos struct gdbarch *gdbarch, CORE_ADDR addr,
776 1.1 christos struct ui_file *stream, int do_demangle)
777 1.1 christos {
778 1.8 christos if (opts->addressprint)
779 1.1 christos {
780 1.1 christos fputs_styled (paddress (gdbarch, addr), address_style.style (), stream);
781 1.1 christos print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
782 1.1 christos }
783 1.1 christos else
784 1.1 christos {
785 1.1 christos return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
786 1.1 christos }
787 1.1 christos return 1;
788 1.1 christos }
789 1.6 christos
790 1.6 christos
792 1.6 christos /* Find the address of the instruction that is INST_COUNT instructions before
793 1.6 christos the instruction at ADDR.
794 1.6 christos Since some architectures have variable-length instructions, we can't just
795 1.6 christos simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
796 1.6 christos number information to locate the nearest known instruction boundary,
797 1.6 christos and disassemble forward from there. If we go out of the symbol range
798 1.6 christos during disassembling, we return the lowest address we've got so far and
799 1.6 christos set the number of instructions read to INST_READ. */
800 1.10 christos
801 1.6 christos static CORE_ADDR
802 1.6 christos find_instruction_backward (struct gdbarch *gdbarch, CORE_ADDR addr,
803 1.6 christos int inst_count, int *inst_read)
804 1.6 christos {
805 1.7 christos /* The vector PCS is used to store instruction addresses within
806 1.6 christos a pc range. */
807 1.6 christos CORE_ADDR loop_start, loop_end, p;
808 1.6 christos std::vector<CORE_ADDR> pcs;
809 1.6 christos struct symtab_and_line sal;
810 1.6 christos
811 1.6 christos *inst_read = 0;
812 1.6 christos loop_start = loop_end = addr;
813 1.6 christos
814 1.6 christos /* In each iteration of the outer loop, we get a pc range that ends before
815 1.6 christos LOOP_START, then we count and store every instruction address of the range
816 1.6 christos iterated in the loop.
817 1.6 christos If the number of instructions counted reaches INST_COUNT, return the
818 1.6 christos stored address that is located INST_COUNT instructions back from ADDR.
819 1.6 christos If INST_COUNT is not reached, we subtract the number of counted
820 1.7 christos instructions from INST_COUNT, and go to the next iteration. */
821 1.6 christos do
822 1.6 christos {
823 1.10 christos pcs.clear ();
824 1.10 christos sal = find_pc_sect_line (loop_start, NULL, 1);
825 1.10 christos if (sal.line <= 0)
826 1.10 christos {
827 1.10 christos /* We reach here when line info is not available. In this case,
828 1.10 christos we print a message and just exit the loop. The return value
829 1.10 christos is calculated after the loop. */
830 1.10 christos gdb_printf (_("No line number information available "
831 1.10 christos "for address "));
832 1.10 christos gdb_stdout->wrap_here (2);
833 1.10 christos print_address (gdbarch, loop_start - 1, gdb_stdout);
834 1.6 christos gdb_printf ("\n");
835 1.6 christos break;
836 1.6 christos }
837 1.6 christos
838 1.6 christos loop_end = loop_start;
839 1.10 christos loop_start = sal.pc;
840 1.6 christos
841 1.10 christos /* This loop pushes instruction addresses in the range from
842 1.7 christos LOOP_START to LOOP_END. */
843 1.10 christos for (p = loop_start; p < loop_end;)
844 1.10 christos {
845 1.6 christos pcs.push_back (p);
846 1.7 christos p += gdb_insn_length (gdbarch, p);
847 1.7 christos }
848 1.6 christos
849 1.6 christos inst_count -= pcs.size ();
850 1.6 christos *inst_read += pcs.size ();
851 1.6 christos }
852 1.6 christos while (inst_count > 0);
853 1.6 christos
854 1.6 christos /* After the loop, the vector PCS has instruction addresses of the last
855 1.6 christos source line we processed, and INST_COUNT has a negative value.
856 1.6 christos We return the address at the index of -INST_COUNT in the vector for
857 1.10 christos the reason below.
858 1.10 christos Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
859 1.10 christos Line X of File
860 1.6 christos 0x4000
861 1.10 christos 0x4001
862 1.10 christos 0x4005
863 1.6 christos Line Y of File
864 1.10 christos 0x4009
865 1.6 christos 0x400c
866 1.6 christos => 0x400e
867 1.6 christos 0x4011
868 1.6 christos find_instruction_backward is called with INST_COUNT = 4 and expected to
869 1.6 christos return 0x4001. When we reach here, INST_COUNT is set to -1 because
870 1.6 christos it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
871 1.6 christos 4001 is located at the index 1 of the last iterated line (= Line X),
872 1.6 christos which is simply calculated by -INST_COUNT.
873 1.7 christos The case when the length of PCS is 0 means that we reached an area for
874 1.6 christos which line info is not available. In such case, we return LOOP_START,
875 1.6 christos which was the lowest instruction address that had line info. */
876 1.6 christos p = pcs.size () > 0 ? pcs[-inst_count] : loop_start;
877 1.6 christos
878 1.6 christos /* INST_READ includes all instruction addresses in a pc range. Need to
879 1.6 christos exclude the beginning part up to the address we're returning. That
880 1.6 christos is, exclude {0x4000} in the example above. */
881 1.6 christos if (inst_count < 0)
882 1.6 christos *inst_read += inst_count;
883 1.6 christos
884 1.6 christos return p;
885 1.6 christos }
886 1.6 christos
887 1.6 christos /* Backward read LEN bytes of target memory from address MEMADDR + LEN,
888 1.6 christos placing the results in GDB's memory from MYADDR + LEN. Returns
889 1.6 christos a count of the bytes actually read. */
890 1.10 christos
891 1.6 christos static int
892 1.6 christos read_memory_backward (struct gdbarch *gdbarch,
893 1.6 christos CORE_ADDR memaddr, gdb_byte *myaddr, int len)
894 1.6 christos {
895 1.6 christos int errcode;
896 1.6 christos int nread; /* Number of bytes actually read. */
897 1.6 christos
898 1.6 christos /* First try a complete read. */
899 1.6 christos errcode = target_read_memory (memaddr, myaddr, len);
900 1.6 christos if (errcode == 0)
901 1.6 christos {
902 1.6 christos /* Got it all. */
903 1.6 christos nread = len;
904 1.6 christos }
905 1.6 christos else
906 1.6 christos {
907 1.6 christos /* Loop, reading one byte at a time until we get as much as we can. */
908 1.10 christos memaddr += len;
909 1.10 christos myaddr += len;
910 1.10 christos for (nread = 0; nread < len; ++nread)
911 1.10 christos {
912 1.10 christos errcode = target_read_memory (--memaddr, --myaddr, 1);
913 1.10 christos if (errcode != 0)
914 1.10 christos {
915 1.10 christos /* The read was unsuccessful, so exit the loop. */
916 1.10 christos gdb_printf (_("Cannot access memory at address %s\n"),
917 1.10 christos paddress (gdbarch, memaddr));
918 1.6 christos break;
919 1.6 christos }
920 1.6 christos }
921 1.6 christos }
922 1.6 christos return nread;
923 1.6 christos }
924 1.6 christos
925 1.6 christos /* Returns true if X (which is LEN bytes wide) is the number zero. */
926 1.6 christos
927 1.6 christos static int
928 1.6 christos integer_is_zero (const gdb_byte *x, int len)
929 1.6 christos {
930 1.6 christos int i = 0;
931 1.6 christos
932 1.6 christos while (i < len && x[i] == 0)
933 1.6 christos ++i;
934 1.6 christos return (i == len);
935 1.6 christos }
936 1.6 christos
937 1.6 christos /* Find the start address of a string in which ADDR is included.
938 1.6 christos Basically we search for '\0' and return the next address,
939 1.6 christos but if OPTIONS->PRINT_MAX is smaller than the length of a string,
940 1.6 christos we stop searching and return the address to print characters as many as
941 1.6 christos PRINT_MAX from the string. */
942 1.10 christos
943 1.10 christos static CORE_ADDR
944 1.10 christos find_string_backward (struct gdbarch *gdbarch,
945 1.6 christos CORE_ADDR addr, int count, int char_size,
946 1.6 christos const struct value_print_options *options,
947 1.6 christos int *strings_counted)
948 1.6 christos {
949 1.6 christos const int chunk_size = 0x20;
950 1.6 christos int read_error = 0;
951 1.6 christos int chars_read = 0;
952 1.6 christos int chars_to_read = chunk_size;
953 1.6 christos int chars_counted = 0;
954 1.6 christos int count_original = count;
955 1.8 christos CORE_ADDR string_start_addr = addr;
956 1.6 christos
957 1.6 christos gdb_assert (char_size == 1 || char_size == 2 || char_size == 4);
958 1.6 christos gdb::byte_vector buffer (chars_to_read * char_size);
959 1.6 christos while (count > 0 && read_error == 0)
960 1.6 christos {
961 1.8 christos int i;
962 1.10 christos
963 1.6 christos addr -= chars_to_read * char_size;
964 1.6 christos chars_read = read_memory_backward (gdbarch, addr, buffer.data (),
965 1.11 christos chars_to_read * char_size);
966 1.6 christos chars_read /= char_size;
967 1.6 christos read_error = (chars_read == chars_to_read) ? 0 : 1;
968 1.10 christos unsigned int print_max_chars = get_print_max_chars (options);
969 1.10 christos /* Searching for '\0' from the end of buffer in backward direction. */
970 1.6 christos for (i = 0; i < chars_read && count > 0 ; ++i, ++chars_counted)
971 1.10 christos {
972 1.11 christos int offset = (chars_to_read - i - 1) * char_size;
973 1.10 christos
974 1.11 christos if (integer_is_zero (&buffer[offset], char_size)
975 1.11 christos || chars_counted == print_max_chars)
976 1.11 christos {
977 1.10 christos /* Found '\0' or reached `print_max_chars'. As OFFSET
978 1.10 christos is the offset to '\0', we add CHAR_SIZE to return
979 1.10 christos the start address of a string. */
980 1.10 christos --count;
981 1.10 christos string_start_addr = addr + offset + char_size;
982 1.6 christos chars_counted = 0;
983 1.6 christos }
984 1.6 christos }
985 1.6 christos }
986 1.6 christos
987 1.6 christos /* Update STRINGS_COUNTED with the actual number of loaded strings. */
988 1.6 christos *strings_counted = count_original - count;
989 1.6 christos
990 1.10 christos if (read_error != 0)
991 1.6 christos {
992 1.6 christos /* In error case, STRING_START_ADDR is pointing to the string that
993 1.6 christos was last successfully loaded. Rewind the partially loaded string. */
994 1.6 christos string_start_addr -= chars_counted * char_size;
995 1.6 christos }
996 1.6 christos
997 1.1 christos return string_start_addr;
998 1.1 christos }
999 1.1 christos
1000 1.1 christos /* Examine data at address ADDR in format FMT.
1001 1.1 christos Fetch it from memory and print on gdb_stdout. */
1002 1.1 christos
1003 1.1 christos static void
1004 1.1 christos do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
1005 1.1 christos {
1006 1.1 christos char format = 0;
1007 1.1 christos char size;
1008 1.1 christos int count = 1;
1009 1.1 christos struct type *val_type = NULL;
1010 1.6 christos int i;
1011 1.6 christos int maxelts;
1012 1.1 christos struct value_print_options opts;
1013 1.1 christos int need_to_update_next_address = 0;
1014 1.1 christos CORE_ADDR addr_rewound = 0;
1015 1.1 christos
1016 1.1 christos format = fmt.format;
1017 1.1 christos size = fmt.size;
1018 1.1 christos count = fmt.count;
1019 1.1 christos next_gdbarch = gdbarch;
1020 1.1 christos next_address = addr;
1021 1.1 christos
1022 1.1 christos /* Instruction format implies fetch single bytes
1023 1.1 christos regardless of the specified size.
1024 1.1 christos The case of strings is handled in decode_format, only explicit
1025 1.1 christos size operator are not changed to 'b'. */
1026 1.1 christos if (format == 'i')
1027 1.1 christos size = 'b';
1028 1.1 christos
1029 1.1 christos if (size == 'a')
1030 1.1 christos {
1031 1.1 christos /* Pick the appropriate size for an address. */
1032 1.1 christos if (gdbarch_ptr_bit (next_gdbarch) == 64)
1033 1.1 christos size = 'g';
1034 1.1 christos else if (gdbarch_ptr_bit (next_gdbarch) == 32)
1035 1.1 christos size = 'w';
1036 1.1 christos else if (gdbarch_ptr_bit (next_gdbarch) == 16)
1037 1.10 christos size = 'h';
1038 1.1 christos else
1039 1.1 christos /* Bad value for gdbarch_ptr_bit. */
1040 1.1 christos internal_error (_("failed internal consistency check"));
1041 1.1 christos }
1042 1.1 christos
1043 1.1 christos if (size == 'b')
1044 1.1 christos val_type = builtin_type (next_gdbarch)->builtin_int8;
1045 1.1 christos else if (size == 'h')
1046 1.1 christos val_type = builtin_type (next_gdbarch)->builtin_int16;
1047 1.1 christos else if (size == 'w')
1048 1.1 christos val_type = builtin_type (next_gdbarch)->builtin_int32;
1049 1.1 christos else if (size == 'g')
1050 1.1 christos val_type = builtin_type (next_gdbarch)->builtin_int64;
1051 1.1 christos
1052 1.1 christos if (format == 's')
1053 1.1 christos {
1054 1.1 christos struct type *char_type = NULL;
1055 1.1 christos
1056 1.1 christos /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1057 1.1 christos if type is not found. */
1058 1.1 christos if (size == 'h')
1059 1.1 christos char_type = builtin_type (next_gdbarch)->builtin_char16;
1060 1.10 christos else if (size == 'w')
1061 1.1 christos char_type = builtin_type (next_gdbarch)->builtin_char32;
1062 1.10 christos if (char_type)
1063 1.1 christos val_type = char_type;
1064 1.1 christos else
1065 1.1 christos {
1066 1.1 christos if (size != '\0' && size != 'b')
1067 1.1 christos warning (_("Unable to display strings with "
1068 1.10 christos "size '%c', using 'b' instead."), size);
1069 1.1 christos size = 'b';
1070 1.1 christos val_type = builtin_type (next_gdbarch)->builtin_int8;
1071 1.1 christos }
1072 1.1 christos }
1073 1.1 christos
1074 1.1 christos maxelts = 8;
1075 1.1 christos if (size == 'w')
1076 1.1 christos maxelts = 4;
1077 1.1 christos if (size == 'g')
1078 1.1 christos maxelts = 2;
1079 1.1 christos if (format == 's' || format == 'i')
1080 1.1 christos maxelts = 1;
1081 1.6 christos
1082 1.6 christos get_formatted_print_options (&opts, format);
1083 1.6 christos
1084 1.10 christos if (count < 0)
1085 1.10 christos {
1086 1.6 christos /* This is the negative repeat count case.
1087 1.6 christos We rewind the address based on the given repeat count and format,
1088 1.6 christos then examine memory from there in forward direction. */
1089 1.10 christos
1090 1.10 christos count = -count;
1091 1.10 christos if (format == 'i')
1092 1.10 christos {
1093 1.6 christos next_address = find_instruction_backward (gdbarch, addr, count,
1094 1.10 christos &count);
1095 1.10 christos }
1096 1.10 christos else if (format == 's')
1097 1.10 christos {
1098 1.10 christos next_address = find_string_backward (gdbarch, addr, count,
1099 1.6 christos val_type->length (),
1100 1.10 christos &opts, &count);
1101 1.10 christos }
1102 1.10 christos else
1103 1.6 christos {
1104 1.6 christos next_address = addr - count * val_type->length ();
1105 1.10 christos }
1106 1.10 christos
1107 1.6 christos /* The following call to print_formatted updates next_address in every
1108 1.10 christos iteration. In backward case, we store the start address here
1109 1.10 christos and update next_address with it before exiting the function. */
1110 1.6 christos addr_rewound = (format == 's'
1111 1.6 christos ? next_address - val_type->length ()
1112 1.6 christos : next_address);
1113 1.10 christos need_to_update_next_address = 1;
1114 1.10 christos }
1115 1.10 christos
1116 1.10 christos /* Whether we need to print the memory tag information for the current
1117 1.10 christos address range. */
1118 1.1 christos bool print_range_tag = true;
1119 1.1 christos uint32_t gsize = gdbarch_memtag_granule_size (gdbarch);
1120 1.1 christos
1121 1.1 christos /* Print as many objects as specified in COUNT, at most maxelts per line,
1122 1.1 christos with the address of the next one at the start of each line. */
1123 1.1 christos
1124 1.10 christos while (count > 0)
1125 1.10 christos {
1126 1.10 christos QUIT;
1127 1.10 christos
1128 1.10 christos CORE_ADDR tag_laddr = 0, tag_haddr = 0;
1129 1.10 christos
1130 1.10 christos /* Print the memory tag information if requested. */
1131 1.10 christos if (fmt.print_tags && print_range_tag
1132 1.10 christos && target_supports_memory_tagging ())
1133 1.10 christos {
1134 1.10 christos tag_laddr = align_down (next_address, gsize);
1135 1.10 christos tag_haddr = align_down (next_address + gsize, gsize);
1136 1.10 christos
1137 1.10 christos struct value *v_addr
1138 1.11 christos = value_from_ulongest (builtin_type (gdbarch)->builtin_data_ptr,
1139 1.10 christos tag_laddr);
1140 1.10 christos
1141 1.10 christos if (target_is_address_tagged (gdbarch, value_as_address (v_addr)))
1142 1.10 christos {
1143 1.10 christos /* Fetch the allocation tag. */
1144 1.10 christos struct value *tag
1145 1.10 christos = gdbarch_get_memtag (gdbarch, v_addr, memtag_type::allocation);
1146 1.10 christos std::string atag
1147 1.10 christos = gdbarch_memtag_to_string (gdbarch, tag);
1148 1.10 christos
1149 1.10 christos if (!atag.empty ())
1150 1.10 christos {
1151 1.10 christos gdb_printf (_("<Allocation Tag %s for range [%s,%s)>\n"),
1152 1.10 christos atag.c_str (),
1153 1.10 christos paddress (gdbarch, tag_laddr),
1154 1.10 christos paddress (gdbarch, tag_haddr));
1155 1.10 christos }
1156 1.10 christos }
1157 1.1 christos print_range_tag = false;
1158 1.10 christos }
1159 1.1 christos
1160 1.10 christos if (format == 'i')
1161 1.1 christos gdb_puts (pc_prefix (next_address));
1162 1.1 christos print_address (next_gdbarch, next_address, gdb_stdout);
1163 1.1 christos gdb_printf (":");
1164 1.1 christos for (i = maxelts;
1165 1.10 christos i > 0 && count > 0;
1166 1.1 christos i--, count--)
1167 1.1 christos {
1168 1.1 christos gdb_printf ("\t");
1169 1.1 christos /* Note that print_formatted sets next_address for the next
1170 1.1 christos object. */
1171 1.1 christos last_examine_address = next_address;
1172 1.1 christos
1173 1.1 christos /* The value to be displayed is not fetched greedily.
1174 1.1 christos Instead, to avoid the possibility of a fetched value not
1175 1.1 christos being used, its retrieval is delayed until the print code
1176 1.1 christos uses it. When examining an instruction stream, the
1177 1.1 christos disassembler will perform its own memory fetch using just
1178 1.1 christos the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1179 1.8 christos the disassembler be modified so that LAST_EXAMINE_VALUE
1180 1.8 christos is left with the byte sequence from the last complete
1181 1.1 christos instruction fetched from memory? */
1182 1.8 christos last_examine_value
1183 1.1 christos = release_value (value_at_lazy (val_type, next_address));
1184 1.1 christos
1185 1.1 christos print_formatted (last_examine_value.get (), size, &opts, gdb_stdout);
1186 1.1 christos
1187 1.10 christos /* Display any branch delay slots following the final insn. */
1188 1.10 christos if (format == 'i' && count == 1)
1189 1.10 christos count += branch_delay_insns;
1190 1.10 christos
1191 1.10 christos /* Update the tag range based on the current address being
1192 1.1 christos processed. */
1193 1.10 christos if (tag_haddr <= next_address)
1194 1.1 christos print_range_tag = true;
1195 1.6 christos }
1196 1.6 christos gdb_printf ("\n");
1197 1.6 christos }
1198 1.1 christos
1199 1.1 christos if (need_to_update_next_address)
1200 1.1 christos next_address = addr_rewound;
1201 1.5 christos }
1202 1.1 christos
1203 1.1 christos static void
1205 1.1 christos validate_format (struct format_data fmt, const char *cmdname)
1206 1.1 christos {
1207 1.1 christos if (fmt.size != 0)
1208 1.1 christos error (_("Size letters are meaningless in \"%s\" command."), cmdname);
1209 1.1 christos if (fmt.count != 1)
1210 1.1 christos error (_("Item count other than 1 is meaningless in \"%s\" command."),
1211 1.1 christos cmdname);
1212 1.1 christos if (fmt.format == 'i')
1213 1.9 christos error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
1214 1.5 christos fmt.format, cmdname);
1215 1.5 christos }
1216 1.5 christos
1217 1.5 christos /* Parse print command format string into *OPTS and update *EXPP.
1218 1.9 christos CMDNAME should name the current command. */
1219 1.5 christos
1220 1.5 christos void
1221 1.5 christos print_command_parse_format (const char **expp, const char *cmdname,
1222 1.9 christos value_print_options *opts)
1223 1.9 christos {
1224 1.9 christos const char *exp = *expp;
1225 1.5 christos
1226 1.5 christos /* opts->raw value might already have been set by 'set print raw-values'
1227 1.9 christos or by using 'print -raw-values'.
1228 1.9 christos So, do not set opts->raw to 0, only set it to 1 if /r is given. */
1229 1.5 christos if (exp && *exp == '/')
1230 1.9 christos {
1231 1.9 christos format_data fmt;
1232 1.9 christos
1233 1.9 christos exp++;
1234 1.9 christos fmt = decode_format (&exp, last_format, 0);
1235 1.9 christos validate_format (fmt, cmdname);
1236 1.5 christos last_format = fmt.format;
1237 1.5 christos
1238 1.5 christos opts->format = fmt.format;
1239 1.9 christos opts->raw = opts->raw || fmt.raw;
1240 1.5 christos }
1241 1.5 christos else
1242 1.5 christos {
1243 1.5 christos opts->format = 0;
1244 1.5 christos }
1245 1.9 christos
1246 1.5 christos *expp = exp;
1247 1.5 christos }
1248 1.9 christos
1249 1.5 christos /* See valprint.h. */
1250 1.11 christos
1251 1.11 christos void
1252 1.11 christos print_value (value *val, const value_print_options &opts)
1253 1.11 christos {
1254 1.11 christos /* This setting allows large arrays to be printed by limiting the
1255 1.11 christos number of elements that are loaded into GDB's memory; we only
1256 1.11 christos need to load as many array elements as we plan to print. */
1257 1.11 christos scoped_array_length_limiting limit_large_arrays (opts.print_max);
1258 1.11 christos
1259 1.11 christos int histindex = val->record_latest ();
1260 1.11 christos
1261 1.11 christos annotate_value_history_begin (histindex, val->type ());
1262 1.5 christos
1263 1.5 christos std::string idx = string_printf ("$%d", histindex);
1264 1.5 christos gdb_printf ("%ps = ", styled_string (variable_name_style.style (),
1265 1.9 christos idx.c_str ()));
1266 1.10 christos
1267 1.5 christos annotate_value_history_value ();
1268 1.5 christos
1269 1.5 christos print_formatted (val, 0, &opts, gdb_stdout);
1270 1.5 christos gdb_printf ("\n");
1271 1.10 christos
1272 1.1 christos annotate_value_history_end ();
1273 1.10 christos }
1274 1.11 christos
1275 1.1 christos /* Returns true if memory tags should be validated. False otherwise. */
1276 1.11 christos
1277 1.10 christos static bool
1278 1.10 christos should_validate_memtags (gdbarch *gdbarch, struct value *value)
1279 1.10 christos {
1280 1.10 christos gdb_assert (value != nullptr && value->type () != nullptr);
1281 1.11 christos
1282 1.10 christos if (!target_supports_memory_tagging ())
1283 1.10 christos return false;
1284 1.10 christos
1285 1.11 christos enum type_code code = value->type ()->code ();
1286 1.10 christos
1287 1.10 christos /* Skip non-address values. */
1288 1.10 christos if (code != TYPE_CODE_PTR
1289 1.10 christos && !TYPE_IS_REFERENCE (value->type ()))
1290 1.11 christos return false;
1291 1.11 christos
1292 1.10 christos /* OK, we have an address value. Check we have a complete value we
1293 1.10 christos can extract. */
1294 1.10 christos if (value->optimized_out ()
1295 1.11 christos || !value->entirely_available ())
1296 1.10 christos return false;
1297 1.10 christos
1298 1.10 christos /* We do. Check whether it includes any tags. */
1299 1.9 christos return target_is_address_tagged (gdbarch, value_as_address (value));
1300 1.10 christos }
1301 1.10 christos
1302 1.10 christos /* Helper for parsing arguments for print_command_1. */
1303 1.10 christos
1304 1.10 christos static struct value *
1305 1.9 christos process_print_command_args (const char *args, value_print_options *print_opts,
1306 1.10 christos bool voidprint)
1307 1.9 christos {
1308 1.9 christos get_user_print_options (print_opts);
1309 1.9 christos /* Override global settings with explicit options, if any. */
1310 1.10 christos auto group = make_value_print_options_def_group (print_opts);
1311 1.1 christos gdb::option::process_options
1312 1.9 christos (&args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
1313 1.1 christos
1314 1.9 christos print_command_parse_format (&args, "print", print_opts);
1315 1.1 christos
1316 1.11 christos const char *exp = args;
1317 1.11 christos
1318 1.11 christos if (exp != nullptr && *exp)
1319 1.11 christos {
1320 1.11 christos /* This setting allows large arrays to be printed by limiting the
1321 1.10 christos number of elements that are loaded into GDB's memory; we only
1322 1.10 christos need to load as many array elements as we plan to print. */
1323 1.11 christos scoped_array_length_limiting limit_large_arrays (print_opts->print_max);
1324 1.11 christos
1325 1.11 christos /* VOIDPRINT is true to indicate that we do want to print a void
1326 1.11 christos value, so invert it for parse_expression. */
1327 1.11 christos parser_flags flags = 0;
1328 1.1 christos if (!voidprint)
1329 1.10 christos flags = PARSER_VOID_CONTEXT;
1330 1.10 christos expression_up expr = parse_expression (exp, nullptr, flags);
1331 1.10 christos return expr->evaluate ();
1332 1.10 christos }
1333 1.10 christos
1334 1.10 christos return access_value_history (0);
1335 1.10 christos }
1336 1.10 christos
1337 1.10 christos /* Implementation of the "print" and "call" commands. */
1338 1.10 christos
1339 1.10 christos static void
1340 1.10 christos print_command_1 (const char *args, int voidprint)
1341 1.1 christos {
1342 1.11 christos value_print_options print_opts;
1343 1.11 christos
1344 1.10 christos struct value *val = process_print_command_args (args, &print_opts, voidprint);
1345 1.10 christos
1346 1.10 christos if (voidprint || (val && val->type () &&
1347 1.10 christos val->type ()->code () != TYPE_CODE_VOID))
1348 1.10 christos {
1349 1.10 christos /* If memory tagging validation is on, check if the tag is valid. */
1350 1.11 christos if (print_opts.memory_tag_violations)
1351 1.11 christos {
1352 1.11 christos try
1353 1.11 christos {
1354 1.10 christos gdbarch *arch = current_inferior ()->arch ();
1355 1.10 christos
1356 1.10 christos if (should_validate_memtags (arch, val)
1357 1.11 christos && !gdbarch_memtag_matches_p (arch, val))
1358 1.11 christos {
1359 1.10 christos /* Fetch the logical tag. */
1360 1.10 christos struct value *tag
1361 1.11 christos = gdbarch_get_memtag (arch, val, memtag_type::logical);
1362 1.10 christos std::string ltag = gdbarch_memtag_to_string (arch, tag);
1363 1.11 christos
1364 1.10 christos /* Fetch the allocation tag. */
1365 1.10 christos tag = gdbarch_get_memtag (arch, val,
1366 1.10 christos memtag_type::allocation);
1367 1.10 christos std::string atag = gdbarch_memtag_to_string (arch, tag);
1368 1.10 christos
1369 1.10 christos gdb_printf (_("Logical tag (%s) does not match the "
1370 1.10 christos "allocation tag (%s).\n"),
1371 1.10 christos ltag.c_str (), atag.c_str ());
1372 1.10 christos }
1373 1.10 christos }
1374 1.10 christos catch (gdb_exception_error &ex)
1375 1.10 christos {
1376 1.10 christos if (ex.error == TARGET_CLOSE_ERROR)
1377 1.10 christos throw;
1378 1.10 christos
1379 1.10 christos gdb_printf (gdb_stderr,
1380 1.10 christos _("Could not validate memory tag: %s\n"),
1381 1.10 christos ex.message->c_str ());
1382 1.10 christos }
1383 1.10 christos }
1384 1.10 christos
1385 1.9 christos print_value (val, print_opts);
1386 1.9 christos }
1387 1.9 christos }
1388 1.9 christos
1389 1.9 christos /* See valprint.h. */
1390 1.9 christos
1391 1.9 christos void
1392 1.9 christos print_command_completer (struct cmd_list_element *ignore,
1393 1.9 christos completion_tracker &tracker,
1394 1.9 christos const char *text, const char * /*word*/)
1395 1.9 christos {
1396 1.9 christos const auto group = make_value_print_options_def_group (nullptr);
1397 1.10 christos if (gdb::option::complete_options
1398 1.10 christos (tracker, &text, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group))
1399 1.10 christos return;
1400 1.9 christos
1401 1.9 christos if (skip_over_slash_fmt (tracker, &text))
1402 1.1 christos return;
1403 1.1 christos
1404 1.1 christos const char *word = advance_to_expression_complete_word_point (tracker, text);
1405 1.8 christos expression_completer (ignore, tracker, text, word);
1406 1.1 christos }
1407 1.10 christos
1408 1.1 christos static void
1409 1.1 christos print_command (const char *exp, int from_tty)
1410 1.1 christos {
1411 1.1 christos print_command_1 (exp, true);
1412 1.8 christos }
1413 1.1 christos
1414 1.10 christos /* Same as print, except it doesn't print void results. */
1415 1.1 christos static void
1416 1.1 christos call_command (const char *exp, int from_tty)
1417 1.1 christos {
1418 1.1 christos print_command_1 (exp, false);
1419 1.1 christos }
1420 1.8 christos
1421 1.1 christos /* Implementation of the "output" command. */
1422 1.1 christos
1423 1.1 christos void
1424 1.1 christos output_command (const char *exp, int from_tty)
1425 1.1 christos {
1426 1.1 christos char format = 0;
1427 1.1 christos struct value *val;
1428 1.1 christos struct format_data fmt;
1429 1.1 christos struct value_print_options opts;
1430 1.1 christos
1431 1.1 christos fmt.size = 0;
1432 1.1 christos fmt.raw = 0;
1433 1.1 christos
1434 1.1 christos if (exp && *exp == '/')
1435 1.1 christos {
1436 1.1 christos exp++;
1437 1.1 christos fmt = decode_format (&exp, 0, 0);
1438 1.7 christos validate_format (fmt, "output");
1439 1.1 christos format = fmt.format;
1440 1.11 christos }
1441 1.1 christos
1442 1.11 christos expression_up expr = parse_expression (exp);
1443 1.1 christos
1444 1.1 christos val = expr->evaluate ();
1445 1.1 christos
1446 1.11 christos annotate_value_begin (val->type ());
1447 1.11 christos
1448 1.11 christos get_formatted_print_options (&opts, format);
1449 1.11 christos opts.raw = fmt.raw;
1450 1.11 christos
1451 1.11 christos /* This setting allows large arrays to be printed by limiting the
1452 1.1 christos number of elements that are loaded into GDB's memory; we only
1453 1.1 christos need to load as many array elements as we plan to print. */
1454 1.1 christos scoped_array_length_limiting limit_large_arrays (opts.print_max);
1455 1.1 christos
1456 1.1 christos print_formatted (val, fmt.size, &opts, gdb_stdout);
1457 1.1 christos
1458 1.1 christos annotate_value_end ();
1459 1.1 christos
1460 1.8 christos gdb_flush (gdb_stdout);
1461 1.1 christos }
1462 1.7 christos
1463 1.1 christos static void
1464 1.11 christos set_command (const char *exp, int from_tty)
1465 1.10 christos {
1466 1.10 christos expression_up expr = parse_expression (exp);
1467 1.10 christos
1468 1.10 christos switch (expr->first_opcode ())
1469 1.10 christos {
1470 1.10 christos case UNOP_PREINCREMENT:
1471 1.10 christos case UNOP_POSTINCREMENT:
1472 1.10 christos case UNOP_PREDECREMENT:
1473 1.10 christos case UNOP_POSTDECREMENT:
1474 1.10 christos case BINOP_ASSIGN:
1475 1.10 christos case BINOP_ASSIGN_MODIFY:
1476 1.10 christos case BINOP_COMMA:
1477 1.10 christos break;
1478 1.1 christos default:
1479 1.11 christos warning
1480 1.1 christos (_("Expression is not an assignment (and might have no effect)"));
1481 1.1 christos }
1482 1.1 christos
1483 1.8 christos expr->evaluate ();
1484 1.1 christos }
1485 1.1 christos
1486 1.1 christos static void
1487 1.1 christos info_symbol_command (const char *arg, int from_tty)
1488 1.1 christos {
1489 1.1 christos struct minimal_symbol *msymbol;
1490 1.1 christos CORE_ADDR addr, sect_addr;
1491 1.1 christos int matches = 0;
1492 1.1 christos unsigned int offset;
1493 1.1 christos
1494 1.8 christos if (!arg)
1495 1.11 christos error_no_arg (_("address"));
1496 1.1 christos
1497 1.8 christos addr = parse_and_eval_address (arg);
1498 1.8 christos for (objfile *objfile : current_program_space->objfiles ())
1499 1.8 christos for (obj_section *osect : objfile->sections ())
1500 1.8 christos {
1501 1.8 christos /* Only process each object file once, even if there's a separate
1502 1.8 christos debug file. */
1503 1.8 christos if (objfile->separate_debug_objfile_backlink)
1504 1.11 christos continue;
1505 1.8 christos
1506 1.8 christos sect_addr = overlay_mapped_address (addr, osect);
1507 1.8 christos
1508 1.8 christos if (osect->contains (sect_addr)
1509 1.8 christos && (msymbol
1510 1.8 christos = lookup_minimal_symbol_by_pc_section (sect_addr,
1511 1.1 christos osect).minsym))
1512 1.8 christos {
1513 1.10 christos const char *obj_name, *mapped, *sec_name, *msym_name;
1514 1.8 christos const char *loc_string;
1515 1.8 christos
1516 1.9 christos matches = 1;
1517 1.8 christos offset = sect_addr - msymbol->value_address (objfile);
1518 1.8 christos mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1519 1.8 christos sec_name = osect->the_bfd_section->name;
1520 1.8 christos msym_name = msymbol->print_name ();
1521 1.8 christos
1522 1.8 christos /* Don't print the offset if it is zero.
1523 1.8 christos We assume there's no need to handle i18n of "sym + offset". */
1524 1.8 christos std::string string_holder;
1525 1.8 christos if (offset)
1526 1.1 christos {
1527 1.8 christos string_holder = string_printf ("%s + %u", msym_name, offset);
1528 1.8 christos loc_string = string_holder.c_str ();
1529 1.8 christos }
1530 1.8 christos else
1531 1.8 christos loc_string = msym_name;
1532 1.9 christos
1533 1.8 christos gdb_assert (osect->objfile && objfile_name (osect->objfile));
1534 1.8 christos obj_name = objfile_name (osect->objfile);
1535 1.10 christos
1536 1.10 christos if (current_program_space->multi_objfile_p ())
1537 1.10 christos if (pc_in_unmapped_range (addr, osect))
1538 1.8 christos if (section_is_overlay (osect))
1539 1.10 christos gdb_printf (_("%s in load address range of "
1540 1.10 christos "%s overlay section %s of %s\n"),
1541 1.10 christos loc_string, mapped, sec_name, obj_name);
1542 1.8 christos else
1543 1.8 christos gdb_printf (_("%s in load address range of "
1544 1.10 christos "section %s of %s\n"),
1545 1.10 christos loc_string, sec_name, obj_name);
1546 1.8 christos else
1547 1.10 christos if (section_is_overlay (osect))
1548 1.10 christos gdb_printf (_("%s in %s overlay section %s of %s\n"),
1549 1.1 christos loc_string, mapped, sec_name, obj_name);
1550 1.8 christos else
1551 1.8 christos gdb_printf (_("%s in section %s of %s\n"),
1552 1.10 christos loc_string, sec_name, obj_name);
1553 1.10 christos else
1554 1.10 christos if (pc_in_unmapped_range (addr, osect))
1555 1.8 christos if (section_is_overlay (osect))
1556 1.10 christos gdb_printf (_("%s in load address range of %s overlay "
1557 1.8 christos "section %s\n"),
1558 1.8 christos loc_string, mapped, sec_name);
1559 1.8 christos else
1560 1.8 christos gdb_printf
1561 1.10 christos (_("%s in load address range of section %s\n"),
1562 1.10 christos loc_string, sec_name);
1563 1.8 christos else
1564 1.10 christos if (section_is_overlay (osect))
1565 1.10 christos gdb_printf (_("%s in %s overlay section %s\n"),
1566 1.8 christos loc_string, mapped, sec_name);
1567 1.1 christos else
1568 1.1 christos gdb_printf (_("%s in section %s\n"),
1569 1.10 christos loc_string, sec_name);
1570 1.1 christos }
1571 1.1 christos }
1572 1.1 christos if (matches == 0)
1573 1.8 christos gdb_printf (_("No symbol matches %s.\n"), arg);
1574 1.1 christos }
1575 1.1 christos
1576 1.1 christos static void
1577 1.1 christos info_address_command (const char *exp, int from_tty)
1578 1.1 christos {
1579 1.1 christos struct gdbarch *gdbarch;
1580 1.1 christos int regno;
1581 1.1 christos struct symbol *sym;
1582 1.1 christos long val;
1583 1.1 christos struct obj_section *section;
1584 1.1 christos CORE_ADDR load_addr, context_pc = 0;
1585 1.1 christos struct field_of_this_result is_a_field_of_this;
1586 1.11 christos
1587 1.6 christos if (exp == 0)
1588 1.1 christos error (_("Argument required."));
1589 1.1 christos
1590 1.1 christos sym = lookup_symbol (exp, get_selected_block (&context_pc), SEARCH_VFT,
1591 1.1 christos &is_a_field_of_this).symbol;
1592 1.10 christos if (sym == NULL)
1593 1.10 christos {
1594 1.10 christos if (is_a_field_of_this.type != NULL)
1595 1.10 christos {
1596 1.1 christos gdb_printf ("Symbol \"");
1597 1.10 christos fprintf_symbol (gdb_stdout, exp,
1598 1.1 christos current_language->la_language, DMGL_ANSI);
1599 1.10 christos gdb_printf ("\" is a field of the local class variable ");
1600 1.1 christos if (current_language->la_language == language_objc)
1601 1.1 christos gdb_printf ("`self'\n"); /* ObjC equivalent of "this" */
1602 1.1 christos else
1603 1.12 christos gdb_printf ("`this'\n");
1604 1.12 christos return;
1605 1.1 christos }
1606 1.1 christos
1607 1.1 christos bound_minimal_symbol msymbol
1608 1.1 christos = lookup_minimal_symbol (current_program_space, exp);
1609 1.1 christos
1610 1.9 christos if (msymbol.minsym != NULL)
1611 1.10 christos {
1612 1.1 christos struct objfile *objfile = msymbol.objfile;
1613 1.10 christos
1614 1.10 christos gdbarch = objfile->arch ();
1615 1.10 christos load_addr = msymbol.value_address ();
1616 1.10 christos
1617 1.8 christos gdb_printf ("Symbol \"");
1618 1.8 christos fprintf_symbol (gdb_stdout, exp,
1619 1.10 christos current_language->la_language, DMGL_ANSI);
1620 1.10 christos gdb_printf ("\" is at ");
1621 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1622 1.1 christos gdb_stdout);
1623 1.1 christos gdb_printf (" in a file compiled without debugging");
1624 1.10 christos section = msymbol.minsym->obj_section (objfile);
1625 1.8 christos if (section_is_overlay (section))
1626 1.8 christos {
1627 1.8 christos load_addr = overlay_unmapped_address (load_addr, section);
1628 1.10 christos gdb_printf (",\n -- loaded at ");
1629 1.10 christos fputs_styled (paddress (gdbarch, load_addr),
1630 1.1 christos address_style.style (),
1631 1.10 christos gdb_stdout);
1632 1.1 christos gdb_printf (" in overlay section %s",
1633 1.1 christos section->the_bfd_section->name);
1634 1.1 christos }
1635 1.1 christos gdb_printf (".\n");
1636 1.1 christos }
1637 1.1 christos else
1638 1.10 christos error (_("No symbol \"%s\" in current context."), exp);
1639 1.10 christos return;
1640 1.10 christos }
1641 1.10 christos
1642 1.10 christos gdb_printf ("Symbol \"");
1643 1.10 christos gdb_puts (sym->print_name ());
1644 1.3 christos gdb_printf ("\" is ");
1645 1.3 christos val = sym->value_longest ();
1646 1.10 christos if (sym->is_objfile_owned ())
1647 1.1 christos section = sym->obj_section (sym->objfile ());
1648 1.11 christos else
1649 1.11 christos section = NULL;
1650 1.1 christos gdbarch = sym->arch ();
1651 1.11 christos
1652 1.10 christos if (const symbol_computed_ops *computed_ops = sym->computed_ops ();
1653 1.1 christos computed_ops != nullptr)
1654 1.1 christos {
1655 1.1 christos computed_ops->describe_location (sym, context_pc, gdb_stdout);
1656 1.10 christos gdb_printf (".\n");
1657 1.1 christos return;
1658 1.1 christos }
1659 1.1 christos
1660 1.10 christos switch (sym->aclass ())
1661 1.1 christos {
1662 1.1 christos case LOC_CONST:
1663 1.1 christos case LOC_CONST_BYTES:
1664 1.10 christos gdb_printf ("constant");
1665 1.10 christos break;
1666 1.8 christos
1667 1.8 christos case LOC_LABEL:
1668 1.1 christos gdb_printf ("a label at address ");
1669 1.1 christos load_addr = sym->value_address ();
1670 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1671 1.10 christos gdb_stdout);
1672 1.8 christos if (section_is_overlay (section))
1673 1.8 christos {
1674 1.10 christos load_addr = overlay_unmapped_address (load_addr, section);
1675 1.10 christos gdb_printf (",\n -- loaded at ");
1676 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1677 1.1 christos gdb_stdout);
1678 1.1 christos gdb_printf (" in overlay section %s",
1679 1.1 christos section->the_bfd_section->name);
1680 1.10 christos }
1681 1.1 christos break;
1682 1.1 christos
1683 1.1 christos case LOC_COMPUTED:
1684 1.1 christos gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
1685 1.1 christos
1686 1.1 christos case LOC_REGISTER:
1687 1.1 christos /* GDBARCH is the architecture associated with the objfile the symbol
1688 1.1 christos is defined in; the target architecture may be different, and may
1689 1.11 christos provide additional registers. However, we do not know the target
1690 1.1 christos architecture at this point. We assume the objfile architecture
1691 1.10 christos will contain all the standard registers that occur in debug info
1692 1.10 christos in that objfile. */
1693 1.10 christos regno = sym->register_ops ()->register_number (sym, gdbarch);
1694 1.1 christos
1695 1.10 christos if (sym->is_argument ())
1696 1.10 christos gdb_printf (_("an argument in register %s"),
1697 1.1 christos gdbarch_register_name (gdbarch, regno));
1698 1.1 christos else
1699 1.1 christos gdb_printf (_("a variable in register %s"),
1700 1.10 christos gdbarch_register_name (gdbarch, regno));
1701 1.10 christos break;
1702 1.8 christos
1703 1.8 christos case LOC_STATIC:
1704 1.1 christos gdb_printf (_("static storage at address "));
1705 1.1 christos load_addr = sym->value_address ();
1706 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1707 1.10 christos gdb_stdout);
1708 1.8 christos if (section_is_overlay (section))
1709 1.8 christos {
1710 1.10 christos load_addr = overlay_unmapped_address (load_addr, section);
1711 1.10 christos gdb_printf (_(",\n -- loaded at "));
1712 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1713 1.1 christos gdb_stdout);
1714 1.1 christos gdb_printf (_(" in overlay section %s"),
1715 1.1 christos section->the_bfd_section->name);
1716 1.1 christos }
1717 1.11 christos break;
1718 1.10 christos
1719 1.10 christos case LOC_REGPARM_ADDR:
1720 1.1 christos /* Note comment at LOC_REGISTER. */
1721 1.1 christos regno = sym->register_ops ()->register_number (sym, gdbarch);
1722 1.1 christos gdb_printf (_("address of an argument in register %s"),
1723 1.10 christos gdbarch_register_name (gdbarch, regno));
1724 1.1 christos break;
1725 1.1 christos
1726 1.1 christos case LOC_ARG:
1727 1.10 christos gdb_printf (_("an argument at offset %ld"), val);
1728 1.1 christos break;
1729 1.1 christos
1730 1.1 christos case LOC_LOCAL:
1731 1.10 christos gdb_printf (_("a local variable at frame offset %ld"), val);
1732 1.1 christos break;
1733 1.1 christos
1734 1.1 christos case LOC_REF_ARG:
1735 1.10 christos gdb_printf (_("a reference argument at offset %ld"), val);
1736 1.1 christos break;
1737 1.1 christos
1738 1.1 christos case LOC_TYPEDEF:
1739 1.10 christos gdb_printf (_("a typedef"));
1740 1.10 christos break;
1741 1.8 christos
1742 1.8 christos case LOC_BLOCK:
1743 1.1 christos gdb_printf (_("a function at address "));
1744 1.1 christos load_addr = sym->value_block ()->entry_pc ();
1745 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1746 1.10 christos gdb_stdout);
1747 1.8 christos if (section_is_overlay (section))
1748 1.8 christos {
1749 1.10 christos load_addr = overlay_unmapped_address (load_addr, section);
1750 1.10 christos gdb_printf (_(",\n -- loaded at "));
1751 1.1 christos fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1752 1.1 christos gdb_stdout);
1753 1.1 christos gdb_printf (_(" in overlay section %s"),
1754 1.1 christos section->the_bfd_section->name);
1755 1.1 christos }
1756 1.12 christos break;
1757 1.12 christos
1758 1.12 christos case LOC_UNRESOLVED:
1759 1.1 christos {
1760 1.10 christos bound_minimal_symbol msym
1761 1.1 christos = lookup_minimal_symbol (current_program_space,
1762 1.1 christos sym->linkage_name ());
1763 1.10 christos if (msym.minsym == NULL)
1764 1.1 christos gdb_printf ("unresolved");
1765 1.1 christos else
1766 1.1 christos {
1767 1.6 christos section = msym.obj_section ();
1768 1.11 christos
1769 1.10 christos if (section
1770 1.10 christos && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
1771 1.10 christos {
1772 1.10 christos load_addr = CORE_ADDR (msym.minsym->unrelocated_address ());
1773 1.6 christos gdb_printf (_("a thread-local variable at offset %s "
1774 1.1 christos "in the thread-local storage for `%s'"),
1775 1.1 christos paddress (gdbarch, load_addr),
1776 1.10 christos objfile_name (section->objfile));
1777 1.10 christos }
1778 1.8 christos else
1779 1.8 christos {
1780 1.1 christos load_addr = msym.value_address ();
1781 1.1 christos gdb_printf (_("static storage at address "));
1782 1.1 christos fputs_styled (paddress (gdbarch, load_addr),
1783 1.10 christos address_style.style (), gdb_stdout);
1784 1.8 christos if (section_is_overlay (section))
1785 1.8 christos {
1786 1.8 christos load_addr = overlay_unmapped_address (load_addr, section);
1787 1.10 christos gdb_printf (_(",\n -- loaded at "));
1788 1.10 christos fputs_styled (paddress (gdbarch, load_addr),
1789 1.1 christos address_style.style (),
1790 1.1 christos gdb_stdout);
1791 1.1 christos gdb_printf (_(" in overlay section %s"),
1792 1.1 christos section->the_bfd_section->name);
1793 1.1 christos }
1794 1.1 christos }
1795 1.1 christos }
1796 1.10 christos }
1797 1.1 christos break;
1798 1.1 christos
1799 1.1 christos case LOC_OPTIMIZED_OUT:
1800 1.10 christos gdb_printf (_("optimized out"));
1801 1.1 christos break;
1802 1.1 christos
1803 1.10 christos default:
1804 1.1 christos gdb_printf (_("of unknown (botched) type"));
1805 1.1 christos break;
1806 1.1 christos }
1807 1.1 christos gdb_printf (".\n");
1808 1.8 christos }
1809 1.1 christos
1810 1.1 christos
1812 1.1 christos static void
1813 1.1 christos x_command (const char *exp, int from_tty)
1814 1.10 christos {
1815 1.1 christos struct format_data fmt;
1816 1.1 christos struct value *val;
1817 1.1 christos
1818 1.1 christos fmt.format = last_format ? last_format : 'x';
1819 1.8 christos fmt.print_tags = last_print_tags;
1820 1.8 christos fmt.size = last_size;
1821 1.8 christos fmt.count = 1;
1822 1.8 christos fmt.raw = 0;
1823 1.8 christos
1824 1.1 christos /* If there is no expression and no format, use the most recent
1825 1.1 christos count. */
1826 1.1 christos if (exp == nullptr && last_count > 0)
1827 1.1 christos fmt.count = last_count;
1828 1.1 christos
1829 1.1 christos if (exp && *exp == '/')
1830 1.1 christos {
1831 1.1 christos const char *tmp = exp + 1;
1832 1.8 christos
1833 1.8 christos fmt = decode_format (&tmp, last_format, last_size);
1834 1.1 christos exp = (char *) tmp;
1835 1.1 christos }
1836 1.1 christos
1837 1.1 christos last_count = fmt.count;
1838 1.7 christos
1839 1.1 christos /* If we have an expression, evaluate it and use it as the address. */
1840 1.10 christos
1841 1.10 christos if (exp != 0 && *exp != 0)
1842 1.1 christos {
1843 1.8 christos expression_up expr = parse_expression (exp);
1844 1.11 christos /* Cause expression not to be there any more if this command is
1845 1.11 christos repeated with Newline. But don't clobber a user-defined
1846 1.1 christos command's definition. */
1847 1.1 christos if (from_tty)
1848 1.10 christos set_repeat_arguments ("");
1849 1.11 christos val = expr->evaluate ();
1850 1.11 christos if (TYPE_IS_REFERENCE (val->type ()))
1851 1.11 christos val = coerce_ref (val);
1852 1.1 christos /* In rvalue contexts, such as this, functions are coerced into
1853 1.1 christos pointers to functions. This makes "x/i main" work. */
1854 1.1 christos if (val->type ()->code () == TYPE_CODE_FUNC
1855 1.1 christos && val->lval () == lval_memory)
1856 1.1 christos next_address = val->address ();
1857 1.1 christos else
1858 1.1 christos next_address = value_as_address (val);
1859 1.1 christos
1860 1.1 christos next_gdbarch = expr->gdbarch;
1861 1.1 christos }
1862 1.1 christos
1863 1.1 christos if (!next_gdbarch)
1864 1.1 christos error_no_arg (_("starting display address"));
1865 1.1 christos
1866 1.1 christos do_examine (fmt, next_gdbarch, next_address);
1867 1.1 christos
1868 1.1 christos /* If the examine succeeds, we remember its size and format for next
1869 1.1 christos time. Set last_size to 'b' for strings. */
1870 1.1 christos if (fmt.format == 's')
1871 1.10 christos last_size = 'b';
1872 1.10 christos else
1873 1.10 christos last_size = fmt.size;
1874 1.1 christos last_format = fmt.format;
1875 1.8 christos
1876 1.1 christos /* Remember tag-printing setting. */
1877 1.1 christos last_print_tags = fmt.print_tags;
1878 1.10 christos
1879 1.1 christos /* Set a couple of internal variables if appropriate. */
1880 1.11 christos if (last_examine_value != nullptr)
1881 1.1 christos {
1882 1.1 christos /* Make last address examined available to the user as $_. Use
1883 1.1 christos the correct pointer type. */
1884 1.1 christos struct type *pointer_type
1885 1.1 christos = lookup_pointer_type (last_examine_value->type ());
1886 1.1 christos set_internalvar (lookup_internalvar ("_"),
1887 1.1 christos value_from_pointer (pointer_type,
1888 1.1 christos last_examine_address));
1889 1.11 christos
1890 1.1 christos /* Make contents of last address examined available to the user
1891 1.1 christos as $__. If the last value has not been fetched from memory
1892 1.8 christos then don't fetch it now; instead mark it by voiding the $__
1893 1.1 christos variable. */
1894 1.1 christos if (last_examine_value->lazy ())
1895 1.10 christos clear_internalvar (lookup_internalvar ("__"));
1896 1.10 christos else
1897 1.10 christos set_internalvar (lookup_internalvar ("__"), last_examine_value.get ());
1898 1.10 christos }
1899 1.10 christos }
1900 1.10 christos
1901 1.10 christos /* Command completion for the 'display' and 'x' commands. */
1902 1.10 christos
1903 1.10 christos static void
1904 1.10 christos display_and_x_command_completer (struct cmd_list_element *ignore,
1905 1.10 christos completion_tracker &tracker,
1906 1.10 christos const char *text, const char * /*word*/)
1907 1.10 christos {
1908 1.10 christos if (skip_over_slash_fmt (tracker, &text))
1909 1.10 christos return;
1910 1.1 christos
1911 1.1 christos const char *word = advance_to_expression_complete_word_point (tracker, text);
1912 1.1 christos expression_completer (ignore, tracker, text, word);
1913 1.1 christos }
1914 1.1 christos
1915 1.1 christos
1916 1.8 christos
1918 1.1 christos /* Add an expression to the auto-display chain.
1919 1.5 christos Specify the expression. */
1920 1.1 christos
1921 1.1 christos static void
1922 1.5 christos display_command (const char *arg, int from_tty)
1923 1.5 christos {
1924 1.5 christos struct format_data fmt;
1925 1.5 christos struct display *newobj;
1926 1.5 christos const char *exp = arg;
1927 1.1 christos
1928 1.5 christos if (exp == 0)
1929 1.1 christos {
1930 1.5 christos do_displays ();
1931 1.5 christos return;
1932 1.5 christos }
1933 1.5 christos
1934 1.5 christos if (*exp == '/')
1935 1.5 christos {
1936 1.5 christos exp++;
1937 1.5 christos fmt = decode_format (&exp, 0, 0);
1938 1.5 christos if (fmt.size && fmt.format == 0)
1939 1.5 christos fmt.format = 'x';
1940 1.5 christos if (fmt.format == 'i' || fmt.format == 's')
1941 1.5 christos fmt.size = 'b';
1942 1.5 christos }
1943 1.5 christos else
1944 1.1 christos {
1945 1.9 christos fmt.format = 0;
1946 1.9 christos fmt.size = 0;
1947 1.6 christos fmt.count = 0;
1948 1.9 christos fmt.raw = 0;
1949 1.9 christos }
1950 1.9 christos
1951 1.1 christos innermost_block_tracker tracker;
1952 1.5 christos expression_up expr = parse_expression (exp, &tracker);
1953 1.5 christos
1954 1.1 christos newobj = new display (exp, std::move (expr), fmt,
1955 1.5 christos current_program_space, tracker.block ());
1956 1.1 christos all_displays.emplace_back (newobj);
1957 1.1 christos
1958 1.1 christos if (from_tty)
1959 1.1 christos do_one_display (newobj);
1960 1.1 christos
1961 1.1 christos dont_repeat ();
1962 1.9 christos }
1963 1.1 christos
1964 1.9 christos /* Clear out the display_chain. Done when new symtabs are loaded,
1965 1.1 christos since this invalidates the types stored in many expressions. */
1966 1.1 christos
1967 1.1 christos void
1968 1.1 christos clear_displays ()
1969 1.1 christos {
1970 1.1 christos all_displays.clear ();
1971 1.1 christos }
1972 1.1 christos
1973 1.1 christos /* Delete the auto-display DISPLAY. */
1974 1.9 christos
1975 1.9 christos static void
1976 1.9 christos delete_display (struct display *display)
1977 1.9 christos {
1978 1.9 christos gdb_assert (display != NULL);
1979 1.9 christos
1980 1.9 christos auto iter = std::find_if (all_displays.begin (),
1981 1.9 christos all_displays.end (),
1982 1.1 christos [=] (const std::unique_ptr<struct display> &item)
1983 1.1 christos {
1984 1.1 christos return item.get () == display;
1985 1.1 christos });
1986 1.1 christos gdb_assert (iter != all_displays.end ());
1987 1.1 christos all_displays.erase (iter);
1988 1.8 christos }
1989 1.9 christos
1990 1.1 christos /* Call FUNCTION on each of the displays whose numbers are given in
1991 1.1 christos ARGS. DATA is passed unmodified to FUNCTION. */
1992 1.1 christos
1993 1.1 christos static void
1994 1.1 christos map_display_numbers (const char *args,
1995 1.1 christos gdb::function_view<void (struct display *)> function)
1996 1.7 christos {
1997 1.1 christos int num;
1998 1.7 christos
1999 1.1 christos if (args == NULL)
2000 1.7 christos error_no_arg (_("one or more display numbers"));
2001 1.1 christos
2002 1.7 christos number_or_range_parser parser (args);
2003 1.1 christos
2004 1.1 christos while (!parser.finished ())
2005 1.1 christos {
2006 1.1 christos const char *p = parser.cur_tok ();
2007 1.9 christos
2008 1.9 christos num = parser.get_number ();
2009 1.9 christos if (num == 0)
2010 1.9 christos warning (_("bad display number at or near '%s'"), p);
2011 1.9 christos else
2012 1.9 christos {
2013 1.9 christos auto iter = std::find_if (all_displays.begin (),
2014 1.10 christos all_displays.end (),
2015 1.1 christos [=] (const std::unique_ptr<display> &item)
2016 1.9 christos {
2017 1.1 christos return item->number == num;
2018 1.1 christos });
2019 1.1 christos if (iter == all_displays.end ())
2020 1.1 christos gdb_printf (_("No display number %d.\n"), num);
2021 1.1 christos else
2022 1.1 christos function (iter->get ());
2023 1.1 christos }
2024 1.8 christos }
2025 1.1 christos }
2026 1.1 christos
2027 1.1 christos /* "undisplay" command. */
2028 1.1 christos
2029 1.1 christos static void
2030 1.1 christos undisplay_command (const char *args, int from_tty)
2031 1.1 christos {
2032 1.1 christos if (args == NULL)
2033 1.1 christos {
2034 1.9 christos if (query (_("Delete all auto-display expressions? ")))
2035 1.1 christos clear_displays ();
2036 1.1 christos dont_repeat ();
2037 1.1 christos return;
2038 1.1 christos }
2039 1.1 christos
2040 1.1 christos map_display_numbers (args, delete_display);
2041 1.1 christos dont_repeat ();
2042 1.1 christos }
2043 1.1 christos
2044 1.1 christos /* Display a single auto-display.
2045 1.1 christos Do nothing if the display cannot be printed in the current context,
2046 1.1 christos or if the display is disabled. */
2047 1.9 christos
2048 1.1 christos static void
2049 1.1 christos do_one_display (struct display *d)
2050 1.1 christos {
2051 1.1 christos int within_current_scope;
2052 1.1 christos
2053 1.1 christos if (!d->enabled_p)
2054 1.1 christos return;
2055 1.1 christos
2056 1.1 christos /* The expression carries the architecture that was used at parse time.
2057 1.1 christos This is a problem if the expression depends on architecture features
2058 1.1 christos (e.g. register numbers), and the current architecture is now different.
2059 1.7 christos For example, a display statement like "display/i $pc" is expected to
2060 1.1 christos display the PC register of the current architecture, not the arch at
2061 1.1 christos the time the display command was given. Therefore, we re-parse the
2062 1.1 christos expression if the current architecture has changed. */
2063 1.1 christos if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
2064 1.1 christos {
2065 1.1 christos d->exp.reset ();
2066 1.9 christos d->block = NULL;
2067 1.1 christos }
2068 1.9 christos
2069 1.9 christos if (d->exp == NULL)
2070 1.9 christos {
2071 1.1 christos
2072 1.11 christos try
2073 1.1 christos {
2074 1.1 christos innermost_block_tracker tracker;
2075 1.9 christos d->exp = parse_expression (d->exp_string.c_str (), &tracker);
2076 1.1 christos d->block = tracker.block ();
2077 1.9 christos }
2078 1.1 christos catch (const gdb_exception_error &ex)
2079 1.1 christos {
2080 1.1 christos /* Can't re-parse the expression. Disable this display item. */
2081 1.1 christos d->enabled_p = false;
2082 1.1 christos warning (_("Unable to display \"%s\": %s"),
2083 1.1 christos d->exp_string.c_str (), ex.what ());
2084 1.1 christos return;
2085 1.11 christos }
2086 1.11 christos }
2087 1.1 christos
2088 1.1 christos if (d->block)
2089 1.1 christos {
2090 1.1 christos if (d->pspace == current_program_space)
2091 1.1 christos within_current_scope = d->block->contains (get_selected_block (0),
2092 1.1 christos true);
2093 1.1 christos else
2094 1.1 christos within_current_scope = 0;
2095 1.7 christos }
2096 1.7 christos else
2097 1.1 christos within_current_scope = 1;
2098 1.1 christos if (!within_current_scope)
2099 1.10 christos return;
2100 1.1 christos
2101 1.10 christos scoped_restore save_display_number
2102 1.1 christos = make_scoped_restore (¤t_display_number, d->number);
2103 1.1 christos
2104 1.1 christos annotate_display_begin ();
2105 1.1 christos gdb_printf ("%d", d->number);
2106 1.1 christos annotate_display_number_end ();
2107 1.10 christos gdb_printf (": ");
2108 1.1 christos if (d->format.size)
2109 1.10 christos {
2110 1.10 christos
2111 1.1 christos annotate_display_format ();
2112 1.10 christos
2113 1.10 christos gdb_printf ("x/");
2114 1.1 christos if (d->format.count != 1)
2115 1.1 christos gdb_printf ("%d", d->format.count);
2116 1.1 christos gdb_printf ("%c", d->format.format);
2117 1.10 christos if (d->format.format != 'i' && d->format.format != 's')
2118 1.1 christos gdb_printf ("%c", d->format.size);
2119 1.1 christos gdb_printf (" ");
2120 1.1 christos
2121 1.10 christos annotate_display_expression ();
2122 1.1 christos
2123 1.10 christos gdb_puts (d->exp_string.c_str ());
2124 1.1 christos annotate_display_expression_end ();
2125 1.1 christos
2126 1.1 christos if (d->format.count != 1 || d->format.format == 'i')
2127 1.9 christos gdb_printf ("\n");
2128 1.10 christos else
2129 1.1 christos gdb_printf (" ");
2130 1.1 christos
2131 1.1 christos annotate_display_value ();
2132 1.11 christos
2133 1.1 christos try
2134 1.1 christos {
2135 1.1 christos struct value *val;
2136 1.1 christos CORE_ADDR addr;
2137 1.1 christos
2138 1.9 christos val = d->exp->evaluate ();
2139 1.5 christos addr = value_as_address (val);
2140 1.10 christos if (d->format.format == 'i')
2141 1.10 christos addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
2142 1.10 christos do_examine (d->format, d->exp->gdbarch, addr);
2143 1.5 christos }
2144 1.1 christos catch (const gdb_exception_error &ex)
2145 1.1 christos {
2146 1.1 christos gdb_printf (_("%p[<error: %s>%p]\n"),
2147 1.1 christos metadata_style.style ().ptr (), ex.what (),
2148 1.1 christos nullptr);
2149 1.1 christos }
2150 1.1 christos }
2151 1.1 christos else
2152 1.10 christos {
2153 1.1 christos struct value_print_options opts;
2154 1.1 christos
2155 1.1 christos annotate_display_format ();
2156 1.10 christos
2157 1.1 christos if (d->format.format)
2158 1.1 christos gdb_printf ("/%c ", d->format.format);
2159 1.10 christos
2160 1.1 christos annotate_display_expression ();
2161 1.1 christos
2162 1.1 christos gdb_puts (d->exp_string.c_str ());
2163 1.1 christos annotate_display_expression_end ();
2164 1.1 christos
2165 1.1 christos gdb_printf (" = ");
2166 1.9 christos
2167 1.10 christos annotate_display_expression ();
2168 1.1 christos
2169 1.1 christos get_formatted_print_options (&opts, d->format.format);
2170 1.11 christos opts.raw = d->format.raw;
2171 1.1 christos
2172 1.1 christos try
2173 1.9 christos {
2174 1.5 christos struct value *val;
2175 1.9 christos
2176 1.9 christos val = d->exp->evaluate ();
2177 1.5 christos print_formatted (val, d->format.size, &opts, gdb_stdout);
2178 1.5 christos }
2179 1.10 christos catch (const gdb_exception_error &ex)
2180 1.1 christos {
2181 1.1 christos fprintf_styled (gdb_stdout, metadata_style.style (),
2182 1.1 christos _("<error: %s>"), ex.what ());
2183 1.1 christos }
2184 1.1 christos
2185 1.1 christos gdb_printf ("\n");
2186 1.1 christos }
2187 1.1 christos
2188 1.1 christos annotate_display_end ();
2189 1.1 christos
2190 1.1 christos gdb_flush (gdb_stdout);
2191 1.1 christos }
2192 1.1 christos
2193 1.9 christos /* Display all of the values on the auto-display chain which can be
2194 1.9 christos evaluated in the current scope. */
2195 1.1 christos
2196 1.1 christos void
2197 1.1 christos do_displays (void)
2198 1.1 christos {
2199 1.1 christos for (auto &d : all_displays)
2200 1.1 christos do_one_display (d.get ());
2201 1.1 christos }
2202 1.1 christos
2203 1.9 christos /* Delete the auto-display which we were in the process of displaying.
2204 1.1 christos This is done when there is an error or a signal. */
2205 1.1 christos
2206 1.9 christos void
2207 1.1 christos disable_display (int num)
2208 1.1 christos {
2209 1.10 christos for (auto &d : all_displays)
2210 1.1 christos if (d->number == num)
2211 1.1 christos {
2212 1.1 christos d->enabled_p = false;
2213 1.1 christos return;
2214 1.1 christos }
2215 1.1 christos gdb_printf (_("No display number %d.\n"), num);
2216 1.1 christos }
2217 1.1 christos
2218 1.10 christos void
2219 1.10 christos disable_current_display (void)
2220 1.10 christos {
2221 1.10 christos if (current_display_number >= 0)
2222 1.1 christos {
2223 1.1 christos disable_display (current_display_number);
2224 1.1 christos gdb_printf (gdb_stderr,
2225 1.1 christos _("Disabling display %d to "
2226 1.1 christos "avoid infinite recursion.\n"),
2227 1.8 christos current_display_number);
2228 1.1 christos }
2229 1.9 christos current_display_number = -1;
2230 1.10 christos }
2231 1.1 christos
2232 1.10 christos static void
2233 1.1 christos info_display_command (const char *ignore, int from_tty)
2234 1.1 christos {
2235 1.9 christos if (all_displays.empty ())
2236 1.1 christos gdb_printf (_("There are no auto-display expressions now.\n"));
2237 1.10 christos else
2238 1.1 christos gdb_printf (_("Auto-display expressions now in effect:\n\
2239 1.10 christos Num Enb Expression\n"));
2240 1.10 christos
2241 1.1 christos for (auto &d : all_displays)
2242 1.10 christos {
2243 1.10 christos gdb_printf ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
2244 1.11 christos if (d->format.size)
2245 1.10 christos gdb_printf ("/%d%c%c ", d->format.count, d->format.size,
2246 1.10 christos d->format.format);
2247 1.1 christos else if (d->format.format)
2248 1.1 christos gdb_printf ("/%c ", d->format.format);
2249 1.1 christos gdb_puts (d->exp_string.c_str ());
2250 1.9 christos if (d->block && !d->block->contains (get_selected_block (0), true))
2251 1.1 christos gdb_printf (_(" (cannot be evaluated in the current context)"));
2252 1.1 christos gdb_printf ("\n");
2253 1.1 christos }
2254 1.9 christos }
2255 1.1 christos
2256 1.1 christos /* Implementation of both the "disable display" and "enable display"
2257 1.1 christos commands. ENABLE decides what to do. */
2258 1.9 christos
2259 1.1 christos static void
2260 1.1 christos enable_disable_display_command (const char *args, int from_tty, bool enable)
2261 1.1 christos {
2262 1.1 christos if (args == NULL)
2263 1.9 christos {
2264 1.9 christos for (auto &d : all_displays)
2265 1.9 christos d->enabled_p = enable;
2266 1.9 christos return;
2267 1.9 christos }
2268 1.1 christos
2269 1.1 christos map_display_numbers (args,
2270 1.1 christos [=] (struct display *d)
2271 1.1 christos {
2272 1.1 christos d->enabled_p = enable;
2273 1.8 christos });
2274 1.1 christos }
2275 1.9 christos
2276 1.1 christos /* The "enable display" command. */
2277 1.1 christos
2278 1.1 christos static void
2279 1.1 christos enable_display_command (const char *args, int from_tty)
2280 1.1 christos {
2281 1.8 christos enable_disable_display_command (args, from_tty, true);
2282 1.1 christos }
2283 1.9 christos
2284 1.1 christos /* The "disable display" command. */
2285 1.1 christos
2286 1.1 christos static void
2287 1.1 christos disable_display_command (const char *args, int from_tty)
2288 1.1 christos {
2289 1.1 christos enable_disable_display_command (args, from_tty, false);
2290 1.1 christos }
2291 1.1 christos
2292 1.1 christos /* display_chain items point to blocks and expressions. Some expressions in
2293 1.1 christos turn may point to symbols.
2294 1.1 christos Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2295 1.1 christos obstack_free'd when a shared library is unloaded.
2296 1.1 christos Clear pointers that are about to become dangling.
2297 1.12 christos Both .exp and .block fields will be restored next time we need to display
2298 1.1 christos an item by re-parsing .exp_string field in the new execution context. */
2299 1.1 christos
2300 1.1 christos static void
2301 1.12 christos clear_dangling_display_expressions (struct objfile *objfile)
2302 1.1 christos {
2303 1.1 christos program_space *pspace = objfile->pspace ();
2304 1.9 christos if (objfile->separate_debug_objfile_backlink)
2305 1.1 christos {
2306 1.1 christos objfile = objfile->separate_debug_objfile_backlink;
2307 1.1 christos gdb_assert (objfile->pspace () == pspace);
2308 1.1 christos }
2309 1.9 christos
2310 1.9 christos for (auto &d : all_displays)
2311 1.9 christos {
2312 1.11 christos if (d->pspace != pspace)
2313 1.9 christos continue;
2314 1.9 christos
2315 1.9 christos struct objfile *bl_objf = nullptr;
2316 1.9 christos if (d->block != nullptr)
2317 1.9 christos {
2318 1.11 christos bl_objf = d->block->objfile ();
2319 1.9 christos if (bl_objf->separate_debug_objfile_backlink != nullptr)
2320 1.9 christos bl_objf = bl_objf->separate_debug_objfile_backlink;
2321 1.9 christos }
2322 1.9 christos
2323 1.1 christos if (bl_objf == objfile
2324 1.1 christos || (d->exp != nullptr && d->exp->uses_objfile (objfile)))
2325 1.1 christos {
2326 1.1 christos d->exp.reset ();
2327 1.1 christos d->block = NULL;
2328 1.1 christos }
2329 1.1 christos }
2330 1.1 christos }
2331 1.11 christos
2332 1.1 christos
2334 1.1 christos /* Print the value in stack frame FRAME of a variable specified by a
2335 1.11 christos struct symbol. NAME is the name to print; if NULL then VAR's print
2336 1.1 christos name will be used. STREAM is the ui_file on which to print the
2337 1.1 christos value. INDENT specifies the number of indent levels to print
2338 1.1 christos before printing the variable name. */
2339 1.1 christos
2340 1.9 christos void
2341 1.1 christos print_variable_and_value (const char *name, struct symbol *var,
2342 1.10 christos const frame_info_ptr &frame,
2343 1.10 christos struct ui_file *stream, int indent)
2344 1.8 christos {
2345 1.9 christos
2346 1.1 christos if (!name)
2347 1.1 christos name = var->print_name ();
2348 1.1 christos
2349 1.1 christos gdb_printf (stream, "%*s%ps = ", 2 * indent, "",
2350 1.6 christos styled_string (variable_name_style.style (), name));
2351 1.6 christos
2352 1.6 christos try
2353 1.6 christos {
2354 1.6 christos struct value *val;
2355 1.1 christos struct value_print_options opts;
2356 1.11 christos
2357 1.10 christos /* READ_VAR_VALUE needs a block in order to deal with non-local
2358 1.1 christos references (i.e. to handle nested functions). In this context, we
2359 1.9 christos print variables that are local to this frame, so we can avoid passing
2360 1.5 christos a block to it. */
2361 1.9 christos val = read_var_value (var, NULL, frame);
2362 1.9 christos get_user_print_options (&opts);
2363 1.9 christos opts.deref_ref = true;
2364 1.5 christos common_val_print_checked (val, stream, indent, &opts, current_language);
2365 1.5 christos }
2366 1.10 christos catch (const gdb_exception_error &except)
2367 1.1 christos {
2368 1.1 christos fprintf_styled (stream, metadata_style.style (),
2369 1.1 christos "<error reading variable %s (%s)>", name,
2370 1.1 christos except.what ());
2371 1.9 christos }
2372 1.9 christos
2373 1.1 christos gdb_printf (stream, "\n");
2374 1.1 christos }
2375 1.1 christos
2376 1.1 christos /* Subroutine of ui_printf to simplify it.
2377 1.1 christos Print VALUE to STREAM using FORMAT.
2378 1.11 christos VALUE is a C-style string either on the target or
2379 1.1 christos in a GDB internal variable. */
2380 1.11 christos
2381 1.11 christos static void
2382 1.11 christos printf_c_string (struct ui_file *stream, const char *format,
2383 1.9 christos struct value *value)
2384 1.11 christos {
2385 1.9 christos gdb::byte_vector str;
2386 1.9 christos
2387 1.9 christos if (((value->type ()->code () != TYPE_CODE_PTR && value->lval () == lval_internalvar)
2388 1.9 christos || value->type ()->code () == TYPE_CODE_ARRAY)
2389 1.9 christos && c_is_string_type_p (value->type ()))
2390 1.11 christos {
2391 1.9 christos size_t len = value->type ()->length ();
2392 1.11 christos
2393 1.11 christos /* Copy the internal var value to TEM_STR and append a terminating null
2394 1.9 christos character. This protects against corrupted C-style strings that lack
2395 1.9 christos the terminating null char. It also allows Ada-style strings (not
2396 1.8 christos null terminated) to be printed without problems. */
2397 1.9 christos str.resize (len + 1);
2398 1.9 christos
2399 1.9 christos memcpy (str.data (), value->contents ().data (), len);
2400 1.9 christos str [len] = 0;
2401 1.9 christos }
2402 1.9 christos else
2403 1.10 christos {
2404 1.9 christos CORE_ADDR tem = value_as_address (value);;
2405 1.9 christos
2406 1.9 christos if (tem == 0)
2407 1.9 christos {
2408 1.11 christos DIAGNOSTIC_PUSH
2409 1.11 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2410 1.11 christos gdb_printf (stream, format, "(null)");
2411 1.9 christos DIAGNOSTIC_POP
2412 1.9 christos return;
2413 1.9 christos }
2414 1.9 christos
2415 1.9 christos /* This is a %s argument. Build the string in STR which is
2416 1.9 christos currently empty. */
2417 1.11 christos gdb_assert (str.size () == 0);
2418 1.9 christos size_t len;
2419 1.11 christos for (len = 0;; len++)
2420 1.11 christos {
2421 1.9 christos gdb_byte c;
2422 1.9 christos
2423 1.9 christos QUIT;
2424 1.1 christos
2425 1.11 christos read_memory (tem + len, &c, 1);
2426 1.11 christos if (!exceeds_max_value_size (len + 1))
2427 1.11 christos str.push_back (c);
2428 1.11 christos if (c == 0)
2429 1.11 christos break;
2430 1.11 christos }
2431 1.11 christos
2432 1.11 christos if (exceeds_max_value_size (len + 1))
2433 1.11 christos error (_("printed string requires %s bytes, which is more than "
2434 1.1 christos "max-value-size"), plongest (len + 1));
2435 1.1 christos
2436 1.8 christos /* We will have passed through the above loop at least once, and will
2437 1.8 christos only exit the loop when we have pushed a zero byte onto the end of
2438 1.11 christos STR. */
2439 1.8 christos gdb_assert (str.size () > 0);
2440 1.1 christos gdb_assert (str.back () == 0);
2441 1.1 christos }
2442 1.1 christos
2443 1.1 christos DIAGNOSTIC_PUSH
2444 1.9 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2445 1.9 christos gdb_printf (stream, format, (char *) str.data ());
2446 1.1 christos DIAGNOSTIC_POP
2447 1.1 christos }
2448 1.1 christos
2449 1.1 christos /* Subroutine of ui_printf to simplify it.
2450 1.1 christos Print VALUE to STREAM using FORMAT.
2451 1.9 christos VALUE is a wide C-style string on the target or
2452 1.9 christos in a GDB internal variable. */
2453 1.11 christos
2454 1.9 christos static void
2455 1.1 christos printf_wide_c_string (struct ui_file *stream, const char *format,
2456 1.10 christos struct value *value)
2457 1.11 christos {
2458 1.1 christos const gdb_byte *str;
2459 1.11 christos size_t len;
2460 1.11 christos struct gdbarch *gdbarch = value->type ()->arch ();
2461 1.8 christos struct type *wctype = lookup_typename (current_language,
2462 1.11 christos "wchar_t", NULL, 0);
2463 1.11 christos int wcwidth = wctype->length ();
2464 1.8 christos std::optional<gdb::byte_vector> tem_str;
2465 1.9 christos
2466 1.9 christos if (value->lval () == lval_internalvar
2467 1.9 christos && c_is_string_type_p (value->type ()))
2468 1.9 christos {
2469 1.9 christos str = value->contents ().data ();
2470 1.9 christos len = value->type ()->length ();
2471 1.9 christos }
2472 1.9 christos else
2473 1.10 christos {
2474 1.9 christos CORE_ADDR tem = value_as_address (value);
2475 1.9 christos
2476 1.9 christos if (tem == 0)
2477 1.9 christos {
2478 1.9 christos DIAGNOSTIC_PUSH
2479 1.9 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2480 1.11 christos gdb_printf (stream, format, "(null)");
2481 1.9 christos DIAGNOSTIC_POP
2482 1.9 christos return;
2483 1.9 christos }
2484 1.9 christos
2485 1.11 christos /* This is a %s argument. Find the length of the string. */
2486 1.11 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2487 1.11 christos tem_str.emplace ();
2488 1.11 christos
2489 1.11 christos for (len = 0;; len += wcwidth)
2490 1.11 christos {
2491 1.11 christos QUIT;
2492 1.11 christos gdb_byte *dst;
2493 1.11 christos if (!exceeds_max_value_size (len + wcwidth))
2494 1.11 christos {
2495 1.11 christos tem_str->resize (tem_str->size () + wcwidth);
2496 1.11 christos dst = tem_str->data () + len;
2497 1.11 christos }
2498 1.11 christos else
2499 1.11 christos {
2500 1.11 christos /* We still need to check for the null-character, so we need
2501 1.11 christos somewhere to place the data read from the inferior. We
2502 1.11 christos can't keep growing TEM_STR, it's gotten too big, so
2503 1.11 christos instead just read the new character into the start of
2504 1.11 christos TEMS_STR. This will corrupt the previously read contents,
2505 1.11 christos but we're not going to print this string anyway, we just
2506 1.11 christos want to know how big it would have been so we can tell the
2507 1.11 christos user in the error message (see below).
2508 1.11 christos
2509 1.9 christos And we know there will be space in this buffer so long as
2510 1.9 christos WCWIDTH is smaller than our LONGEST type, the
2511 1.1 christos max-value-size can't be smaller than a LONGEST. */
2512 1.11 christos dst = tem_str->data ();
2513 1.11 christos }
2514 1.11 christos read_memory (tem + len, dst, wcwidth);
2515 1.9 christos if (extract_unsigned_integer (dst, wcwidth, byte_order) == 0)
2516 1.11 christos break;
2517 1.1 christos }
2518 1.1 christos
2519 1.8 christos if (exceeds_max_value_size (len + wcwidth))
2520 1.1 christos error (_("printed string requires %s bytes, which is more than "
2521 1.1 christos "max-value-size"), plongest (len + wcwidth));
2522 1.1 christos
2523 1.9 christos str = tem_str->data ();
2524 1.1 christos }
2525 1.1 christos
2526 1.1 christos auto_obstack output;
2527 1.8 christos
2528 1.8 christos convert_between_encodings (target_wide_charset (gdbarch),
2529 1.10 christos host_charset (),
2530 1.8 christos str, len, wcwidth,
2531 1.1 christos &output, translit_char);
2532 1.1 christos obstack_grow_str0 (&output, "");
2533 1.1 christos
2534 1.8 christos DIAGNOSTIC_PUSH
2535 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2536 1.1 christos gdb_printf (stream, format, obstack_base (&output));
2537 1.8 christos DIAGNOSTIC_POP
2538 1.8 christos }
2539 1.1 christos
2540 1.1 christos /* Subroutine of ui_printf to simplify it.
2541 1.11 christos Print VALUE, a floating point value, to STREAM using FORMAT. */
2542 1.10 christos
2543 1.1 christos static void
2544 1.8 christos printf_floating (struct ui_file *stream, const char *format,
2545 1.8 christos struct value *value, enum argclass argclass)
2546 1.8 christos {
2547 1.1 christos /* Parameter data. */
2548 1.8 christos struct type *param_type = value->type ();
2549 1.8 christos struct gdbarch *gdbarch = param_type->arch ();
2550 1.8 christos
2551 1.8 christos /* Determine target type corresponding to the format string. */
2552 1.8 christos struct type *fmt_type;
2553 1.8 christos switch (argclass)
2554 1.8 christos {
2555 1.8 christos case double_arg:
2556 1.8 christos fmt_type = builtin_type (gdbarch)->builtin_double;
2557 1.8 christos break;
2558 1.8 christos case long_double_arg:
2559 1.8 christos fmt_type = builtin_type (gdbarch)->builtin_long_double;
2560 1.8 christos break;
2561 1.8 christos case dec32float_arg:
2562 1.8 christos fmt_type = builtin_type (gdbarch)->builtin_decfloat;
2563 1.8 christos break;
2564 1.8 christos case dec64float_arg:
2565 1.1 christos fmt_type = builtin_type (gdbarch)->builtin_decdouble;
2566 1.1 christos break;
2567 1.8 christos case dec128float_arg:
2568 1.8 christos fmt_type = builtin_type (gdbarch)->builtin_declong;
2569 1.8 christos break;
2570 1.8 christos default:
2571 1.8 christos gdb_assert_not_reached ("unexpected argument class");
2572 1.8 christos }
2573 1.8 christos
2574 1.8 christos /* To match the traditional GDB behavior, the conversion is
2575 1.8 christos done differently depending on the type of the parameter:
2576 1.8 christos
2577 1.8 christos - if the parameter has floating-point type, it's value
2578 1.8 christos is converted to the target type;
2579 1.8 christos
2580 1.8 christos - otherwise, if the parameter has a type that is of the
2581 1.8 christos same size as a built-in floating-point type, the value
2582 1.8 christos bytes are interpreted as if they were of that type, and
2583 1.1 christos then converted to the target type (this is not done for
2584 1.8 christos decimal floating-point argument classes);
2585 1.8 christos
2586 1.1 christos - otherwise, if the source value has an integer value,
2587 1.9 christos it's value is converted to the target type;
2588 1.8 christos
2589 1.8 christos - otherwise, an error is raised.
2590 1.11 christos
2591 1.10 christos In either case, the result of the conversion is a byte buffer
2592 1.11 christos formatted in the target format for the target type. */
2593 1.8 christos
2594 1.1 christos if (fmt_type->code () == TYPE_CODE_FLT)
2595 1.8 christos {
2596 1.1 christos param_type = float_type_from_length (param_type);
2597 1.8 christos if (param_type != value->type ())
2598 1.8 christos value = value_from_contents (param_type,
2599 1.11 christos value->contents ().data ());
2600 1.10 christos }
2601 1.1 christos
2602 1.1 christos value = value_cast (fmt_type, value);
2603 1.1 christos
2604 1.1 christos /* Convert the value to a string and print it. */
2605 1.1 christos std::string str
2606 1.1 christos = target_float_to_string (value->contents ().data (), fmt_type, format);
2607 1.1 christos gdb_puts (str.c_str (), stream);
2608 1.1 christos }
2609 1.1 christos
2610 1.1 christos /* Subroutine of ui_printf to simplify it.
2611 1.1 christos Print VALUE, a target pointer, to STREAM using FORMAT. */
2612 1.1 christos
2613 1.1 christos static void
2614 1.1 christos printf_pointer (struct ui_file *stream, const char *format,
2615 1.1 christos struct value *value)
2616 1.1 christos {
2617 1.1 christos /* We avoid the host's %p because pointers are too
2618 1.1 christos likely to be the wrong size. The only interesting
2619 1.1 christos modifier for %p is a width; extract that, and then
2620 1.1 christos handle %p as glibc would: %#x or a literal "(nil)". */
2621 1.11 christos
2622 1.11 christos #ifdef PRINTF_HAS_LONG_LONG
2623 1.1 christos long long val = value_as_long (value);
2624 1.1 christos #else
2625 1.11 christos long val = value_as_long (value);
2626 1.1 christos #endif
2627 1.1 christos
2628 1.1 christos /* Build the new output format in FMT. */
2629 1.1 christos std::string fmt;
2630 1.11 christos
2631 1.1 christos /* Copy up to the leading %. */
2632 1.1 christos const char *p = format;
2633 1.1 christos while (*p)
2634 1.11 christos {
2635 1.1 christos int is_percent = (*p == '%');
2636 1.1 christos
2637 1.1 christos fmt.push_back (*p++);
2638 1.1 christos if (is_percent)
2639 1.1 christos {
2640 1.1 christos if (*p == '%')
2641 1.11 christos fmt.push_back (*p++);
2642 1.1 christos else
2643 1.8 christos break;
2644 1.8 christos }
2645 1.8 christos }
2646 1.11 christos
2647 1.1 christos if (val != 0)
2648 1.1 christos fmt.push_back ('#');
2649 1.1 christos
2650 1.1 christos /* Copy any width or flags. Only the "-" flag is valid for pointers
2651 1.1 christos -- see the format_pieces constructor. */
2652 1.11 christos while (*p == '-' || (*p >= '0' && *p < '9'))
2653 1.1 christos fmt.push_back (*p++);
2654 1.11 christos
2655 1.11 christos gdb_assert (*p == 'p' && *(p + 1) == '\0');
2656 1.8 christos if (val != 0)
2657 1.8 christos {
2658 1.11 christos #ifdef PRINTF_HAS_LONG_LONG
2659 1.8 christos fmt.push_back ('l');
2660 1.1 christos #endif
2661 1.1 christos fmt.push_back ('l');
2662 1.1 christos fmt.push_back ('x');
2663 1.11 christos DIAGNOSTIC_PUSH
2664 1.8 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2665 1.8 christos gdb_printf (stream, fmt.c_str (), val);
2666 1.11 christos DIAGNOSTIC_POP
2667 1.8 christos }
2668 1.1 christos else
2669 1.1 christos {
2670 1.1 christos fmt.push_back ('s');
2671 1.1 christos DIAGNOSTIC_PUSH
2672 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2673 1.1 christos gdb_printf (stream, fmt.c_str (), "(nil)");
2674 1.1 christos DIAGNOSTIC_POP
2675 1.1 christos }
2676 1.1 christos }
2677 1.8 christos
2678 1.1 christos /* printf "printf format string" ARG to STREAM. */
2679 1.1 christos
2680 1.1 christos static void
2681 1.1 christos ui_printf (const char *arg, struct ui_file *stream)
2682 1.8 christos {
2683 1.1 christos const char *s = arg;
2684 1.1 christos std::vector<struct value *> val_args;
2685 1.1 christos
2686 1.1 christos if (s == 0)
2687 1.1 christos error_no_arg (_("format-control string and values to print"));
2688 1.11 christos
2689 1.1 christos s = skip_spaces (s);
2690 1.1 christos
2691 1.1 christos /* A format string should follow, enveloped in double quotes. */
2692 1.1 christos if (*s++ != '"')
2693 1.8 christos error (_("Bad format string, missing '\"'."));
2694 1.1 christos
2695 1.1 christos format_pieces fpieces (&s, false, true);
2696 1.1 christos
2697 1.1 christos if (*s++ != '"')
2698 1.1 christos error (_("Bad format string, non-terminated '\"'."));
2699 1.1 christos
2700 1.8 christos s = skip_spaces (s);
2701 1.1 christos
2702 1.1 christos if (*s != ',' && *s != 0)
2703 1.1 christos error (_("Invalid argument syntax"));
2704 1.8 christos
2705 1.8 christos if (*s == ',')
2706 1.1 christos s++;
2707 1.1 christos s = skip_spaces (s);
2708 1.8 christos
2709 1.8 christos {
2710 1.1 christos int nargs_wanted;
2711 1.1 christos int i;
2712 1.1 christos const char *current_substring;
2713 1.1 christos
2714 1.1 christos nargs_wanted = 0;
2715 1.1 christos for (auto &&piece : fpieces)
2716 1.1 christos if (piece.argclass != literal_piece)
2717 1.1 christos ++nargs_wanted;
2718 1.1 christos
2719 1.1 christos /* Now, parse all arguments and evaluate them.
2720 1.8 christos Store the VALUEs in VAL_ARGS. */
2721 1.1 christos
2722 1.1 christos while (*s != '\0')
2723 1.1 christos {
2724 1.1 christos const char *s1;
2725 1.1 christos
2726 1.1 christos s1 = s;
2727 1.8 christos val_args.push_back (parse_to_comma_and_eval (&s1));
2728 1.1 christos
2729 1.1 christos s = s1;
2730 1.1 christos if (*s == ',')
2731 1.1 christos s++;
2732 1.8 christos }
2733 1.1 christos
2734 1.8 christos if (val_args.size () != nargs_wanted)
2735 1.8 christos error (_("Wrong number of arguments for specified format-string"));
2736 1.1 christos
2737 1.1 christos /* Now actually print them. */
2738 1.1 christos i = 0;
2739 1.1 christos for (auto &&piece : fpieces)
2740 1.1 christos {
2741 1.1 christos current_substring = piece.string;
2742 1.1 christos switch (piece.argclass)
2743 1.1 christos {
2744 1.1 christos case string_arg:
2745 1.11 christos printf_c_string (stream, current_substring, val_args[i]);
2746 1.9 christos break;
2747 1.1 christos case wide_string_arg:
2748 1.1 christos printf_wide_c_string (stream, current_substring, val_args[i]);
2749 1.1 christos break;
2750 1.1 christos case wide_char_arg:
2751 1.11 christos {
2752 1.10 christos struct gdbarch *gdbarch = val_args[i]->type ()->arch ();
2753 1.9 christos struct type *wctype = lookup_typename (current_language,
2754 1.1 christos "wchar_t", NULL, 0);
2755 1.1 christos struct type *valtype;
2756 1.11 christos const gdb_byte *bytes;
2757 1.1 christos
2758 1.8 christos valtype = val_args[i]->type ();
2759 1.1 christos if (valtype->length () != wctype->length ()
2760 1.1 christos || valtype->code () != TYPE_CODE_INT)
2761 1.1 christos error (_("expected wchar_t argument for %%lc"));
2762 1.10 christos
2763 1.10 christos bytes = val_args[i]->contents ().data ();
2764 1.1 christos
2765 1.1 christos auto_obstack output;
2766 1.1 christos
2767 1.8 christos convert_between_encodings (target_wide_charset (gdbarch),
2768 1.8 christos host_charset (),
2769 1.10 christos bytes, valtype->length (),
2770 1.10 christos valtype->length (),
2771 1.8 christos &output, translit_char);
2772 1.1 christos obstack_grow_str0 (&output, "");
2773 1.1 christos
2774 1.1 christos DIAGNOSTIC_PUSH
2775 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2776 1.1 christos gdb_printf (stream, current_substring,
2777 1.1 christos obstack_base (&output));
2778 1.1 christos DIAGNOSTIC_POP
2779 1.8 christos }
2780 1.8 christos break;
2781 1.10 christos case long_long_arg:
2782 1.8 christos #ifdef PRINTF_HAS_LONG_LONG
2783 1.1 christos {
2784 1.1 christos long long val = value_as_long (val_args[i]);
2785 1.1 christos
2786 1.1 christos DIAGNOSTIC_PUSH
2787 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2788 1.1 christos gdb_printf (stream, current_substring, val);
2789 1.1 christos DIAGNOSTIC_POP
2790 1.1 christos break;
2791 1.1 christos }
2792 1.8 christos #else
2793 1.8 christos error (_("long long not supported in printf"));
2794 1.10 christos #endif
2795 1.8 christos case int_arg:
2796 1.1 christos {
2797 1.1 christos int val = value_as_long (val_args[i]);
2798 1.1 christos
2799 1.1 christos DIAGNOSTIC_PUSH
2800 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2801 1.1 christos gdb_printf (stream, current_substring, val);
2802 1.8 christos DIAGNOSTIC_POP
2803 1.8 christos break;
2804 1.10 christos }
2805 1.8 christos case long_arg:
2806 1.1 christos {
2807 1.1 christos long val = value_as_long (val_args[i]);
2808 1.9 christos
2809 1.9 christos DIAGNOSTIC_PUSH
2810 1.9 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2811 1.9 christos gdb_printf (stream, current_substring, val);
2812 1.9 christos DIAGNOSTIC_POP
2813 1.9 christos break;
2814 1.10 christos }
2815 1.9 christos case size_t_arg:
2816 1.9 christos {
2817 1.9 christos size_t val = value_as_long (val_args[i]);
2818 1.8 christos
2819 1.8 christos DIAGNOSTIC_PUSH
2820 1.8 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2821 1.8 christos gdb_printf (stream, current_substring, val);
2822 1.8 christos DIAGNOSTIC_POP
2823 1.8 christos break;
2824 1.8 christos }
2825 1.8 christos /* Handles floating-point values. */
2826 1.1 christos case double_arg:
2827 1.1 christos case long_double_arg:
2828 1.1 christos case dec32float_arg:
2829 1.1 christos case dec64float_arg:
2830 1.11 christos case dec128float_arg:
2831 1.11 christos printf_floating (stream, current_substring, val_args[i],
2832 1.11 christos piece.argclass);
2833 1.11 christos break;
2834 1.11 christos case ptr_arg:
2835 1.11 christos printf_pointer (stream, current_substring, val_args[i]);
2836 1.11 christos break;
2837 1.11 christos case value_arg:
2838 1.11 christos {
2839 1.11 christos value_print_options print_opts;
2840 1.11 christos get_user_print_options (&print_opts);
2841 1.11 christos
2842 1.11 christos if (current_substring[2] == '[')
2843 1.11 christos {
2844 1.11 christos std::string args (¤t_substring[3],
2845 1.11 christos strlen (¤t_substring[3]) - 1);
2846 1.11 christos
2847 1.11 christos const char *args_ptr = args.c_str ();
2848 1.11 christos
2849 1.11 christos /* Override global settings with explicit options, if
2850 1.11 christos any. */
2851 1.11 christos auto group
2852 1.11 christos = make_value_print_options_def_group (&print_opts);
2853 1.11 christos gdb::option::process_options
2854 1.11 christos (&args_ptr, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR,
2855 1.11 christos group);
2856 1.11 christos
2857 1.11 christos if (*args_ptr != '\0')
2858 1.1 christos error (_("unexpected content in print options: %s"),
2859 1.1 christos args_ptr);
2860 1.1 christos }
2861 1.1 christos
2862 1.10 christos print_formatted (val_args[i], 0, &print_opts, stream);
2863 1.1 christos }
2864 1.1 christos break;
2865 1.1 christos case literal_piece:
2866 1.1 christos /* Print a portion of the format string that has no
2867 1.8 christos directives. Note that this will not include any
2868 1.8 christos ordinary %-specs, but it might include "%%". That is
2869 1.10 christos why we use gdb_printf and not gdb_puts here.
2870 1.8 christos Also, we pass a dummy argument because some platforms
2871 1.1 christos have modified GCC to include -Wformat-security by
2872 1.1 christos default, which will warn here if there is no
2873 1.10 christos argument. */
2874 1.1 christos DIAGNOSTIC_PUSH
2875 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
2876 1.8 christos gdb_printf (stream, current_substring, 0);
2877 1.1 christos DIAGNOSTIC_POP
2878 1.1 christos break;
2879 1.1 christos default:
2880 1.1 christos internal_error (_("failed internal consistency check"));
2881 1.1 christos }
2882 1.1 christos /* Maybe advance to the next argument. */
2883 1.1 christos if (piece.argclass != literal_piece)
2884 1.1 christos ++i;
2885 1.8 christos }
2886 1.1 christos }
2887 1.1 christos }
2888 1.10 christos
2889 1.10 christos /* Implement the "printf" command. */
2890 1.9 christos
2891 1.1 christos static void
2892 1.1 christos printf_command (const char *arg, int from_tty)
2893 1.1 christos {
2894 1.1 christos ui_printf (arg, gdb_stdout);
2895 1.1 christos gdb_stdout->reset_style ();
2896 1.8 christos gdb_stdout->wrap_here (0);
2897 1.1 christos gdb_stdout->flush ();
2898 1.7 christos }
2899 1.1 christos
2900 1.7 christos /* Implement the "eval" command. */
2901 1.1 christos
2902 1.7 christos static void
2903 1.1 christos eval_command (const char *arg, int from_tty)
2904 1.8 christos {
2905 1.1 christos string_file stb;
2906 1.1 christos
2907 1.10 christos ui_printf (arg, &stb);
2908 1.10 christos
2909 1.10 christos std::string expanded = insert_user_defined_cmd_args (stb.c_str ());
2910 1.10 christos
2911 1.10 christos execute_command (expanded.c_str (), from_tty);
2912 1.10 christos }
2913 1.11 christos
2914 1.10 christos /* Convenience function for error checking in memory-tag commands. */
2915 1.10 christos
2916 1.10 christos static void
2917 1.10 christos show_addr_not_tagged (CORE_ADDR address)
2918 1.10 christos {
2919 1.10 christos error (_("Address %s not in a region mapped with a memory tagging flag."),
2920 1.10 christos paddress (current_inferior ()->arch (), address));
2921 1.10 christos }
2922 1.10 christos
2923 1.10 christos /* Convenience function for error checking in memory-tag commands. */
2924 1.10 christos
2925 1.10 christos static void
2926 1.10 christos show_memory_tagging_unsupported (void)
2927 1.10 christos {
2928 1.10 christos error (_("Memory tagging not supported or disabled by the current"
2929 1.10 christos " architecture."));
2930 1.10 christos }
2931 1.10 christos
2932 1.10 christos /* Implement the "memory-tag" prefix command. */
2933 1.10 christos
2934 1.10 christos static void
2935 1.10 christos memory_tag_command (const char *arg, int from_tty)
2936 1.10 christos {
2937 1.10 christos help_list (memory_tag_list, "memory-tag ", all_commands, gdb_stdout);
2938 1.10 christos }
2939 1.10 christos
2940 1.10 christos /* Helper for print-logical-tag and print-allocation-tag. */
2941 1.10 christos
2942 1.10 christos static void
2943 1.10 christos memory_tag_print_tag_command (const char *args, enum memtag_type tag_type)
2944 1.10 christos {
2945 1.10 christos if (args == nullptr)
2946 1.11 christos error_no_arg (_("address or pointer"));
2947 1.10 christos
2948 1.10 christos /* Parse args into a value. If the value is a pointer or an address,
2949 1.10 christos then fetch the logical or allocation tag. */
2950 1.10 christos value_print_options print_opts;
2951 1.10 christos
2952 1.11 christos struct value *val = process_print_command_args (args, &print_opts, true);
2953 1.10 christos gdbarch *arch = current_inferior ()->arch ();
2954 1.11 christos
2955 1.11 christos /* If the address is not in a region memory mapped with a memory tagging
2956 1.10 christos flag, it is no use trying to access/manipulate its allocation tag.
2957 1.11 christos
2958 1.11 christos It is OK to manipulate the logical tag though. */
2959 1.10 christos CORE_ADDR addr = value_as_address (val);
2960 1.10 christos if (tag_type == memtag_type::allocation
2961 1.10 christos && !target_is_address_tagged (arch, addr))
2962 1.10 christos show_addr_not_tagged (addr);
2963 1.10 christos
2964 1.10 christos value *tag_value = gdbarch_get_memtag (arch, val, tag_type);
2965 1.10 christos std::string tag = gdbarch_memtag_to_string (arch, tag_value);
2966 1.10 christos
2967 1.10 christos if (tag.empty ())
2968 1.10 christos gdb_printf (_("%s tag unavailable.\n"),
2969 1.10 christos tag_type
2970 1.10 christos == memtag_type::logical? "Logical" : "Allocation");
2971 1.10 christos
2972 1.10 christos struct value *v_tag = process_print_command_args (tag.c_str (),
2973 1.10 christos &print_opts,
2974 1.10 christos true);
2975 1.10 christos print_opts.output_format = 'x';
2976 1.10 christos print_value (v_tag, print_opts);
2977 1.10 christos }
2978 1.10 christos
2979 1.10 christos /* Implement the "memory-tag print-logical-tag" command. */
2980 1.10 christos
2981 1.10 christos static void
2982 1.10 christos memory_tag_print_logical_tag_command (const char *args, int from_tty)
2983 1.10 christos {
2984 1.10 christos if (!target_supports_memory_tagging ())
2985 1.10 christos show_memory_tagging_unsupported ();
2986 1.10 christos
2987 1.10 christos memory_tag_print_tag_command (args, memtag_type::logical);
2988 1.10 christos }
2989 1.10 christos
2990 1.10 christos /* Implement the "memory-tag print-allocation-tag" command. */
2991 1.10 christos
2992 1.10 christos static void
2993 1.10 christos memory_tag_print_allocation_tag_command (const char *args, int from_tty)
2994 1.10 christos {
2995 1.10 christos if (!target_supports_memory_tagging ())
2996 1.10 christos show_memory_tagging_unsupported ();
2997 1.10 christos
2998 1.10 christos memory_tag_print_tag_command (args, memtag_type::allocation);
2999 1.10 christos }
3000 1.10 christos
3001 1.10 christos /* Parse ARGS and extract ADDR and TAG.
3002 1.10 christos ARGS should have format <expression> <tag bytes>. */
3003 1.10 christos
3004 1.10 christos static void
3005 1.10 christos parse_with_logical_tag_input (const char *args, struct value **val,
3006 1.10 christos gdb::byte_vector &tags,
3007 1.10 christos value_print_options *print_opts)
3008 1.10 christos {
3009 1.10 christos /* Fetch the address. */
3010 1.10 christos std::string address_string = extract_string_maybe_quoted (&args);
3011 1.10 christos
3012 1.10 christos /* Parse the address into a value. */
3013 1.10 christos *val = process_print_command_args (address_string.c_str (), print_opts,
3014 1.10 christos true);
3015 1.10 christos
3016 1.10 christos /* Fetch the tag bytes. */
3017 1.10 christos std::string tag_string = extract_string_maybe_quoted (&args);
3018 1.10 christos
3019 1.10 christos /* Validate the input. */
3020 1.10 christos if (address_string.empty () || tag_string.empty ())
3021 1.10 christos error (_("Missing arguments."));
3022 1.10 christos
3023 1.10 christos if (tag_string.length () != 2)
3024 1.10 christos error (_("Error parsing tags argument. The tag should be 2 digits."));
3025 1.10 christos
3026 1.10 christos tags = hex2bin (tag_string.c_str ());
3027 1.10 christos }
3028 1.10 christos
3029 1.10 christos /* Implement the "memory-tag with-logical-tag" command. */
3030 1.10 christos
3031 1.10 christos static void
3032 1.10 christos memory_tag_with_logical_tag_command (const char *args, int from_tty)
3033 1.10 christos {
3034 1.10 christos if (!target_supports_memory_tagging ())
3035 1.10 christos show_memory_tagging_unsupported ();
3036 1.11 christos
3037 1.10 christos if (args == nullptr)
3038 1.10 christos error_no_arg (_("<address> <tag>"));
3039 1.10 christos
3040 1.10 christos gdb::byte_vector tags;
3041 1.10 christos struct value *val;
3042 1.10 christos value_print_options print_opts;
3043 1.10 christos gdbarch *arch = current_inferior ()->arch ();
3044 1.10 christos
3045 1.10 christos /* Parse the input. */
3046 1.10 christos parse_with_logical_tag_input (args, &val, tags, &print_opts);
3047 1.10 christos
3048 1.10 christos /* Setting the logical tag is just a local operation that does not touch
3049 1.10 christos any memory from the target. Given an input value, we modify the value
3050 1.10 christos to include the appropriate tag.
3051 1.10 christos
3052 1.10 christos For this reason we need to cast the argument value to a
3053 1.10 christos (void *) pointer. This is so we have the right type for the gdbarch
3054 1.11 christos hook to manipulate the value and insert the tag.
3055 1.10 christos
3056 1.10 christos Otherwise, this would fail if, for example, GDB parsed the argument value
3057 1.10 christos into an int-sized value and the pointer value has a type of greater
3058 1.11 christos length. */
3059 1.10 christos
3060 1.10 christos /* Cast to (void *). */
3061 1.10 christos val = value_cast (builtin_type (current_inferior ()->arch ())->builtin_data_ptr,
3062 1.10 christos val);
3063 1.10 christos
3064 1.10 christos /* Length doesn't matter for a logical tag. Pass 0. */
3065 1.10 christos if (!gdbarch_set_memtags (arch, val, 0, tags, memtag_type::logical))
3066 1.10 christos gdb_printf (_("Could not update the logical tag data.\n"));
3067 1.10 christos else
3068 1.10 christos {
3069 1.10 christos /* Always print it in hex format. */
3070 1.10 christos print_opts.output_format = 'x';
3071 1.10 christos print_value (val, print_opts);
3072 1.10 christos }
3073 1.10 christos }
3074 1.10 christos
3075 1.10 christos /* Parse ARGS and extract ADDR, LENGTH and TAGS. */
3076 1.10 christos
3077 1.10 christos static void
3078 1.10 christos parse_set_allocation_tag_input (const char *args, struct value **val,
3079 1.10 christos size_t *length, gdb::byte_vector &tags)
3080 1.10 christos {
3081 1.10 christos /* Fetch the address. */
3082 1.10 christos std::string address_string = extract_string_maybe_quoted (&args);
3083 1.10 christos
3084 1.10 christos /* Parse the address into a value. */
3085 1.10 christos value_print_options print_opts;
3086 1.10 christos *val = process_print_command_args (address_string.c_str (), &print_opts,
3087 1.10 christos true);
3088 1.10 christos
3089 1.10 christos /* Fetch the length. */
3090 1.10 christos std::string length_string = extract_string_maybe_quoted (&args);
3091 1.10 christos
3092 1.10 christos /* Fetch the tag bytes. */
3093 1.10 christos std::string tags_string = extract_string_maybe_quoted (&args);
3094 1.10 christos
3095 1.10 christos /* Validate the input. */
3096 1.10 christos if (address_string.empty () || length_string.empty () || tags_string.empty ())
3097 1.10 christos error (_("Missing arguments."));
3098 1.10 christos
3099 1.10 christos errno = 0;
3100 1.10 christos const char *trailer = nullptr;
3101 1.10 christos LONGEST parsed_length = strtoulst (length_string.c_str (), &trailer, 10);
3102 1.10 christos
3103 1.10 christos if (errno != 0 || (trailer != nullptr && trailer[0] != '\0'))
3104 1.10 christos error (_("Error parsing length argument."));
3105 1.10 christos
3106 1.10 christos if (parsed_length <= 0)
3107 1.10 christos error (_("Invalid zero or negative length."));
3108 1.10 christos
3109 1.10 christos *length = parsed_length;
3110 1.10 christos
3111 1.10 christos if (tags_string.length () % 2)
3112 1.10 christos error (_("Error parsing tags argument. Tags should be 2 digits per byte."));
3113 1.10 christos
3114 1.10 christos tags = hex2bin (tags_string.c_str ());
3115 1.10 christos }
3116 1.10 christos
3117 1.10 christos /* Implement the "memory-tag set-allocation-tag" command.
3118 1.10 christos ARGS should be in the format <address> <length> <tags>. */
3119 1.10 christos
3120 1.10 christos static void
3121 1.10 christos memory_tag_set_allocation_tag_command (const char *args, int from_tty)
3122 1.10 christos {
3123 1.10 christos if (!target_supports_memory_tagging ())
3124 1.10 christos show_memory_tagging_unsupported ();
3125 1.10 christos
3126 1.10 christos if (args == nullptr)
3127 1.10 christos error_no_arg (_("<starting address> <length> <tag bytes>"));
3128 1.10 christos
3129 1.11 christos gdb::byte_vector tags;
3130 1.11 christos size_t length = 0;
3131 1.11 christos struct value *val;
3132 1.11 christos
3133 1.11 christos /* Parse the input. */
3134 1.11 christos parse_set_allocation_tag_input (args, &val, &length, tags);
3135 1.11 christos
3136 1.10 christos /* If the address is not in a region memory-mapped with a memory tagging
3137 1.10 christos flag, it is no use trying to manipulate its allocation tag. */
3138 1.10 christos CORE_ADDR addr = value_as_address (val);
3139 1.10 christos if (!target_is_address_tagged (current_inferior ()-> arch(), addr))
3140 1.10 christos show_addr_not_tagged (addr);
3141 1.10 christos
3142 1.10 christos if (!gdbarch_set_memtags (current_inferior ()->arch (), val, length, tags,
3143 1.10 christos memtag_type::allocation))
3144 1.10 christos gdb_printf (_("Could not update the allocation tag(s).\n"));
3145 1.10 christos else
3146 1.10 christos gdb_printf (_("Allocation tag(s) updated successfully.\n"));
3147 1.10 christos }
3148 1.10 christos
3149 1.10 christos /* Implement the "memory-tag check" command. */
3150 1.10 christos
3151 1.11 christos static void
3152 1.10 christos memory_tag_check_command (const char *args, int from_tty)
3153 1.10 christos {
3154 1.10 christos if (!target_supports_memory_tagging ())
3155 1.10 christos show_memory_tagging_unsupported ();
3156 1.10 christos
3157 1.10 christos if (args == nullptr)
3158 1.11 christos error_no_arg (_("address or pointer"));
3159 1.11 christos
3160 1.11 christos /* Parse the expression into a value. If the value is an address or
3161 1.10 christos pointer, then check its logical tag against the allocation tag. */
3162 1.10 christos value_print_options print_opts;
3163 1.10 christos
3164 1.11 christos struct value *val = process_print_command_args (args, &print_opts, true);
3165 1.11 christos gdbarch *arch = current_inferior ()->arch ();
3166 1.10 christos
3167 1.10 christos CORE_ADDR addr = value_as_address (val);
3168 1.11 christos
3169 1.10 christos /* If the address is not in a region memory mapped with a memory tagging
3170 1.11 christos flag, it is no use trying to access/manipulate its allocation tag. */
3171 1.11 christos if (!target_is_address_tagged (arch, addr))
3172 1.10 christos show_addr_not_tagged (addr);
3173 1.11 christos
3174 1.11 christos /* Check if the tag is valid. */
3175 1.10 christos if (!gdbarch_memtag_matches_p (arch, val))
3176 1.10 christos {
3177 1.10 christos value *tag = gdbarch_get_memtag (arch, val, memtag_type::logical);
3178 1.10 christos std::string ltag = gdbarch_memtag_to_string (arch, tag);
3179 1.11 christos
3180 1.10 christos tag = gdbarch_get_memtag (arch, val, memtag_type::allocation);
3181 1.10 christos std::string atag = gdbarch_memtag_to_string (arch, tag);
3182 1.10 christos
3183 1.10 christos gdb_printf (_("Logical tag (%s) does not match"
3184 1.11 christos " the allocation tag (%s) for address %s.\n"),
3185 1.11 christos ltag.c_str (), atag.c_str (),
3186 1.10 christos paddress (current_inferior ()->arch (), addr));
3187 1.11 christos }
3188 1.10 christos else
3189 1.10 christos {
3190 1.11 christos struct value *tag
3191 1.10 christos = gdbarch_get_memtag (current_inferior ()->arch (), val,
3192 1.10 christos memtag_type::logical);
3193 1.10 christos std::string ltag
3194 1.9 christos = gdbarch_memtag_to_string (current_inferior ()->arch (), tag);
3195 1.1 christos
3196 1.9 christos gdb_printf (_("Memory tags for address %s match (%s).\n"),
3197 1.1 christos paddress (current_inferior ()->arch (), addr), ltag.c_str ());
3198 1.1 christos }
3199 1.1 christos }
3200 1.1 christos
3201 1.1 christos void _initialize_printcmd ();
3202 1.10 christos void
3203 1.10 christos _initialize_printcmd ()
3204 1.1 christos {
3205 1.8 christos struct cmd_list_element *c;
3206 1.9 christos
3207 1.9 christos current_display_number = -1;
3208 1.1 christos
3209 1.8 christos gdb::observers::free_objfile.attach (clear_dangling_display_expressions,
3210 1.1 christos "printcmd");
3211 1.9 christos
3212 1.1 christos add_info ("address", info_address_command,
3213 1.1 christos _("Describe where symbol SYM is stored.\n\
3214 1.10 christos Usage: info address SYM"));
3215 1.1 christos
3216 1.1 christos add_info ("symbol", info_symbol_command, _("\
3217 1.1 christos Describe what symbol is at location ADDR.\n\
3218 1.1 christos Usage: info symbol ADDR\n\
3219 1.1 christos Only for symbols with fixed locations (global or static scope)."));
3220 1.1 christos
3221 1.1 christos c = add_com ("x", class_vars, x_command, _("\
3222 1.1 christos Examine memory: x/FMT ADDRESS.\n\
3223 1.6 christos ADDRESS is an expression for the memory address to examine.\n\
3224 1.6 christos FMT is a repeat count followed by a format letter and a size letter.\n\
3225 1.1 christos Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
3226 1.1 christos t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
3227 1.1 christos and z(hex, zero padded on the left).\n\
3228 1.10 christos Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
3229 1.1 christos The specified number of objects of the specified size are printed\n\
3230 1.8 christos according to the format. If a negative number is specified, memory is\n\
3231 1.9 christos examined backward from the address.\n\n\
3232 1.9 christos Defaults for format and size letters are those previously used.\n\
3233 1.1 christos Default count is 1. Default address is following last thing printed\n\
3234 1.1 christos with this command or \"print\"."));
3235 1.1 christos set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
3236 1.9 christos
3237 1.1 christos add_info ("display", info_display_command, _("\
3238 1.1 christos Expressions to display when program stops, with code numbers.\n\
3239 1.1 christos Usage: info display"));
3240 1.1 christos
3241 1.1 christos add_cmd ("undisplay", class_vars, undisplay_command, _("\
3242 1.1 christos Cancel some expressions to be displayed when program stops.\n\
3243 1.10 christos Usage: undisplay [NUM]...\n\
3244 1.1 christos Arguments are the code numbers of the expressions to stop displaying.\n\
3245 1.9 christos No argument means cancel all automatic-display expressions.\n\
3246 1.1 christos \"delete display\" has the same effect as this command.\n\
3247 1.1 christos Do \"info display\" to see current list of code numbers."),
3248 1.1 christos &cmdlist);
3249 1.1 christos
3250 1.1 christos c = add_com ("display", class_vars, display_command, _("\
3251 1.1 christos Print value of expression EXP each time the program stops.\n\
3252 1.10 christos Usage: display[/FMT] EXP\n\
3253 1.1 christos /FMT may be used before EXP as in the \"print\" command.\n\
3254 1.1 christos /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
3255 1.1 christos as in the \"x\" command, and then EXP is used to get the address to examine\n\
3256 1.9 christos and examining is done as in the \"x\" command.\n\n\
3257 1.1 christos With no argument, display all currently requested auto-display expressions.\n\
3258 1.1 christos Use \"undisplay\" to cancel display requests previously made."));
3259 1.1 christos set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
3260 1.1 christos
3261 1.1 christos add_cmd ("display", class_vars, enable_display_command, _("\
3262 1.1 christos Enable some expressions to be displayed when program stops.\n\
3263 1.9 christos Usage: enable display [NUM]...\n\
3264 1.1 christos Arguments are the code numbers of the expressions to resume displaying.\n\
3265 1.1 christos No argument means enable all automatic-display expressions.\n\
3266 1.1 christos Do \"info display\" to see current list of code numbers."), &enablelist);
3267 1.1 christos
3268 1.1 christos add_cmd ("display", class_vars, disable_display_command, _("\
3269 1.1 christos Disable some expressions to be displayed when program stops.\n\
3270 1.9 christos Usage: disable display [NUM]...\n\
3271 1.1 christos Arguments are the code numbers of the expressions to stop displaying.\n\
3272 1.1 christos No argument means disable all automatic-display expressions.\n\
3273 1.1 christos Do \"info display\" to see current list of code numbers."), &disablelist);
3274 1.1 christos
3275 1.1 christos add_cmd ("display", class_vars, undisplay_command, _("\
3276 1.8 christos Cancel some expressions to be displayed when program stops.\n\
3277 1.9 christos Usage: delete display [NUM]...\n\
3278 1.8 christos Arguments are the code numbers of the expressions to stop displaying.\n\
3279 1.1 christos No argument means cancel all automatic-display expressions.\n\
3280 1.1 christos Do \"info display\" to see current list of code numbers."), &deletelist);
3281 1.1 christos
3282 1.9 christos add_com ("printf", class_vars, printf_command, _("\
3283 1.1 christos Formatted printing, like the C \"printf\" function.\n\
3284 1.1 christos Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
3285 1.1 christos This supports most C printf format specifications, like %s, %d, etc."));
3286 1.9 christos
3287 1.9 christos add_com ("output", class_vars, output_command, _("\
3288 1.9 christos Like \"print\" but don't put in value history and don't print newline.\n\
3289 1.9 christos Usage: output EXP\n\
3290 1.9 christos This is useful in user-defined commands."));
3291 1.1 christos
3292 1.1 christos add_prefix_cmd ("set", class_vars, set_command, _("\
3293 1.1 christos Evaluate expression EXP and assign result to variable VAR.\n\
3294 1.1 christos Usage: set VAR = EXP\n\
3295 1.1 christos This uses assignment syntax appropriate for the current language\n\
3296 1.1 christos (VAR = EXP or VAR := EXP for example).\n\
3297 1.10 christos VAR may be a debugger \"convenience\" variable (names starting\n\
3298 1.1 christos with $), a register (a few standard names starting with $), or an actual\n\
3299 1.1 christos variable in the program being debugged. EXP is any valid expression.\n\
3300 1.1 christos Use \"set variable\" for variables with names identical to set subcommands.\n\
3301 1.1 christos \n\
3302 1.9 christos With a subcommand, this command modifies parts of the gdb environment.\n\
3303 1.1 christos You can see these environment settings with the \"show\" command."),
3304 1.1 christos &setlist, 1, &cmdlist);
3305 1.1 christos
3306 1.9 christos /* "call" is the same as "set", but handy for dbx users to call fns. */
3307 1.1 christos c = add_com ("call", class_vars, call_command, _("\
3308 1.10 christos Call a function in the program.\n\
3309 1.10 christos Usage: call EXP\n\
3310 1.9 christos The argument is the function name and arguments, in the notation of the\n\
3311 1.9 christos current working language. The result is printed and saved in the value\n\
3312 1.9 christos history, if it is not void."));
3313 1.9 christos set_cmd_completer_handle_brkchars (c, print_command_completer);
3314 1.9 christos
3315 1.1 christos cmd_list_element *set_variable_cmd
3316 1.1 christos = add_cmd ("variable", class_vars, set_command, _("\
3317 1.1 christos Evaluate expression EXP and assign result to variable VAR.\n\
3318 1.10 christos Usage: set variable VAR = EXP\n\
3319 1.10 christos This uses assignment syntax appropriate for the current language\n\
3320 1.1 christos (VAR = EXP or VAR := EXP for example).\n\
3321 1.9 christos VAR may be a debugger \"convenience\" variable (names starting\n\
3322 1.9 christos with $), a register (a few standard names starting with $), or an actual\n\
3323 1.9 christos variable in the program being debugged. EXP is any valid expression.\n\
3324 1.1 christos This may usually be abbreviated to simply \"set\"."),
3325 1.9 christos &setlist);
3326 1.9 christos add_alias_cmd ("var", set_variable_cmd, class_vars, 0, &setlist);
3327 1.9 christos
3328 1.9 christos const auto print_opts = make_value_print_options_def_group (nullptr);
3329 1.9 christos
3330 1.9 christos static const std::string print_help = gdb::option::build_help (_("\
3331 1.9 christos Print value of expression EXP.\n\
3332 1.9 christos Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
3333 1.9 christos \n\
3334 1.1 christos Options:\n\
3335 1.1 christos %OPTIONS%\n\
3336 1.1 christos \n\
3337 1.1 christos Note: because this command accepts arbitrary expressions, if you\n\
3338 1.1 christos specify any command option, you must use a double dash (\"--\")\n\
3339 1.1 christos to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
3340 1.1 christos \n\
3341 1.1 christos Variables accessible are those of the lexical environment of the selected\n\
3342 1.1 christos stack frame, plus all those whose scope is global or an entire file.\n\
3343 1.1 christos \n\
3344 1.1 christos $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
3345 1.1 christos $$NUM refers to NUM'th value back from the last one.\n\
3346 1.1 christos Names starting with $ refer to registers (with the values they would have\n\
3347 1.1 christos if the program were to return to the stack frame now selected, restoring\n\
3348 1.1 christos all registers saved by frames farther in) or else to debugger\n\
3349 1.1 christos \"convenience\" variables (any such name not a known register).\n\
3350 1.1 christos Use assignment expressions to give values to convenience variables.\n\
3351 1.1 christos \n\
3352 1.1 christos {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
3353 1.9 christos @ is a binary operator for treating consecutive data objects\n\
3354 1.9 christos anywhere in memory as an array. FOO@NUM gives an array whose first\n\
3355 1.9 christos element is FOO, whose second element is stored in the space following\n\
3356 1.10 christos where FOO is stored, etc. FOO must be an expression whose value\n\
3357 1.10 christos resides in memory.\n\
3358 1.10 christos \n\
3359 1.10 christos EXP may be preceded with /FMT, where FMT is a format letter\n\
3360 1.10 christos but no count or size letter (see \"x\" command)."),
3361 1.1 christos print_opts);
3362 1.1 christos
3363 1.1 christos cmd_list_element *print_cmd
3364 1.9 christos = add_com ("print", class_vars, print_command, print_help.c_str ());
3365 1.9 christos set_cmd_completer_handle_brkchars (print_cmd, print_command_completer);
3366 1.1 christos add_com_alias ("p", print_cmd, class_vars, 1);
3367 1.1 christos add_com_alias ("inspect", print_cmd, class_vars, 1);
3368 1.1 christos
3369 1.1 christos add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
3370 1.1 christos &max_symbolic_offset, _("\
3371 1.1 christos Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
3372 1.1 christos Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
3373 1.1 christos Tell GDB to only display the symbolic form of an address if the\n\
3374 1.1 christos offset between the closest earlier symbol and the address is less than\n\
3375 1.1 christos the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
3376 1.9 christos to always print the symbolic form of an address if any symbol precedes\n\
3377 1.9 christos it. Zero is equivalent to \"unlimited\"."),
3378 1.1 christos NULL,
3379 1.1 christos show_max_symbolic_offset,
3380 1.1 christos &setprintlist, &showprintlist);
3381 1.1 christos add_setshow_boolean_cmd ("symbol-filename", no_class,
3382 1.1 christos &print_symbol_filename, _("\
3383 1.9 christos Set printing of source filename and line number with <SYMBOL>."), _("\
3384 1.9 christos Show printing of source filename and line number with <SYMBOL>."), NULL,
3385 1.9 christos NULL,
3386 1.9 christos show_print_symbol_filename,
3387 1.10 christos &setprintlist, &showprintlist);
3388 1.10 christos
3389 1.10 christos add_com ("eval", no_class, eval_command, _("\
3390 1.10 christos Construct a GDB command and then evaluate it.\n\
3391 1.10 christos Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
3392 1.10 christos Convert the arguments to a string as \"printf\" would, but then\n\
3393 1.10 christos treat this string as a command line, and evaluate it."));
3394 1.10 christos
3395 1.10 christos /* Memory tagging commands. */
3396 1.10 christos add_prefix_cmd ("memory-tag", class_vars, memory_tag_command, _("\
3397 1.10 christos Generic command for printing and manipulating memory tag properties."),
3398 1.10 christos &memory_tag_list, 0, &cmdlist);
3399 1.10 christos add_cmd ("print-logical-tag", class_vars,
3400 1.10 christos memory_tag_print_logical_tag_command,
3401 1.10 christos ("Print the logical tag from POINTER.\n\
3402 1.10 christos Usage: memory-tag print-logical-tag <POINTER>.\n\
3403 1.10 christos <POINTER> is an expression that evaluates to a pointer.\n\
3404 1.10 christos Print the logical tag contained in POINTER. The tag interpretation is\n\
3405 1.10 christos architecture-specific."),
3406 1.10 christos &memory_tag_list);
3407 1.10 christos add_cmd ("print-allocation-tag", class_vars,
3408 1.10 christos memory_tag_print_allocation_tag_command,
3409 1.10 christos _("Print the allocation tag for ADDRESS.\n\
3410 1.10 christos Usage: memory-tag print-allocation-tag <ADDRESS>.\n\
3411 1.10 christos <ADDRESS> is an expression that evaluates to a memory address.\n\
3412 1.10 christos Print the allocation tag associated with the memory address ADDRESS.\n\
3413 1.10 christos The tag interpretation is architecture-specific."),
3414 1.10 christos &memory_tag_list);
3415 1.10 christos add_cmd ("with-logical-tag", class_vars, memory_tag_with_logical_tag_command,
3416 1.10 christos _("Print a POINTER with a specific logical TAG.\n\
3417 1.10 christos Usage: memory-tag with-logical-tag <POINTER> <TAG>\n\
3418 1.10 christos <POINTER> is an expression that evaluates to a pointer.\n\
3419 1.10 christos <TAG> is a sequence of hex bytes that is interpreted by the architecture\n\
3420 1.10 christos as a single memory tag."),
3421 1.10 christos &memory_tag_list);
3422 1.10 christos add_cmd ("set-allocation-tag", class_vars,
3423 1.10 christos memory_tag_set_allocation_tag_command,
3424 1.10 christos _("Set the allocation tag(s) for a memory range.\n\
3425 1.10 christos Usage: memory-tag set-allocation-tag <ADDRESS> <LENGTH> <TAG_BYTES>\n\
3426 1.10 christos <ADDRESS> is an expression that evaluates to a memory address\n\
3427 1.10 christos <LENGTH> is the number of bytes that is added to <ADDRESS> to calculate\n\
3428 1.10 christos the memory range.\n\
3429 1.10 christos <TAG_BYTES> is a sequence of hex bytes that is interpreted by the\n\
3430 1.10 christos architecture as one or more memory tags.\n\
3431 1.10 christos Sets the tags of the memory range [ADDRESS, ADDRESS + LENGTH)\n\
3432 1.10 christos to TAG_BYTES.\n\
3433 1.10 christos \n\
3434 1.10 christos If the number of tags is greater than or equal to the number of tag granules\n\
3435 1.10 christos in the [ADDRESS, ADDRESS + LENGTH) range, only the tags up to the\n\
3436 1.10 christos number of tag granules are updated.\n\
3437 1.10 christos \n\
3438 1.10 christos If the number of tags is less than the number of tag granules, then the\n\
3439 1.10 christos command is a fill operation. The TAG_BYTES are interpreted as a pattern\n\
3440 1.10 christos that gets repeated until the number of tag granules in the memory range\n\
3441 1.10 christos [ADDRESS, ADDRESS + LENGTH) is updated."),
3442 1.10 christos &memory_tag_list);
3443 1.10 christos add_cmd ("check", class_vars, memory_tag_check_command,
3444 1.1 christos _("Validate a pointer's logical tag against the allocation tag.\n\
3445 Usage: memory-tag check <POINTER>\n\
3446 <POINTER> is an expression that evaluates to a pointer\n\
3447 Fetch the logical and allocation tags for POINTER and compare them\n\
3448 for equality. If the tags do not match, print additional information about\n\
3449 the tag mismatch."),
3450 &memory_tag_list);
3451 }
3452