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