binutils-gdb/gdb/python/py-param.c
Christian Biesinger 491144b5e2 Change boolean options to bool instead of int
This is for add_setshow_boolean_cmd as well as the gdb::option interface.

gdb/ChangeLog:

2019-09-17  Christian Biesinger  <cbiesinger@google.com>

	* ada-lang.c (ada_ignore_descriptive_types_p): Change to bool.
	(print_signatures): Likewise.
	(trust_pad_over_xvs): Likewise.
	* arch/aarch64-insn.c (aarch64_debug): Likewise.
	* arch/aarch64-insn.h (aarch64_debug): Likewise.
	* arm-linux-nat.c (arm_apcs_32): Likewise.
	* arm-linux-tdep.c (arm_apcs_32): Likewise.
	* arm-nbsd-nat.c (arm_apcs_32): Likewise.
	* arm-tdep.c (arm_debug): Likewise.
	(arm_apcs_32): Likewise.
	* auto-load.c (debug_auto_load): Likewise.
	(auto_load_gdb_scripts): Likewise.
	(global_auto_load): Likewise.
	(auto_load_local_gdbinit): Likewise.
	(auto_load_local_gdbinit_loaded): Likewise.
	* auto-load.h (global_auto_load): Likewise.
	(auto_load_local_gdbinit): Likewise.
	(auto_load_local_gdbinit_loaded): Likewise.
	* breakpoint.c (disconnected_dprintf): Likewise.
	(breakpoint_proceeded): Likewise.
	(automatic_hardware_breakpoints): Likewise.
	(always_inserted_mode): Likewise.
	(target_exact_watchpoints): Likewise.
	(_initialize_breakpoint): Update.
	* breakpoint.h (target_exact_watchpoints): Change to bool.
	* btrace.c (maint_btrace_pt_skip_pad): Likewise.
	* cli/cli-cmds.c (trace_commands): Likewise.
	* cli/cli-cmds.h (trace_commands): Likewise.
	* cli/cli-decode.c (add_setshow_boolean_cmd): Change int* argument
	to bool*.
	* cli/cli-logging.c (logging_overwrite): Change to bool.
	(logging_redirect): Likewise.
	(debug_redirect): Likewise.
	* cli/cli-option.h (option_def) <boolean>: Change return type to bool*.
	(struct boolean_option_def) <get_var_address_cb_>: Change return type
	to bool.
	<boolean_option_def>: Update.
	(struct flag_option_def): Change default type of Context to bool
	from int.
	<flag_option_def>: Change return type of var_address_cb_ to bool*.
	* cli/cli-setshow.c (do_set_command): Cast to bool* instead of int*.
	(get_setshow_command_value_string): Likewise.
	* cli/cli-style.c (cli_styling): Change to bool.
	(source_styling): Likewise.
	* cli/cli-style.h (source_styling): Likewise.
	(cli_styling): Likewise.
	* cli/cli-utils.h (struct qcs_flags) <quiet, cont, silent>: Change
	to bool.
	* command.h (var_types): Update comment.
	(add_setshow_boolean_cmd): Change int* var argument to bool*.
	* compile/compile-cplus-types.c (debug_compile_cplus_types): Change to
	bool.
	(debug_compile_cplus_scopes): Likewise.
	* compile/compile-internal.h (compile_debug): Likewise.
	* compile/compile.c (compile_debug): Likewise.
	(struct compile_options) <raw>: Likewise.
	* cp-support.c (catch_demangler_crashes): Likewise.
	* cris-tdep.c (usr_cmd_cris_version_valid): Likewise.
	(usr_cmd_cris_dwarf2_cfi): Likewise.
	* csky-tdep.c (csky_debug): Likewise.
	* darwin-nat.c (enable_mach_exceptions): Likewise.
	* dcache.c (dcache_enabled_p): Likewise.
	* defs.h (info_verbose): Likewise.
	* demangle.c (demangle): Likewise.
	(asm_demangle): Likewise.
	* dwarf-index-cache.c (debug_index_cache): Likewise.
	* dwarf2-frame.c (dwarf2_frame_unwinders_enabled_p): Likewise.
	* dwarf2-frame.h (dwarf2_frame_unwinders_enabled_p): Likewise.
	* dwarf2read.c (check_physname): Likewise.
	(use_deprecated_index_sections): Likewise.
	(dwarf_always_disassemble): Likewise.
	* eval.c (overload_resolution): Likewise.
	* event-top.c (set_editing_cmd_var): Likewise.
	(exec_done_display_p): Likewise.
	* event-top.h (set_editing_cmd_var): Likewise.
	(exec_done_display_p): Likewise.
	* exec.c (write_files): Likewise.
	* fbsd-nat.c (debug_fbsd_lwp): Likewise
	(debug_fbsd_nat): Likewise.
	* frame.h (struct frame_print_options) <print_raw_frame_arguments>:
	Likewise.
	(struct set_backtrace_options) <backtrace_past_main>: Likewise.
	<backtrace_past_entry> Likewise.
	* gdb-demangle.h (demangle): Likewise.
	(asm_demangle): Likewise.
	* gdb_bfd.c (bfd_sharing): Likewise.
	* gdbcore.h (write_files): Likewise.
	* gdbsupport/common-debug.c (show_debug_regs): Likewise.
	* gdbsupport/common-debug.h (show_debug_regs): Likewise.
	* gdbthread.h (print_thread_events): Likewise.
	* gdbtypes.c (opaque_type_resolution): Likewise.
	(strict_type_checking): Likewise.
	* gnu-nat.c (gnu_debug_flag): Likewise.
	* guile/scm-auto-load.c (auto_load_guile_scripts): Likewise.
	* guile/scm-param.c (pascm_variable): Add boolval.
	(add_setshow_generic): Update.
	(pascm_param_value): Update.
	(pascm_set_param_value_x): Update.
	* hppa-tdep.c (hppa_debug): Change to bool..
	* infcall.c (may_call_functions_p): Likewise.
	(coerce_float_to_double_p): Likewise.
	(unwind_on_signal_p): Likewise.
	(unwind_on_terminating_exception_p): Likewise.
	* infcmd.c (startup_with_shell): Likewise.
	* inferior.c (print_inferior_events): Likewise.
	* inferior.h (startup_with_shell): Likewise.
	(print_inferior_events): Likewise.
	* infrun.c (step_stop_if_no_debug): Likewise.
	(detach_fork): Likewise.
	(debug_displaced): Likewise.
	(disable_randomization): Likewise.
	(non_stop): Likewise.
	(non_stop_1): Likewise.
	(observer_mode): Likewise.
	(observer_mode_1): Likewise.
	(set_observer_mode): Update.
	(sched_multi): Change to bool.
	* infrun.h (debug_displaced): Likewise.
	(sched_multi): Likewise.
	(step_stop_if_no_debug): Likewise.
	(non_stop): Likewise.
	(disable_randomization): Likewise.
	* linux-tdep.c (use_coredump_filter): Likewise.
	(dump_excluded_mappings): Likewise.
	* linux-thread-db.c (auto_load_thread_db): Likewise.
	(check_thread_db_on_load): Likewise.
	* main.c (captured_main_1): Update.
	* maint-test-options.c (struct test_options_opts) <flag_opt, xx1_opt,
	xx2_opt, boolean_opt>: Change to bool.
	* maint-test-settings.c (maintenance_test_settings_boolean): Likewise.
	* maint.c (maintenance_profile_p): Likewise.
	(per_command_time): Likewise.
	(per_command_space): Likewise.
	(per_command_symtab): Likewise.
	* memattr.c (inaccessible_by_default): Likewise.
	* mi/mi-main.c (mi_async): Likewise.
	(mi_async_1): Likewise.
	* mips-tdep.c (mips64_transfers_32bit_regs_p): Likewise.
	* nat/fork-inferior.h (startup_with_shell): Likewise.
	* nat/linux-namespaces.c (debug_linux_namespaces): Likewise.
	* nat/linux-namespaces.h (debug_linux_namespaces): Likewise.
	* nios2-tdep.c (nios2_debug): Likewise.
	* or1k-tdep.c (or1k_debug): Likewise.
	* parse.c (parser_debug): Likewise.
	* parser-defs.h (parser_debug): Likewise.
	* printcmd.c (print_symbol_filename): Likewise.
	* proc-api.c (procfs_trace): Likewise.
	* python/py-auto-load.c (auto_load_python_scripts): Likewise.
	* python/py-param.c (union parmpy_variable): Add "bool boolval" field.
	(set_parameter_value): Update.
	(add_setshow_generic): Update.
	* python/py-value.c (copy_py_bool_obj): Change argument from int*
	to bool*.
	* python/python.c (gdbpy_parameter_value): Cast to bool* instead of
	int*.
	* ravenscar-thread.c (ravenscar_task_support): Change to bool.
	* record-btrace.c (record_btrace_target::store_registers): Update.
	* record-full.c (record_full_memory_query): Change to bool.
	(record_full_stop_at_limit): Likewise.
	* record-full.h (record_full_memory_query): Likewise.
	* remote-notif.c (notif_debug): Likewise.
	* remote-notif.h (notif_debug): Likewise.
	* remote.c (use_range_stepping): Likewise.
	(interrupt_on_connect): Likewise.
	(remote_break): Likewise.
	* ser-tcp.c (tcp_auto_retry): Likewise.
	* ser-unix.c (serial_hwflow): Likewise.
	* skip.c (debug_skip): Likewise.
	* solib-aix.c (solib_aix_debug): Likewise.
	* spu-tdep.c (spu_stop_on_load_p): Likewise.
	(spu_auto_flush_cache_p): Likewise.
	* stack.c (struct backtrace_cmd_options) <full, no_filters, hide>:
	Likewise.
	(struct info_print_options) <quiet>: Likewise.
	* symfile-debug.c (debug_symfile): Likewise.
	* symfile.c (auto_solib_add): Likewise.
	(separate_debug_file_debug): Likewise.
	* symfile.h (auto_solib_add): Likewise.
	(separate_debug_file_debug): Likewise.
	* symtab.c (basenames_may_differ): Likewise.
	(struct filename_partial_match_opts) <dirname, basename>: Likewise.
	(struct info_print_options) <quiet, exclude_minsyms>: Likewise.
	(struct info_types_options) <quiet>: Likewise.
	* symtab.h (demangle): Likewise.
	(basenames_may_differ): Likewise.
	* target-dcache.c (stack_cache_enabled_1): Likewise.
	(code_cache_enabled_1): Likewise.
	* target.c (trust_readonly): Likewise.
	(may_write_registers): Likewise.
	(may_write_memory): Likewise.
	(may_insert_breakpoints): Likewise.
	(may_insert_tracepoints): Likewise.
	(may_insert_fast_tracepoints): Likewise.
	(may_stop): Likewise.
	(auto_connect_native_target): Likewise.
	(target_stop_and_wait): Update.
	(target_async_permitted): Change to bool.
	(target_async_permitted_1): Likewise.
	(may_write_registers_1): Likewise.
	(may_write_memory_1): Likewise.
	(may_insert_breakpoints_1): Likewise.
	(may_insert_tracepoints_1): Likewise.
	(may_insert_fast_tracepoints_1): Likewise.
	(may_stop_1): Likewise.
	* target.h (target_async_permitted): Likewise.
	(may_write_registers): Likewise.
	(may_write_memory): Likewise.
	(may_insert_breakpoints): Likewise.
	(may_insert_tracepoints): Likewise.
	(may_insert_fast_tracepoints): Likewise.
	(may_stop): Likewise.
	* thread.c (struct info_threads_opts) <show_global_ids>: Likewise.
	(make_thread_apply_all_options_def_group): Change argument from int*
	to bool*.
	(thread_apply_all_command): Update.
	(print_thread_events): Change to bool.
	* top.c (confirm): Likewise.
	(command_editing_p): Likewise.
	(history_expansion_p): Likewise.
	(write_history_p): Likewise.
	(info_verbose): Likewise.
	* top.h (confirm): Likewise.
	(history_expansion_p): Likewise.
	* tracepoint.c (disconnected_tracing): Likewise.
	(circular_trace_buffer): Likewise.
	* typeprint.c (print_methods): Likewise.
	(print_typedefs): Likewise.
	* utils.c (debug_timestamp): Likewise.
	(sevenbit_strings): Likewise.
	(pagination_enabled): Likewise.
	* utils.h (sevenbit_strings): Likewise.
	(pagination_enabled): Likewise.
	* valops.c (overload_resolution): Likewise.
	* valprint.h (struct value_print_options) <prettyformat_arrays,
	prettyformat_structs, vtblprint, unionprint, addressprint, objectprint,
	stop_print_at_null, print_array_indexes, deref_ref, static_field_print,
	pascal_static_field_print, raw, summary, symbol_print, finish_print>:
	Likewise.
	* windows-nat.c (new_console): Likewise.
	(cygwin_exceptions): Likewise.
	(new_group): Likewise.
	(debug_exec): Likewise.
	(debug_events): Likewise.
	(debug_memory): Likewise.
	(debug_exceptions): Likewise.
	(useshell): Likewise.
	* windows-tdep.c (maint_display_all_tib): Likewise.
	* xml-support.c (debug_xml): Likewise.
