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