+2018-12-27 Tom Tromey <tom@tromey.com>
+
+ * python/python.c (python_interactive_command): Use std::string.
+ (gdbpy_parameter): Likewise.
+ * python/py-utils.c (unicode_to_encoded_string): Update comment.
+ * python/py-symtab.c (salpy_str): Use PyString_FromFormat.
+ * python/py-record-btrace.c (recpy_bt_insn_data): Use
+ byte_vector.
+ * python/py-objfile.c (objfpy_get_build_id): Use
+ unique_xmalloc_ptr.
+ * python/py-inferior.c (infpy_read_memory): Use
+ unique_xmalloc_ptr.
+ * python/py-cmd.c (gdbpy_parse_command_name): Use std::string.
+
2018-12-26 Simon Marchi <simon.marchi@polymtl.ca>
* target.c (target_terminal::restore_inferior): Remove struct keyword.
struct cmd_list_element *elt;
int len = strlen (name);
int i, lastchar;
- char *prefix_text;
const char *prefix_text2;
char *result;
return result;
}
- prefix_text = (char *) xmalloc (i + 2);
- memcpy (prefix_text, name, i + 1);
- prefix_text[i + 1] = '\0';
+ std::string prefix_text (name, i + 1);
- prefix_text2 = prefix_text;
+ prefix_text2 = prefix_text.c_str ();
elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1);
if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
{
PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
- prefix_text);
- xfree (prefix_text);
+ prefix_text.c_str ());
xfree (result);
return NULL;
}
if (elt->prefixlist)
{
- xfree (prefix_text);
*base_list = elt->prefixlist;
return result;
}
PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
- prefix_text);
- xfree (prefix_text);
+ prefix_text.c_str ());
xfree (result);
return NULL;
}
infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
{
CORE_ADDR addr, length;
- gdb_byte *buffer = NULL;
+ gdb::unique_xmalloc_ptr<gdb_byte> buffer;
PyObject *addr_obj, *length_obj, *result;
static const char *keywords[] = { "address", "length", NULL };
TRY
{
- buffer = (gdb_byte *) xmalloc (length);
+ buffer.reset ((gdb_byte *) xmalloc (length));
- read_memory (addr, buffer, length);
+ read_memory (addr, buffer.get (), length);
}
CATCH (except, RETURN_MASK_ALL)
{
- xfree (buffer);
GDB_PY_HANDLE_EXCEPTION (except);
}
END_CATCH
gdbpy_ref<membuf_object> membuf_obj (PyObject_New (membuf_object,
&membuf_object_type));
if (membuf_obj == NULL)
- {
- xfree (buffer);
- return NULL;
- }
+ return NULL;
- membuf_obj->buffer = buffer;
+ membuf_obj->buffer = buffer.release ();
membuf_obj->addr = addr;
membuf_obj->length = length;
if (build_id != NULL)
{
- char *hex_form = make_hex_string (build_id->data, build_id->size);
- PyObject *result;
+ gdb::unique_xmalloc_ptr<char> hex_form
+ (make_hex_string (build_id->data, build_id->size));
- result = host_string_to_python_string (hex_form).release ();
- xfree (hex_form);
- return result;
+ return host_string_to_python_string (hex_form.get ()).release ();
}
Py_RETURN_NONE;
recpy_bt_insn_data (PyObject *self, void *closure)
{
const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
- gdb_byte *buffer = NULL;
+ gdb::byte_vector buffer;
PyObject *object;
if (insn == NULL)
TRY
{
- buffer = (gdb_byte *) xmalloc (insn->size);
- read_memory (insn->pc, buffer, insn->size);
+ buffer.resize (insn->size);
+ read_memory (insn->pc, buffer.data (), insn->size);
}
CATCH (except, RETURN_MASK_ALL)
{
- xfree (buffer);
GDB_PY_HANDLE_EXCEPTION (except);
}
END_CATCH
- object = PyBytes_FromStringAndSize ((const char*) buffer, insn->size);
- xfree (buffer);
+ object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
+ insn->size);
if (object == NULL)
return NULL;
static PyObject *
salpy_str (PyObject *self)
{
- char *s;
const char *filename;
sal_object *sal_obj;
- PyObject *result;
struct symtab_and_line *sal = NULL;
SALPY_REQUIRE_VALID (self, sal);
filename = (sal_obj->symtab == (symtab_object *) Py_None)
? "<unknown>" : symtab_to_filename_for_display (sal_obj->symtab->symtab);
- s = xstrprintf ("symbol and line for %s, line %d", filename,
- sal->line);
-
- result = PyString_FromString (s);
- xfree (s);
-
- return result;
+ return PyString_FromFormat ("symbol and line for %s, line %d", filename,
+ sal->line);
}
static void
/* Returns a newly allocated string with the contents of the given unicode
string object converted to CHARSET. If an error occurs during the
- conversion, NULL will be returned and a python exception will be set.
-
- The caller is responsible for xfree'ing the string. */
+ conversion, NULL will be returned and a python exception will be
+ set. */
static gdb::unique_xmalloc_ptr<char>
unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
{
if (arg && *arg)
{
- int len = strlen (arg);
- char *script = (char *) xmalloc (len + 2);
-
- strcpy (script, arg);
- script[len] = '\n';
- script[len + 1] = '\0';
- err = eval_python_command (script);
- xfree (script);
+ std::string script = std::string (arg) + "\n";
+ err = eval_python_command (script.c_str ());
}
else
{
static PyObject *
gdbpy_parameter (PyObject *self, PyObject *args)
{
- struct gdb_exception except = exception_none;
struct cmd_list_element *alias, *prefix, *cmd;
const char *arg;
- char *newarg;
int found = -1;
if (! PyArg_ParseTuple (args, "s", &arg))
return NULL;
- newarg = concat ("show ", arg, (char *) NULL);
+ std::string newarg = std::string ("show ") + arg;
TRY
{
- found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd);
+ found = lookup_cmd_composition (newarg.c_str (), &alias, &prefix, &cmd);
}
CATCH (ex, RETURN_MASK_ALL)
{
- except = ex;
+ GDB_PY_HANDLE_EXCEPTION (ex);
}
END_CATCH
- xfree (newarg);
- GDB_PY_HANDLE_EXCEPTION (except);
if (!found)
return PyErr_Format (PyExc_RuntimeError,
_("Could not find parameter `%s'."), arg);