aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/py-breakpoint.c15
-rw-r--r--gdb/python/py-cmd.c66
-rw-r--r--gdb/python/py-param.c24
-rw-r--r--gdb/python/py-type.c8
-rw-r--r--gdb/python/py-unwind.c2
-rw-r--r--gdb/python/py-varobj.c9
-rw-r--r--gdb/python/python.c67
7 files changed, 72 insertions, 119 deletions
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 64de803..6156eb6 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1026,15 +1026,12 @@ local_setattro (PyObject *self, PyObject *name, PyObject *v)
extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
if (extlang != NULL)
{
- char *error_text;
-
- error_text
- = xstrprintf (_("Only one stop condition allowed. There is"
- " currently a %s stop condition defined for"
- " this breakpoint."),
- ext_lang_capitalized_name (extlang));
- PyErr_SetString (PyExc_RuntimeError, error_text);
- xfree (error_text);
+ std::string error_text
+ = string_printf (_("Only one stop condition allowed. There is"
+ " currently a %s stop condition defined for"
+ " this breakpoint."),
+ ext_lang_capitalized_name (extlang));
+ PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
return -1;
}
}
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index 6203211..2a7c613 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -44,7 +44,7 @@ static const struct cmdpy_completer completers[] =
{ "COMPLETE_FILENAME", filename_completer },
{ "COMPLETE_LOCATION", location_completer },
{ "COMPLETE_COMMAND", command_completer },
- { "COMPLETE_SYMBOL", make_symbol_completion_list_fn },
+ { "COMPLETE_SYMBOL", symbol_completer },
{ "COMPLETE_EXPRESSION", expression_completer },
};
@@ -242,10 +242,21 @@ cmdpy_completer_helper (struct cmd_list_element *command,
NULL));
if (textobj == NULL)
error (_("Could not convert argument to Python string."));
- gdbpy_ref<> wordobj (PyUnicode_Decode (word, strlen (word), host_charset (),
- NULL));
- if (wordobj == NULL)
- error (_("Could not convert argument to Python string."));
+
+ gdbpy_ref<> wordobj;
+ if (word == NULL)
+ {
+ /* "brkchars" phase. */
+ wordobj.reset (Py_None);
+ Py_INCREF (Py_None);
+ }
+ else
+ {
+ wordobj.reset (PyUnicode_Decode (word, strlen (word), host_charset (),
+ NULL));
+ if (wordobj == NULL)
+ error (_("Could not convert argument to Python string."));
+ }
gdbpy_ref<> resultobj (PyObject_CallMethodObjArgs ((PyObject *) obj,
complete_cst,
@@ -267,6 +278,7 @@ cmdpy_completer_helper (struct cmd_list_element *command,
static void
cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
+ completion_tracker &tracker,
const char *text, const char *word)
{
gdbpy_enter enter_py (get_current_arch (), current_language);
@@ -293,23 +305,25 @@ cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
}
else if (value >= 0 && value < (long) N_COMPLETERS)
{
+ completer_handle_brkchars_ftype *brkchars_fn;
+
/* This is the core of this function. Depending on which
completer type the Python function returns, we have to
adjust the break characters accordingly. */
- set_gdb_completion_word_break_characters
- (completers[value].completer);
+ brkchars_fn = (completer_handle_brkchars_func_for_completer
+ (completers[value].completer));
+ brkchars_fn (command, tracker, text, word);
}
}
}
/* Called by gdb for command completion. */
-static VEC (char_ptr) *
+static void
cmdpy_completer (struct cmd_list_element *command,
+ completion_tracker &tracker,
const char *text, const char *word)
{
- VEC (char_ptr) *result = NULL;
-
gdbpy_enter enter_py (get_current_arch (), current_language);
/* Calling our helper to obtain the PyObject of the Python
@@ -317,12 +331,10 @@ cmdpy_completer (struct cmd_list_element *command,
gdbpy_ref<> resultobj (cmdpy_completer_helper (command, text, word));
/* If the result object of calling the Python function is NULL, it
- means that there was an error. In this case, just give up and
- return NULL. */
+ means that there was an error. In this case, just give up. */
if (resultobj == NULL)
- return NULL;
+ return;
- result = NULL;
if (PyInt_Check (resultobj.get ()))
{
/* User code may also return one of the completion constants,
@@ -335,15 +347,16 @@ cmdpy_completer (struct cmd_list_element *command,
PyErr_Clear ();
}
else if (value >= 0 && value < (long) N_COMPLETERS)
- result = completers[value].completer (command, text, word);
+ completers[value].completer (command, tracker, text, word);
}
else
{
gdbpy_ref<> iter (PyObject_GetIter (resultobj.get ()));
if (iter == NULL)
- return NULL;
+ return;
+ bool got_matches = false;
while (true)
{
gdbpy_ref<> elt (PyIter_Next (iter.get ()));
@@ -363,16 +376,15 @@ cmdpy_completer (struct cmd_list_element *command,
PyErr_Clear ();
continue;
}
- VEC_safe_push (char_ptr, result, item.release ());
+ tracker.add_completion (std::move (item));
+ got_matches = true;
}
/* If we got some results, ignore problems. Otherwise, report
the problem. */
- if (result != NULL && PyErr_Occurred ())
+ if (got_matches && PyErr_Occurred ())
PyErr_Clear ();
}
-
- return result;
}
/* Helper for cmdpy_init which locates the command list to use and
@@ -763,22 +775,16 @@ gdbpy_string_to_argv (PyObject *self, PyObject *args)
if (*input != '\0')
{
- char **c_argv = gdb_buildargv (input);
- int i;
+ gdb_argv c_argv (input);
- for (i = 0; c_argv[i] != NULL; ++i)
+ for (char *arg : c_argv)
{
- gdbpy_ref<> argp (PyString_FromString (c_argv[i]));
+ gdbpy_ref<> argp (PyString_FromString (arg));
if (argp == NULL
|| PyList_Append (py_argv.get (), argp.get ()) < 0)
- {
- freeargv (c_argv);
- return NULL;
- }
+ return NULL;
}
-
- freeargv (c_argv);
}
return py_argv.release ();
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index f0d3423..455c99e 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -555,7 +555,6 @@ static int
compute_enum_values (parmpy_object *self, PyObject *enum_values)
{
Py_ssize_t size, i;
- struct cleanup *back_to;
if (! enum_values)
{
@@ -581,36 +580,27 @@ compute_enum_values (parmpy_object *self, PyObject *enum_values)
return 0;
}
- self->enumeration = XCNEWVEC (const char *, size + 1);
- back_to = make_cleanup (free_current_contents, &self->enumeration);
+ gdb_argv holder (XCNEWVEC (char *, size + 1));
+ char **enumeration = holder.get ();
for (i = 0; i < size; ++i)
{
gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
if (item == NULL)
- {
- do_cleanups (back_to);
- return 0;
- }
+ return 0;
if (! gdbpy_is_string (item.get ()))
{
- do_cleanups (back_to);
PyErr_SetString (PyExc_RuntimeError,
_("The enumeration item not a string."));
return 0;
}
- self->enumeration[i]
- = python_string_to_host_string (item.get ()).release ();
- if (self->enumeration[i] == NULL)
- {
- do_cleanups (back_to);
- return 0;
- }
- make_cleanup (xfree, (char *) self->enumeration[i]);
+ enumeration[i] = python_string_to_host_string (item.get ()).release ();
+ if (enumeration[i] == NULL)
+ return 0;
}
- discard_cleanups (back_to);
+ self->enumeration = const_cast<const char**> (holder.release ());
return 1;
}
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index aa20d4c..51184ca 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -761,7 +761,6 @@ typy_lookup_type (struct demangle_component *demangled,
const struct block *block)
{
struct type *type, *rtype = NULL;
- char *type_name = NULL;
enum demangle_component_type demangled_type;
/* Save the type: typy_lookup_type() may (indirectly) overwrite
@@ -816,11 +815,8 @@ typy_lookup_type (struct demangle_component *demangled,
return rtype;
/* We don't have a type, so lookup the type. */
- type_name = cp_comp_to_string (demangled, 10);
- type = typy_lookup_typename (type_name, block);
- xfree (type_name);
-
- return type;
+ gdb::unique_xmalloc_ptr<char> type_name = cp_comp_to_string (demangled, 10);
+ return typy_lookup_typename (type_name.get (), block);
}
/* This is a helper function for typy_template_argument that is used
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 1d800a7..acfce7d 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -595,7 +595,7 @@ pyuw_dealloc_cache (struct frame_info *this_frame, void *cache)
TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__);
cached_frame_info *cached_frame = (cached_frame_info *) cache;
- for (int i = 0; cached_frame->reg_count; i++)
+ for (int i = 0; i < cached_frame->reg_count; i++)
xfree (cached_frame->reg[i].data);
xfree (cache);
diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c
index e858556..5f6ab64 100644
--- a/gdb/python/py-varobj.c
+++ b/gdb/python/py-varobj.c
@@ -71,7 +71,6 @@ py_varobj_iter_next (struct varobj_iter *self)
if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
{
PyObject *type, *value, *trace;
- char *name_str;
PyErr_Fetch (&type, &value, &trace);
gdb::unique_xmalloc_ptr<char>
@@ -85,10 +84,10 @@ py_varobj_iter_next (struct varobj_iter *self)
return NULL;
}
- name_str = xstrprintf ("<error at %d>",
- self->next_raw_index++);
- item.reset (Py_BuildValue ("(ss)", name_str, value_str.get ()));
- xfree (name_str);
+ std::string name_str = string_printf ("<error at %d>",
+ self->next_raw_index++);
+ item.reset (Py_BuildValue ("(ss)", name_str.c_str (),
+ value_str.get ()));
if (item == NULL)
{
gdbpy_print_stack ();
diff --git a/gdb/python/python.c b/gdb/python/python.c
index be92f36..c53e10f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -365,32 +365,19 @@ python_run_simple_file (FILE *file, const char *filename)
}
/* Given a command_line, return a command string suitable for passing
- to Python. Lines in the string are separated by newlines. The
- return value is allocated using xmalloc and the caller is
- responsible for freeing it. */
+ to Python. Lines in the string are separated by newlines. */
-static char *
+static std::string
compute_python_string (struct command_line *l)
{
struct command_line *iter;
- char *script = NULL;
- int size = 0;
- int here;
+ std::string script;
for (iter = l; iter; iter = iter->next)
- size += strlen (iter->line) + 1;
-
- script = (char *) xmalloc (size + 1);
- here = 0;
- for (iter = l; iter; iter = iter->next)
{
- int len = strlen (iter->line);
-
- strcpy (&script[here], iter->line);
- here += len;
- script[here++] = '\n';
+ script += iter->line;
+ script += '\n';
}
- script[here] = '\0';
return script;
}
@@ -402,16 +389,14 @@ gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
struct command_line *cmd)
{
int ret;
- char *script;
if (cmd->body_count != 1)
error (_("Invalid \"python\" block structure."));
gdbpy_enter enter_py (get_current_arch (), current_language);
- script = compute_python_string (cmd->body_list[0]);
- ret = PyRun_SimpleString (script);
- xfree (script);
+ std::string script = compute_python_string (cmd->body_list[0]);
+ ret = PyRun_SimpleString (script.c_str ());
if (ret)
error (_("Error while executing Python code."));
}
@@ -667,7 +652,6 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
appease gcc. */
struct symtab_and_line sal;
char *arg = NULL;
- struct cleanup *cleanups;
gdbpy_ref<> result;
gdbpy_ref<> unparsed;
event_location_up location;
@@ -675,8 +659,6 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
if (! PyArg_ParseTuple (args, "|s", &arg))
return NULL;
- cleanups = make_cleanup (null_cleanup, NULL);
-
sals.sals = NULL;
if (arg != NULL)
@@ -700,12 +682,13 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
}
END_CATCH
+ /* Ensure that the sals data is freed, when needed. */
+ gdb::unique_xmalloc_ptr<struct symtab_and_line> free_sals;
if (sals.sals != NULL && sals.sals != &sal)
- make_cleanup (xfree, sals.sals);
+ free_sals.reset (sals.sals);
if (except.reason < 0)
{
- do_cleanups (cleanups);
/* We know this will always throw. */
gdbpy_convert_exception (except);
return NULL;
@@ -717,20 +700,14 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
result.reset (PyTuple_New (sals.nelts));
if (result == NULL)
- {
- do_cleanups (cleanups);
- return NULL;
- }
+ return NULL;
for (i = 0; i < sals.nelts; ++i)
{
PyObject *obj;
obj = symtab_and_line_to_sal_object (sals.sals[i]);
if (! obj)
- {
- do_cleanups (cleanups);
- return NULL;
- }
+ return NULL;
PyTuple_SetItem (result.get (), i, obj);
}
@@ -743,19 +720,13 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
gdbpy_ref<> return_result (PyTuple_New (2));
if (return_result == NULL)
- {
- do_cleanups (cleanups);
- return NULL;
- }
+ return NULL;
if (arg != NULL && strlen (arg) > 0)
{
unparsed.reset (PyString_FromString (arg));
if (unparsed == NULL)
- {
- do_cleanups (cleanups);
- return NULL;
- }
+ return NULL;
}
else
{
@@ -766,8 +737,6 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
PyTuple_SetItem (return_result.get (), 1, result.release ());
- do_cleanups (cleanups);
-
return return_result.release ();
}
@@ -1532,7 +1501,6 @@ do_start_initialization ()
#ifdef IS_PY3K
int i;
size_t progsize, count;
- char *oldloc;
wchar_t *progname_copy;
#endif
@@ -1546,25 +1514,22 @@ do_start_initialization ()
progname = concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin",
SLASH_STRING, "python", (char *) NULL);
#ifdef IS_PY3K
- oldloc = xstrdup (setlocale (LC_ALL, NULL));
+ std::string oldloc = setlocale (LC_ALL, NULL);
setlocale (LC_ALL, "");
progsize = strlen (progname);
progname_copy = (wchar_t *) PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
if (!progname_copy)
{
- xfree (oldloc);
fprintf (stderr, "out of memory\n");
return false;
}
count = mbstowcs (progname_copy, progname, progsize + 1);
if (count == (size_t) -1)
{
- xfree (oldloc);
fprintf (stderr, "Could not convert python path to string\n");
return false;
}
- setlocale (LC_ALL, oldloc);
- xfree (oldloc);
+ setlocale (LC_ALL, oldloc.c_str ());
/* Note that Py_SetProgramName expects the string it is passed to
remain alive for the duration of the program's execution, so