2008-08-07 03:41:33 +08:00
|
|
|
|
/* General utility routines for GDB/Python.
|
|
|
|
|
|
2021-01-01 16:03:39 +08:00
|
|
|
|
Copyright (C) 2008-2021 Free Software Foundation, Inc.
|
2008-08-07 03:41:33 +08:00
|
|
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
#include "charset.h"
|
2010-06-29 05:16:04 +08:00
|
|
|
|
#include "value.h"
|
2008-08-07 03:41:33 +08:00
|
|
|
|
#include "python-internal.h"
|
|
|
|
|
|
|
|
|
|
/* Converts a Python 8-bit string to a unicode string object. Assumes the
|
|
|
|
|
8-bit string is in the host charset. If an error occurs during conversion,
|
|
|
|
|
returns NULL with a python exception set.
|
|
|
|
|
|
|
|
|
|
As an added bonus, the functions accepts a unicode string and returns it
|
|
|
|
|
right away, so callers don't need to check which kind of string they've
|
2013-11-30 04:00:47 +08:00
|
|
|
|
got. In Python 3, all strings are Unicode so this case is always the
|
2012-12-13 00:47:30 +08:00
|
|
|
|
one that applies.
|
2008-08-07 03:41:33 +08:00
|
|
|
|
|
|
|
|
|
If the given object is not one of the mentioned string types, NULL is
|
|
|
|
|
returned, with the TypeError python exception set. */
|
2018-10-25 06:40:00 +08:00
|
|
|
|
gdbpy_ref<>
|
2008-08-07 03:41:33 +08:00
|
|
|
|
python_string_to_unicode (PyObject *obj)
|
|
|
|
|
{
|
|
|
|
|
PyObject *unicode_str;
|
|
|
|
|
|
|
|
|
|
/* If obj is already a unicode string, just return it.
|
|
|
|
|
I wish life was always that simple... */
|
|
|
|
|
if (PyUnicode_Check (obj))
|
2009-02-27 04:45:21 +08:00
|
|
|
|
{
|
|
|
|
|
unicode_str = obj;
|
|
|
|
|
Py_INCREF (obj);
|
|
|
|
|
}
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#ifndef IS_PY3K
|
2008-08-07 03:41:33 +08:00
|
|
|
|
else if (PyString_Check (obj))
|
|
|
|
|
unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#endif
|
2008-08-07 03:41:33 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PyErr_SetString (PyExc_TypeError,
|
|
|
|
|
_("Expected a string or unicode object."));
|
|
|
|
|
unicode_str = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 06:40:00 +08:00
|
|
|
|
return gdbpy_ref<> (unicode_str);
|
2008-08-07 03:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns a newly allocated string with the contents of the given unicode
|
2009-02-05 05:55:40 +08:00
|
|
|
|
string object converted to CHARSET. If an error occurs during the
|
2018-12-27 02:05:57 +08:00
|
|
|
|
conversion, NULL will be returned and a python exception will be
|
|
|
|
|
set. */
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
static gdb::unique_xmalloc_ptr<char>
|
2009-02-05 05:55:40 +08:00
|
|
|
|
unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
|
2008-08-07 03:41:33 +08:00
|
|
|
|
{
|
2009-02-05 05:55:40 +08:00
|
|
|
|
/* Translate string to named charset. */
|
Turn gdbpy_ref into a template
This turns gdbpy_ref into a template class, so that it can be used to
wrap subclasses of PyObject. The default argument remains PyObject;
and this necessitated renaming uses of "gdbpy_ref" to "gdbpy_ref<>".
gdb/ChangeLog
2017-02-10 Tom Tromey <tom@tromey.com>
* python/py-ref.h (gdbpy_ref_policy): Now a template.
(gdbpy_ref): Now a template; allow subclasses of PyObject to be
used.
* python/py-arch.c, python/py-bpevent.c, python/py-breakpoint.c,
python/py-cmd.c, python/py-continueevent.c, python/py-event.c,
python/py-exitedevent.c, python/py-finishbreakpoint.c,
python/py-framefilter.c, python/py-function.c,
python/py-inferior.c, python/py-infevents.c,
python/py-linetable.c, python/py-newobjfileevent.c,
python/py-param.c, python/py-prettyprint.c, python/py-ref.h,
python/py-signalevent.c, python/py-stopevent.c,
python/py-symbol.c, python/py-threadevent.c, python/py-type.c,
python/py-unwind.c, python/py-utils.c, python/py-value.c,
python/py-varobj.c, python/py-xmethods.c, python/python.c,
varobj.c: Change gdbpy_ref to gdbpy_ref<>.
2017-02-10 04:16:36 +08:00
|
|
|
|
gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
|
2008-08-07 03:41:33 +08:00
|
|
|
|
if (string == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2019-03-06 05:58:24 +08:00
|
|
|
|
return gdb::unique_xmalloc_ptr<char>
|
|
|
|
|
(xstrdup (PyBytes_AsString (string.get ())));
|
2009-02-05 05:55:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-10 18:35:17 +08:00
|
|
|
|
/* Returns a PyObject with the contents of the given unicode string
|
|
|
|
|
object converted to a named charset. If an error occurs during
|
|
|
|
|
the conversion, NULL will be returned and a python exception will
|
|
|
|
|
be set. */
|
2018-10-25 06:40:00 +08:00
|
|
|
|
static gdbpy_ref<>
|
2009-07-10 18:35:17 +08:00
|
|
|
|
unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
|
|
|
|
|
{
|
|
|
|
|
/* Translate string to named charset. */
|
2018-10-25 06:40:00 +08:00
|
|
|
|
return gdbpy_ref<> (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
|
2009-07-10 18:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
/* Returns a newly allocated string with the contents of the given
|
|
|
|
|
unicode string object converted to the target's charset. If an
|
|
|
|
|
error occurs during the conversion, NULL will be returned and a
|
|
|
|
|
python exception will be set. */
|
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
2009-02-05 05:55:40 +08:00
|
|
|
|
unicode_to_target_string (PyObject *unicode_str)
|
|
|
|
|
{
|
2010-03-06 04:18:19 +08:00
|
|
|
|
return unicode_to_encoded_string (unicode_str,
|
|
|
|
|
target_charset (python_gdbarch));
|
2008-08-07 03:41:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-10 18:35:17 +08:00
|
|
|
|
/* Returns a PyObject with the contents of the given unicode string
|
|
|
|
|
object converted to the target's charset. If an error occurs
|
|
|
|
|
during the conversion, NULL will be returned and a python exception
|
|
|
|
|
will be set. */
|
2018-10-25 06:40:00 +08:00
|
|
|
|
static gdbpy_ref<>
|
2009-07-10 18:35:17 +08:00
|
|
|
|
unicode_to_target_python_string (PyObject *unicode_str)
|
|
|
|
|
{
|
2010-03-06 04:18:19 +08:00
|
|
|
|
return unicode_to_encoded_python_string (unicode_str,
|
|
|
|
|
target_charset (python_gdbarch));
|
2009-07-10 18:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-07 03:41:33 +08:00
|
|
|
|
/* Converts a python string (8-bit or unicode) to a target string in
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
the target's charset. Returns NULL on error, with a python
|
|
|
|
|
exception set. */
|
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
2008-08-07 03:41:33 +08:00
|
|
|
|
python_string_to_target_string (PyObject *obj)
|
|
|
|
|
{
|
2018-10-25 06:40:00 +08:00
|
|
|
|
gdbpy_ref<> str = python_string_to_unicode (obj);
|
2008-08-07 03:41:33 +08:00
|
|
|
|
if (str == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2016-11-21 01:52:25 +08:00
|
|
|
|
return unicode_to_target_string (str.get ());
|
2008-08-07 03:41:33 +08:00
|
|
|
|
}
|
2009-02-05 05:55:40 +08:00
|
|
|
|
|
2009-07-10 18:35:17 +08:00
|
|
|
|
/* Converts a python string (8-bit or unicode) to a target string in the
|
|
|
|
|
target's charset. Returns NULL on error, with a python exception
|
2012-12-13 00:47:30 +08:00
|
|
|
|
set.
|
|
|
|
|
|
|
|
|
|
In Python 3, the returned object is a "bytes" object (not a string). */
|
2018-10-25 06:40:00 +08:00
|
|
|
|
gdbpy_ref<>
|
2009-07-10 18:35:17 +08:00
|
|
|
|
python_string_to_target_python_string (PyObject *obj)
|
|
|
|
|
{
|
2018-10-25 06:40:00 +08:00
|
|
|
|
gdbpy_ref<> str = python_string_to_unicode (obj);
|
2009-07-10 18:35:17 +08:00
|
|
|
|
if (str == NULL)
|
2018-10-25 06:40:00 +08:00
|
|
|
|
return str;
|
2009-07-10 18:35:17 +08:00
|
|
|
|
|
2016-11-21 01:52:25 +08:00
|
|
|
|
return unicode_to_target_python_string (str.get ());
|
2009-07-10 18:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 05:55:40 +08:00
|
|
|
|
/* Converts a python string (8-bit or unicode) to a target string in
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
the host's charset. Returns NULL on error, with a python exception
|
|
|
|
|
set. */
|
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
2009-02-05 05:55:40 +08:00
|
|
|
|
python_string_to_host_string (PyObject *obj)
|
|
|
|
|
{
|
2018-10-25 06:40:00 +08:00
|
|
|
|
gdbpy_ref<> str = python_string_to_unicode (obj);
|
2009-02-05 05:55:40 +08:00
|
|
|
|
if (str == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2016-11-21 01:52:25 +08:00
|
|
|
|
return unicode_to_encoded_string (str.get (), host_charset ());
|
2009-02-05 05:55:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 14:48:35 +08:00
|
|
|
|
/* Convert a host string to a python string. */
|
|
|
|
|
|
2018-10-25 06:40:00 +08:00
|
|
|
|
gdbpy_ref<>
|
2016-03-30 14:48:35 +08:00
|
|
|
|
host_string_to_python_string (const char *str)
|
|
|
|
|
{
|
2018-10-25 06:40:00 +08:00
|
|
|
|
return gdbpy_ref<> (PyString_Decode (str, strlen (str), host_charset (),
|
|
|
|
|
NULL));
|
2016-03-30 14:48:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 05:55:40 +08:00
|
|
|
|
/* Return true if OBJ is a Python string or unicode object, false
|
|
|
|
|
otherwise. */
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
gdbpy_is_string (PyObject *obj)
|
|
|
|
|
{
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#ifdef IS_PY3K
|
|
|
|
|
return PyUnicode_Check (obj);
|
|
|
|
|
#else
|
2009-02-05 05:55:40 +08:00
|
|
|
|
return PyString_Check (obj) || PyUnicode_Check (obj);
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#endif
|
2009-02-05 05:55:40 +08:00
|
|
|
|
}
|
2010-05-25 23:27:17 +08:00
|
|
|
|
|
|
|
|
|
/* Return the string representation of OBJ, i.e., str (obj).
|
|
|
|
|
If the result is NULL a python error occurred, the caller must clear it. */
|
|
|
|
|
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
2010-05-25 23:27:17 +08:00
|
|
|
|
gdbpy_obj_to_string (PyObject *obj)
|
|
|
|
|
{
|
Turn gdbpy_ref into a template
This turns gdbpy_ref into a template class, so that it can be used to
wrap subclasses of PyObject. The default argument remains PyObject;
and this necessitated renaming uses of "gdbpy_ref" to "gdbpy_ref<>".
gdb/ChangeLog
2017-02-10 Tom Tromey <tom@tromey.com>
* python/py-ref.h (gdbpy_ref_policy): Now a template.
(gdbpy_ref): Now a template; allow subclasses of PyObject to be
used.
* python/py-arch.c, python/py-bpevent.c, python/py-breakpoint.c,
python/py-cmd.c, python/py-continueevent.c, python/py-event.c,
python/py-exitedevent.c, python/py-finishbreakpoint.c,
python/py-framefilter.c, python/py-function.c,
python/py-inferior.c, python/py-infevents.c,
python/py-linetable.c, python/py-newobjfileevent.c,
python/py-param.c, python/py-prettyprint.c, python/py-ref.h,
python/py-signalevent.c, python/py-stopevent.c,
python/py-symbol.c, python/py-threadevent.c, python/py-type.c,
python/py-unwind.c, python/py-utils.c, python/py-value.c,
python/py-varobj.c, python/py-xmethods.c, python/python.c,
varobj.c: Change gdbpy_ref to gdbpy_ref<>.
2017-02-10 04:16:36 +08:00
|
|
|
|
gdbpy_ref<> str_obj (PyObject_Str (obj));
|
2010-05-25 23:27:17 +08:00
|
|
|
|
|
|
|
|
|
if (str_obj != NULL)
|
|
|
|
|
{
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
gdb::unique_xmalloc_ptr<char> msg;
|
|
|
|
|
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#ifdef IS_PY3K
|
2016-11-21 01:52:25 +08:00
|
|
|
|
msg = python_string_to_host_string (str_obj.get ());
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#else
|
2016-11-21 01:52:25 +08:00
|
|
|
|
msg.reset (xstrdup (PyString_AsString (str_obj.get ())));
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#endif
|
2010-05-25 23:27:17 +08:00
|
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 02:32:01 +08:00
|
|
|
|
/* See python-internal.h. */
|
2010-05-25 23:27:17 +08:00
|
|
|
|
|
Use unique_xmalloc_ptr in Python code
This changes some utility functions in the Python code to return
unique_xmalloc_ptr, and then fixes up the callers.
I chose unique_xmalloc_ptr rather than std::string because at a few
call points the xmalloc'd string is released and ownership transferred
elsewhere.
This patch found a few existing memory leaks. For example,
py-unwind.c called gdbpy_obj_to_string but never freed the result.
Built and regression tested on the buildbot.
2016-11-09 Tom Tromey <tom@tromey.com>
* varobj.h (varobj_get_display_hint): Change return type.
* varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr.
(varobj_value_get_print_value): Update.
* python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack)
(gdbpy_apply_type_printers): Update.
* python/python-internal.h (unicode_to_target_string)
(python_string_to_target_string, python_string_to_host_string)
(gdbpy_obj_to_string, gdbpy_exception_to_string)
(gdbpy_get_display_hint): Change return types.
* python/py-varobj.c (py_varobj_iter_next): Update.
* python/py-value.c (valpy_getitem, convert_value_from_python):
Update.
* python/py-utils.c (unicode_to_encoded_string)
(unicode_to_target_string, python_string_to_target_string)
(python_string_to_host_string, gdbpy_obj_to_string)
(gdbpy_exception_to_string): Return unique_xmalloc_ptr.
* python/py-unwind.c (pyuw_parse_register_id): Update.
* python/py-type.c (typy_getitem): Update.
* python/py-prettyprint.c (gdbpy_get_display_hint)
(print_stack_unless_memory_error, print_children)
(gdbpy_apply_val_pretty_printer): Update.
* python/py-param.c (set_parameter_value): Update.
(get_doc_string, call_doc_function): Return unique_xmalloc_ptr.
(get_set_value, get_show_value, compute_enum_values, parmpy_init):
Update.
* python/py-infthread.c (thpy_set_name): Update.
* python/py-function.c (fnpy_call, fnpy_init): Update.
* python/py-framefilter.c (extract_sym): Change "name" to
unique_xmalloc_ptr.
(enumerate_args, enumerate_locals): Update.
(py_print_frame): Use unique_xmalloc_ptr.
* python/py-frame.c (frapy_read_var): Update. Remove cleanup.
* python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init):
Update.
* python/py-breakpoint.c (bppy_set_condition): Use
unique_xmalloc_ptr.
(bppy_init): Likewise. Remove cleanup.
(local_setattro): Update.
* mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children)
(varobj_update_one): Update.
2016-10-15 23:20:02 +08:00
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
2018-12-28 02:32:01 +08:00
|
|
|
|
gdbpy_err_fetch::to_string () const
|
2010-05-25 23:27:17 +08:00
|
|
|
|
{
|
|
|
|
|
/* There are a few cases to consider.
|
|
|
|
|
For example:
|
2018-12-28 02:32:01 +08:00
|
|
|
|
value is a string when PyErr_SetString is used.
|
|
|
|
|
value is not a string when raise "foo" is used, instead it is None
|
|
|
|
|
and type is "foo".
|
|
|
|
|
So the algorithm we use is to print `str (value)' if it's not
|
|
|
|
|
None, otherwise we print `str (type)'.
|
2010-05-25 23:27:17 +08:00
|
|
|
|
Using str (aka PyObject_Str) will fetch the error message from
|
|
|
|
|
gdb.GdbError ("message"). */
|
|
|
|
|
|
2018-12-28 02:32:01 +08:00
|
|
|
|
if (m_error_value && m_error_value != Py_None)
|
|
|
|
|
return gdbpy_obj_to_string (m_error_value);
|
2010-05-25 23:27:17 +08:00
|
|
|
|
else
|
2018-12-28 02:32:01 +08:00
|
|
|
|
return gdbpy_obj_to_string (m_error_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* See python-internal.h. */
|
|
|
|
|
|
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
|
|
|
|
gdbpy_err_fetch::type_to_string () const
|
|
|
|
|
{
|
|
|
|
|
return gdbpy_obj_to_string (m_error_type);
|
2010-05-25 23:27:17 +08:00
|
|
|
|
}
|
2010-06-29 05:16:04 +08:00
|
|
|
|
|
2010-11-13 04:49:43 +08:00
|
|
|
|
/* Convert a GDB exception to the appropriate Python exception.
|
2013-11-30 04:00:47 +08:00
|
|
|
|
|
2013-05-21 04:19:03 +08:00
|
|
|
|
This sets the Python error indicator. */
|
2010-11-13 04:49:43 +08:00
|
|
|
|
|
2013-05-21 04:19:03 +08:00
|
|
|
|
void
|
Make exception handling more efficient
This makes exception handling more efficient in a few spots, through
the use of const- and rvalue-references.
I wrote this patch by commenting out the gdb_exception copy
constructor and then examining the resulting error messages one by
one, introducing the use of std::move where appropriate.
gdb/ChangeLog
2019-04-25 Tom Tromey <tromey@adacore.com>
* xml-support.c (struct gdb_xml_parser) <set_error>: Take an
rvalue reference.
(gdb_xml_start_element_wrapper, gdb_xml_end_element_wrapper)
(gdb_xml_parser::parse): Use std::move.
* python/python-internal.h (gdbpy_convert_exception): Take a const
reference.
* python/py-value.c (valpy_getitem, valpy_nonzero): Use
std::move.
* python/py-utils.c (gdbpy_convert_exception): Take a const
reference.
* python/py-inferior.c (infpy_write_memory, infpy_search_memory):
Use std::move.
* python/py-breakpoint.c (bppy_set_condition, bppy_set_commands):
Use std::move.
* mi/mi-main.c (mi_print_exception): Take a const reference.
* main.c (handle_command_errors): Take a const reference.
* linespec.c (parse_linespec): Use std::move.
* infcall.c (run_inferior_call): Use std::move.
(call_function_by_hand_dummy): Use std::move.
* exec.c (try_open_exec_file): Use std::move.
* exceptions.h (exception_print, exception_fprintf)
(exception_print_same): Update.
* exceptions.c (print_exception, exception_print)
(exception_fprintf, exception_print_same): Change parameters to
const reference.
* event-top.c (gdb_rl_callback_read_char_wrapper): Update.
* common/new-op.c: Use std::move.
* common/common-exceptions.h (struct gdb_exception): Add move
constructor.
(struct gdb_exception_error, struct gdb_exception_quit, struct
gdb_quit_bad_alloc): Change constructor to move constructor.
(throw_exception): Change parameter to rvalue reference.
* common/common-exceptions.c (throw_exception): Take rvalue
reference.
* cli/cli-interp.c (safe_execute_command): Use std::move.
* breakpoint.c (insert_bp_location, location_to_sals): Use
std::move.
2019-04-24 20:50:06 +08:00
|
|
|
|
gdbpy_convert_exception (const struct gdb_exception &exception)
|
2010-11-13 04:49:43 +08:00
|
|
|
|
{
|
|
|
|
|
PyObject *exc_class;
|
|
|
|
|
|
|
|
|
|
if (exception.reason == RETURN_QUIT)
|
|
|
|
|
exc_class = PyExc_KeyboardInterrupt;
|
|
|
|
|
else if (exception.error == MEMORY_ERROR)
|
|
|
|
|
exc_class = gdbpy_gdb_memory_error;
|
|
|
|
|
else
|
|
|
|
|
exc_class = gdbpy_gdb_error;
|
|
|
|
|
|
Make exceptions use std::string and be self-managing
This changes the exception's "message" member to be a shared_ptr
wrapping a std::string. This allows removing the stack of exception
messages, because now exceptions will self-destruct when needed. This
also adds a noexcept copy constructor and operator= to gdb_exception,
plus a "what" method.
gdb/ChangeLog
2019-04-08 Tom Tromey <tom@tromey.com>
* xml-support.c (gdb_xml_parser::parse): Update.
* x86-linux-nat.c (x86_linux_nat_target::enable_btrace): Update.
* value.c (show_convenience): Update.
* unittests/cli-utils-selftests.c (test_number_or_range_parser)
(test_parse_flags_qcs): Update.
* thread.c (thr_try_catch_cmd): Update.
* target.c (target_translate_tls_address): Update.
* stack.c (print_frame_arg, read_frame_local, read_frame_arg)
(info_frame_command_core, frame_apply_command_count): Update.
* rust-exp.y (rust_lex_exception_test): Update.
* riscv-tdep.c (riscv_print_one_register_info): Update.
* remote.c (remote_target::enable_btrace): Update.
* record-btrace.c (record_btrace_enable_warn): Update.
* python/py-utils.c (gdbpy_convert_exception): Update.
* printcmd.c (do_one_display, print_variable_and_value): Update.
* mi/mi-main.c (mi_print_exception): Update.
* mi/mi-interp.c (mi_cmd_interpreter_exec): Use SCOPE_EXIT.
* mi/mi-cmd-stack.c (list_arg_or_local): Update.
* linux-nat.c (linux_nat_target::attach): Update.
* linux-fork.c (class scoped_switch_fork_info): Update.
* infrun.c (displaced_step_prepare): Update.
* infcall.c (call_function_by_hand_dummy): Update.
* guile/scm-exception.c (gdbscm_scm_from_gdb_exception): Update.
* gnu-v3-abi.c (print_one_vtable): Update.
* frame.c (get_prev_frame_always): Update.
* f-valprint.c (info_common_command_for_block): Update.
* exec.c (try_open_exec_file): Update.
* exceptions.c (print_exception, exception_print)
(exception_fprintf, exception_print_same): Update.
* dwarf2-frame.c (dwarf2_build_frame_info): Update.
* dwarf-index-cache.c (index_cache::store)
(index_cache::lookup_gdb_index): Update.
* darwin-nat.c (maybe_cache_shell): Update.
* cp-valprint.c (cp_print_value_fields): Update.
* compile/compile-cplus-symbols.c (gcc_cplus_convert_symbol)
(gcc_cplus_symbol_address): Update.
* compile/compile-c-symbols.c (gcc_convert_symbol)
(gcc_symbol_address, generate_c_for_for_one_variable): Update.
* common/selftest.c: Update.
* common/common-exceptions.h (struct gdb_exception) <message>: Now
a std::string.
(exception_try_scope_entry, exception_try_scope_exit): Don't
declare.
(struct exception_try_scope): Remove.
(TRY): Don't use exception_try_scope.
(struct gdb_exception): Add constructor, operator=.
<what>: New method.
(struct gdb_exception_RETURN_MASK_ALL)
(struct gdb_exception_RETURN_MASK_ERROR)
(struct gdb_exception_RETURN_MASK_QUIT): Add constructor.
(struct gdb_quit_bad_alloc): Update.
* common/common-exceptions.c (exception_none): Change
initializer.
(struct catcher) <state, exception>: Initialize inline.
<prev>: Remove member.
(current_catcher): Remove.
(catchers): New global.
(exceptions_state_mc_init): Simplify.
(catcher_pop): Remove.
(exceptions_state_mc, exceptions_state_mc_catch): Update.
(try_scope_depth, exception_try_scope_entry)
(exception_try_scope_exit): Remove.
(throw_exception_sjlj): Update.
(exception_messages, exception_messages_size): Remove.
(throw_it): Simplify.
(gdb_exception_sliced_copy): Remove.
(throw_exception_cxx): Update.
* cli/cli-script.c (script_from_file): Update.
* breakpoint.c (insert_bp_location, update_breakpoint_locations):
Update.
* ada-valprint.c (ada_val_print): Update.
* ada-lang.c (ada_to_fixed_type_1, ada_exception_name_addr)
(create_excep_cond_exprs): Update.
gdb/gdbserver/ChangeLog
2019-04-08 Tom Tromey <tom@tromey.com>
* server.c (handle_btrace_general_set, handle_qxfer_btrace)
(handle_qxfer_btrace_conf, detach_or_kill_for_exit_cleanup)
(captured_main, main): Update.
* gdbreplay.c (main): Update.
2019-01-29 01:11:10 +08:00
|
|
|
|
PyErr_Format (exc_class, "%s", exception.what ());
|
2010-11-13 04:49:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-29 05:16:04 +08:00
|
|
|
|
/* Converts OBJ to a CORE_ADDR value.
|
|
|
|
|
|
2013-05-21 04:24:49 +08:00
|
|
|
|
Returns 0 on success or -1 on failure, with a Python exception set.
|
2010-06-29 05:16:04 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
|
|
|
|
|
{
|
|
|
|
|
if (gdbpy_is_value_object (obj))
|
2013-05-21 04:24:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
2019-04-04 06:02:42 +08:00
|
|
|
|
try
|
2013-05-21 04:24:49 +08:00
|
|
|
|
{
|
|
|
|
|
*addr = value_as_address (value_object_to_value (obj));
|
|
|
|
|
}
|
2019-04-04 05:59:07 +08:00
|
|
|
|
catch (const gdb_exception &except)
|
Split TRY_CATCH into TRY + CATCH
This patch splits the TRY_CATCH macro into three, so that we go from
this:
~~~
volatile gdb_exception ex;
TRY_CATCH (ex, RETURN_MASK_ERROR)
{
}
if (ex.reason < 0)
{
}
~~~
to this:
~~~
TRY
{
}
CATCH (ex, RETURN_MASK_ERROR)
{
}
END_CATCH
~~~
Thus, we'll be getting rid of the local volatile exception object, and
declaring the caught exception in the catch block.
This allows reimplementing TRY/CATCH in terms of C++ exceptions when
building in C++ mode, while still allowing to build GDB in C mode
(using setjmp/longjmp), as a transition step.
TBC, after this patch, is it _not_ valid to have code between the TRY
and the CATCH blocks, like:
TRY
{
}
// some code here.
CATCH (ex, RETURN_MASK_ERROR)
{
}
END_CATCH
Just like it isn't valid to do that with C++'s native try/catch.
By switching to creating the exception object inside the CATCH block
scope, we can get rid of all the explicitly allocated volatile
exception objects all over the tree, and map the CATCH block more
directly to C++'s catch blocks.
The majority of the TRY_CATCH -> TRY+CATCH+END_CATCH conversion was
done with a script, rerun from scratch at every rebase, no manual
editing involved. After the mechanical conversion, a few places
needed manual intervention, to fix preexisting cases where we were
using the exception object outside of the TRY_CATCH block, and cases
where we were using "else" after a 'if (ex.reason) < 0)' [a CATCH
after this patch]. The result was folded into this patch so that GDB
still builds at each incremental step.
END_CATCH is necessary for two reasons:
First, because we name the exception object in the CATCH block, which
requires creating a scope, which in turn must be closed somewhere.
Declaring the exception variable in the initializer field of a for
block, like:
#define CATCH(EXCEPTION, mask) \
for (struct gdb_exception EXCEPTION; \
exceptions_state_mc_catch (&EXCEPTION, MASK); \
EXCEPTION = exception_none)
would avoid needing END_CATCH, but alas, in C mode, we build with C90,
which doesn't allow mixed declarations and code.
Second, because when TRY/CATCH are wired to real C++ try/catch, as
long as we need to handle cleanup chains, even if there's no CATCH
block that wants to catch the exception, we need for stop at every
frame in the unwind chain and run cleanups, then rethrow. That will
be done in END_CATCH.
After we require C++, we'll still need TRY/CATCH/END_CATCH until
cleanups are completely phased out -- TRY/CATCH in C++ mode will
save/restore the current cleanup chain, like in C mode, and END_CATCH
catches otherwise uncaugh exceptions, runs cleanups and rethrows, so
that C++ cleanups and exceptions can coexist.
IMO, this still makes the TRY/CATCH code look a bit more like a
newcomer would expect, so IMO worth it even if we weren't considering
C++.
gdb/ChangeLog.
2015-03-07 Pedro Alves <palves@redhat.com>
* common/common-exceptions.c (struct catcher) <exception>: No
longer a pointer to volatile exception. Now an exception value.
<mask>: Delete field.
(exceptions_state_mc_init): Remove all parameters. Adjust.
(exceptions_state_mc): No longer pop the catcher here.
(exceptions_state_mc_catch): New function.
(throw_exception): Adjust.
* common/common-exceptions.h (exceptions_state_mc_init): Remove
all parameters.
(exceptions_state_mc_catch): Declare.
(TRY_CATCH): Rename to ...
(TRY): ... this. Remove EXCEPTION and MASK parameters.
(CATCH, END_CATCH): New.
All callers adjusted.
gdb/gdbserver/ChangeLog:
2015-03-07 Pedro Alves <palves@redhat.com>
Adjust all callers of TRY_CATCH to use TRY/CATCH/END_CATCH
instead.
2015-03-07 23:14:14 +08:00
|
|
|
|
{
|
|
|
|
|
GDB_PY_SET_HANDLE_EXCEPTION (except);
|
|
|
|
|
}
|
2013-05-21 04:24:49 +08:00
|
|
|
|
}
|
2011-01-27 04:53:45 +08:00
|
|
|
|
else
|
2010-06-29 05:16:04 +08:00
|
|
|
|
{
|
Turn gdbpy_ref into a template
This turns gdbpy_ref into a template class, so that it can be used to
wrap subclasses of PyObject. The default argument remains PyObject;
and this necessitated renaming uses of "gdbpy_ref" to "gdbpy_ref<>".
gdb/ChangeLog
2017-02-10 Tom Tromey <tom@tromey.com>
* python/py-ref.h (gdbpy_ref_policy): Now a template.
(gdbpy_ref): Now a template; allow subclasses of PyObject to be
used.
* python/py-arch.c, python/py-bpevent.c, python/py-breakpoint.c,
python/py-cmd.c, python/py-continueevent.c, python/py-event.c,
python/py-exitedevent.c, python/py-finishbreakpoint.c,
python/py-framefilter.c, python/py-function.c,
python/py-inferior.c, python/py-infevents.c,
python/py-linetable.c, python/py-newobjfileevent.c,
python/py-param.c, python/py-prettyprint.c, python/py-ref.h,
python/py-signalevent.c, python/py-stopevent.c,
python/py-symbol.c, python/py-threadevent.c, python/py-type.c,
python/py-unwind.c, python/py-utils.c, python/py-value.c,
python/py-varobj.c, python/py-xmethods.c, python/python.c,
varobj.c: Change gdbpy_ref to gdbpy_ref<>.
2017-02-10 04:16:36 +08:00
|
|
|
|
gdbpy_ref<> num (PyNumber_Long (obj));
|
2011-01-27 04:53:45 +08:00
|
|
|
|
gdb_py_ulongest val;
|
|
|
|
|
|
|
|
|
|
if (num == NULL)
|
2013-05-21 04:24:49 +08:00
|
|
|
|
return -1;
|
2010-06-29 05:16:04 +08:00
|
|
|
|
|
2016-11-21 01:52:25 +08:00
|
|
|
|
val = gdb_py_long_as_ulongest (num.get ());
|
2011-01-27 04:53:45 +08:00
|
|
|
|
if (PyErr_Occurred ())
|
2013-05-21 04:24:49 +08:00
|
|
|
|
return -1;
|
2010-06-29 05:16:04 +08:00
|
|
|
|
|
2011-01-27 04:53:45 +08:00
|
|
|
|
if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
|
|
|
|
|
{
|
|
|
|
|
PyErr_SetString (PyExc_ValueError,
|
|
|
|
|
_("Overflow converting to address."));
|
2013-05-21 04:24:49 +08:00
|
|
|
|
return -1;
|
2011-01-27 04:53:45 +08:00
|
|
|
|
}
|
2010-06-29 05:16:04 +08:00
|
|
|
|
|
2011-01-27 04:53:45 +08:00
|
|
|
|
*addr = val;
|
2010-06-29 05:16:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 04:24:49 +08:00
|
|
|
|
return 0;
|
2010-06-29 05:16:04 +08:00
|
|
|
|
}
|
2011-01-27 04:53:45 +08:00
|
|
|
|
|
|
|
|
|
/* Convert a LONGEST to the appropriate Python object -- either an
|
|
|
|
|
integer object or a long object, depending on its value. */
|
|
|
|
|
|
2018-10-25 06:33:23 +08:00
|
|
|
|
gdbpy_ref<>
|
2011-01-27 04:53:45 +08:00
|
|
|
|
gdb_py_object_from_longest (LONGEST l)
|
|
|
|
|
{
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#ifdef IS_PY3K
|
|
|
|
|
if (sizeof (l) > sizeof (long))
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyLong_FromLongLong (l));
|
|
|
|
|
return gdbpy_ref<> (PyLong_FromLong (l));
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#else
|
2011-01-27 04:53:45 +08:00
|
|
|
|
#ifdef HAVE_LONG_LONG /* Defined by Python. */
|
|
|
|
|
/* If we have 'long long', and the value overflows a 'long', use a
|
|
|
|
|
Python Long; otherwise use a Python Int. */
|
|
|
|
|
if (sizeof (l) > sizeof (long)
|
|
|
|
|
&& (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1))
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyLong_FromLongLong (l));
|
2011-01-27 04:53:45 +08:00
|
|
|
|
#endif
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyInt_FromLong (l));
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#endif
|
2011-01-27 04:53:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Convert a ULONGEST to the appropriate Python object -- either an
|
|
|
|
|
integer object or a long object, depending on its value. */
|
|
|
|
|
|
2018-10-25 06:33:23 +08:00
|
|
|
|
gdbpy_ref<>
|
2011-01-27 04:53:45 +08:00
|
|
|
|
gdb_py_object_from_ulongest (ULONGEST l)
|
|
|
|
|
{
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#ifdef IS_PY3K
|
|
|
|
|
if (sizeof (l) > sizeof (unsigned long))
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
|
|
|
|
|
return gdbpy_ref<> (PyLong_FromUnsignedLong (l));
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#else
|
2011-01-27 04:53:45 +08:00
|
|
|
|
#ifdef HAVE_LONG_LONG /* Defined by Python. */
|
|
|
|
|
/* If we have 'long long', and the value overflows a 'long', use a
|
|
|
|
|
Python Long; otherwise use a Python Int. */
|
|
|
|
|
if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ())
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
|
2011-01-27 04:53:45 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (l > PyInt_GetMax ())
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyLong_FromUnsignedLong (l));
|
2011-01-27 04:53:45 +08:00
|
|
|
|
|
2018-10-25 06:33:23 +08:00
|
|
|
|
return gdbpy_ref<> (PyInt_FromLong (l));
|
2012-12-13 00:47:30 +08:00
|
|
|
|
#endif
|
2011-01-27 04:53:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts
|
|
|
|
|
the value into an out parameter. */
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
gdb_py_int_as_long (PyObject *obj, long *result)
|
|
|
|
|
{
|
|
|
|
|
*result = PyInt_AsLong (obj);
|
|
|
|
|
return ! (*result == -1 && PyErr_Occurred ());
|
|
|
|
|
}
|
2012-02-18 03:24:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Generic implementation of the __dict__ attribute for objects that
|
|
|
|
|
have a dictionary. The CLOSURE argument should be the type object.
|
|
|
|
|
This only handles positive values for tp_dictoffset. */
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
|
gdb_py_generic_dict (PyObject *self, void *closure)
|
|
|
|
|
{
|
|
|
|
|
PyObject *result;
|
2015-09-26 02:08:07 +08:00
|
|
|
|
PyTypeObject *type_obj = (PyTypeObject *) closure;
|
2012-02-18 03:24:27 +08:00
|
|
|
|
char *raw_ptr;
|
|
|
|
|
|
|
|
|
|
raw_ptr = (char *) self + type_obj->tp_dictoffset;
|
|
|
|
|
result = * (PyObject **) raw_ptr;
|
|
|
|
|
|
|
|
|
|
Py_INCREF (result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-05-21 04:36:19 +08:00
|
|
|
|
|
|
|
|
|
/* Like PyModule_AddObject, but does not steal a reference to
|
|
|
|
|
OBJECT. */
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
Py_INCREF (object);
|
Remove Python 2.4 and 2.5 support
This removes all the remainings spots I could find that work around
issues in Python 2.4 and 2.5.
I don't have a good way to test that Python 2.6 still works.
Tested by the buildbot.
gdb/ChangeLog
2019-02-27 Tom Tromey <tromey@adacore.com>
* config.in, configure: Rebuild.
* configure.ac (HAVE_LIBPYTHON2_4, HAVE_LIBPYTHON2_5): Never
define.
* python/py-value.c: Remove Python 2.4 workaround.
* python/py-utils.c (gdb_pymodule_addobject): Remove Python 2.4
workaround.
* python/py-type.c (convert_field, gdbpy_initialize_types): Remove
Python 2.4 workaround.
* python/python-internal.h: Remove Python 2.4 comment.
(Py_ssize_t): Don't define.
(PyVarObject_HEAD_INIT, Py_TYPE): Don't define.
(gdb_Py_DECREF): Remove Python 2.4 workaround.
(gdb_PyObject_GetAttrString, PyObject_GetAttrString): Remove.
(gdb_PyObject_HasAttrString, PyObject_HasAttrString): Remove.
* python/python.c (do_start_initialization): Remove Python 2.4
workaround.
* python/py-prettyprint.c (class dummy_python_frame): Remove.
(print_children): Remove Python 2.4 workaround.
* python/py-inferior.c (buffer_procs): Remove Python 2.4
workaround.
(CHARBUFFERPROC_NAME): Remove.
* python/py-breakpoint.c (gdbpy_initialize_breakpoints): Remove
Python 2.4 workaround.
gdb/testsuite/ChangeLog
2019-02-27 Tom Tromey <tromey@adacore.com>
* lib/gdb.exp (skip_python_tests_prompt): Don't check for Python
2.4.
* gdb.python/py-finish-breakpoint.exp: Remove Python 2.4
workaround.
gdb/ChangeLog
2019-02-27 Tom Tromey <tromey@adacore.com>
* config.in, configure: Rebuild.
* configure.ac (HAVE_LIBPYTHON2_4, HAVE_LIBPYTHON2_5): Never
define.
* python/py-value.c: Remove Python 2.4 workaround.
* python/py-utils.c (gdb_pymodule_addobject): Remove Python 2.4
workaround.
* python/py-type.c (convert_field, gdbpy_initialize_types): Remove
Python 2.4 workaround.
* python/python-internal.h: Remove Python 2.4 comment.
(Py_ssize_t): Don't define.
(PyVarObject_HEAD_INIT, Py_TYPE): Don't define.
(gdb_Py_DECREF): Remove Python 2.4 workaround.
(gdb_PyObject_GetAttrString, PyObject_GetAttrString): Remove.
(gdb_PyObject_HasAttrString, PyObject_HasAttrString): Remove.
* python/python.c (do_start_initialization): Remove Python 2.4
workaround.
* python/py-prettyprint.c (class dummy_python_frame): Remove.
(print_children): Remove Python 2.4 workaround.
* python/py-inferior.c (buffer_procs): Remove Python 2.4
workaround.
(CHARBUFFERPROC_NAME): Remove.
* python/py-breakpoint.c (gdbpy_initialize_breakpoints): Remove
Python 2.4 workaround.
2019-02-27 02:58:47 +08:00
|
|
|
|
result = PyModule_AddObject (module, name, object);
|
2013-05-21 04:36:19 +08:00
|
|
|
|
if (result < 0)
|
2013-05-22 04:52:30 +08:00
|
|
|
|
Py_DECREF (object);
|
2013-05-21 04:36:19 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-09-15 14:57:12 +08:00
|
|
|
|
|
|
|
|
|
/* Handle a Python exception when the special gdb.GdbError treatment
|
|
|
|
|
is desired. This should only be called when an exception is set.
|
|
|
|
|
If the exception is a gdb.GdbError, throw a gdb exception with the
|
|
|
|
|
exception text. For other exceptions, print the Python stack and
|
|
|
|
|
then throw a gdb exception. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
gdbpy_handle_exception ()
|
|
|
|
|
{
|
2018-12-28 02:32:01 +08:00
|
|
|
|
gdbpy_err_fetch fetched_error;
|
|
|
|
|
gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string ();
|
2018-09-15 14:57:12 +08:00
|
|
|
|
|
|
|
|
|
if (msg == NULL)
|
|
|
|
|
{
|
|
|
|
|
/* An error occurred computing the string representation of the
|
|
|
|
|
error message. This is rare, but we should inform the user. */
|
|
|
|
|
printf_filtered (_("An error occurred in Python "
|
|
|
|
|
"and then another occurred computing the "
|
|
|
|
|
"error message.\n"));
|
|
|
|
|
gdbpy_print_stack ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Don't print the stack for gdb.GdbError exceptions.
|
|
|
|
|
It is generally used to flag user errors.
|
|
|
|
|
|
|
|
|
|
We also don't want to print "Error occurred in Python command"
|
|
|
|
|
for user errors. However, a missing message for gdb.GdbError
|
|
|
|
|
exceptions is arguably a bug, so we flag it as such. */
|
|
|
|
|
|
2018-12-28 02:32:01 +08:00
|
|
|
|
if (fetched_error.type_matches (PyExc_KeyboardInterrupt))
|
2018-12-26 03:38:01 +08:00
|
|
|
|
throw_quit ("Quit");
|
2018-12-28 02:32:01 +08:00
|
|
|
|
else if (! fetched_error.type_matches (gdbpy_gdberror_exc)
|
|
|
|
|
|| msg == NULL || *msg == '\0')
|
2018-09-15 14:57:12 +08:00
|
|
|
|
{
|
2018-12-28 02:32:01 +08:00
|
|
|
|
fetched_error.restore ();
|
2018-09-15 14:57:12 +08:00
|
|
|
|
gdbpy_print_stack ();
|
|
|
|
|
if (msg != NULL && *msg != '\0')
|
|
|
|
|
error (_("Error occurred in Python: %s"), msg.get ());
|
|
|
|
|
else
|
|
|
|
|
error (_("Error occurred in Python."));
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-12-28 02:32:01 +08:00
|
|
|
|
error ("%s", msg.get ());
|
2018-09-15 14:57:12 +08:00
|
|
|
|
}
|