1 /* General python/gdb code
3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
24 #include "cli/cli-script.h"
26 #include "progspace.h"
30 #include "exceptions.h"
31 #include "event-loop.h"
36 /* True if we should print the stack when catching a Python error,
38 static int gdbpy_should_print_stack
= 1;
43 #include "libiberty.h"
44 #include "cli/cli-decode.h"
48 #include "python-internal.h"
53 #include "gdbthread.h"
55 static PyMethodDef GdbMethods
[];
59 /* Some string constants we may wish to use. */
60 PyObject
*gdbpy_to_string_cst
;
61 PyObject
*gdbpy_children_cst
;
62 PyObject
*gdbpy_display_hint_cst
;
63 PyObject
*gdbpy_doc_cst
;
64 PyObject
*gdbpy_enabled_cst
;
66 /* The GdbError exception. */
67 PyObject
*gdbpy_gdberror_exc
;
69 /* The `gdb.error' base class. */
70 PyObject
*gdbpy_gdb_error
;
72 /* The `gdb.MemoryError' exception. */
73 PyObject
*gdbpy_gdb_memory_error
;
75 /* Architecture and language to be used in callbacks from
76 the Python interpreter. */
77 struct gdbarch
*python_gdbarch
;
78 const struct language_defn
*python_language
;
80 /* Restore global language and architecture and Python GIL state
81 when leaving the Python interpreter. */
85 PyGILState_STATE state
;
86 struct gdbarch
*gdbarch
;
87 const struct language_defn
*language
;
88 PyObject
*error_type
, *error_value
, *error_traceback
;
92 restore_python_env (void *p
)
94 struct python_env
*env
= (struct python_env
*)p
;
96 /* Leftover Python error is forbidden by Python Exception Handling. */
97 if (PyErr_Occurred ())
99 /* This order is similar to the one calling error afterwards. */
100 gdbpy_print_stack ();
101 warning (_("internal error: Unhandled Python exception"));
104 PyErr_Restore (env
->error_type
, env
->error_value
, env
->error_traceback
);
106 PyGILState_Release (env
->state
);
107 python_gdbarch
= env
->gdbarch
;
108 python_language
= env
->language
;
112 /* Called before entering the Python interpreter to install the
113 current language and architecture to be used for Python values. */
116 ensure_python_env (struct gdbarch
*gdbarch
,
117 const struct language_defn
*language
)
119 struct python_env
*env
= xmalloc (sizeof *env
);
121 env
->state
= PyGILState_Ensure ();
122 env
->gdbarch
= python_gdbarch
;
123 env
->language
= python_language
;
125 python_gdbarch
= gdbarch
;
126 python_language
= language
;
128 /* Save it and ensure ! PyErr_Occurred () afterwards. */
129 PyErr_Fetch (&env
->error_type
, &env
->error_value
, &env
->error_traceback
);
131 return make_cleanup (restore_python_env
, env
);
135 /* Given a command_line, return a command string suitable for passing
136 to Python. Lines in the string are separated by newlines. The
137 return value is allocated using xmalloc and the caller is
138 responsible for freeing it. */
141 compute_python_string (struct command_line
*l
)
143 struct command_line
*iter
;
148 for (iter
= l
; iter
; iter
= iter
->next
)
149 size
+= strlen (iter
->line
) + 1;
151 script
= xmalloc (size
+ 1);
153 for (iter
= l
; iter
; iter
= iter
->next
)
155 int len
= strlen (iter
->line
);
157 strcpy (&script
[here
], iter
->line
);
159 script
[here
++] = '\n';
165 /* Take a command line structure representing a 'python' command, and
166 evaluate its body using the Python interpreter. */
169 eval_python_from_control_command (struct command_line
*cmd
)
173 struct cleanup
*cleanup
;
175 if (cmd
->body_count
!= 1)
176 error (_("Invalid \"python\" block structure."));
178 cleanup
= ensure_python_env (get_current_arch (), current_language
);
180 script
= compute_python_string (cmd
->body_list
[0]);
181 ret
= PyRun_SimpleString (script
);
185 gdbpy_print_stack ();
186 error (_("Error while executing Python code."));
189 do_cleanups (cleanup
);
192 /* Implementation of the gdb "python" command. */
195 python_command (char *arg
, int from_tty
)
197 struct cleanup
*cleanup
;
199 cleanup
= ensure_python_env (get_current_arch (), current_language
);
200 while (arg
&& *arg
&& isspace (*arg
))
204 if (PyRun_SimpleString (arg
))
206 gdbpy_print_stack ();
207 error (_("Error while executing Python code."));
212 struct command_line
*l
= get_command_line (python_control
, "");
214 make_cleanup_free_command_lines (&l
);
215 execute_control_command_untraced (l
);
218 do_cleanups (cleanup
);
223 /* Transform a gdb parameters's value into a Python value. May return
224 NULL (and set a Python exception) on error. Helper function for
227 gdbpy_parameter_value (enum var_types type
, void *var
)
232 case var_string_noescape
:
233 case var_optional_filename
:
237 char *str
= * (char **) var
;
241 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
252 case var_auto_boolean
:
254 enum auto_boolean ab
= * (enum auto_boolean
*) var
;
256 if (ab
== AUTO_BOOLEAN_TRUE
)
258 else if (ab
== AUTO_BOOLEAN_FALSE
)
265 if ((* (int *) var
) == INT_MAX
)
269 return PyLong_FromLong (* (int *) var
);
273 unsigned int val
= * (unsigned int *) var
;
277 return PyLong_FromUnsignedLong (val
);
281 return PyErr_Format (PyExc_RuntimeError
,
282 _("Programmer error: unhandled type."));
285 /* A Python function which returns a gdb parameter's value as a Python
289 gdbpy_parameter (PyObject
*self
, PyObject
*args
)
291 struct cmd_list_element
*alias
, *prefix
, *cmd
;
294 volatile struct gdb_exception except
;
296 if (! PyArg_ParseTuple (args
, "s", &arg
))
299 newarg
= concat ("show ", arg
, (char *) NULL
);
301 TRY_CATCH (except
, RETURN_MASK_ALL
)
303 found
= lookup_cmd_composition (newarg
, &alias
, &prefix
, &cmd
);
306 GDB_PY_HANDLE_EXCEPTION (except
);
308 return PyErr_Format (PyExc_RuntimeError
,
309 _("Could not find parameter `%s'."), arg
);
312 return PyErr_Format (PyExc_RuntimeError
,
313 _("`%s' is not a parameter."), arg
);
314 return gdbpy_parameter_value (cmd
->var_type
, cmd
->var
);
317 /* Wrapper for target_charset. */
320 gdbpy_target_charset (PyObject
*self
, PyObject
*args
)
322 const char *cset
= target_charset (python_gdbarch
);
324 return PyUnicode_Decode (cset
, strlen (cset
), host_charset (), NULL
);
327 /* Wrapper for target_wide_charset. */
330 gdbpy_target_wide_charset (PyObject
*self
, PyObject
*args
)
332 const char *cset
= target_wide_charset (python_gdbarch
);
334 return PyUnicode_Decode (cset
, strlen (cset
), host_charset (), NULL
);
337 /* A Python function which evaluates a string using the gdb CLI. */
340 execute_gdb_command (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
343 PyObject
*from_tty_obj
= NULL
, *to_string_obj
= NULL
;
344 int from_tty
, to_string
;
345 volatile struct gdb_exception except
;
346 static char *keywords
[] = {"command", "from_tty", "to_string", NULL
};
349 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!O!", keywords
, &arg
,
350 &PyBool_Type
, &from_tty_obj
,
351 &PyBool_Type
, &to_string_obj
))
357 int cmp
= PyObject_IsTrue (from_tty_obj
);
366 int cmp
= PyObject_IsTrue (to_string_obj
);
372 TRY_CATCH (except
, RETURN_MASK_ALL
)
374 /* Copy the argument text in case the command modifies it. */
375 char *copy
= xstrdup (arg
);
376 struct cleanup
*cleanup
= make_cleanup (xfree
, copy
);
379 result
= execute_command_to_string (copy
, from_tty
);
383 execute_command (copy
, from_tty
);
386 do_cleanups (cleanup
);
388 GDB_PY_HANDLE_EXCEPTION (except
);
390 /* Do any commands attached to breakpoint we stopped at. */
391 bpstat_do_actions ();
395 PyObject
*r
= PyString_FromString (result
);
402 /* Implementation of gdb.solib_name (Long) -> String.
403 Returns the name of the shared library holding a given address, or None. */
406 gdbpy_solib_name (PyObject
*self
, PyObject
*args
)
412 if (!PyArg_ParseTuple (args
, GDB_PY_LL_ARG
, &pc
))
415 soname
= solib_name_from_address (current_program_space
, pc
);
417 str_obj
= PyString_Decode (soname
, strlen (soname
), host_charset (), NULL
);
427 /* A Python function which is a wrapper for decode_line_1. */
430 gdbpy_decode_line (PyObject
*self
, PyObject
*args
)
432 struct symtabs_and_lines sals
= { NULL
, 0 }; /* Initialize to
434 struct symtab_and_line sal
;
437 struct cleanup
*cleanups
;
438 PyObject
*result
= NULL
;
439 PyObject
*return_result
= NULL
;
440 PyObject
*unparsed
= NULL
;
441 volatile struct gdb_exception except
;
443 if (! PyArg_ParseTuple (args
, "|s", &arg
))
446 cleanups
= ensure_python_env (get_current_arch (), current_language
);
448 TRY_CATCH (except
, RETURN_MASK_ALL
)
453 make_cleanup (xfree
, arg
);
455 sals
= decode_line_1 (©
, 0, 0, 0, 0, 0);
456 make_cleanup (xfree
, sals
.sals
);
460 set_default_source_symtab_and_line ();
461 sal
= get_current_source_symtab_and_line ();
466 if (except
.reason
< 0)
468 do_cleanups (cleanups
);
469 /* We know this will always throw. */
470 GDB_PY_HANDLE_EXCEPTION (except
);
477 result
= PyTuple_New (sals
.nelts
);
480 for (i
= 0; i
< sals
.nelts
; ++i
)
485 obj
= symtab_and_line_to_sal_object (sals
.sals
[i
]);
492 PyTuple_SetItem (result
, i
, obj
);
501 return_result
= PyTuple_New (2);
508 if (copy
&& strlen (copy
) > 0)
509 unparsed
= PyString_FromString (copy
);
516 PyTuple_SetItem (return_result
, 0, unparsed
);
517 PyTuple_SetItem (return_result
, 1, result
);
519 do_cleanups (cleanups
);
521 return return_result
;
524 do_cleanups (cleanups
);
528 /* Parse a string and evaluate it as an expression. */
530 gdbpy_parse_and_eval (PyObject
*self
, PyObject
*args
)
533 struct value
*result
= NULL
;
534 volatile struct gdb_exception except
;
536 if (!PyArg_ParseTuple (args
, "s", &expr_str
))
539 TRY_CATCH (except
, RETURN_MASK_ALL
)
541 result
= parse_and_eval (expr_str
);
543 GDB_PY_HANDLE_EXCEPTION (except
);
545 return value_to_value_object (result
);
548 /* Read a file as Python code. STREAM is the input file; FILE is the
550 STREAM is not closed, that is the caller's responsibility. */
553 source_python_script (FILE *stream
, const char *file
)
555 struct cleanup
*cleanup
;
557 cleanup
= ensure_python_env (get_current_arch (), current_language
);
559 /* Note: If an exception occurs python will print the traceback and
560 clear the error indicator. */
561 PyRun_SimpleFile (stream
, file
);
563 do_cleanups (cleanup
);
568 /* Posting and handling events. */
570 /* A single event. */
573 /* The Python event. This is just a callable object. */
575 /* The next event. */
576 struct gdbpy_event
*next
;
579 /* All pending events. */
580 static struct gdbpy_event
*gdbpy_event_list
;
581 /* The final link of the event list. */
582 static struct gdbpy_event
**gdbpy_event_list_end
;
584 /* We use a file handler, and not an async handler, so that we can
585 wake up the main thread even when it is blocked in poll(). */
586 static struct serial
*gdbpy_event_fds
[2];
588 /* The file handler callback. This reads from the internal pipe, and
589 then processes the Python event queue. This will always be run in
590 the main gdb thread. */
593 gdbpy_run_events (struct serial
*scb
, void *context
)
595 struct cleanup
*cleanup
;
598 cleanup
= ensure_python_env (get_current_arch (), current_language
);
600 /* Flush the fd. Do this before flushing the events list, so that
601 any new event post afterwards is sure to re-awake the event
603 while (serial_readchar (gdbpy_event_fds
[0], 0) >= 0)
606 while (gdbpy_event_list
)
608 /* Dispatching the event might push a new element onto the event
609 loop, so we update here "atomically enough". */
610 struct gdbpy_event
*item
= gdbpy_event_list
;
611 gdbpy_event_list
= gdbpy_event_list
->next
;
612 if (gdbpy_event_list
== NULL
)
613 gdbpy_event_list_end
= &gdbpy_event_list
;
616 if (PyObject_CallObject (item
->event
, NULL
) == NULL
)
619 Py_DECREF (item
->event
);
623 do_cleanups (cleanup
);
626 /* Submit an event to the gdb thread. */
628 gdbpy_post_event (PyObject
*self
, PyObject
*args
)
630 struct gdbpy_event
*event
;
634 if (!PyArg_ParseTuple (args
, "O", &func
))
637 if (!PyCallable_Check (func
))
639 PyErr_SetString (PyExc_RuntimeError
,
640 _("Posted event is not callable"));
646 /* From here until the end of the function, we have the GIL, so we
647 can operate on our global data structures without worrying. */
648 wakeup
= gdbpy_event_list
== NULL
;
650 event
= XNEW (struct gdbpy_event
);
653 *gdbpy_event_list_end
= event
;
654 gdbpy_event_list_end
= &event
->next
;
656 /* Wake up gdb when needed. */
659 char c
= 'q'; /* Anything. */
661 if (serial_write (gdbpy_event_fds
[1], &c
, 1))
662 return PyErr_SetFromErrno (PyExc_IOError
);
668 /* Initialize the Python event handler. */
670 gdbpy_initialize_events (void)
672 if (serial_pipe (gdbpy_event_fds
) == 0)
674 gdbpy_event_list_end
= &gdbpy_event_list
;
675 serial_async (gdbpy_event_fds
[0], gdbpy_run_events
, NULL
);
681 /* A python function to write a single string using gdb's filtered
684 gdbpy_write (PyObject
*self
, PyObject
*args
)
688 if (! PyArg_ParseTuple (args
, "s", &arg
))
690 printf_filtered ("%s", arg
);
694 /* A python function to flush gdb's filtered output stream. */
696 gdbpy_flush (PyObject
*self
, PyObject
*args
)
698 gdb_flush (gdb_stdout
);
702 /* Print a python exception trace, or print nothing and clear the
703 python exception, depending on gdbpy_should_print_stack. Only call
704 this if a python exception is set. */
706 gdbpy_print_stack (void)
708 if (gdbpy_should_print_stack
)
711 /* PyErr_Print doesn't necessarily end output with a newline.
712 This works because Python's stdout/stderr is fed through
722 /* Return the current Progspace.
723 There always is one. */
726 gdbpy_get_current_progspace (PyObject
*unused1
, PyObject
*unused2
)
730 result
= pspace_to_pspace_object (current_program_space
);
736 /* Return a sequence holding all the Progspaces. */
739 gdbpy_progspaces (PyObject
*unused1
, PyObject
*unused2
)
741 struct program_space
*ps
;
744 list
= PyList_New (0);
750 PyObject
*item
= pspace_to_pspace_object (ps
);
752 if (!item
|| PyList_Append (list
, item
) == -1)
764 /* The "current" objfile. This is set when gdb detects that a new
765 objfile has been loaded. It is only set for the duration of a call to
766 source_python_script_for_objfile; it is NULL at other times. */
767 static struct objfile
*gdbpy_current_objfile
;
769 /* Set the current objfile to OBJFILE and then read STREAM,FILE as
773 source_python_script_for_objfile (struct objfile
*objfile
,
774 FILE *stream
, const char *file
)
776 struct cleanup
*cleanups
;
778 cleanups
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
779 gdbpy_current_objfile
= objfile
;
781 /* Note: If an exception occurs python will print the traceback and
782 clear the error indicator. */
783 PyRun_SimpleFile (stream
, file
);
785 do_cleanups (cleanups
);
786 gdbpy_current_objfile
= NULL
;
789 /* Return the current Objfile, or None if there isn't one. */
792 gdbpy_get_current_objfile (PyObject
*unused1
, PyObject
*unused2
)
796 if (! gdbpy_current_objfile
)
799 result
= objfile_to_objfile_object (gdbpy_current_objfile
);
805 /* Return a sequence holding all the Objfiles. */
808 gdbpy_objfiles (PyObject
*unused1
, PyObject
*unused2
)
810 struct objfile
*objf
;
813 list
= PyList_New (0);
819 PyObject
*item
= objfile_to_objfile_object (objf
);
821 if (!item
|| PyList_Append (list
, item
) == -1)
831 #else /* HAVE_PYTHON */
833 /* Dummy implementation of the gdb "python" command. */
836 python_command (char *arg
, int from_tty
)
838 while (arg
&& *arg
&& isspace (*arg
))
841 error (_("Python scripting is not supported in this copy of GDB."));
844 struct command_line
*l
= get_command_line (python_control
, "");
845 struct cleanup
*cleanups
= make_cleanup_free_command_lines (&l
);
847 execute_control_command_untraced (l
);
848 do_cleanups (cleanups
);
853 eval_python_from_control_command (struct command_line
*cmd
)
855 error (_("Python scripting is not supported in this copy of GDB."));
859 source_python_script (FILE *stream
, const char *file
)
861 throw_error (UNSUPPORTED_ERROR
,
862 _("Python scripting is not supported in this copy of GDB."));
865 #endif /* HAVE_PYTHON */
869 /* Lists for 'maint set python' commands. */
871 struct cmd_list_element
*set_python_list
;
872 struct cmd_list_element
*show_python_list
;
874 /* Function for use by 'maint set python' prefix command. */
877 set_python (char *args
, int from_tty
)
879 help_list (set_python_list
, "maintenance set python ", -1, gdb_stdout
);
882 /* Function for use by 'maint show python' prefix command. */
885 show_python (char *args
, int from_tty
)
887 cmd_show_list (show_python_list
, from_tty
, "");
890 /* Initialize the Python code. */
892 /* Provide a prototype to silence -Wmissing-prototypes. */
893 extern initialize_file_ftype _initialize_python
;
896 _initialize_python (void)
898 add_com ("python", class_obscure
, python_command
,
901 Evaluate a Python command.\n\
903 The command can be given as an argument, for instance:\n\
907 If no argument is given, the following lines are read and used\n\
908 as the Python commands. Type a line containing \"end\" to indicate\n\
909 the end of the command.")
910 #else /* HAVE_PYTHON */
912 Evaluate a Python command.\n\
914 Python scripting is not supported in this copy of GDB.\n\
915 This command is only a placeholder.")
916 #endif /* HAVE_PYTHON */
919 add_prefix_cmd ("python", no_class
, show_python
,
920 _("Prefix command for python maintenance settings."),
921 &show_python_list
, "maintenance show python ", 0,
922 &maintenance_show_cmdlist
);
923 add_prefix_cmd ("python", no_class
, set_python
,
924 _("Prefix command for python maintenance settings."),
925 &set_python_list
, "maintenance set python ", 0,
926 &maintenance_set_cmdlist
);
928 add_setshow_boolean_cmd ("print-stack", class_maintenance
,
929 &gdbpy_should_print_stack
, _("\
930 Enable or disable printing of Python stack dump on error."), _("\
931 Show whether Python stack will be printed on error."), _("\
932 Enables or disables printing of Python stack traces."),
938 #ifdef WITH_PYTHON_PATH
939 /* Work around problem where python gets confused about where it is,
940 and then can't find its libraries, etc.
941 NOTE: Python assumes the following layout:
943 /foo/lib/pythonX.Y/...
944 This must be done before calling Py_Initialize. */
945 Py_SetProgramName (concat (ldirname (python_libdir
), SLASH_STRING
, "bin",
946 SLASH_STRING
, "python", NULL
));
950 PyEval_InitThreads ();
952 gdb_module
= Py_InitModule ("gdb", GdbMethods
);
954 /* The casts to (char*) are for python 2.4. */
955 PyModule_AddStringConstant (gdb_module
, "VERSION", (char*) version
);
956 PyModule_AddStringConstant (gdb_module
, "HOST_CONFIG", (char*) host_name
);
957 PyModule_AddStringConstant (gdb_module
, "TARGET_CONFIG",
958 (char*) target_name
);
960 /* gdb.parameter ("data-directory") doesn't necessarily exist when the python
961 script below is run (depending on order of _initialize_* functions).
962 Define the initial value of gdb.PYTHONDIR here. */
966 gdb_pythondir
= concat (gdb_datadir
, SLASH_STRING
, "python", NULL
);
967 PyModule_AddStringConstant (gdb_module
, "PYTHONDIR", gdb_pythondir
);
968 xfree (gdb_pythondir
);
971 gdbpy_gdb_error
= PyErr_NewException ("gdb.error", PyExc_RuntimeError
, NULL
);
972 PyModule_AddObject (gdb_module
, "error", gdbpy_gdb_error
);
974 gdbpy_gdb_memory_error
= PyErr_NewException ("gdb.MemoryError",
975 gdbpy_gdb_error
, NULL
);
976 PyModule_AddObject (gdb_module
, "MemoryError", gdbpy_gdb_memory_error
);
978 gdbpy_gdberror_exc
= PyErr_NewException ("gdb.GdbError", NULL
, NULL
);
979 PyModule_AddObject (gdb_module
, "GdbError", gdbpy_gdberror_exc
);
981 gdbpy_initialize_auto_load ();
982 gdbpy_initialize_values ();
983 gdbpy_initialize_frames ();
984 gdbpy_initialize_commands ();
985 gdbpy_initialize_symbols ();
986 gdbpy_initialize_symtabs ();
987 gdbpy_initialize_blocks ();
988 gdbpy_initialize_functions ();
989 gdbpy_initialize_parameters ();
990 gdbpy_initialize_types ();
991 gdbpy_initialize_pspace ();
992 gdbpy_initialize_objfile ();
993 gdbpy_initialize_breakpoints ();
994 gdbpy_initialize_lazy_string ();
995 gdbpy_initialize_thread ();
996 gdbpy_initialize_inferior ();
997 gdbpy_initialize_events ();
999 PyRun_SimpleString ("import gdb");
1000 PyRun_SimpleString ("gdb.pretty_printers = []");
1002 gdbpy_to_string_cst
= PyString_FromString ("to_string");
1003 gdbpy_children_cst
= PyString_FromString ("children");
1004 gdbpy_display_hint_cst
= PyString_FromString ("display_hint");
1005 gdbpy_doc_cst
= PyString_FromString ("__doc__");
1006 gdbpy_enabled_cst
= PyString_FromString ("enabled");
1008 /* Release the GIL while gdb runs. */
1009 PyThreadState_Swap (NULL
);
1010 PyEval_ReleaseLock ();
1012 #endif /* HAVE_PYTHON */
1017 /* Perform the remaining python initializations.
1018 These must be done after GDB is at least mostly initialized.
1019 E.g., The "info pretty-printer" command needs the "info" prefix
1020 command installed. */
1023 finish_python_initialization (void)
1025 struct cleanup
*cleanup
;
1027 cleanup
= ensure_python_env (get_current_arch (), current_language
);
1029 PyRun_SimpleString ("\
1033 class GdbOutputFile:\n\
1038 def isatty(self):\n\
1041 def write(self, s):\n\
1044 def writelines(self, iterable):\n\
1045 for line in iterable:\n\
1051 sys.stderr = GdbOutputFile()\n\
1052 sys.stdout = GdbOutputFile()\n\
1054 # Ideally this would live in the gdb module, but it's intentionally written\n\
1055 # in python, and we need this to bootstrap the gdb module.\n\
1057 def GdbSetPythonDirectory (dir):\n\
1058 \"Set gdb.PYTHONDIR and update sys.path,etc.\"\n\
1059 old_dir = gdb.PYTHONDIR\n\
1060 gdb.PYTHONDIR = dir\n\
1061 # GDB's python scripts are stored inside gdb.PYTHONDIR. So insert\n\
1062 # that directory name at the start of sys.path to allow the Python\n\
1063 # interpreter to find them.\n\
1064 if old_dir in sys.path:\n\
1065 sys.path.remove (old_dir)\n\
1066 sys.path.insert (0, gdb.PYTHONDIR)\n\
1068 # Tell python where to find submodules of gdb.\n\
1069 gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
1071 # The gdb module is implemented in C rather than in Python. As a result,\n\
1072 # the associated __init.py__ script is not not executed by default when\n\
1073 # the gdb module gets imported. Execute that script manually if it\n\
1075 ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\
1076 if os.path.exists (ipy):\n\
1079 # Install the default gdb.PYTHONDIR.\n\
1080 GdbSetPythonDirectory (gdb.PYTHONDIR)\n\
1083 do_cleanups (cleanup
);
1086 #endif /* HAVE_PYTHON */
1092 static PyMethodDef GdbMethods
[] =
1094 { "history", gdbpy_history
, METH_VARARGS
,
1095 "Get a value from history" },
1096 { "execute", (PyCFunction
) execute_gdb_command
, METH_VARARGS
| METH_KEYWORDS
,
1097 "Execute a gdb command" },
1098 { "parameter", gdbpy_parameter
, METH_VARARGS
,
1099 "Return a gdb parameter's value" },
1101 { "breakpoints", gdbpy_breakpoints
, METH_NOARGS
,
1102 "Return a tuple of all breakpoint objects" },
1104 { "default_visualizer", gdbpy_default_visualizer
, METH_VARARGS
,
1105 "Find the default visualizer for a Value." },
1107 { "current_progspace", gdbpy_get_current_progspace
, METH_NOARGS
,
1108 "Return the current Progspace." },
1109 { "progspaces", gdbpy_progspaces
, METH_NOARGS
,
1110 "Return a sequence of all progspaces." },
1112 { "current_objfile", gdbpy_get_current_objfile
, METH_NOARGS
,
1113 "Return the current Objfile being loaded, or None." },
1114 { "objfiles", gdbpy_objfiles
, METH_NOARGS
,
1115 "Return a sequence of all loaded objfiles." },
1117 { "newest_frame", gdbpy_newest_frame
, METH_NOARGS
,
1118 "newest_frame () -> gdb.Frame.\n\
1119 Return the newest frame object." },
1120 { "selected_frame", gdbpy_selected_frame
, METH_NOARGS
,
1121 "selected_frame () -> gdb.Frame.\n\
1122 Return the selected frame object." },
1123 { "frame_stop_reason_string", gdbpy_frame_stop_reason_string
, METH_VARARGS
,
1124 "stop_reason_string (Integer) -> String.\n\
1125 Return a string explaining unwind stop reason." },
1127 { "lookup_type", (PyCFunction
) gdbpy_lookup_type
,
1128 METH_VARARGS
| METH_KEYWORDS
,
1129 "lookup_type (name [, block]) -> type\n\
1130 Return a Type corresponding to the given name." },
1131 { "lookup_symbol", (PyCFunction
) gdbpy_lookup_symbol
,
1132 METH_VARARGS
| METH_KEYWORDS
,
1133 "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
1134 Return a tuple with the symbol corresponding to the given name (or None) and\n\
1135 a boolean indicating if name is a field of the current implied argument\n\
1136 `this' (when the current language is object-oriented)." },
1137 { "block_for_pc", gdbpy_block_for_pc
, METH_VARARGS
,
1138 "Return the block containing the given pc value, or None." },
1139 { "solib_name", gdbpy_solib_name
, METH_VARARGS
,
1140 "solib_name (Long) -> String.\n\
1141 Return the name of the shared library holding a given address, or None." },
1142 { "decode_line", gdbpy_decode_line
, METH_VARARGS
,
1143 "decode_line (String) -> Tuple. Decode a string argument the way\n\
1144 that 'break' or 'edit' does. Return a tuple containing two elements.\n\
1145 The first element contains any unparsed portion of the String parameter\n\
1146 (or None if the string was fully parsed). The second element contains\n\
1147 a tuple that contains all the locations that match, represented as\n\
1148 gdb.Symtab_and_line objects (or None)."},
1149 { "parse_and_eval", gdbpy_parse_and_eval
, METH_VARARGS
,
1150 "parse_and_eval (String) -> Value.\n\
1151 Parse String as an expression, evaluate it, and return the result as a Value."
1154 { "post_event", gdbpy_post_event
, METH_VARARGS
,
1155 "Post an event into gdb's event loop." },
1157 { "target_charset", gdbpy_target_charset
, METH_NOARGS
,
1158 "target_charset () -> string.\n\
1159 Return the name of the current target charset." },
1160 { "target_wide_charset", gdbpy_target_wide_charset
, METH_NOARGS
,
1161 "target_wide_charset () -> string.\n\
1162 Return the name of the current target wide charset." },
1164 { "string_to_argv", gdbpy_string_to_argv
, METH_VARARGS
,
1165 "string_to_argv (String) -> Array.\n\
1166 Parse String and return an argv-like array.\n\
1167 Arguments are separate by spaces and may be quoted."
1170 { "write", gdbpy_write
, METH_VARARGS
,
1171 "Write a string using gdb's filtered stream." },
1172 { "flush", gdbpy_flush
, METH_NOARGS
,
1173 "Flush gdb's filtered stdout stream." },
1174 { "selected_thread", gdbpy_selected_thread
, METH_NOARGS
,
1175 "selected_thread () -> gdb.InferiorThread.\n\
1176 Return the selected thread object." },
1177 { "inferiors", gdbpy_inferiors
, METH_NOARGS
,
1178 "inferiors () -> (gdb.Inferior, ...).\n\
1179 Return a tuple containing all inferiors." },
1180 {NULL
, NULL
, 0, NULL
}
1183 #endif /* HAVE_PYTHON */