{
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type);
- return gdb::unique_xmalloc_ptr<char>
- (xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr)));
+ return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
}
/* See language.h. */
_initialize_auto_load ()
{
struct cmd_list_element *cmd;
- char *scripts_directory_help, *gdb_name_help, *python_name_help;
- char *guile_name_help;
+ gdb::unique_xmalloc_ptr<char> scripts_directory_help, gdb_name_help,
+ python_name_help, guile_name_help;
const char *suffix;
gdb::observers::new_objfile.attach (auto_load_new_objfile,
Directories listed here need to be present also \
in the 'set auto-load safe-path'\n\
option."),
- gdb_name_help,
- python_name_help ? python_name_help : "",
- guile_name_help ? guile_name_help : "");
+ gdb_name_help.get (),
+ python_name_help.get () ? python_name_help.get () : "",
+ guile_name_help.get () ? guile_name_help.get () : "");
add_setshow_optional_filename_cmd ("scripts-directory", class_support,
&auto_load_dir, _("\
Set the list of directories from which to load auto-loaded scripts."), _("\
Show the list of directories from which to load auto-loaded scripts."),
- scripts_directory_help,
+ scripts_directory_help.get (),
set_auto_load_dir, show_auto_load_dir,
auto_load_set_cmdlist_get (),
auto_load_show_cmdlist_get ());
- xfree (scripts_directory_help);
- xfree (python_name_help);
- xfree (gdb_name_help);
- xfree (guile_name_help);
-
auto_load_safe_path_vec_update ();
add_setshow_optional_filename_cmd ("safe-path", class_support,
&auto_load_safe_path, _("\
update_dprintf_command_list (struct breakpoint *b)
{
const char *dprintf_args = b->extra_string.get ();
- char *printf_line = NULL;
+ gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
if (!dprintf_args)
return;
/* Manufacture a printf sequence. */
struct command_line *printf_cmd_line
- = new struct command_line (simple_control, printf_line);
+ = new struct command_line (simple_control, printf_line.release ());
breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
command_lines_deleter ()));
}
w->exp_string_reparse
= current_language->watch_location_expression (t, addr);
- w->exp_string.reset (xstrprintf ("-location %.*s",
- (int) (exp_end - exp_start),
- exp_start));
+ w->exp_string = xstrprintf ("-location %.*s",
+ (int) (exp_end - exp_start), exp_start);
}
else
w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
{
iconv_t desc;
static const char *stored_result = NULL;
- char *result;
+ gdb::unique_xmalloc_ptr<char> result;
if (stored_result)
return stored_result;
result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
ENDIAN_SUFFIX);
/* Check that the name is supported by iconv_open. */
- desc = iconv_open (result, host_charset ());
+ desc = iconv_open (result.get (), host_charset ());
if (desc != (iconv_t) -1)
{
iconv_close (desc);
- stored_result = result;
- return result;
+ stored_result = result.release ();
+ return stored_result;
}
- /* Not valid, free the allocated memory. */
- xfree (result);
/* Second try, with UCS-2 type. */
result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
ENDIAN_SUFFIX);
/* Check that the name is supported by iconv_open. */
- desc = iconv_open (result, host_charset ());
+ desc = iconv_open (result.get (), host_charset ());
if (desc != (iconv_t) -1)
{
iconv_close (desc);
- stored_result = result;
- return result;
+ stored_result = result.release ();
+ return stored_result;
}
- /* Not valid, free the allocated memory. */
- xfree (result);
/* No valid charset found, generate error here. */
error (_("Unable to find a valid charset for string conversions"));
}
struct symtab_and_line sal;
struct symbol *sym;
const char *editor;
- char *p;
const char *fn;
/* Pull in the current default source line if necessary. */
/* Quote the file name, in case it has whitespace or other special
characters. */
- p = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
- shell_escape (p, from_tty);
- xfree (p);
+ gdb::unique_xmalloc_ptr<char> p
+ = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
+ shell_escape (p.get (), from_tty);
}
/* The options for the "pipe" command. */
-v: each command in FILE is echoed as it is executed.\n\
\n\
Note that the file \"%s\" is read automatically in this way\n\
-when GDB is started."), GDBINIT);
+when GDB is started."), GDBINIT).release ();
c = add_cmd ("source", class_support, source_command,
source_help_text, &cmdlist);
set_cmd_completer (c, filename_completer);
{
struct cmd_list_element *set;
struct cmd_list_element *show;
- char *full_set_doc;
- char *full_show_doc;
+ gdb::unique_xmalloc_ptr<char> full_set_doc;
+ gdb::unique_xmalloc_ptr<char> full_show_doc;
if (help_doc != NULL)
{
}
else
{
- full_set_doc = xstrdup (set_doc);
- full_show_doc = xstrdup (show_doc);
+ full_set_doc = make_unique_xstrdup (set_doc);
+ full_show_doc = make_unique_xstrdup (show_doc);
}
set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, args,
- full_set_doc, set_list);
+ full_set_doc.release (), set_list);
set->doc_allocated = 1;
if (set_func != NULL)
set->func = set_func;
show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, args,
- full_show_doc, show_list);
+ full_show_doc.release (), show_list);
show->doc_allocated = 1;
show->show_value_func = show_func;
/* Disable the default symbol completer. Doesn't make much sense
prefix, entity_kind, entity_kind, entity_kind,
(document_n_flag ? _("\n\
By default, the command will include non-debug symbols in the output;\n\
-these can be excluded using the -n flag.") : ""));
+these can be excluded using the -n flag.") : "")).release ();
}
/* See documentation in cli-utils.h. */
void
set_cp_abi_as_auto_default (const char *short_name)
{
- char *new_longname, *new_doc;
struct cp_abi_ops *abi = find_cp_abi (short_name);
if (abi == NULL)
auto_cp_abi = *abi;
auto_cp_abi.shortname = "auto";
- new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
- auto_cp_abi.longname = new_longname;
-
- new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
- abi->shortname);
- auto_cp_abi.doc = new_doc;
+ auto_cp_abi.longname = xstrprintf ("currently \"%s\"",
+ abi->shortname).release ();
+ auto_cp_abi.doc = xstrprintf ("Automatically selected; currently \"%s\"",
+ abi->shortname).release ();
/* Since we copy the current ABI into current_cp_abi instead of
using a pointer, if auto is currently the default, we need to
in the G packet. If we need more in the future, we'll add them
elsewhere. */
for (r = acc0_regnum; r <= acc7_regnum; r++)
- {
- char *buf;
- buf = xstrprintf ("acc%d", r - acc0_regnum);
- var->register_names[r] = buf;
- }
+ var->register_names[r]
+ = xstrprintf ("acc%d", r - acc0_regnum).release ();
/* accg0 - accg7: These are one byte registers. The remote protocol
provides the raw values packed four into a slot. accg0123 and
likely not want to see these raw values. */
for (r = accg0_regnum; r <= accg7_regnum; r++)
- {
- char *buf;
- buf = xstrprintf ("accg%d", r - accg0_regnum);
- var->register_names[r] = buf;
- }
+ var->register_names[r]
+ = xstrprintf ("accg%d", r - accg0_regnum).release ();
/* msr0 and msr1. */
else
{
/* Default corefile name is "core.PID". */
- corefilename.reset (xstrprintf ("core.%d", inferior_ptid.pid ()));
+ corefilename = xstrprintf ("core.%d", inferior_ptid.pid ());
}
if (info_verbose)
= xstrprintf (_("Only one stop condition allowed. There is"
" currently a %s stop condition defined for"
" this breakpoint."),
- ext_lang_capitalized_name (extlang));
+ ext_lang_capitalized_name (extlang)).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (error_text);
struct cmd_list_element *elt;
int len = strlen (name);
int i, lastchar;
- char *prefix_text;
- const char *prefix_text2;
- char *result, *msg;
+ char *msg;
/* Skip trailing whitespace. */
for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
/* Find first character of the final word. */
for (; i > 0 && valid_cmd_char_p (name[i - 1]); --i)
;
- result = (char *) xmalloc (lastchar - i + 2);
- memcpy (result, &name[i], lastchar - i + 1);
- result[lastchar - i + 1] = '\0';
+ gdb::unique_xmalloc_ptr<char> result ((char *) xmalloc (lastchar - i + 2));
+ memcpy (result.get (), &name[i], lastchar - i + 1);
+ result.get ()[lastchar - i + 1] = '\0';
/* Skip whitespace again. */
for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
if (i < 0)
{
*base_list = start_list;
- return result;
+ return result.release ();
}
- prefix_text = (char *) xmalloc (i + 2);
- memcpy (prefix_text, name, i + 1);
- prefix_text[i + 1] = '\0';
+ gdb::unique_xmalloc_ptr<char> prefix_text ((char *) xmalloc (i + 2));
+ memcpy (prefix_text.get (), name, i + 1);
+ prefix_text.get ()[i + 1] = '\0';
- prefix_text2 = prefix_text;
+ const char *prefix_text2 = prefix_text.get ();
elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, NULL, 1);
if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
{
- msg = xstrprintf (_("could not find command prefix '%s'"), prefix_text);
- xfree (prefix_text);
- xfree (result);
+ msg = xstrprintf (_("could not find command prefix '%s'"),
+ prefix_text.get ()).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (msg);
gdbscm_out_of_range_error (func_name, arg_pos,
if (elt->is_prefix ())
{
- xfree (prefix_text);
*base_list = elt->subcommands;
- return result;
+ return result.release ();
}
- msg = xstrprintf (_("'%s' is not a prefix command"), prefix_text);
- xfree (prefix_text);
- xfree (result);
+ msg = xstrprintf (_("'%s' is not a prefix command"),
+ prefix_text.get ()).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (msg);
gdbscm_out_of_range_error (func_name, arg_pos,
gdbscm_make_type_error (const char *subr, int arg_pos, SCM bad_value,
const char *expected_type)
{
- char *msg;
+ gdb::unique_xmalloc_ptr<char> msg;
SCM result;
if (arg_pos > 0)
msg = xstrprintf (_("Wrong type argument: ~S"));
}
- result = gdbscm_make_error (scm_arg_type_key, subr, msg,
+ result = gdbscm_make_error (scm_arg_type_key, subr, msg.get (),
scm_list_1 (bad_value), scm_list_1 (bad_value));
- xfree (msg);
return result;
}
gdbscm_make_arg_error (SCM key, const char *subr, int arg_pos, SCM bad_value,
const char *error_prefix, const char *error)
{
- char *msg;
+ gdb::unique_xmalloc_ptr<char> msg;
SCM result;
if (error_prefix != NULL)
msg = xstrprintf (_("%s: ~S"), error);
}
- result = gdbscm_make_error (key, subr, msg,
- scm_list_1 (bad_value), scm_list_1 (bad_value));
- xfree (msg);
+ result = gdbscm_make_error (key, subr, msg.get (), scm_list_1 (bad_value),
+ scm_list_1 (bad_value));
return result;
}
SCM smob, result;
scm_t_bits smobnum;
const char *name;
- char *kind;
smob = gsscm_get_gsmob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
smobnum = SCM_SMOBNUM (smob);
name = SCM_SMOBNAME (smobnum);
- kind = xstrprintf ("<%s>", name);
- result = scm_from_latin1_symbol (kind);
- xfree (kind);
-
+ gdb::unique_xmalloc_ptr<char> kind = xstrprintf ("<%s>", name);
+ result = scm_from_latin1_symbol (kind.get ());
return result;
}
static const unsigned max_memory_port_buf_size = 4096;
/* "out of range" error message for buf sizes. */
-static char *out_of_range_buf_size;
+static gdb::unique_xmalloc_ptr<char> out_of_range_buf_size;
#else
max_memory_port_buf_size))
{
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, size,
- out_of_range_buf_size);
+ out_of_range_buf_size.get ());
}
iomem = (ioscm_memory_port *) SCM_STREAM (port);
max_memory_port_buf_size))
{
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, size,
- out_of_range_buf_size);
+ out_of_range_buf_size.get ());
}
iomem = (ioscm_memory_port *) SCM_STREAM (port);
error (_("JIT reader already loaded. Run jit-reader-unload first."));
if (!IS_ABSOLUTE_PATH (file.get ()))
- file.reset (xstrprintf ("%s%s%s", jit_reader_dir.c_str (), SLASH_STRING,
- file.get ()));
+ file = xstrprintf ("%s%s%s", jit_reader_dir.c_str (),
+ SLASH_STRING, file.get ());
loaded_jit_reader = jit_reader_load (file.get ());
reinit_frame_cache ();
/* Generates an expression that assumes a C like syntax is valid. */
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type);
- return gdb::unique_xmalloc_ptr<char>
- (xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
+ return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
}
/* See language.h. */
the time being. */
if (symname != NULL && sal->line != 0
&& self->language->la_language == language_ada)
- canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
+ canonical->suffix = xstrprintf ("%s:%d", symname,
+ sal->line).release ();
else if (symname != NULL)
canonical->suffix = xstrdup (symname);
else
- canonical->suffix = xstrprintf ("%d", sal->line);
+ canonical->suffix = xstrprintf ("%d", sal->line).release ();
canonical->symtab = sal->symtab;
}
else
break;
case ADDRESS_LOCATION:
- EL_STRING (location)
- = xstrprintf ("*%s",
- core_addr_to_string (EL_ADDRESS (location)));
+ {
+ const char *addr_string
+ = core_addr_to_string (EL_ADDRESS (location));
+ EL_STRING (location)
+ = xstrprintf ("*%s", addr_string).release ();
+ }
break;
case EXPLICIT_LOCATION:
}
else if (def->argc == macro_LINE)
{
- saved_expansion.reset (xstrprintf ("%d", line));
+ saved_expansion = xstrprintf ("%d", line);
def->replacement = saved_expansion.get ();
}
}
/* Prefix warning messages with the command name. */
gdb::unique_xmalloc_ptr<char> tmp_warn_preprint
- (xstrprintf ("%s: warning: ", gdb_program_name));
+ = xstrprintf ("%s: warning: ", gdb_program_name);
warning_pre_print = tmp_warn_preprint.get ();
current_directory = getcwd (NULL, 0);
gdb::unique_xmalloc_ptr<char> run;
if (args != NULL)
- run.reset (xstrprintf ("%s %s", cmd, args));
+ run = xstrprintf ("%s %s", cmd, args);
else
run.reset (xstrdup (cmd));
execute_command ( /*ui */ run.get (), 0 /*from_tty */ );
if (lineobj != NULL)
{
if (PyInt_Check (lineobj))
- line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
+ line = xstrprintf ("%ld", PyInt_AsLong (lineobj));
else if (PyString_Check (lineobj))
line = python_string_to_host_string (lineobj);
else
add_packet_config_cmd (struct packet_config *config, const char *name,
const char *title, int legacy)
{
- char *set_doc;
- char *show_doc;
- char *cmd_name;
-
config->name = name;
config->title = title;
- set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
- name, title);
- show_doc = xstrprintf ("Show current use of remote "
- "protocol `%s' (%s) packet.",
- name, title);
+ gdb::unique_xmalloc_ptr<char> set_doc
+ = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
+ name, title);
+ gdb::unique_xmalloc_ptr<char> show_doc
+ = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
+ name, title);
/* set/show TITLE-packet {auto,on,off} */
- cmd_name = xstrprintf ("%s-packet", title);
+ gdb::unique_xmalloc_ptr<char> cmd_name = xstrprintf ("%s-packet", title);
set_show_commands cmds
- = add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
- &config->detect, set_doc,
- show_doc, NULL, /* help_doc */
+ = add_setshow_auto_boolean_cmd (cmd_name.release (), class_obscure,
+ &config->detect, set_doc.get (),
+ show_doc.get (), NULL, /* help_doc */
NULL,
show_remote_protocol_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist);
config->show_cmd = cmds.show;
- /* The command code copies the documentation strings. */
- xfree (set_doc);
- xfree (show_doc);
-
/* set/show remote NAME-packet {auto,on,off} -- legacy. */
if (legacy)
{
- char *legacy_name;
-
- legacy_name = xstrprintf ("%s-packet", name);
- add_alias_cmd (legacy_name, cmds.set, class_obscure, 0,
+ /* It's not clear who should take ownership of this string, so, for
+ now, make it static, and give copies to each of the add_alias_cmd
+ calls below. */
+ static gdb::unique_xmalloc_ptr<char> legacy_name
+ = xstrprintf ("%s-packet", name);
+ add_alias_cmd (legacy_name.get (), cmds.set, class_obscure, 0,
&remote_set_cmdlist);
- add_alias_cmd (legacy_name, cmds.show, class_obscure, 0,
+ add_alias_cmd (legacy_name.get (), cmds.show, class_obscure, 0,
&remote_show_cmdlist);
}
}
for (auto ® : m_registers)
{
int csr_num = reg.regnum - RISCV_FIRST_CSR_REGNUM;
- const char *alias = xstrprintf ("csr%d", csr_num);
- reg.names.push_back (alias);
+ gdb::unique_xmalloc_ptr<char> alias = xstrprintf ("csr%d", csr_num);
+ reg.names.push_back (alias.release ());
}
}
};
{
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type);
- return gdb::unique_xmalloc_ptr<char>
- (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
- name.c_str ()));
+ return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
+ name.c_str ());
}
/* See language.h. */
for (const skiplist_entry &entry : skiplist_entries)
{
- gdb::unique_xmalloc_ptr<char> name (xstrprintf ("%d", entry.number ()));
+ gdb::unique_xmalloc_ptr<char> name = xstrprintf ("%d", entry.number ());
if (strncmp (word, name.get (), word_len) == 0)
completer.add_completion (std::move (name));
}
add_deprecated_target_alias (const target_info &tinfo, const char *alias)
{
struct cmd_list_element *c;
- char *alt;
/* If we use add_alias_cmd, here, we do not get the deprecated warning,
see PR cli/15104. */
c = add_cmd (alias, no_class, tinfo.doc, &targetlist);
c->func = open_target;
c->set_context ((void *) &tinfo);
- alt = xstrprintf ("target %s", tinfo.shortname);
- deprecate_cmd (c, alt);
+ gdb::unique_xmalloc_ptr<char> alt
+ = xstrprintf ("target %s", tinfo.shortname);
+ deprecate_cmd (c, alt.release ());
}
/* Stub functions */
if (!default_collect.empty ())
{
gdb::unique_xmalloc_ptr<char> default_collect_line
- (xstrprintf ("collect %s", default_collect.c_str ()));
+ = xstrprintf ("collect %s", default_collect.c_str ());
validate_actionline (default_collect_line.get (), t);
actions.reset (new struct command_line (simple_control,
layout->specification (&spec, 0);
gdb::unique_xmalloc_ptr<char> doc
- (xstrprintf (_("Apply the \"%s\" layout.\n\
+ = xstrprintf (_("Apply the \"%s\" layout.\n\
This layout was created using:\n\
tui new-layout %s %s"),
- name, name, spec.c_str ()));
+ name, name, spec.c_str ());
cmd = add_cmd (name, class_tui, nullptr, doc.get (), &layout_list);
cmd->set_context (layout);
set_doc and show_doc in this function. */
const char *set_doc
= xstrprintf (_("Configure what GDB does when %s is detected."),
- problem->name);
+ problem->name).release ();
const char *show_doc
= xstrprintf (_("Show what GDB does when %s is detected."),
- problem->name);
+ problem->name).release ();
add_setshow_prefix_cmd (problem->name, class_maintenance,
set_doc, show_doc, set_cmd_list, show_cmd_list,
xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
- xtensa_cp[i] = reggroup_new (xstrprintf ("cp%d", i), USER_REGGROUP);
+ xtensa_cp[i] = reggroup_new (xstrprintf ("cp%d", i).release (),
+ USER_REGGROUP);
}
static void
/* Like asprintf/vasprintf but get an internal_error if the call
fails. */
-char *
+gdb::unique_xmalloc_ptr<char>
xstrprintf (const char *format, ...)
{
- char *ret;
va_list args;
va_start (args, format);
- ret = xstrvprintf (format, args);
+ gdb::unique_xmalloc_ptr<char> ret = xstrvprintf (format, args);
va_end (args);
return ret;
}
-char *
+gdb::unique_xmalloc_ptr<char>
xstrvprintf (const char *format, va_list ap)
{
char *ret = NULL;
happen, but just to be sure. */
if (ret == NULL || status < 0)
internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
- return ret;
+ return gdb::unique_xmalloc_ptr<char> (ret);
}
int
#include <string>
#include <vector>
#include "gdbsupport/byte-vector.h"
+#include "gdbsupport/gdb_unique_ptr.h"
#include "poison.h"
/* Like asprintf and vasprintf, but return the string, throw an error
if no memory. */
-char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
-char *xstrvprintf (const char *format, va_list ap)
+gdb::unique_xmalloc_ptr<char> xstrprintf (const char *format, ...)
+ ATTRIBUTE_PRINTF (1, 2);
+gdb::unique_xmalloc_ptr<char> xstrvprintf (const char *format, va_list ap)
ATTRIBUTE_PRINTF (1, 0);
/* Like snprintf, but throw an error if the output buffer is too small. */