+/* 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);
+ }
+}