2019-09-18 09:35:12 +09:00

823 lines
21 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* GDB parameters implemented in Python
Copyright (C) 2008-2019 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"
/* 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_ZUINTEGER", var_zuinteger },
{ "PARAM_ZUINTEGER_UNLIMITED", var_zuinteger_unlimited },
{ "PARAM_ENUM", var_enum },
{ NULL, 0 }
};
/* A union that can hold anything described by enum var_types. */
union parmpy_variable
{
/* Hold a boolean value. */
bool boolval;
/* Hold an integer value. */
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.boolval = 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:
case var_zuinteger:
case var_zuinteger_unlimited:
{
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;
switch (self->type)
{
case var_uinteger:
if (l == 0)
l = UINT_MAX;
/* Fall through. */
case var_zuinteger:
ok = (l >= 0 && l <= UINT_MAX);
break;
case var_zuinteger_unlimited:
ok = (l >= -1 && l <= INT_MAX);
break;
case var_integer:
ok = (l >= INT_MIN && l <= INT_MAX);
if (l == 0)
l = INT_MAX;
break;
case var_zinteger:
ok = (l >= INT_MIN && l <= INT_MAX);
break;
default:
gdb_assert_not_reached ("unknown var_ constant");
}
if (! ok)
{
PyErr_SetString (PyExc_RuntimeError,
_("Range exceeded."));
return -1;
}
if (self->type == var_uinteger || self->type == var_zuinteger)
self->value.uintval = (unsigned) l;
else
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 (const 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_handle_exception ();
}
const char *str = set_doc_string.get ();
if (str != nullptr && str[0] != '\0')
fprintf_filtered (gdb_stdout, "%s\n", str);
}
/* 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,
const char *cmd_name, parmpy_object *self,
const char *set_doc, const char *show_doc,
const 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.boolval, 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_zuinteger:
add_setshow_zuinteger_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_zuinteger_unlimited:
add_setshow_zuinteger_unlimited_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;
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;
}
gdb_argv holder (XCNEWVEC (char *, size + 1));
char **enumeration = holder.get ();
for (i = 0; i < size; ++i)
{
gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
if (item == NULL)
return 0;
if (! gdbpy_is_string (item.get ()))
{
PyErr_SetString (PyExc_RuntimeError,
_("The enumeration item not a string."));
return 0;
}
enumeration[i] = python_string_to_host_string (item.get ()).release ();
if (enumeration[i] == NULL)
return 0;
}
self->enumeration = const_cast<const char**> (holder.release ());
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;
gdb::unique_xmalloc_ptr<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_zuinteger
&& parmclass != var_zuinteger_unlimited && 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);
show_doc = get_doc_string (self, show_doc_cst);
doc = get_doc_string (self, gdbpy_doc_cst);
Py_INCREF (self);
try
{
add_setshow_generic (parmclass, (enum command_class) cmdtype,
cmd_name, obj,
set_doc.get (), show_doc.get (),
doc.get (), set_list, show_list);
}
catch (const gdb_exception &except)
{
xfree (cmd_name);
Py_DECREF (self);
gdbpy_convert_exception (except);
return -1;
}
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 */
};