/* Print values for GNU debugger GDB.
- Copyright (C) 1986-2019 Free Software Foundation, Inc.
+ Copyright (C) 1986-2020 Free Software Foundation, Inc.
This file is part of GDB.
/* Append the source filename and linenumber of the symbol when
printing a symbolic value as `<symbol at filename:linenum>' if set. */
-static int print_symbol_filename = 0;
+static bool print_symbol_filename = false;
static void
show_print_symbol_filename (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
static int current_display_number;
+/* Last allocated display number. */
+
+static int display_number;
+
struct display
{
- /* Chain link to next auto-display item. */
- struct display *next;
+ display (const char *exp_string_, expression_up &&exp_,
+ const struct format_data &format_, struct program_space *pspace_,
+ const struct block *block_)
+ : exp_string (exp_string_),
+ exp (std::move (exp_)),
+ number (++display_number),
+ format (format_),
+ pspace (pspace_),
+ block (block_),
+ enabled_p (true)
+ {
+ }
/* The expression as the user typed it. */
- char *exp_string;
+ std::string exp_string;
/* Expression to be evaluated and displayed. */
expression_up exp;
const struct block *block;
/* Status of this display (enabled or disabled). */
- int enabled_p;
+ bool enabled_p;
};
-/* Chain of expressions whose values should be displayed
- automatically each time the program stops. */
-
-static struct display *display_chain;
-
-static int display_number;
-
-/* Walk the following statement or block through all displays.
- ALL_DISPLAYS_SAFE does so even if the statement deletes the current
- display. */
+/* Expressions whose values should be displayed automatically each
+ time the program stops. */
-#define ALL_DISPLAYS(B) \
- for (B = display_chain; B; B = B->next)
-
-#define ALL_DISPLAYS_SAFE(B,TMP) \
- for (B = display_chain; \
- B ? (TMP = B->next, 1): 0; \
- B = TMP)
+static std::vector<std::unique_ptr<struct display>> all_displays;
/* Prototypes for local functions. */
}
if (options->format == 0 || options->format == 's'
- || TYPE_CODE (type) == TYPE_CODE_REF
- || TYPE_CODE (type) == TYPE_CODE_ARRAY
- || TYPE_CODE (type) == TYPE_CODE_STRING
- || TYPE_CODE (type) == TYPE_CODE_STRUCT
- || TYPE_CODE (type) == TYPE_CODE_UNION
- || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
+ || type->code () == TYPE_CODE_REF
+ || type->code () == TYPE_CODE_ARRAY
+ || type->code () == TYPE_CODE_STRING
+ || type->code () == TYPE_CODE_STRUCT
+ || type->code () == TYPE_CODE_UNION
+ || type->code () == TYPE_CODE_NAMESPACE)
value_print (val, stream, options);
else
/* User specified format, so don't look to the type to tell us
what to do. */
- val_print_scalar_formatted (type,
- value_embedded_offset (val),
- val,
- options, size, stream);
+ value_print_scalar_formatted (val, options, size, stream);
}
/* Return builtin floating point type of same length as TYPE.
{
struct gdbarch *gdbarch = get_type_arch (type);
unsigned int len = TYPE_LENGTH (type);
- enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
+ enum bfd_endian byte_order = type_byte_order (type);
/* String printing should go through val_print_scalar_formatted. */
gdb_assert (options->format != 's');
/* If the value is a pointer, and pointers and addresses are not the
same, then at this point, the value's length (in target bytes) is
gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
- if (TYPE_CODE (type) == TYPE_CODE_PTR)
+ if (type->code () == TYPE_CODE_PTR)
len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
/* If we are printing it as unsigned, truncate it in case it is actually
/* Historically gdb has printed floats by first casting them to a
long, and then printing the long. PR cli/16242 suggests changing
- this to using C-style hex float format. */
- gdb::byte_vector converted_float_bytes;
- if (TYPE_CODE (type) == TYPE_CODE_FLT
- && (options->format == 'o'
- || options->format == 'x'
- || options->format == 't'
- || options->format == 'z'
- || options->format == 'd'
- || options->format == 'u'))
- {
- LONGEST val_long = unpack_long (type, valaddr);
- converted_float_bytes.resize (TYPE_LENGTH (type));
- store_signed_integer (converted_float_bytes.data (), TYPE_LENGTH (type),
- byte_order, val_long);
- valaddr = converted_float_bytes.data ();
+ this to using C-style hex float format.
+
+ Biased range types must also be unbiased here; the unbiasing is
+ done by unpack_long. */
+ gdb::byte_vector converted_bytes;
+ /* Some cases below will unpack the value again. In the biased
+ range case, we want to avoid this, so we store the unpacked value
+ here for possible use later. */
+ gdb::optional<LONGEST> val_long;
+ if ((type->code () == TYPE_CODE_FLT
+ && (options->format == 'o'
+ || options->format == 'x'
+ || options->format == 't'
+ || options->format == 'z'
+ || options->format == 'd'
+ || options->format == 'u'))
+ || (type->code () == TYPE_CODE_RANGE
+ && TYPE_RANGE_DATA (type)->bias != 0))
+ {
+ val_long.emplace (unpack_long (type, valaddr));
+ converted_bytes.resize (TYPE_LENGTH (type));
+ store_signed_integer (converted_bytes.data (), TYPE_LENGTH (type),
+ byte_order, *val_long);
+ valaddr = converted_bytes.data ();
}
/* Printing a non-float type as 'f' will interpret the data as if it were
of a floating-point type of the same length, if that exists. Otherwise,
the data is printed as integer. */
char format = options->format;
- if (format == 'f' && TYPE_CODE (type) != TYPE_CODE_FLT)
+ if (format == 'f' && type->code () != TYPE_CODE_FLT)
{
type = float_type_from_length (type);
- if (TYPE_CODE (type) != TYPE_CODE_FLT)
+ if (type->code () != TYPE_CODE_FLT)
format = 0;
}
print_decimal_chars (stream, valaddr, len, false, byte_order);
break;
case 0:
- if (TYPE_CODE (type) != TYPE_CODE_FLT)
+ if (type->code () != TYPE_CODE_FLT)
{
print_decimal_chars (stream, valaddr, len, !TYPE_UNSIGNED (type),
byte_order);
{
struct value_print_options opts = *options;
- LONGEST val_long = unpack_long (type, valaddr);
+ if (!val_long.has_value ())
+ val_long.emplace (unpack_long (type, valaddr));
opts.format = 0;
if (TYPE_UNSIGNED (type))
else
type = builtin_type (gdbarch)->builtin_true_char;
- value_print (value_from_longest (type, val_long), stream, &opts);
+ value_print (value_from_longest (type, *val_long), stream, &opts);
}
break;
case 'a':
{
- CORE_ADDR addr = unpack_pointer (type, valaddr);
-
- print_address (gdbarch, addr, stream);
+ if (!val_long.has_value ())
+ val_long.emplace (unpack_long (type, valaddr));
+ print_address (gdbarch, *val_long, stream);
}
break;
name_location = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol));
if (do_demangle || asm_demangle)
- name_temp = SYMBOL_PRINT_NAME (symbol);
+ name_temp = symbol->print_name ();
else
- name_temp = SYMBOL_LINKAGE_NAME (symbol);
+ name_temp = symbol->linkage_name ();
}
if (msymbol.minsym != NULL
symbol = 0;
name_location = BMSYMBOL_VALUE_ADDRESS (msymbol);
if (do_demangle || asm_demangle)
- name_temp = MSYMBOL_PRINT_NAME (msymbol.minsym);
+ name_temp = msymbol.minsym->print_name ();
else
- name_temp = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
+ name_temp = msymbol.minsym->linkage_name ();
}
}
if (symbol == NULL && msymbol.minsym == NULL)
{
const char *exp = *expp;
+ /* opts->raw value might already have been set by 'set print raw-values'
+ or by using 'print -raw-values'.
+ So, do not set opts->raw to 0, only set it to 1 if /r is given. */
if (exp && *exp == '/')
{
format_data fmt;
last_format = fmt.format;
opts->format = fmt.format;
- opts->raw = fmt.raw;
+ opts->raw = opts->raw || fmt.raw;
}
else
{
opts->format = 0;
- opts->raw = 0;
}
*expp = exp;
val = access_value_history (0);
if (voidprint || (val && value_type (val) &&
- TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
+ value_type (val)->code () != TYPE_CODE_VOID))
print_value (val, print_opts);
}
offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
sec_name = osect->the_bfd_section->name;
- msym_name = MSYMBOL_PRINT_NAME (msymbol);
+ msym_name = msymbol->print_name ();
/* Don't print the offset if it is zero.
We assume there's no need to handle i18n of "sym + offset". */
gdb_assert (osect->objfile && objfile_name (osect->objfile));
obj_name = objfile_name (osect->objfile);
- if (MULTI_OBJFILE_P ())
+ if (current_program_space->multi_objfile_p ())
if (pc_in_unmapped_range (addr, osect))
if (section_is_overlay (osect))
printf_filtered (_("%s in load address range of "
{
struct objfile *objfile = msymbol.objfile;
- gdbarch = get_objfile_arch (objfile);
+ gdbarch = objfile->arch ();
load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
printf_filtered ("Symbol \"");
}
printf_filtered ("Symbol \"");
- fprintf_symbol_filtered (gdb_stdout, SYMBOL_PRINT_NAME (sym),
+ fprintf_symbol_filtered (gdb_stdout, sym->print_name (),
current_language->la_language, DMGL_ANSI);
printf_filtered ("\" is ");
val = SYMBOL_VALUE (sym);
{
struct bound_minimal_symbol msym;
- msym = lookup_bound_minimal_symbol (SYMBOL_LINKAGE_NAME (sym));
+ msym = lookup_bound_minimal_symbol (sym->linkage_name ());
if (msym.minsym == NULL)
printf_filtered ("unresolved");
else
val = coerce_ref (val);
/* In rvalue contexts, such as this, functions are coerced into
pointers to functions. This makes "x/i main" work. */
- if (/* last_format == 'i' && */
- TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
+ if (value_type (val)->code () == TYPE_CODE_FUNC
&& VALUE_LVAL (val) == lval_memory)
next_address = value_address (val);
else
innermost_block_tracker tracker;
expression_up expr = parse_expression (exp, &tracker);
- newobj = new display ();
-
- newobj->exp_string = xstrdup (exp);
- newobj->exp = std::move (expr);
- newobj->block = tracker.block ();
- newobj->pspace = current_program_space;
- newobj->number = ++display_number;
- newobj->format = fmt;
- newobj->enabled_p = 1;
- newobj->next = NULL;
-
- if (display_chain == NULL)
- display_chain = newobj;
- else
- {
- struct display *last;
-
- for (last = display_chain; last->next != NULL; last = last->next)
- ;
- last->next = newobj;
- }
+ newobj = new display (exp, std::move (expr), fmt,
+ current_program_space, tracker.block ());
+ all_displays.emplace_back (newobj);
if (from_tty)
do_one_display (newobj);
dont_repeat ();
}
-static void
-free_display (struct display *d)
-{
- xfree (d->exp_string);
- delete d;
-}
-
/* Clear out the display_chain. Done when new symtabs are loaded,
since this invalidates the types stored in many expressions. */
void
-clear_displays (void)
+clear_displays ()
{
- struct display *d;
-
- while ((d = display_chain) != NULL)
- {
- display_chain = d->next;
- free_display (d);
- }
+ all_displays.clear ();
}
/* Delete the auto-display DISPLAY. */
static void
delete_display (struct display *display)
{
- struct display *d;
-
gdb_assert (display != NULL);
- if (display_chain == display)
- display_chain = display->next;
-
- ALL_DISPLAYS (d)
- if (d->next == display)
- {
- d->next = display->next;
- break;
- }
-
- free_display (display);
+ auto iter = std::find_if (all_displays.begin (),
+ all_displays.end (),
+ [=] (const std::unique_ptr<struct display> &item)
+ {
+ return item.get () == display;
+ });
+ gdb_assert (iter != all_displays.end ());
+ all_displays.erase (iter);
}
/* Call FUNCTION on each of the displays whose numbers are given in
static void
map_display_numbers (const char *args,
- void (*function) (struct display *,
- void *),
- void *data)
+ gdb::function_view<void (struct display *)> function)
{
int num;
warning (_("bad display number at or near '%s'"), p);
else
{
- struct display *d, *tmp;
-
- ALL_DISPLAYS_SAFE (d, tmp)
- if (d->number == num)
- break;
- if (d == NULL)
+ auto iter = std::find_if (all_displays.begin (),
+ all_displays.end (),
+ [=] (const std::unique_ptr<display> &item)
+ {
+ return item->number == num;
+ });
+ if (iter == all_displays.end ())
printf_unfiltered (_("No display number %d.\n"), num);
else
- function (d, data);
+ function (iter->get ());
}
}
}
-/* Callback for map_display_numbers, that deletes a display. */
-
-static void
-do_delete_display (struct display *d, void *data)
-{
- delete_display (d);
-}
-
/* "undisplay" command. */
static void
return;
}
- map_display_numbers (args, do_delete_display, NULL);
+ map_display_numbers (args, delete_display);
dont_repeat ();
}
{
int within_current_scope;
- if (d->enabled_p == 0)
+ if (!d->enabled_p)
return;
/* The expression carries the architecture that was used at parse time.
try
{
innermost_block_tracker tracker;
- d->exp = parse_expression (d->exp_string, &tracker);
+ d->exp = parse_expression (d->exp_string.c_str (), &tracker);
d->block = tracker.block ();
}
catch (const gdb_exception &ex)
{
/* Can't re-parse the expression. Disable this display item. */
- d->enabled_p = 0;
+ d->enabled_p = false;
warning (_("Unable to display \"%s\": %s"),
- d->exp_string, ex.what ());
+ d->exp_string.c_str (), ex.what ());
return;
}
}
if (d->block)
{
if (d->pspace == current_program_space)
- within_current_scope = contained_in (get_selected_block (0), d->block);
+ within_current_scope = contained_in (get_selected_block (0), d->block,
+ true);
else
within_current_scope = 0;
}
annotate_display_expression ();
- puts_filtered (d->exp_string);
+ puts_filtered (d->exp_string.c_str ());
annotate_display_expression_end ();
if (d->format.count != 1 || d->format.format == 'i')
}
catch (const gdb_exception_error &ex)
{
- fprintf_filtered (gdb_stdout, _("<error: %s>\n"),
- ex.what ());
+ fprintf_filtered (gdb_stdout, _("%p[<error: %s>%p]\n"),
+ metadata_style.style ().ptr (), ex.what (),
+ nullptr);
}
}
else
annotate_display_expression ();
- puts_filtered (d->exp_string);
+ puts_filtered (d->exp_string.c_str ());
annotate_display_expression_end ();
printf_filtered (" = ");
}
catch (const gdb_exception_error &ex)
{
- fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.what ());
+ fprintf_styled (gdb_stdout, metadata_style.style (),
+ _("<error: %s>"), ex.what ());
}
printf_filtered ("\n");
void
do_displays (void)
{
- struct display *d;
-
- for (d = display_chain; d; d = d->next)
- do_one_display (d);
+ for (auto &d : all_displays)
+ do_one_display (d.get ());
}
/* Delete the auto-display which we were in the process of displaying.
void
disable_display (int num)
{
- struct display *d;
-
- for (d = display_chain; d; d = d->next)
+ for (auto &d : all_displays)
if (d->number == num)
{
- d->enabled_p = 0;
+ d->enabled_p = false;
return;
}
printf_unfiltered (_("No display number %d.\n"), num);
static void
info_display_command (const char *ignore, int from_tty)
{
- struct display *d;
-
- if (!display_chain)
+ if (all_displays.empty ())
printf_unfiltered (_("There are no auto-display expressions now.\n"));
else
printf_filtered (_("Auto-display expressions now in effect:\n\
Num Enb Expression\n"));
- for (d = display_chain; d; d = d->next)
+ for (auto &d : all_displays)
{
printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
if (d->format.size)
d->format.format);
else if (d->format.format)
printf_filtered ("/%c ", d->format.format);
- puts_filtered (d->exp_string);
- if (d->block && !contained_in (get_selected_block (0), d->block))
+ puts_filtered (d->exp_string.c_str ());
+ if (d->block && !contained_in (get_selected_block (0), d->block, true))
printf_filtered (_(" (cannot be evaluated in the current context)"));
printf_filtered ("\n");
}
}
-/* Callback fo map_display_numbers, that enables or disables the
- passed in display D. */
-
-static void
-do_enable_disable_display (struct display *d, void *data)
-{
- d->enabled_p = *(int *) data;
-}
-
-/* Implamentation of both the "disable display" and "enable display"
+/* Implementation of both the "disable display" and "enable display"
commands. ENABLE decides what to do. */
static void
-enable_disable_display_command (const char *args, int from_tty, int enable)
+enable_disable_display_command (const char *args, int from_tty, bool enable)
{
if (args == NULL)
{
- struct display *d;
-
- ALL_DISPLAYS (d)
+ for (auto &d : all_displays)
d->enabled_p = enable;
return;
}
- map_display_numbers (args, do_enable_disable_display, &enable);
+ map_display_numbers (args,
+ [=] (struct display *d)
+ {
+ d->enabled_p = enable;
+ });
}
/* The "enable display" command. */
static void
enable_display_command (const char *args, int from_tty)
{
- enable_disable_display_command (args, from_tty, 1);
+ enable_disable_display_command (args, from_tty, true);
}
/* The "disable display" command. */
static void
disable_display_command (const char *args, int from_tty)
{
- enable_disable_display_command (args, from_tty, 0);
+ enable_disable_display_command (args, from_tty, false);
}
/* display_chain items point to blocks and expressions. Some expressions in
static void
clear_dangling_display_expressions (struct objfile *objfile)
{
- struct display *d;
struct program_space *pspace;
/* With no symbol file we cannot have a block or expression from it. */
gdb_assert (objfile->pspace == pspace);
}
- for (d = display_chain; d != NULL; d = d->next)
+ for (auto &d : all_displays)
{
if (d->pspace != pspace)
continue;
- if (lookup_objfile_from_block (d->block) == objfile
+ struct objfile *bl_objf = nullptr;
+ if (d->block != nullptr)
+ {
+ bl_objf = block_objfile (d->block);
+ if (bl_objf->separate_debug_objfile_backlink != nullptr)
+ bl_objf = bl_objf->separate_debug_objfile_backlink;
+ }
+
+ if (bl_objf == objfile
|| (d->exp != NULL && exp_uses_objfile (d->exp.get (), objfile)))
- {
- d->exp.reset ();
- d->block = NULL;
- }
+ {
+ d->exp.reset ();
+ d->block = NULL;
+ }
}
}
\f
{
if (!name)
- name = SYMBOL_PRINT_NAME (var);
+ name = var->print_name ();
- fputs_filtered (n_spaces (2 * indent), stream);
- fputs_styled (name, variable_name_style.style (), stream);
- fputs_filtered (" = ", stream);
+ fprintf_filtered (stream, "%s%ps = ", n_spaces (2 * indent),
+ styled_string (variable_name_style.style (), name));
try
{
}
catch (const gdb_exception_error &except)
{
- fprintf_filtered (stream, "<error reading variable %s (%s)>", name,
- except.what ());
+ fprintf_styled (stream, metadata_style.style (),
+ "<error reading variable %s (%s)>", name,
+ except.what ());
}
fprintf_filtered (stream, "\n");
{
const gdb_byte *str;
- if (VALUE_LVAL (value) == lval_internalvar
+ if (value_type (value)->code () != TYPE_CODE_PTR
+ && VALUE_LVAL (value) == lval_internalvar
&& c_is_string_type_p (value_type (value)))
{
size_t len = TYPE_LENGTH (value_type (value));
const gdb_byte *str;
size_t len;
struct gdbarch *gdbarch = get_type_arch (value_type (value));
- struct type *wctype = lookup_typename (current_language, gdbarch,
+ struct type *wctype = lookup_typename (current_language,
"wchar_t", NULL, 0);
int wcwidth = TYPE_LENGTH (wctype);
In either case, the result of the conversion is a byte buffer
formatted in the target format for the target type. */
- if (TYPE_CODE (fmt_type) == TYPE_CODE_FLT)
+ if (fmt_type->code () == TYPE_CODE_FLT)
{
param_type = float_type_from_length (param_type);
if (param_type != value_type (value))
{
struct gdbarch *gdbarch
= get_type_arch (value_type (val_args[i]));
- struct type *wctype = lookup_typename (current_language, gdbarch,
+ struct type *wctype = lookup_typename (current_language,
"wchar_t", NULL, 0);
struct type *valtype;
const gdb_byte *bytes;
valtype = value_type (val_args[i]);
if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
- || TYPE_CODE (valtype) != TYPE_CODE_INT)
+ || valtype->code () != TYPE_CODE_INT)
error (_("expected wchar_t argument for %%lc"));
bytes = value_contents (val_args[i]);
{
long val = value_as_long (val_args[i]);
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
+ fprintf_filtered (stream, current_substring, val);
+ DIAGNOSTIC_POP
+ break;
+ }
+ case size_t_arg:
+ {
+ size_t val = value_as_long (val_args[i]);
+
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
fprintf_filtered (stream, current_substring, val);
ui_printf (arg, gdb_stdout);
reset_terminal_style (gdb_stdout);
wrap_here ("");
- gdb_flush (gdb_stdout);
+ gdb_stdout->flush ();
}
/* Implement the "eval" command. */
execute_command (expanded.c_str (), from_tty);
}
+void _initialize_printcmd ();
void
-_initialize_printcmd (void)
+_initialize_printcmd ()
{
struct cmd_list_element *c;
This is useful in user-defined commands."));
add_prefix_cmd ("set", class_vars, set_command, _("\
-Evaluate expression EXP and assign result to variable VAR\n\
+Evaluate expression EXP and assign result to variable VAR.\n\
Usage: set VAR = EXP\n\
This uses assignment syntax appropriate for the current language\n\
(VAR = EXP or VAR := EXP for example).\n\
&setlist, "set ", 1, &cmdlist);
if (dbx_commands)
add_com ("assign", class_vars, set_command, _("\
-Evaluate expression EXP and assign result to variable VAR\n\
+Evaluate expression EXP and assign result to variable VAR.\n\
Usage: assign VAR = EXP\n\
This uses assignment syntax appropriate for the current language\n\
(VAR = EXP or VAR := EXP for example).\n\
set_cmd_completer_handle_brkchars (c, print_command_completer);
add_cmd ("variable", class_vars, set_command, _("\
-Evaluate expression EXP and assign result to variable VAR\n\
+Evaluate expression EXP and assign result to variable VAR.\n\
Usage: set variable VAR = EXP\n\
This uses assignment syntax appropriate for the current language\n\
(VAR = EXP or VAR := EXP for example).\n\
const auto print_opts = make_value_print_options_def_group (nullptr);
- static const std::string print_help = gdb::option::build_help (N_("\
+ static const std::string print_help = gdb::option::build_help (_("\
Print value of expression EXP.\n\
Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
\n\
Options:\n\
-%OPTIONS%\
+%OPTIONS%\n\
+\n\
Note: because this command accepts arbitrary expressions, if you\n\
specify any command option, you must use a double dash (\"--\")\n\
to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\