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