a121b7c1ac
This is the remainder boring constification that all looks more of less borderline obvious IMO. gdb/ChangeLog: 2017-04-05 Pedro Alves <palves@redhat.com> * ada-exp.y (yyerror): Constify. * ada-lang.c (bound_name, get_selections) (ada_variant_discrim_type) (ada_variant_discrim_name, ada_value_struct_elt) (ada_lookup_struct_elt_type, is_unchecked_variant) (ada_which_variant_applies, standard_exc, ada_get_next_arg) (catch_ada_exception_command_split) (catch_ada_assert_command_split, catch_assert_command) (ada_op_name): Constify. * ada-lang.h (ada_yyerror, get_selections) (ada_variant_discrim_name, ada_value_struct_elt): Constify. * arc-tdep.c (arc_print_frame_cache): Constify. * arm-tdep.c (arm_skip_stub): Constify. * ax-gdb.c (gen_binop, gen_struct_ref_recursive, gen_struct_ref) (gen_aggregate_elt_ref): Constify. * bcache.c (print_bcache_statistics): Constify. * bcache.h (print_bcache_statistics): Constify. * break-catch-throw.c (catch_exception_command_1): * breakpoint.c (struct ep_type_description::description): Constify. (add_solib_catchpoint): Constify. (catch_fork_command_1): Add cast. (add_catch_command): Constify. * breakpoint.h (add_catch_command, add_solib_catchpoint): Constify. * bsd-uthread.c (bsd_uthread_state): Constify. * buildsym.c (patch_subfile_names): Constify. * buildsym.h (next_symbol_text_func, patch_subfile_names): Constify. * c-exp.y (yyerror): Constify. (token::oper): Constify. * c-lang.h (c_yyerror, cp_print_class_member): Constify. * c-varobj.c (cplus_describe_child): Constify. * charset.c (find_charset_names): Add cast. (find_charset_names): Constify array and add const_cast. * cli/cli-cmds.c (complete_command, cd_command): Constify. (edit_command): Constify. * cli/cli-decode.c (lookup_cmd): Constify. * cli/cli-dump.c (dump_memory_command, dump_value_command): Constify. (struct dump_context): Constify. (add_dump_command, restore_command): Constify. * cli/cli-script.c (get_command_line): Constify. * cli/cli-script.h (get_command_line): Constify. * cli/cli-utils.c (check_for_argument): Constify. * cli/cli-utils.h (check_for_argument): Constify. * coff-pe-read.c (struct read_pe_section_data): Constify. * command.h (lookup_cmd): Constify. * common/print-utils.c (decimal2str): Constify. * completer.c (gdb_print_filename): Constify. * corefile.c (set_gnutarget): Constify. * cp-name-parser.y (yyerror): Constify. * cp-valprint.c (cp_print_class_member): Constify. * cris-tdep.c (cris_register_name, crisv32_register_name): Constify. * d-exp.y (yyerror): Constify. (struct token::oper): Constify. * d-lang.h (d_yyerror): Constify. * dbxread.c (struct header_file_location::name): Constify. (add_old_header_file, add_new_header_file, last_function_name) (dbx_next_symbol_text, add_bincl_to_list) (find_corresponding_bincl_psymtab, set_namestring) (find_stab_function_addr, read_dbx_symtab, start_psymtab) (dbx_end_psymtab, read_ofile_symtab, process_one_symbol): * defs.h (command_line_input, print_address_symbolic) (deprecated_readline_begin_hook): Constify. * dwarf2read.c (anonymous_struct_prefix, dwarf_bool_name): Constify. * event-top.c (handle_line_of_input): Constify and add cast. * exceptions.c (catch_errors): Constify. * exceptions.h (catch_errors): Constify. * expprint.c (print_subexp_standard, op_string, op_name) (op_name_standard, dump_raw_expression, dump_raw_expression): * expression.h (op_name, op_string, dump_raw_expression): Constify. * f-exp.y (yyerror): Constify. (struct token::oper): Constify. (struct f77_boolean_val::name): Constify. * f-lang.c (f_word_break_characters): Constify. * f-lang.h (f_yyerror): Constify. * fork-child.c (fork_inferior): Add cast. * frv-tdep.c (struct gdbarch_tdep::register_names): Constify. (new_variant): Constify. * gdbarch.sh (pstring_ptr, pstring_list): Constify. * gdbarch.c: Regenerate. * gdbcore.h (set_gnutarget): Constify. * go-exp.y (yyerror): Constify. (token::oper): Constify. * go-lang.h (go_yyerror): Constify. * go32-nat.c (go32_sysinfo): Constify. * guile/scm-breakpoint.c (gdbscm_breakpoint_expression): Constify. * guile/scm-cmd.c (cmdscm_function): Constify. * guile/scm-param.c (pascm_param_value): Constify. * h8300-tdep.c (h8300_register_name, h8300s_register_name) (h8300sx_register_name): Constify. * hppa-tdep.c (hppa32_register_name, hppa64_register_name): Constify. * ia64-tdep.c (ia64_register_names): Constify. * infcmd.c (construct_inferior_arguments): Constify. (path_command, attach_post_wait): Constify. * language.c (show_range_command, show_case_command) (unk_lang_error): Constify. * language.h (language_defn::la_error) (language_defn::la_name_of_this): Constify. * linespec.c (decode_line_2): Constify. * linux-thread-db.c (thread_db_err_str): Constify. * lm32-tdep.c (lm32_register_name): Constify. * m2-exp.y (yyerror): Constify. * m2-lang.h (m2_yyerror): Constify. * m32r-tdep.c (m32r_register_names): Constify and make static. * m68hc11-tdep.c (m68hc11_register_names): Constify. * m88k-tdep.c (m88k_register_name): Constify. * macroexp.c (appendmem): Constify. * mdebugread.c (fdr_name, add_data_symbol, parse_type) (upgrade_type, parse_external, parse_partial_symbols) (mdebug_next_symbol_text, cross_ref, mylookup_symbol, new_psymtab) (new_symbol): Constify. * memattr.c (mem_info_command): Constify. * mep-tdep.c (register_name_from_keyword): Constify. * mi/mi-cmd-env.c (mi_cmd_env_path, _initialize_mi_cmd_env): Constify. * mi/mi-cmd-stack.c (list_args_or_locals): Constify. * mi/mi-cmd-var.c (mi_cmd_var_show_attributes): Constify. * mi/mi-main.c (captured_mi_execute_command): Constify and add cast. (mi_execute_async_cli_command): Constify. * mips-tdep.c (mips_register_name): Constify. * mn10300-tdep.c (register_name, mn10300_generic_register_name) (am33_register_name, am33_2_register_name) * moxie-tdep.c (moxie_register_names): Constify. * nat/linux-osdata.c (osdata_type): Constify fields. * nto-tdep.c (nto_parse_redirection): Constify. * objc-lang.c (lookup_struct_typedef, lookup_objc_class) (lookup_child_selector): Constify. (objc_methcall::name): Constify. * objc-lang.h (lookup_objc_class, lookup_child_selector) (lookup_struct_typedef): Constify. * objfiles.c (pc_in_section): Constify. * objfiles.h (pc_in_section): Constify. * p-exp.y (struct token::oper): Constify. (yyerror): Constify. * p-lang.h (pascal_yyerror): Constify. * parser-defs.h (op_name_standard): Constify. (op_print::string): Constify. (exp_descriptor::op_name): Constify. * printcmd.c (print_address_symbolic): Constify. * psymtab.c (print_partial_symbols): Constify. * python/py-breakpoint.c (stop_func): Constify. (bppy_get_expression): Constify. * python/py-cmd.c (cmdpy_completer::name): Constify. (cmdpy_function): Constify. * python/py-event.c (evpy_add_attribute) (gdbpy_initialize_event_generic): Constify. * python/py-event.h (evpy_add_attribute) (gdbpy_initialize_event_generic): Constify. * python/py-evts.c (add_new_registry): Constify. * python/py-finishbreakpoint.c (outofscope_func): Constify. * python/py-framefilter.c (get_py_iter_from_func): Constify. * python/py-inferior.c (get_buffer): Add cast. * python/py-param.c (parm_constant::name): Constify. * python/py-unwind.c (fprint_frame_id): Constify. * python/python.c (gdbpy_parameter_value): Constify. * remote-fileio.c (remote_fio_func_map): Make 'name' const. * remote.c (memory_packet_config::name): Constify. (show_packet_config_cmd, remote_write_bytes) (remote_buffer_add_string): * reverse.c (exec_reverse_once): Constify. * rs6000-tdep.c (variant::name, variant::description): Constify. * rust-exp.y (rustyyerror): Constify. * rust-lang.c (rust_op_name): Constify. * rust-lang.h (rustyyerror): Constify. * serial.h (serial_ops::name): Constify. * sh-tdep.c (sh_sh_register_name, sh_sh3_register_name) (sh_sh3e_register_name, sh_sh2e_register_name) (sh_sh2a_register_name, sh_sh2a_nofpu_register_name) (sh_sh_dsp_register_name, sh_sh3_dsp_register_name) (sh_sh4_register_name, sh_sh4_nofpu_register_name) (sh_sh4al_dsp_register_name): Constify. * sh64-tdep.c (sh64_register_name): Constify. * solib-darwin.c (lookup_symbol_from_bfd): Constify. * spu-tdep.c (spu_register_name, info_spu_dma_cmdlist): Constify. * stabsread.c (patch_block_stabs, read_type_number) (ref_map::stabs, ref_add, process_reference) (symbol_reference_defined, define_symbol, define_symbol) (error_type, read_type, read_member_functions, read_cpp_abbrev) (read_one_struct_field, read_struct_fields, read_baseclasses) (read_tilde_fields, read_struct_type, read_array_type) (read_enum_type, read_sun_builtin_type, read_sun_floating_type) (read_huge_number, read_range_type, read_args, common_block_start) (find_name_end): Constify. * stabsread.h (common_block_start, define_symbol) (process_one_symbol, symbol_reference_defined, ref_add): * symfile.c (get_section_index, add_symbol_file_command): * symfile.h (get_section_index): Constify. * target-descriptions.c (tdesc_type::name): Constify. (tdesc_free_type): Add cast. * target.c (find_default_run_target): (add_deprecated_target_alias, find_default_run_target) (target_announce_detach): Constify. (do_option): Constify. * target.h (add_deprecated_target_alias): Constify. * thread.c (print_thread_info_1): Constify. * top.c (deprecated_readline_begin_hook, command_line_input): Constify. (init_main): Add casts. * top.h (handle_line_of_input): Constify. * tracefile-tfile.c (tfile_write_uploaded_tsv): Constify. * tracepoint.c (tvariables_info_1, trace_status_mi): Constify. (tfind_command): Rename to ... (tfind_command_1): ... this and constify. (tfind_command): New function. (tfind_end_command, tfind_start_command): Adjust. (encode_source_string): Constify. * tracepoint.h (encode_source_string): Constify. * tui/tui-data.c (tui_partial_win_by_name): Constify. * tui/tui-data.h (tui_partial_win_by_name): Constify. * tui/tui-source.c (tui_set_source_content_nil): Constify. * tui/tui-source.h (tui_set_source_content_nil): Constify. * tui/tui-win.c (parse_scrolling_args): Constify. * tui/tui-windata.c (tui_erase_data_content): Constify. * tui/tui-windata.h (tui_erase_data_content): Constify. * tui/tui-winsource.c (tui_erase_source_content): Constify. * tui/tui.c (tui_enable): Add cast. * utils.c (defaulted_query): Constify. (init_page_info): Add cast. (puts_debug, subset_compare): Constify. * utils.h (subset_compare): Constify. * varobj.c (varobj_format_string): Constify. * varobj.h (varobj_format_string): Constify. * vax-tdep.c (vax_register_name): Constify. * windows-nat.c (windows_detach): Constify. * xcoffread.c (process_linenos, xcoff_next_symbol_text): Constify. * xml-support.c (gdb_xml_end_element): Constify. * xml-tdesc.c (tdesc_start_reg): Constify. * xstormy16-tdep.c (xstormy16_register_name): Constify. * xtensa-tdep.c (xtensa_find_register_by_name): Constify. * xtensa-tdep.h (xtensa_register_t::name): Constify. gdb/gdbserver/ChangeLog: 2017-04-05 Pedro Alves <palves@redhat.com> * gdbreplay.c (sync_error): Constify. * linux-x86-low.c (push_opcode): Constify.
808 lines
21 KiB
C
808 lines
21 KiB
C
/* GDB parameters implemented in Python
|
||
|
||
Copyright (C) 2008-2017 Free Software Foundation, Inc.
|
||
|
||
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 "value.h"
|
||
#include "python-internal.h"
|
||
#include "charset.h"
|
||
#include "gdbcmd.h"
|
||
#include "cli/cli-decode.h"
|
||
#include "completer.h"
|
||
#include "language.h"
|
||
#include "arch-utils.h"
|
||
#include "py-ref.h"
|
||
|
||
/* Parameter constants and their values. */
|
||
struct parm_constant
|
||
{
|
||
const char *name;
|
||
int value;
|
||
};
|
||
|
||
struct parm_constant parm_constants[] =
|
||
{
|
||
{ "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
|
||
{ "PARAM_AUTO_BOOLEAN", var_auto_boolean },
|
||
{ "PARAM_UINTEGER", var_uinteger },
|
||
{ "PARAM_INTEGER", var_integer },
|
||
{ "PARAM_STRING", var_string },
|
||
{ "PARAM_STRING_NOESCAPE", var_string_noescape },
|
||
{ "PARAM_OPTIONAL_FILENAME", var_optional_filename },
|
||
{ "PARAM_FILENAME", var_filename },
|
||
{ "PARAM_ZINTEGER", var_zinteger },
|
||
{ "PARAM_ENUM", var_enum },
|
||
{ NULL, 0 }
|
||
};
|
||
|
||
/* A union that can hold anything described by enum var_types. */
|
||
union parmpy_variable
|
||
{
|
||
/* Hold an integer value, for boolean and integer types. */
|
||
int intval;
|
||
|
||
/* Hold an auto_boolean. */
|
||
enum auto_boolean autoboolval;
|
||
|
||
/* Hold an unsigned integer value, for uinteger. */
|
||
unsigned int uintval;
|
||
|
||
/* Hold a string, for the various string types. */
|
||
char *stringval;
|
||
|
||
/* Hold a string, for enums. */
|
||
const char *cstringval;
|
||
};
|
||
|
||
/* A GDB parameter. */
|
||
struct parmpy_object
|
||
{
|
||
PyObject_HEAD
|
||
|
||
/* The type of the parameter. */
|
||
enum var_types type;
|
||
|
||
/* The value of the parameter. */
|
||
union parmpy_variable value;
|
||
|
||
/* For an enum command, the possible values. The vector is
|
||
allocated with xmalloc, as is each element. It is
|
||
NULL-terminated. */
|
||
const char **enumeration;
|
||
};
|
||
|
||
typedef struct parmpy_object parmpy_object;
|
||
|
||
extern PyTypeObject parmpy_object_type
|
||
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
|
||
|
||
/* Some handy string constants. */
|
||
static PyObject *set_doc_cst;
|
||
static PyObject *show_doc_cst;
|
||
|
||
|
||
|
||
/* Get an attribute. */
|
||
static PyObject *
|
||
get_attr (PyObject *obj, PyObject *attr_name)
|
||
{
|
||
if (PyString_Check (attr_name)
|
||
#ifdef IS_PY3K
|
||
&& ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
|
||
#else
|
||
&& ! strcmp (PyString_AsString (attr_name), "value"))
|
||
#endif
|
||
{
|
||
parmpy_object *self = (parmpy_object *) obj;
|
||
|
||
return gdbpy_parameter_value (self->type, &self->value);
|
||
}
|
||
|
||
return PyObject_GenericGetAttr (obj, attr_name);
|
||
}
|
||
|
||
/* Set a parameter value from a Python value. Return 0 on success. Returns
|
||
-1 on error, with a python exception set. */
|
||
static int
|
||
set_parameter_value (parmpy_object *self, PyObject *value)
|
||
{
|
||
int cmp;
|
||
|
||
switch (self->type)
|
||
{
|
||
case var_string:
|
||
case var_string_noescape:
|
||
case var_optional_filename:
|
||
case var_filename:
|
||
if (! gdbpy_is_string (value)
|
||
&& (self->type == var_filename
|
||
|| value != Py_None))
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("String required for filename."));
|
||
|
||
return -1;
|
||
}
|
||
if (value == Py_None)
|
||
{
|
||
xfree (self->value.stringval);
|
||
if (self->type == var_optional_filename)
|
||
self->value.stringval = xstrdup ("");
|
||
else
|
||
self->value.stringval = NULL;
|
||
}
|
||
else
|
||
{
|
||
gdb::unique_xmalloc_ptr<char>
|
||
string (python_string_to_host_string (value));
|
||
if (string == NULL)
|
||
return -1;
|
||
|
||
xfree (self->value.stringval);
|
||
self->value.stringval = string.release ();
|
||
}
|
||
break;
|
||
|
||
case var_enum:
|
||
{
|
||
int i;
|
||
|
||
if (! gdbpy_is_string (value))
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("ENUM arguments must be a string."));
|
||
return -1;
|
||
}
|
||
|
||
gdb::unique_xmalloc_ptr<char>
|
||
str (python_string_to_host_string (value));
|
||
if (str == NULL)
|
||
return -1;
|
||
for (i = 0; self->enumeration[i]; ++i)
|
||
if (! strcmp (self->enumeration[i], str.get ()))
|
||
break;
|
||
if (! self->enumeration[i])
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("The value must be member of an enumeration."));
|
||
return -1;
|
||
}
|
||
self->value.cstringval = self->enumeration[i];
|
||
break;
|
||
}
|
||
|
||
case var_boolean:
|
||
if (! PyBool_Check (value))
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("A boolean argument is required."));
|
||
return -1;
|
||
}
|
||
cmp = PyObject_IsTrue (value);
|
||
if (cmp < 0)
|
||
return -1;
|
||
self->value.intval = cmp;
|
||
break;
|
||
|
||
case var_auto_boolean:
|
||
if (! PyBool_Check (value) && value != Py_None)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("A boolean or None is required"));
|
||
return -1;
|
||
}
|
||
|
||
if (value == Py_None)
|
||
self->value.autoboolval = AUTO_BOOLEAN_AUTO;
|
||
else
|
||
{
|
||
cmp = PyObject_IsTrue (value);
|
||
if (cmp < 0 )
|
||
return -1;
|
||
if (cmp == 1)
|
||
self->value.autoboolval = AUTO_BOOLEAN_TRUE;
|
||
else
|
||
self->value.autoboolval = AUTO_BOOLEAN_FALSE;
|
||
}
|
||
break;
|
||
|
||
case var_integer:
|
||
case var_zinteger:
|
||
case var_uinteger:
|
||
{
|
||
long l;
|
||
int ok;
|
||
|
||
if (! PyInt_Check (value))
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("The value must be integer."));
|
||
return -1;
|
||
}
|
||
|
||
if (! gdb_py_int_as_long (value, &l))
|
||
return -1;
|
||
|
||
if (self->type == var_uinteger)
|
||
{
|
||
ok = (l >= 0 && l <= UINT_MAX);
|
||
if (l == 0)
|
||
l = UINT_MAX;
|
||
}
|
||
else if (self->type == var_integer)
|
||
{
|
||
ok = (l >= INT_MIN && l <= INT_MAX);
|
||
if (l == 0)
|
||
l = INT_MAX;
|
||
}
|
||
else
|
||
ok = (l >= INT_MIN && l <= INT_MAX);
|
||
|
||
if (! ok)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("Range exceeded."));
|
||
return -1;
|
||
}
|
||
|
||
self->value.intval = (int) l;
|
||
break;
|
||
}
|
||
|
||
default:
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("Unhandled type in parameter value."));
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
/* Set an attribute. Returns -1 on error, with a python exception set. */
|
||
static int
|
||
set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
|
||
{
|
||
if (PyString_Check (attr_name)
|
||
#ifdef IS_PY3K
|
||
&& ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
|
||
#else
|
||
&& ! strcmp (PyString_AsString (attr_name), "value"))
|
||
#endif
|
||
{
|
||
if (!val)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("Cannot delete a parameter's value."));
|
||
return -1;
|
||
}
|
||
return set_parameter_value ((parmpy_object *) obj, val);
|
||
}
|
||
|
||
return PyObject_GenericSetAttr (obj, attr_name, val);
|
||
}
|
||
|
||
/* A helper function which returns a documentation string for an
|
||
object. */
|
||
|
||
static gdb::unique_xmalloc_ptr<char>
|
||
get_doc_string (PyObject *object, PyObject *attr)
|
||
{
|
||
gdb::unique_xmalloc_ptr<char> result;
|
||
|
||
if (PyObject_HasAttr (object, attr))
|
||
{
|
||
gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
|
||
|
||
if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
|
||
{
|
||
result = python_string_to_host_string (ds_obj.get ());
|
||
if (result == NULL)
|
||
gdbpy_print_stack ();
|
||
}
|
||
}
|
||
if (! result)
|
||
result.reset (xstrdup (_("This command is not documented.")));
|
||
return result;
|
||
}
|
||
|
||
/* Helper function which will execute a METHOD in OBJ passing the
|
||
argument ARG. ARG can be NULL. METHOD should return a Python
|
||
string. If this function returns NULL, there has been an error and
|
||
the appropriate exception set. */
|
||
static gdb::unique_xmalloc_ptr<char>
|
||
call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
|
||
{
|
||
gdb::unique_xmalloc_ptr<char> data;
|
||
gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
|
||
|
||
if (result == NULL)
|
||
return NULL;
|
||
|
||
if (gdbpy_is_string (result.get ()))
|
||
{
|
||
data = python_string_to_host_string (result.get ());
|
||
if (! data)
|
||
return NULL;
|
||
}
|
||
else
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("Parameter must return a string value."));
|
||
return NULL;
|
||
}
|
||
|
||
return data;
|
||
}
|
||
|
||
/* A callback function that is registered against the respective
|
||
add_setshow_* set_doc prototype. This function will either call
|
||
the Python function "get_set_string" or extract the Python
|
||
attribute "set_doc" and return the contents as a string. If
|
||
neither exist, insert a string indicating the Parameter is not
|
||
documented. */
|
||
static void
|
||
get_set_value (char *args, int from_tty,
|
||
struct cmd_list_element *c)
|
||
{
|
||
PyObject *obj = (PyObject *) get_cmd_context (c);
|
||
gdb::unique_xmalloc_ptr<char> set_doc_string;
|
||
|
||
gdbpy_enter enter_py (get_current_arch (), current_language);
|
||
gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
|
||
|
||
if (set_doc_func == NULL)
|
||
{
|
||
gdbpy_print_stack ();
|
||
return;
|
||
}
|
||
|
||
if (PyObject_HasAttr (obj, set_doc_func.get ()))
|
||
{
|
||
set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
|
||
if (! set_doc_string)
|
||
{
|
||
gdbpy_print_stack ();
|
||
return;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
/* We have to preserve the existing < GDB 7.3 API. If a
|
||
callback function does not exist, then attempt to read the
|
||
set_doc attribute. */
|
||
set_doc_string = get_doc_string (obj, set_doc_cst);
|
||
}
|
||
|
||
fprintf_filtered (gdb_stdout, "%s\n", set_doc_string.get ());
|
||
}
|
||
|
||
/* A callback function that is registered against the respective
|
||
add_setshow_* show_doc prototype. This function will either call
|
||
the Python function "get_show_string" or extract the Python
|
||
attribute "show_doc" and return the contents as a string. If
|
||
neither exist, insert a string indicating the Parameter is not
|
||
documented. */
|
||
static void
|
||
get_show_value (struct ui_file *file, int from_tty,
|
||
struct cmd_list_element *c,
|
||
const char *value)
|
||
{
|
||
PyObject *obj = (PyObject *) get_cmd_context (c);
|
||
gdb::unique_xmalloc_ptr<char> show_doc_string;
|
||
|
||
gdbpy_enter enter_py (get_current_arch (), current_language);
|
||
gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
|
||
|
||
if (show_doc_func == NULL)
|
||
{
|
||
gdbpy_print_stack ();
|
||
return;
|
||
}
|
||
|
||
if (PyObject_HasAttr (obj, show_doc_func.get ()))
|
||
{
|
||
gdbpy_ref<> val_obj (PyString_FromString (value));
|
||
|
||
if (val_obj == NULL)
|
||
{
|
||
gdbpy_print_stack ();
|
||
return;
|
||
}
|
||
|
||
show_doc_string = call_doc_function (obj, show_doc_func.get (),
|
||
val_obj.get ());
|
||
if (! show_doc_string)
|
||
{
|
||
gdbpy_print_stack ();
|
||
return;
|
||
}
|
||
|
||
fprintf_filtered (file, "%s\n", show_doc_string.get ());
|
||
}
|
||
else
|
||
{
|
||
/* We have to preserve the existing < GDB 7.3 API. If a
|
||
callback function does not exist, then attempt to read the
|
||
show_doc attribute. */
|
||
show_doc_string = get_doc_string (obj, show_doc_cst);
|
||
fprintf_filtered (file, "%s %s\n", show_doc_string.get (), value);
|
||
}
|
||
}
|
||
|
||
|
||
/* A helper function that dispatches to the appropriate add_setshow
|
||
function. */
|
||
static void
|
||
add_setshow_generic (int parmclass, enum command_class cmdclass,
|
||
char *cmd_name, parmpy_object *self,
|
||
char *set_doc, char *show_doc, char *help_doc,
|
||
struct cmd_list_element **set_list,
|
||
struct cmd_list_element **show_list)
|
||
{
|
||
struct cmd_list_element *param = NULL;
|
||
const char *tmp_name = NULL;
|
||
|
||
switch (parmclass)
|
||
{
|
||
case var_boolean:
|
||
|
||
add_setshow_boolean_cmd (cmd_name, cmdclass,
|
||
&self->value.intval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list);
|
||
|
||
break;
|
||
|
||
case var_auto_boolean:
|
||
add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
|
||
&self->value.autoboolval,
|
||
set_doc, show_doc, help_doc,
|
||
get_set_value, get_show_value,
|
||
set_list, show_list);
|
||
break;
|
||
|
||
case var_uinteger:
|
||
add_setshow_uinteger_cmd (cmd_name, cmdclass,
|
||
&self->value.uintval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list);
|
||
break;
|
||
|
||
case var_integer:
|
||
add_setshow_integer_cmd (cmd_name, cmdclass,
|
||
&self->value.intval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list); break;
|
||
|
||
case var_string:
|
||
add_setshow_string_cmd (cmd_name, cmdclass,
|
||
&self->value.stringval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list); break;
|
||
|
||
case var_string_noescape:
|
||
add_setshow_string_noescape_cmd (cmd_name, cmdclass,
|
||
&self->value.stringval,
|
||
set_doc, show_doc, help_doc,
|
||
get_set_value, get_show_value,
|
||
set_list, show_list);
|
||
|
||
break;
|
||
|
||
case var_optional_filename:
|
||
add_setshow_optional_filename_cmd (cmd_name, cmdclass,
|
||
&self->value.stringval, set_doc,
|
||
show_doc, help_doc, get_set_value,
|
||
get_show_value, set_list,
|
||
show_list);
|
||
break;
|
||
|
||
case var_filename:
|
||
add_setshow_filename_cmd (cmd_name, cmdclass,
|
||
&self->value.stringval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list); break;
|
||
|
||
case var_zinteger:
|
||
add_setshow_zinteger_cmd (cmd_name, cmdclass,
|
||
&self->value.intval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list);
|
||
break;
|
||
|
||
case var_enum:
|
||
add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
|
||
&self->value.cstringval, set_doc, show_doc,
|
||
help_doc, get_set_value, get_show_value,
|
||
set_list, show_list);
|
||
/* Initialize the value, just in case. */
|
||
self->value.cstringval = self->enumeration[0];
|
||
break;
|
||
}
|
||
|
||
/* Lookup created parameter, and register Python object against the
|
||
parameter context. Perform this task against both lists. */
|
||
tmp_name = cmd_name;
|
||
param = lookup_cmd (&tmp_name, *show_list, "", 0, 1);
|
||
if (param)
|
||
set_cmd_context (param, self);
|
||
|
||
tmp_name = cmd_name;
|
||
param = lookup_cmd (&tmp_name, *set_list, "", 0, 1);
|
||
if (param)
|
||
set_cmd_context (param, self);
|
||
}
|
||
|
||
/* A helper which computes enum values. Returns 1 on success. Returns 0 on
|
||
error, with a python exception set. */
|
||
static int
|
||
compute_enum_values (parmpy_object *self, PyObject *enum_values)
|
||
{
|
||
Py_ssize_t size, i;
|
||
struct cleanup *back_to;
|
||
|
||
if (! enum_values)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("An enumeration is required for PARAM_ENUM."));
|
||
return 0;
|
||
}
|
||
|
||
if (! PySequence_Check (enum_values))
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("The enumeration is not a sequence."));
|
||
return 0;
|
||
}
|
||
|
||
size = PySequence_Size (enum_values);
|
||
if (size < 0)
|
||
return 0;
|
||
if (size == 0)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("The enumeration is empty."));
|
||
return 0;
|
||
}
|
||
|
||
self->enumeration = XCNEWVEC (const char *, size + 1);
|
||
back_to = make_cleanup (free_current_contents, &self->enumeration);
|
||
|
||
for (i = 0; i < size; ++i)
|
||
{
|
||
gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
|
||
|
||
if (item == NULL)
|
||
{
|
||
do_cleanups (back_to);
|
||
return 0;
|
||
}
|
||
if (! gdbpy_is_string (item.get ()))
|
||
{
|
||
do_cleanups (back_to);
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("The enumeration item not a string."));
|
||
return 0;
|
||
}
|
||
self->enumeration[i]
|
||
= python_string_to_host_string (item.get ()).release ();
|
||
if (self->enumeration[i] == NULL)
|
||
{
|
||
do_cleanups (back_to);
|
||
return 0;
|
||
}
|
||
make_cleanup (xfree, (char *) self->enumeration[i]);
|
||
}
|
||
|
||
discard_cleanups (back_to);
|
||
return 1;
|
||
}
|
||
|
||
/* Object initializer; sets up gdb-side structures for command.
|
||
|
||
Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
|
||
|
||
NAME is the name of the parameter. It may consist of multiple
|
||
words, in which case the final word is the name of the new command,
|
||
and earlier words must be prefix commands.
|
||
|
||
CMDCLASS is the kind of command. It should be one of the COMMAND_*
|
||
constants defined in the gdb module.
|
||
|
||
PARMCLASS is the type of the parameter. It should be one of the
|
||
PARAM_* constants defined in the gdb module.
|
||
|
||
If PARMCLASS is PARAM_ENUM, then the final argument should be a
|
||
collection of strings. These strings are the valid values for this
|
||
parameter.
|
||
|
||
The documentation for the parameter is taken from the doc string
|
||
for the python class.
|
||
|
||
Returns -1 on error, with a python exception set. */
|
||
|
||
static int
|
||
parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
|
||
{
|
||
parmpy_object *obj = (parmpy_object *) self;
|
||
const char *name;
|
||
char *set_doc, *show_doc, *doc;
|
||
char *cmd_name;
|
||
int parmclass, cmdtype;
|
||
PyObject *enum_values = NULL;
|
||
struct cmd_list_element **set_list, **show_list;
|
||
|
||
if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
|
||
&enum_values))
|
||
return -1;
|
||
|
||
if (cmdtype != no_class && cmdtype != class_run
|
||
&& cmdtype != class_vars && cmdtype != class_stack
|
||
&& cmdtype != class_files && cmdtype != class_support
|
||
&& cmdtype != class_info && cmdtype != class_breakpoint
|
||
&& cmdtype != class_trace && cmdtype != class_obscure
|
||
&& cmdtype != class_maintenance)
|
||
{
|
||
PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
|
||
return -1;
|
||
}
|
||
|
||
if (parmclass != var_boolean /* ARI: var_boolean */
|
||
&& parmclass != var_auto_boolean
|
||
&& parmclass != var_uinteger && parmclass != var_integer
|
||
&& parmclass != var_string && parmclass != var_string_noescape
|
||
&& parmclass != var_optional_filename && parmclass != var_filename
|
||
&& parmclass != var_zinteger && parmclass != var_enum)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("Invalid parameter class argument."));
|
||
return -1;
|
||
}
|
||
|
||
if (enum_values && parmclass != var_enum)
|
||
{
|
||
PyErr_SetString (PyExc_RuntimeError,
|
||
_("Only PARAM_ENUM accepts a fourth argument."));
|
||
return -1;
|
||
}
|
||
if (parmclass == var_enum)
|
||
{
|
||
if (! compute_enum_values (obj, enum_values))
|
||
return -1;
|
||
}
|
||
else
|
||
obj->enumeration = NULL;
|
||
obj->type = (enum var_types) parmclass;
|
||
memset (&obj->value, 0, sizeof (obj->value));
|
||
|
||
cmd_name = gdbpy_parse_command_name (name, &set_list,
|
||
&setlist);
|
||
|
||
if (! cmd_name)
|
||
return -1;
|
||
xfree (cmd_name);
|
||
cmd_name = gdbpy_parse_command_name (name, &show_list,
|
||
&showlist);
|
||
if (! cmd_name)
|
||
return -1;
|
||
|
||
set_doc = get_doc_string (self, set_doc_cst).release ();
|
||
show_doc = get_doc_string (self, show_doc_cst).release ();
|
||
doc = get_doc_string (self, gdbpy_doc_cst).release ();
|
||
|
||
Py_INCREF (self);
|
||
|
||
TRY
|
||
{
|
||
add_setshow_generic (parmclass, (enum command_class) cmdtype,
|
||
cmd_name, obj,
|
||
set_doc, show_doc,
|
||
doc, set_list, show_list);
|
||
}
|
||
CATCH (except, RETURN_MASK_ALL)
|
||
{
|
||
xfree (cmd_name);
|
||
xfree (set_doc);
|
||
xfree (show_doc);
|
||
xfree (doc);
|
||
Py_DECREF (self);
|
||
PyErr_Format (except.reason == RETURN_QUIT
|
||
? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
|
||
"%s", except.message);
|
||
return -1;
|
||
}
|
||
END_CATCH
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
|
||
/* Initialize the 'parameters' module. */
|
||
int
|
||
gdbpy_initialize_parameters (void)
|
||
{
|
||
int i;
|
||
|
||
parmpy_object_type.tp_new = PyType_GenericNew;
|
||
if (PyType_Ready (&parmpy_object_type) < 0)
|
||
return -1;
|
||
|
||
set_doc_cst = PyString_FromString ("set_doc");
|
||
if (! set_doc_cst)
|
||
return -1;
|
||
show_doc_cst = PyString_FromString ("show_doc");
|
||
if (! show_doc_cst)
|
||
return -1;
|
||
|
||
for (i = 0; parm_constants[i].name; ++i)
|
||
{
|
||
if (PyModule_AddIntConstant (gdb_module,
|
||
parm_constants[i].name,
|
||
parm_constants[i].value) < 0)
|
||
return -1;
|
||
}
|
||
|
||
return gdb_pymodule_addobject (gdb_module, "Parameter",
|
||
(PyObject *) &parmpy_object_type);
|
||
}
|
||
|
||
|
||
|
||
PyTypeObject parmpy_object_type =
|
||
{
|
||
PyVarObject_HEAD_INIT (NULL, 0)
|
||
"gdb.Parameter", /*tp_name*/
|
||
sizeof (parmpy_object), /*tp_basicsize*/
|
||
0, /*tp_itemsize*/
|
||
0, /*tp_dealloc*/
|
||
0, /*tp_print*/
|
||
0, /*tp_getattr*/
|
||
0, /*tp_setattr*/
|
||
0, /*tp_compare*/
|
||
0, /*tp_repr*/
|
||
0, /*tp_as_number*/
|
||
0, /*tp_as_sequence*/
|
||
0, /*tp_as_mapping*/
|
||
0, /*tp_hash */
|
||
0, /*tp_call*/
|
||
0, /*tp_str*/
|
||
get_attr, /*tp_getattro*/
|
||
set_attr, /*tp_setattro*/
|
||
0, /*tp_as_buffer*/
|
||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
||
"GDB parameter object", /* tp_doc */
|
||
0, /* tp_traverse */
|
||
0, /* tp_clear */
|
||
0, /* tp_richcompare */
|
||
0, /* tp_weaklistoffset */
|
||
0, /* tp_iter */
|
||
0, /* tp_iternext */
|
||
0, /* tp_methods */
|
||
0, /* tp_members */
|
||
0, /* tp_getset */
|
||
0, /* tp_base */
|
||
0, /* tp_dict */
|
||
0, /* tp_descr_get */
|
||
0, /* tp_descr_set */
|
||
0, /* tp_dictoffset */
|
||
parmpy_init, /* tp_init */
|
||
0, /* tp_alloc */
|
||
};
|