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