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