2008-08-07 03:41:33 +08:00
|
|
|
|
/* General utility routines for GDB/Python.
|
|
|
|
|
|
2023-01-01 20:49:04 +08:00
|
|
|
|
Copyright (C) 2008-2023 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"
|
2023-02-28 07:11:37 +08:00
|
|
|
|
#include "top.h" /* For quit_force (). */
|
2008-08-07 03:41:33 +08:00
|
|
|
|
#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);
|
|
|
|
|
}
|
2008-08-07 03:41:33 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PyErr_SetString (PyExc_TypeError,
|
2021-12-24 09:20:46 +08:00
|
|
|
|
_("Expected a string object."));
|
2008-08-07 03:41:33 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
Change how Python architecture and language are handled
Currently, gdb's Python layer captures the current architecture and
language when "entering" Python code. This has some undesirable
effects, and so this series changes how this is handled.
First, there is code like this:
gdbpy_enter enter_py (python_gdbarch, python_language);
This is incorrect, because both of these are NULL when not otherwise
assigned. This can cause crashes in some cases -- I've added one to
the test suite. (Note that this crasher is just an example, other
ones along the same lines are possible.)
Second, when the language is captured in this way, it means that
Python code cannot affect the current language for its own purposes.
It's reasonable to want to write code like this:
gdb.execute('set language mumble')
... stuff using the current language
gdb.execute('set language previous-value')
However, this won't actually work, because the language is captured on
entry. I've added a test to show this as well.
This patch changes gdb to try to avoid capturing the current values.
The Python concept of the current gdbarch is only set in those few
cases where a non-default value is computed or needed; and the
language is not captured at all -- instead, in the cases where it's
required, the current language is temporarily changed.
2022-01-04 23:02:24 +08:00
|
|
|
|
return (unicode_to_encoded_string
|
|
|
|
|
(unicode_str,
|
|
|
|
|
target_charset (gdbpy_enter::get_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)
|
|
|
|
|
{
|
Change how Python architecture and language are handled
Currently, gdb's Python layer captures the current architecture and
language when "entering" Python code. This has some undesirable
effects, and so this series changes how this is handled.
First, there is code like this:
gdbpy_enter enter_py (python_gdbarch, python_language);
This is incorrect, because both of these are NULL when not otherwise
assigned. This can cause crashes in some cases -- I've added one to
the test suite. (Note that this crasher is just an example, other
ones along the same lines are possible.)
Second, when the language is captured in this way, it means that
Python code cannot affect the current language for its own purposes.
It's reasonable to want to write code like this:
gdb.execute('set language mumble')
... stuff using the current language
gdb.execute('set language previous-value')
However, this won't actually work, because the language is captured on
entry. I've added a test to show this as well.
This patch changes gdb to try to avoid capturing the current values.
The Python concept of the current gdbarch is only set in those few
cases where a non-default value is computed or needed; and the
language is not captured at all -- instead, in the cases where it's
required, the current language is temporarily changed.
2022-01-04 23:02:24 +08:00
|
|
|
|
return (unicode_to_encoded_python_string
|
|
|
|
|
(unicode_str,
|
|
|
|
|
target_charset (gdbpy_enter::get_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)
|
|
|
|
|
{
|
2022-03-21 22:07:41 +08:00
|
|
|
|
return gdbpy_ref<> (PyUnicode_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
|
|
|
|
return PyUnicode_Check (obj);
|
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)
|
2021-12-24 09:20:46 +08:00
|
|
|
|
return python_string_to_host_string (str_obj.get ());
|
2010-05-25 23:27:17 +08:00
|
|
|
|
|
|
|
|
|
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"). */
|
|
|
|
|
|
2022-05-24 18:54:40 +08:00
|
|
|
|
if (m_error_value.get () != nullptr && m_error_value.get () != Py_None)
|
|
|
|
|
return gdbpy_obj_to_string (m_error_value.get ());
|
2010-05-25 23:27:17 +08:00
|
|
|
|
else
|
2022-05-24 18:54:40 +08:00
|
|
|
|
return gdbpy_obj_to_string (m_error_type.get ());
|
2018-12-28 02:32:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* See python-internal.h. */
|
|
|
|
|
|
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
|
|
|
|
gdbpy_err_fetch::type_to_string () const
|
|
|
|
|
{
|
2022-05-24 18:54:40 +08:00
|
|
|
|
return gdbpy_obj_to_string (m_error_type.get ());
|
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;
|
2023-02-28 07:11:37 +08:00
|
|
|
|
else if (exception.reason == RETURN_FORCED_QUIT)
|
|
|
|
|
quit_force (NULL, 0);
|
2010-11-13 04:49:43 +08:00
|
|
|
|
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
|
|
|
|
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));
|
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
|
|
|
|
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));
|
2011-01-27 04:53:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 22:07:41 +08:00
|
|
|
|
/* Like PyLong_AsLong, but returns 0 on failure, 1 on success, and puts
|
2011-01-27 04:53:45 +08:00
|
|
|
|
the value into an out parameter. */
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
gdb_py_int_as_long (PyObject *obj, long *result)
|
|
|
|
|
{
|
2022-03-21 22:07:41 +08:00
|
|
|
|
*result = PyLong_AsLong (obj);
|
2011-01-27 04:53:45 +08:00
|
|
|
|
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
|
|
|
|
|
gdb/python/mi: create MI commands using python
This commit allows a user to create custom MI commands using Python
similarly to what is possible for Python CLI commands.
A new subclass of mi_command is defined for Python MI commands,
mi_command_py. A new file, gdb/python/py-micmd.c contains the logic
for Python MI commands.
This commit is based on work linked too from this mailing list thread:
https://sourceware.org/pipermail/gdb/2021-November/049774.html
Which has also been previously posted to the mailing list here:
https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html
And was recently reposted here:
https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html
The version in this patch takes some core code from the previously
posted patches, but also has some significant differences, especially
after the feedback given here:
https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html
A new MI command can be implemented in Python like this:
class echo_args(gdb.MICommand):
def invoke(self, args):
return { 'args': args }
echo_args("-echo-args")
The 'args' parameter (to the invoke method) is a list
containing (almost) all command line arguments passed to the MI
command (--thread and --frame are handled before the Python code is
called, and removed from the args list). This list can be empty if
the MI command was passed no arguments.
When used within gdb the above command produced output like this:
(gdb)
-echo-args a b c
^done,args=["a","b","c"]
(gdb)
The 'invoke' method of the new command must return a dictionary. The
keys of this dictionary are then used as the field names in the mi
command output (e.g. 'args' in the above).
The values of the result returned by invoke can be dictionaries,
lists, iterators, or an object that can be converted to a string.
These are processed recursively to create the mi output. And so, this
is valid:
class new_command(gdb.MICommand):
def invoke(self,args):
return { 'result_one': { 'abc': 123, 'def': 'Hello' },
'result_two': [ { 'a': 1, 'b': 2 },
{ 'c': 3, 'd': 4 } ] }
Which produces output like:
(gdb)
-new-command
^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
(gdb)
I have required that the fields names used in mi result output must
match the regexp: "^[a-zA-Z][-_a-zA-Z0-9]*$" (without the quotes).
This restriction was never written down anywhere before, but seems
sensible to me, and we can always loosen this rule later if it proves
to be a problem. Much harder to try and add a restriction later, once
people are already using the API.
What follows are some details about how this implementation differs
from the original patch that was posted to the mailing list.
In this patch, I have changed how the lifetime of the Python
gdb.MICommand objects is managed. In the original patch, these object
were kept alive by an owned reference within the mi_command_py object.
As such, the Python object would not be deleted until the
mi_command_py object itself was deleted.
This caused a problem, the mi_command_py were held in the global mi
command table (in mi/mi-cmds.c), which, as a global, was not cleared
until program shutdown. By this point the Python interpreter has
already been shutdown. Attempting to delete the mi_command_py object
at this point was causing GDB to try and invoke Python code after
finalising the Python interpreter, and we would crash.
To work around this problem, the original patch added code in
python/python.c that would search the mi command table, and delete the
mi_command_py objects before the Python environment was finalised.
In contrast, in this patch, I have added a new global dictionary to
the gdb module, gdb._mi_commands. We already have several such global
data stores related to pretty printers, and frame unwinders.
The MICommand objects are placed into the new gdb.mi_commands
dictionary, and it is this reference that keeps the objects alive.
When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
and any MICommand objects within it are deleted at this point.
This change avoids having to make the mi_cmd_table global, and walk
over it from within GDB's python related code.
This patch handles command redefinition entirely within GDB's python
code, though this does impose one small restriction which is not
present in the original code (detailed below), I don't think this is a
big issue. However, the original patch relied on being able to
finish executing the mi_command::do_invoke member function after the
mi_command object had been deleted. Though continuing to execute a
member function after an object is deleted is well defined, it is
also (IMHO) risky, its too easy for someone to later add a use of the
object without realising that the object might sometimes, have been
deleted. The new patch avoids this issue.
The one restriction that is added to avoid this, is that an MICommand
object can't be reinitialised with a different command name, so:
(gdb) python cmd = MyMICommand("-abc")
(gdb) python cmd.__init__("-def")
can't reinitialize object with a different command name
This feels like a pretty weird edge case, and I'm happy to live with
this restriction.
I have also changed how the memory is managed for the command name.
In the most recently posted patch series, the command name is moved
into a subclass of mi_command, the python mi_command_py, which
inherits from mi_command is then free to use a smart pointer to manage
the memory for the name.
In this patch, I leave the mi_command class unchanged, and instead
hold the memory for the name within the Python object, as the lifetime
of the Python object always exceeds the c++ object stored in the
mi_cmd_table. This adds a little more complexity in py-micmd.c, but
leaves the mi_command class nice and simple.
Next, this patch adds some extra functionality, there's a
MICommand.name read-only attribute containing the name of the command,
and a read-write MICommand.installed attribute that can be used to
install (make the command available for use) and uninstall (remove the
command from the mi_cmd_table so it can't be used) the command. This
attribute will be automatically updated if a second command replaces
an earlier command.
This patch adds additional error handling, and makes more use the
gdbpy_handle_exception function.
Co-Authored-By: Jan Vrany <jan.vrany@labware.com>
2020-06-23 21:45:38 +08:00
|
|
|
|
/* See python-internal.h. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
gdbpy_error (const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
std::string str = string_vprintf (fmt, ap);
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
const char *msg = str.c_str ();
|
|
|
|
|
if (msg != nullptr && *msg != '\0')
|
|
|
|
|
error (_("Error occurred in Python: %s"), msg);
|
|
|
|
|
else
|
|
|
|
|
error (_("Error occurred in Python."));
|
|
|
|
|
}
|
|
|
|
|
|
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. */
|
2022-01-03 02:46:15 +08:00
|
|
|
|
gdb_printf (_("An error occurred in Python "
|
|
|
|
|
"and then another occurred computing the "
|
|
|
|
|
"error message.\n"));
|
2018-09-15 14:57:12 +08:00
|
|
|
|
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
|
|
|
|
}
|
gdb/python: improve formatting of help text for user defined commands
Consider this command defined in Python (in the file test-cmd.py):
class test_cmd (gdb.Command):
"""
This is the first line.
Indented second line.
This is the third line.
"""
def __init__ (self):
super ().__init__ ("test-cmd", gdb.COMMAND_OBSCURE)
def invoke (self, arg, from_tty):
print ("In test-cmd")
test_cmd()
Now, within a GDB session:
(gdb) source test-cmd.py
(gdb) help test-cmd
This is the first line.
Indented second line.
This is the third line.
(gdb)
I think there's three things wrong here:
1. The leading blank line,
2. The trailing blank line, and
3. Every line is indented from the left edge slightly.
The problem of course, is that GDB is using the Python doc string
verbatim as its help text. While the user has formatted the help text
so that it appears clear within the .py file, this means that the text
appear less well formatted when displayed in the "help" output.
The same problem can be observed for gdb.Parameter objects in their
set/show output.
In this commit I aim to improve the "help" output for commands and
parameters.
To do this I have added gdbpy_fix_doc_string_indentation, a new
function that rewrites the doc string text following the following
rules:
1. Leading blank lines are removed,
2. Trailing blank lines are removed, and
3. Leading whitespace is removed in a "smart" way such that the
relative indentation of lines is retained.
With this commit in place the above example now looks like this:
(gdb) source ~/tmp/test-cmd.py
(gdb) help test-cmd
This is the first line.
Indented second line.
This is the third line.
(gdb)
Which I think is much neater. Notice that the indentation of the
second line is retained. Any blank lines within the help text (not
leading or trailing) will be retained.
I've added a NEWS entry to note that there has been a change in
behaviour, but I didn't update the manual. The existing manual is
suitably vague about how the doc string is used, so I think the new
behaviour is covered just as well by the existing text.
2022-05-17 02:26:54 +08:00
|
|
|
|
|
|
|
|
|
/* See python-internal.h. */
|
|
|
|
|
|
|
|
|
|
gdb::unique_xmalloc_ptr<char>
|
|
|
|
|
gdbpy_fix_doc_string_indentation (gdb::unique_xmalloc_ptr<char> doc)
|
|
|
|
|
{
|
|
|
|
|
/* A structure used to track the white-space information on each line of
|
|
|
|
|
DOC. */
|
|
|
|
|
struct line_whitespace
|
|
|
|
|
{
|
|
|
|
|
/* Constructor. OFFSET is the offset from the start of DOC, WS_COUNT
|
|
|
|
|
is the number of whitespace characters starting at OFFSET. */
|
|
|
|
|
line_whitespace (size_t offset, int ws_count)
|
|
|
|
|
: m_offset (offset),
|
|
|
|
|
m_ws_count (ws_count)
|
|
|
|
|
{ /* Nothing. */ }
|
|
|
|
|
|
|
|
|
|
/* The offset from the start of DOC. */
|
|
|
|
|
size_t offset () const
|
|
|
|
|
{ return m_offset; }
|
|
|
|
|
|
|
|
|
|
/* The number of white-space characters at the start of this line. */
|
|
|
|
|
int ws () const
|
|
|
|
|
{ return m_ws_count; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
/* The offset from the start of DOC to the first character of this
|
|
|
|
|
line. */
|
|
|
|
|
size_t m_offset;
|
|
|
|
|
|
|
|
|
|
/* White space count on this line, the first character of this
|
|
|
|
|
whitespace is at OFFSET. */
|
|
|
|
|
int m_ws_count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Count the number of white-space character starting at TXT. We
|
|
|
|
|
currently only count true single space characters, things like tabs,
|
|
|
|
|
newlines, etc are not counted. */
|
|
|
|
|
auto count_whitespace = [] (const char *txt) -> int
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
while (*txt == ' ')
|
|
|
|
|
{
|
|
|
|
|
++txt;
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* In MIN_WHITESPACE we track the smallest number of whitespace
|
|
|
|
|
characters seen at the start of a line (that has actual content), this
|
|
|
|
|
is the number of characters that we can delete off all lines without
|
|
|
|
|
altering the relative indentation of all lines in DOC.
|
|
|
|
|
|
|
|
|
|
The first line often has no indentation, but instead starts immediates
|
|
|
|
|
after the 3-quotes marker within the Python doc string, so, if the
|
|
|
|
|
first line has zero white-space then we just ignore it, and don't set
|
|
|
|
|
MIN_WHITESPACE to zero.
|
|
|
|
|
|
|
|
|
|
Lines without any content should (ideally) have no white-space at
|
|
|
|
|
all, but if they do then they might have an artificially low number
|
|
|
|
|
(user left a single stray space at the start of an otherwise blank
|
|
|
|
|
line), we don't consider lines without content when updating the
|
|
|
|
|
MIN_WHITESPACE value. */
|
|
|
|
|
gdb::optional<int> min_whitespace;
|
|
|
|
|
|
|
|
|
|
/* The index into WS_INFO at which the processing of DOC can be
|
|
|
|
|
considered "all done", that is, after this point there are no further
|
|
|
|
|
lines with useful content and we should just stop. */
|
|
|
|
|
gdb::optional<size_t> all_done_idx;
|
|
|
|
|
|
|
|
|
|
/* White-space information for each line in DOC. */
|
|
|
|
|
std::vector<line_whitespace> ws_info;
|
|
|
|
|
|
|
|
|
|
/* Now look through DOC and collect the required information. */
|
|
|
|
|
const char *tmp = doc.get ();
|
|
|
|
|
while (*tmp != '\0')
|
|
|
|
|
{
|
|
|
|
|
/* Add an entry for the offset to the start of this line, and how
|
|
|
|
|
much white-space there is at the start of this line. */
|
|
|
|
|
size_t offset = tmp - doc.get ();
|
|
|
|
|
int ws_count = count_whitespace (tmp);
|
|
|
|
|
ws_info.emplace_back (offset, ws_count);
|
|
|
|
|
|
|
|
|
|
/* Skip over the white-space. */
|
|
|
|
|
tmp += ws_count;
|
|
|
|
|
|
|
|
|
|
/* Remember where the content of this line starts, and skip forward
|
|
|
|
|
to either the end of this line (newline) or the end of the DOC
|
|
|
|
|
string (null character), whichever comes first. */
|
|
|
|
|
const char *content_start = tmp;
|
|
|
|
|
while (*tmp != '\0' && *tmp != '\n')
|
|
|
|
|
++tmp;
|
|
|
|
|
|
|
|
|
|
/* If this is not the first line, and if this line has some content,
|
|
|
|
|
then update MIN_WHITESPACE, this reflects the smallest number of
|
|
|
|
|
whitespace characters we can delete from all lines without
|
|
|
|
|
impacting the relative indentation of all the lines of DOC. */
|
|
|
|
|
if (offset > 0 && tmp > content_start)
|
|
|
|
|
{
|
|
|
|
|
if (!min_whitespace.has_value ())
|
|
|
|
|
min_whitespace = ws_count;
|
|
|
|
|
else
|
|
|
|
|
min_whitespace = std::min (*min_whitespace, ws_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Each time we encounter a line that has some content we update
|
|
|
|
|
ALL_DONE_IDX to be the index of the next line. If the last lines
|
|
|
|
|
of DOC don't contain any content then ALL_DONE_IDX will be left
|
|
|
|
|
pointing at an earlier line. When we rewrite DOC, when we reach
|
|
|
|
|
ALL_DONE_IDX then we can stop, the allows us to trim any blank
|
|
|
|
|
lines from the end of DOC. */
|
|
|
|
|
if (tmp > content_start)
|
|
|
|
|
all_done_idx = ws_info.size ();
|
|
|
|
|
|
|
|
|
|
/* If we reached a newline then skip forward to the start of the next
|
|
|
|
|
line. The other possibility at this point is that we're at the
|
|
|
|
|
very end of the DOC string (null terminator). */
|
|
|
|
|
if (*tmp == '\n')
|
|
|
|
|
++tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We found no lines with content, fail safe by just returning the
|
|
|
|
|
original documentation string. */
|
|
|
|
|
if (!all_done_idx.has_value () || !min_whitespace.has_value ())
|
|
|
|
|
return doc;
|
|
|
|
|
|
|
|
|
|
/* Setup DST and SRC, both pointing into the DOC string. We're going to
|
|
|
|
|
rewrite DOC in-place, as we only ever make DOC shorter (by removing
|
|
|
|
|
white-space), thus we know this will not overflow. */
|
|
|
|
|
char *dst = doc.get ();
|
|
|
|
|
char *src = doc.get ();
|
|
|
|
|
|
|
|
|
|
/* Array indices used with DST, SRC, and WS_INFO respectively. */
|
|
|
|
|
size_t dst_offset = 0;
|
|
|
|
|
size_t src_offset = 0;
|
|
|
|
|
size_t ws_info_offset = 0;
|
|
|
|
|
|
|
|
|
|
/* Now, walk over the source string, this is the original DOC. */
|
|
|
|
|
while (src[src_offset] != '\0')
|
|
|
|
|
{
|
|
|
|
|
/* If we are at the start of the next line (in WS_INFO), then we may
|
|
|
|
|
need to skip some white-space characters. */
|
|
|
|
|
if (src_offset == ws_info[ws_info_offset].offset ())
|
|
|
|
|
{
|
|
|
|
|
/* If a line has leading white-space then we need to skip over
|
|
|
|
|
some number of characters now. */
|
|
|
|
|
if (ws_info[ws_info_offset].ws () > 0)
|
|
|
|
|
{
|
|
|
|
|
/* If the line is entirely white-space then we skip all of
|
|
|
|
|
the white-space, the next character to copy will be the
|
|
|
|
|
newline or null character. Otherwise, we skip the just
|
|
|
|
|
some portion of the leading white-space. */
|
|
|
|
|
if (src[src_offset + ws_info[ws_info_offset].ws ()] == '\n'
|
|
|
|
|
|| src[src_offset + ws_info[ws_info_offset].ws ()] == '\0')
|
|
|
|
|
src_offset += ws_info[ws_info_offset].ws ();
|
|
|
|
|
else
|
|
|
|
|
src_offset += std::min (*min_whitespace,
|
|
|
|
|
ws_info[ws_info_offset].ws ());
|
|
|
|
|
|
|
|
|
|
/* If we skipped white-space, and are now at the end of the
|
|
|
|
|
input, then we're done. */
|
|
|
|
|
if (src[src_offset] == '\0')
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ws_info_offset < (ws_info.size () - 1))
|
|
|
|
|
++ws_info_offset;
|
|
|
|
|
if (ws_info_offset > *all_done_idx)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Don't copy a newline to the start of the DST string, this would
|
|
|
|
|
result in a leading blank line. But in all other cases, copy the
|
|
|
|
|
next character into the destination string. */
|
|
|
|
|
if ((dst_offset > 0 || src[src_offset] != '\n'))
|
|
|
|
|
{
|
|
|
|
|
dst[dst_offset] = src[src_offset];
|
|
|
|
|
++dst_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Move to the next source character. */
|
|
|
|
|
++src_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Remove the trailing newline character(s), and ensure we have a null
|
|
|
|
|
terminator in place. */
|
|
|
|
|
while (dst_offset > 1 && dst[dst_offset - 1] == '\n')
|
|
|
|
|
--dst_offset;
|
|
|
|
|
dst[dst_offset] = '\0';
|
|
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
|
}
|