python.c revision 1.6 1 1.1 christos /* General python/gdb code
2 1.1 christos
3 1.6 christos Copyright (C) 2008-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "arch-utils.h"
22 1.1 christos #include "command.h"
23 1.1 christos #include "ui-out.h"
24 1.1 christos #include "cli/cli-script.h"
25 1.1 christos #include "gdbcmd.h"
26 1.1 christos #include "progspace.h"
27 1.1 christos #include "objfiles.h"
28 1.1 christos #include "value.h"
29 1.1 christos #include "language.h"
30 1.1 christos #include "event-loop.h"
31 1.1 christos #include "serial.h"
32 1.1 christos #include "readline/tilde.h"
33 1.1 christos #include "python.h"
34 1.3 christos #include "extension-priv.h"
35 1.1 christos #include "cli/cli-utils.h"
36 1.1 christos #include <ctype.h>
37 1.6 christos #include "location.h"
38 1.6 christos #include "ser-event.h"
39 1.1 christos
40 1.1 christos /* Declared constants and enum for python stack printing. */
41 1.1 christos static const char python_excp_none[] = "none";
42 1.1 christos static const char python_excp_full[] = "full";
43 1.1 christos static const char python_excp_message[] = "message";
44 1.1 christos
45 1.1 christos /* "set python print-stack" choices. */
46 1.1 christos static const char *const python_excp_enums[] =
47 1.1 christos {
48 1.1 christos python_excp_none,
49 1.1 christos python_excp_full,
50 1.1 christos python_excp_message,
51 1.1 christos NULL
52 1.1 christos };
53 1.1 christos
54 1.1 christos /* The exception printing variable. 'full' if we want to print the
55 1.1 christos error message and stack, 'none' if we want to print nothing, and
56 1.1 christos 'message' if we only want to print the error message. 'message' is
57 1.1 christos the default. */
58 1.1 christos static const char *gdbpy_should_print_stack = python_excp_message;
59 1.1 christos
60 1.1 christos #ifdef HAVE_PYTHON
61 1.3 christos /* Forward decls, these are defined later. */
62 1.5 christos extern const struct extension_language_script_ops python_extension_script_ops;
63 1.5 christos extern const struct extension_language_ops python_extension_ops;
64 1.3 christos #endif
65 1.3 christos
66 1.3 christos /* The main struct describing GDB's interface to the Python
67 1.3 christos extension language. */
68 1.3 christos const struct extension_language_defn extension_language_python =
69 1.3 christos {
70 1.3 christos EXT_LANG_PYTHON,
71 1.3 christos "python",
72 1.3 christos "Python",
73 1.3 christos
74 1.3 christos ".py",
75 1.3 christos "-gdb.py",
76 1.3 christos
77 1.3 christos python_control,
78 1.3 christos
79 1.3 christos #ifdef HAVE_PYTHON
80 1.3 christos &python_extension_script_ops,
81 1.3 christos &python_extension_ops
82 1.3 christos #else
83 1.3 christos NULL,
84 1.3 christos NULL
85 1.3 christos #endif
86 1.3 christos };
87 1.3 christos
88 1.3 christos #ifdef HAVE_PYTHON
90 1.1 christos
91 1.1 christos #include "cli/cli-decode.h"
92 1.1 christos #include "charset.h"
93 1.1 christos #include "top.h"
94 1.1 christos #include "solib.h"
95 1.1 christos #include "python-internal.h"
96 1.1 christos #include "linespec.h"
97 1.1 christos #include "source.h"
98 1.1 christos #include "version.h"
99 1.1 christos #include "target.h"
100 1.1 christos #include "gdbthread.h"
101 1.1 christos #include "interps.h"
102 1.1 christos #include "event-top.h"
103 1.1 christos
104 1.1 christos /* True if Python has been successfully initialized, false
105 1.1 christos otherwise. */
106 1.1 christos
107 1.1 christos int gdb_python_initialized;
108 1.5 christos
109 1.1 christos extern PyMethodDef python_GdbMethods[];
110 1.1 christos
111 1.5 christos #ifdef IS_PY3K
112 1.1 christos extern struct PyModuleDef python_GdbModuleDef;
113 1.1 christos #endif
114 1.1 christos
115 1.1 christos PyObject *gdb_module;
116 1.1 christos PyObject *gdb_python_module;
117 1.1 christos
118 1.1 christos /* Some string constants we may wish to use. */
119 1.1 christos PyObject *gdbpy_to_string_cst;
120 1.1 christos PyObject *gdbpy_children_cst;
121 1.1 christos PyObject *gdbpy_display_hint_cst;
122 1.1 christos PyObject *gdbpy_doc_cst;
123 1.1 christos PyObject *gdbpy_enabled_cst;
124 1.1 christos PyObject *gdbpy_value_cst;
125 1.1 christos
126 1.1 christos /* The GdbError exception. */
127 1.1 christos PyObject *gdbpy_gdberror_exc;
128 1.1 christos
129 1.1 christos /* The `gdb.error' base class. */
130 1.1 christos PyObject *gdbpy_gdb_error;
131 1.1 christos
132 1.1 christos /* The `gdb.MemoryError' exception. */
133 1.1 christos PyObject *gdbpy_gdb_memory_error;
134 1.3 christos
135 1.3 christos static script_sourcer_func gdbpy_source_script;
136 1.5 christos static objfile_script_sourcer_func gdbpy_source_objfile_script;
137 1.3 christos static objfile_script_executor_func gdbpy_execute_objfile_script;
138 1.3 christos static void gdbpy_finish_initialization
139 1.3 christos (const struct extension_language_defn *);
140 1.3 christos static int gdbpy_initialized (const struct extension_language_defn *);
141 1.3 christos static void gdbpy_eval_from_control_command
142 1.3 christos (const struct extension_language_defn *, struct command_line *cmd);
143 1.3 christos static void gdbpy_start_type_printers (const struct extension_language_defn *,
144 1.3 christos struct ext_lang_type_printers *);
145 1.3 christos static enum ext_lang_rc gdbpy_apply_type_printers
146 1.3 christos (const struct extension_language_defn *,
147 1.3 christos const struct ext_lang_type_printers *, struct type *, char **);
148 1.3 christos static void gdbpy_free_type_printers (const struct extension_language_defn *,
149 1.3 christos struct ext_lang_type_printers *);
150 1.3 christos static void gdbpy_set_quit_flag (const struct extension_language_defn *);
151 1.3 christos static int gdbpy_check_quit_flag (const struct extension_language_defn *);
152 1.3 christos static enum ext_lang_rc gdbpy_before_prompt_hook
153 1.3 christos (const struct extension_language_defn *, const char *current_gdb_prompt);
154 1.3 christos
155 1.3 christos /* The interface between gdb proper and loading of python scripts. */
156 1.5 christos
157 1.3 christos const struct extension_language_script_ops python_extension_script_ops =
158 1.3 christos {
159 1.3 christos gdbpy_source_script,
160 1.5 christos gdbpy_source_objfile_script,
161 1.3 christos gdbpy_execute_objfile_script,
162 1.3 christos gdbpy_auto_load_enabled
163 1.3 christos };
164 1.3 christos
165 1.3 christos /* The interface between gdb proper and python extensions. */
166 1.5 christos
167 1.3 christos const struct extension_language_ops python_extension_ops =
168 1.3 christos {
169 1.3 christos gdbpy_finish_initialization,
170 1.3 christos gdbpy_initialized,
171 1.3 christos
172 1.3 christos gdbpy_eval_from_control_command,
173 1.3 christos
174 1.3 christos gdbpy_start_type_printers,
175 1.3 christos gdbpy_apply_type_printers,
176 1.3 christos gdbpy_free_type_printers,
177 1.3 christos
178 1.3 christos gdbpy_apply_val_pretty_printer,
179 1.3 christos
180 1.3 christos gdbpy_apply_frame_filter,
181 1.3 christos
182 1.3 christos gdbpy_preserve_values,
183 1.3 christos
184 1.3 christos gdbpy_breakpoint_has_cond,
185 1.3 christos gdbpy_breakpoint_cond_says_stop,
186 1.3 christos
187 1.3 christos gdbpy_set_quit_flag,
188 1.3 christos gdbpy_check_quit_flag,
189 1.3 christos
190 1.3 christos gdbpy_before_prompt_hook,
191 1.3 christos
192 1.3 christos gdbpy_clone_xmethod_worker_data,
193 1.3 christos gdbpy_free_xmethod_worker_data,
194 1.3 christos gdbpy_get_matching_xmethod_workers,
195 1.3 christos gdbpy_get_xmethod_arg_types,
196 1.3 christos gdbpy_get_xmethod_result_type,
197 1.3 christos gdbpy_invoke_xmethod
198 1.3 christos };
199 1.1 christos
200 1.1 christos /* Architecture and language to be used in callbacks from
201 1.1 christos the Python interpreter. */
202 1.1 christos struct gdbarch *python_gdbarch;
203 1.1 christos const struct language_defn *python_language;
204 1.1 christos
205 1.1 christos /* Restore global language and architecture and Python GIL state
206 1.1 christos when leaving the Python interpreter. */
207 1.1 christos
208 1.1 christos struct python_env
209 1.3 christos {
210 1.1 christos struct active_ext_lang_state *previous_active;
211 1.1 christos PyGILState_STATE state;
212 1.1 christos struct gdbarch *gdbarch;
213 1.1 christos const struct language_defn *language;
214 1.1 christos PyObject *error_type, *error_value, *error_traceback;
215 1.1 christos };
216 1.1 christos
217 1.1 christos static void
218 1.1 christos restore_python_env (void *p)
219 1.1 christos {
220 1.1 christos struct python_env *env = (struct python_env *)p;
221 1.1 christos
222 1.1 christos /* Leftover Python error is forbidden by Python Exception Handling. */
223 1.1 christos if (PyErr_Occurred ())
224 1.1 christos {
225 1.1 christos /* This order is similar to the one calling error afterwards. */
226 1.1 christos gdbpy_print_stack ();
227 1.1 christos warning (_("internal error: Unhandled Python exception"));
228 1.1 christos }
229 1.1 christos
230 1.1 christos PyErr_Restore (env->error_type, env->error_value, env->error_traceback);
231 1.1 christos
232 1.1 christos PyGILState_Release (env->state);
233 1.1 christos python_gdbarch = env->gdbarch;
234 1.3 christos python_language = env->language;
235 1.3 christos
236 1.3 christos restore_active_ext_lang (env->previous_active);
237 1.1 christos
238 1.1 christos xfree (env);
239 1.1 christos }
240 1.1 christos
241 1.3 christos /* Called before entering the Python interpreter to install the
242 1.3 christos current language and architecture to be used for Python values.
243 1.3 christos Also set the active extension language for GDB so that SIGINT's
244 1.3 christos are directed our way, and if necessary install the right SIGINT
245 1.1 christos handler. */
246 1.1 christos
247 1.1 christos struct cleanup *
248 1.1 christos ensure_python_env (struct gdbarch *gdbarch,
249 1.1 christos const struct language_defn *language)
250 1.6 christos {
251 1.1 christos struct python_env *env = XNEW (struct python_env);
252 1.1 christos
253 1.1 christos /* We should not ever enter Python unless initialized. */
254 1.1 christos if (!gdb_python_initialized)
255 1.1 christos error (_("Python not initialized"));
256 1.3 christos
257 1.3 christos env->previous_active = set_active_ext_lang (&extension_language_python);
258 1.1 christos
259 1.1 christos env->state = PyGILState_Ensure ();
260 1.1 christos env->gdbarch = python_gdbarch;
261 1.1 christos env->language = python_language;
262 1.1 christos
263 1.1 christos python_gdbarch = gdbarch;
264 1.1 christos python_language = language;
265 1.1 christos
266 1.1 christos /* Save it and ensure ! PyErr_Occurred () afterwards. */
267 1.1 christos PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback);
268 1.1 christos
269 1.1 christos return make_cleanup (restore_python_env, env);
270 1.1 christos }
271 1.1 christos
272 1.1 christos /* Set the quit flag. */
273 1.3 christos
274 1.3 christos static void
275 1.1 christos gdbpy_set_quit_flag (const struct extension_language_defn *extlang)
276 1.1 christos {
277 1.1 christos PyErr_SetInterrupt ();
278 1.1 christos }
279 1.1 christos
280 1.1 christos /* Return true if the quit flag has been set, false otherwise. */
281 1.3 christos
282 1.3 christos static int
283 1.1 christos gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
284 1.1 christos {
285 1.1 christos return PyOS_InterruptOccurred ();
286 1.1 christos }
287 1.1 christos
288 1.1 christos /* Evaluate a Python command like PyRun_SimpleString, but uses
289 1.1 christos Py_single_input which prints the result of expressions, and does
290 1.1 christos not automatically print the stack on errors. */
291 1.1 christos
292 1.1 christos static int
293 1.1 christos eval_python_command (const char *command)
294 1.1 christos {
295 1.1 christos PyObject *m, *d, *v;
296 1.1 christos
297 1.1 christos m = PyImport_AddModule ("__main__");
298 1.1 christos if (m == NULL)
299 1.1 christos return -1;
300 1.1 christos
301 1.1 christos d = PyModule_GetDict (m);
302 1.1 christos if (d == NULL)
303 1.1 christos return -1;
304 1.1 christos v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
305 1.1 christos if (v == NULL)
306 1.1 christos return -1;
307 1.1 christos
308 1.1 christos Py_DECREF (v);
309 1.1 christos #ifndef IS_PY3K
310 1.1 christos if (Py_FlushLine ())
311 1.1 christos PyErr_Clear ();
312 1.1 christos #endif
313 1.1 christos
314 1.1 christos return 0;
315 1.1 christos }
316 1.1 christos
317 1.1 christos /* Implementation of the gdb "python-interactive" command. */
318 1.1 christos
319 1.1 christos static void
320 1.1 christos python_interactive_command (char *arg, int from_tty)
321 1.6 christos {
322 1.1 christos struct ui *ui = current_ui;
323 1.1 christos struct cleanup *cleanup;
324 1.1 christos int err;
325 1.6 christos
326 1.6 christos cleanup = make_cleanup_restore_integer (¤t_ui->async);
327 1.1 christos current_ui->async = 0;
328 1.1 christos
329 1.1 christos arg = skip_spaces (arg);
330 1.1 christos
331 1.1 christos ensure_python_env (get_current_arch (), current_language);
332 1.1 christos
333 1.1 christos if (arg && *arg)
334 1.1 christos {
335 1.6 christos int len = strlen (arg);
336 1.1 christos char *script = (char *) xmalloc (len + 2);
337 1.1 christos
338 1.1 christos strcpy (script, arg);
339 1.1 christos script[len] = '\n';
340 1.1 christos script[len + 1] = '\0';
341 1.1 christos err = eval_python_command (script);
342 1.1 christos xfree (script);
343 1.1 christos }
344 1.1 christos else
345 1.6 christos {
346 1.1 christos err = PyRun_InteractiveLoop (ui->instream, "<stdin>");
347 1.1 christos dont_repeat ();
348 1.1 christos }
349 1.1 christos
350 1.1 christos if (err)
351 1.1 christos {
352 1.1 christos gdbpy_print_stack ();
353 1.1 christos error (_("Error while executing Python code."));
354 1.1 christos }
355 1.1 christos
356 1.1 christos do_cleanups (cleanup);
357 1.1 christos }
358 1.1 christos
359 1.1 christos /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
360 1.1 christos named FILENAME.
361 1.1 christos
362 1.1 christos On Windows hosts few users would build Python themselves (this is no
363 1.1 christos trivial task on this platform), and thus use binaries built by
364 1.1 christos someone else instead. There may happen situation where the Python
365 1.1 christos library and GDB are using two different versions of the C runtime
366 1.1 christos library. Python, being built with VC, would use one version of the
367 1.1 christos msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
368 1.1 christos A FILE * from one runtime does not necessarily operate correctly in
369 1.1 christos the other runtime.
370 1.1 christos
371 1.1 christos To work around this potential issue, we create on Windows hosts the
372 1.1 christos FILE object using Python routines, thus making sure that it is
373 1.1 christos compatible with the Python library. */
374 1.1 christos
375 1.1 christos static void
376 1.1 christos python_run_simple_file (FILE *file, const char *filename)
377 1.1 christos {
378 1.1 christos #ifndef _WIN32
379 1.1 christos
380 1.1 christos PyRun_SimpleFile (file, filename);
381 1.1 christos
382 1.1 christos #else /* _WIN32 */
383 1.1 christos
384 1.1 christos char *full_path;
385 1.1 christos PyObject *python_file;
386 1.1 christos struct cleanup *cleanup;
387 1.1 christos
388 1.1 christos /* Because we have a string for a filename, and are using Python to
389 1.1 christos open the file, we need to expand any tilde in the path first. */
390 1.1 christos full_path = tilde_expand (filename);
391 1.1 christos cleanup = make_cleanup (xfree, full_path);
392 1.1 christos python_file = PyFile_FromString (full_path, "r");
393 1.1 christos if (! python_file)
394 1.1 christos {
395 1.1 christos do_cleanups (cleanup);
396 1.1 christos gdbpy_print_stack ();
397 1.1 christos error (_("Error while opening file: %s"), full_path);
398 1.1 christos }
399 1.1 christos
400 1.1 christos make_cleanup_py_decref (python_file);
401 1.1 christos PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
402 1.1 christos do_cleanups (cleanup);
403 1.1 christos
404 1.1 christos #endif /* _WIN32 */
405 1.1 christos }
406 1.1 christos
407 1.1 christos /* Given a command_line, return a command string suitable for passing
408 1.1 christos to Python. Lines in the string are separated by newlines. The
409 1.1 christos return value is allocated using xmalloc and the caller is
410 1.1 christos responsible for freeing it. */
411 1.1 christos
412 1.1 christos static char *
413 1.1 christos compute_python_string (struct command_line *l)
414 1.1 christos {
415 1.1 christos struct command_line *iter;
416 1.1 christos char *script = NULL;
417 1.1 christos int size = 0;
418 1.1 christos int here;
419 1.1 christos
420 1.1 christos for (iter = l; iter; iter = iter->next)
421 1.1 christos size += strlen (iter->line) + 1;
422 1.6 christos
423 1.1 christos script = (char *) xmalloc (size + 1);
424 1.1 christos here = 0;
425 1.1 christos for (iter = l; iter; iter = iter->next)
426 1.1 christos {
427 1.1 christos int len = strlen (iter->line);
428 1.1 christos
429 1.1 christos strcpy (&script[here], iter->line);
430 1.1 christos here += len;
431 1.1 christos script[here++] = '\n';
432 1.1 christos }
433 1.1 christos script[here] = '\0';
434 1.1 christos return script;
435 1.1 christos }
436 1.1 christos
437 1.1 christos /* Take a command line structure representing a 'python' command, and
438 1.1 christos evaluate its body using the Python interpreter. */
439 1.3 christos
440 1.3 christos static void
441 1.3 christos gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
442 1.1 christos struct command_line *cmd)
443 1.1 christos {
444 1.1 christos int ret;
445 1.1 christos char *script;
446 1.1 christos struct cleanup *cleanup;
447 1.1 christos
448 1.1 christos if (cmd->body_count != 1)
449 1.1 christos error (_("Invalid \"python\" block structure."));
450 1.1 christos
451 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
452 1.1 christos
453 1.1 christos script = compute_python_string (cmd->body_list[0]);
454 1.1 christos ret = PyRun_SimpleString (script);
455 1.1 christos xfree (script);
456 1.1 christos if (ret)
457 1.1 christos error (_("Error while executing Python code."));
458 1.1 christos
459 1.1 christos do_cleanups (cleanup);
460 1.1 christos }
461 1.1 christos
462 1.1 christos /* Implementation of the gdb "python" command. */
463 1.1 christos
464 1.1 christos static void
465 1.1 christos python_command (char *arg, int from_tty)
466 1.1 christos {
467 1.1 christos struct cleanup *cleanup;
468 1.1 christos
469 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
470 1.6 christos
471 1.6 christos make_cleanup_restore_integer (¤t_ui->async);
472 1.1 christos current_ui->async = 0;
473 1.1 christos
474 1.1 christos arg = skip_spaces (arg);
475 1.1 christos if (arg && *arg)
476 1.1 christos {
477 1.1 christos if (PyRun_SimpleString (arg))
478 1.1 christos error (_("Error while executing Python code."));
479 1.1 christos }
480 1.1 christos else
481 1.1 christos {
482 1.1 christos struct command_line *l = get_command_line (python_control, "");
483 1.1 christos
484 1.1 christos make_cleanup_free_command_lines (&l);
485 1.1 christos execute_control_command_untraced (l);
486 1.1 christos }
487 1.1 christos
488 1.1 christos do_cleanups (cleanup);
489 1.1 christos }
490 1.1 christos
491 1.1 christos
492 1.1 christos
494 1.1 christos /* Transform a gdb parameters's value into a Python value. May return
495 1.1 christos NULL (and set a Python exception) on error. Helper function for
496 1.1 christos get_parameter. */
497 1.1 christos PyObject *
498 1.1 christos gdbpy_parameter_value (enum var_types type, void *var)
499 1.1 christos {
500 1.1 christos switch (type)
501 1.1 christos {
502 1.1 christos case var_string:
503 1.1 christos case var_string_noescape:
504 1.1 christos case var_optional_filename:
505 1.1 christos case var_filename:
506 1.1 christos case var_enum:
507 1.1 christos {
508 1.1 christos char *str = * (char **) var;
509 1.1 christos
510 1.6 christos if (! str)
511 1.1 christos str = "";
512 1.1 christos return host_string_to_python_string (str);
513 1.1 christos }
514 1.1 christos
515 1.1 christos case var_boolean:
516 1.1 christos {
517 1.1 christos if (* (int *) var)
518 1.1 christos Py_RETURN_TRUE;
519 1.1 christos else
520 1.1 christos Py_RETURN_FALSE;
521 1.1 christos }
522 1.1 christos
523 1.1 christos case var_auto_boolean:
524 1.1 christos {
525 1.1 christos enum auto_boolean ab = * (enum auto_boolean *) var;
526 1.1 christos
527 1.1 christos if (ab == AUTO_BOOLEAN_TRUE)
528 1.1 christos Py_RETURN_TRUE;
529 1.1 christos else if (ab == AUTO_BOOLEAN_FALSE)
530 1.1 christos Py_RETURN_FALSE;
531 1.1 christos else
532 1.1 christos Py_RETURN_NONE;
533 1.1 christos }
534 1.1 christos
535 1.1 christos case var_integer:
536 1.1 christos if ((* (int *) var) == INT_MAX)
537 1.1 christos Py_RETURN_NONE;
538 1.1 christos /* Fall through. */
539 1.1 christos case var_zinteger:
540 1.1 christos return PyLong_FromLong (* (int *) var);
541 1.1 christos
542 1.1 christos case var_uinteger:
543 1.1 christos {
544 1.1 christos unsigned int val = * (unsigned int *) var;
545 1.1 christos
546 1.1 christos if (val == UINT_MAX)
547 1.1 christos Py_RETURN_NONE;
548 1.1 christos return PyLong_FromUnsignedLong (val);
549 1.1 christos }
550 1.1 christos }
551 1.1 christos
552 1.1 christos return PyErr_Format (PyExc_RuntimeError,
553 1.1 christos _("Programmer error: unhandled type."));
554 1.1 christos }
555 1.1 christos
556 1.1 christos /* A Python function which returns a gdb parameter's value as a Python
557 1.6 christos value. */
558 1.1 christos
559 1.1 christos static PyObject *
560 1.5 christos gdbpy_parameter (PyObject *self, PyObject *args)
561 1.1 christos {
562 1.1 christos struct gdb_exception except = exception_none;
563 1.1 christos struct cmd_list_element *alias, *prefix, *cmd;
564 1.1 christos const char *arg;
565 1.1 christos char *newarg;
566 1.1 christos int found = -1;
567 1.1 christos
568 1.1 christos if (! PyArg_ParseTuple (args, "s", &arg))
569 1.1 christos return NULL;
570 1.1 christos
571 1.5 christos newarg = concat ("show ", arg, (char *) NULL);
572 1.1 christos
573 1.1 christos TRY
574 1.1 christos {
575 1.5 christos found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd);
576 1.5 christos }
577 1.5 christos CATCH (ex, RETURN_MASK_ALL)
578 1.5 christos {
579 1.5 christos except = ex;
580 1.5 christos }
581 1.1 christos END_CATCH
582 1.1 christos
583 1.1 christos xfree (newarg);
584 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
585 1.1 christos if (!found)
586 1.1 christos return PyErr_Format (PyExc_RuntimeError,
587 1.1 christos _("Could not find parameter `%s'."), arg);
588 1.1 christos
589 1.1 christos if (! cmd->var)
590 1.1 christos return PyErr_Format (PyExc_RuntimeError,
591 1.1 christos _("`%s' is not a parameter."), arg);
592 1.1 christos return gdbpy_parameter_value (cmd->var_type, cmd->var);
593 1.1 christos }
594 1.1 christos
595 1.1 christos /* Wrapper for target_charset. */
596 1.1 christos
597 1.1 christos static PyObject *
598 1.1 christos gdbpy_target_charset (PyObject *self, PyObject *args)
599 1.1 christos {
600 1.1 christos const char *cset = target_charset (python_gdbarch);
601 1.1 christos
602 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
603 1.1 christos }
604 1.1 christos
605 1.1 christos /* Wrapper for target_wide_charset. */
606 1.1 christos
607 1.1 christos static PyObject *
608 1.1 christos gdbpy_target_wide_charset (PyObject *self, PyObject *args)
609 1.1 christos {
610 1.1 christos const char *cset = target_wide_charset (python_gdbarch);
611 1.1 christos
612 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
613 1.1 christos }
614 1.1 christos
615 1.1 christos /* A Python function which evaluates a string using the gdb CLI. */
616 1.1 christos
617 1.1 christos static PyObject *
618 1.1 christos execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
619 1.1 christos {
620 1.1 christos const char *arg;
621 1.1 christos PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
622 1.1 christos int from_tty, to_string;
623 1.1 christos static char *keywords[] = {"command", "from_tty", "to_string", NULL };
624 1.1 christos char *result = NULL;
625 1.1 christos
626 1.1 christos if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
627 1.1 christos &PyBool_Type, &from_tty_obj,
628 1.1 christos &PyBool_Type, &to_string_obj))
629 1.1 christos return NULL;
630 1.1 christos
631 1.1 christos from_tty = 0;
632 1.1 christos if (from_tty_obj)
633 1.1 christos {
634 1.1 christos int cmp = PyObject_IsTrue (from_tty_obj);
635 1.1 christos if (cmp < 0)
636 1.1 christos return NULL;
637 1.1 christos from_tty = cmp;
638 1.1 christos }
639 1.1 christos
640 1.1 christos to_string = 0;
641 1.1 christos if (to_string_obj)
642 1.1 christos {
643 1.1 christos int cmp = PyObject_IsTrue (to_string_obj);
644 1.1 christos if (cmp < 0)
645 1.1 christos return NULL;
646 1.1 christos to_string = cmp;
647 1.5 christos }
648 1.1 christos
649 1.1 christos TRY
650 1.1 christos {
651 1.1 christos /* Copy the argument text in case the command modifies it. */
652 1.6 christos char *copy = xstrdup (arg);
653 1.6 christos struct cleanup *cleanup = make_cleanup (xfree, copy);
654 1.6 christos struct interp *interp;
655 1.6 christos
656 1.6 christos make_cleanup_restore_integer (¤t_ui->async);
657 1.6 christos current_ui->async = 0;
658 1.1 christos
659 1.6 christos make_cleanup_restore_current_uiout ();
660 1.6 christos
661 1.6 christos /* Use the console interpreter uiout to have the same print format
662 1.6 christos for console or MI. */
663 1.1 christos interp = interp_lookup (current_ui, "console");
664 1.1 christos current_uiout = interp_ui_out (interp);
665 1.1 christos
666 1.1 christos prevent_dont_repeat ();
667 1.1 christos if (to_string)
668 1.1 christos result = execute_command_to_string (copy, from_tty);
669 1.1 christos else
670 1.1 christos {
671 1.1 christos result = NULL;
672 1.1 christos execute_command (copy, from_tty);
673 1.1 christos }
674 1.1 christos
675 1.5 christos do_cleanups (cleanup);
676 1.5 christos }
677 1.5 christos CATCH (except, RETURN_MASK_ALL)
678 1.5 christos {
679 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
680 1.1 christos }
681 1.1 christos END_CATCH
682 1.1 christos
683 1.1 christos /* Do any commands attached to breakpoint we stopped at. */
684 1.1 christos bpstat_do_actions ();
685 1.1 christos
686 1.1 christos if (result)
687 1.1 christos {
688 1.1 christos PyObject *r = PyString_FromString (result);
689 1.1 christos xfree (result);
690 1.1 christos return r;
691 1.1 christos }
692 1.1 christos Py_RETURN_NONE;
693 1.1 christos }
694 1.1 christos
695 1.1 christos /* Implementation of gdb.solib_name (Long) -> String.
696 1.1 christos Returns the name of the shared library holding a given address, or None. */
697 1.1 christos
698 1.1 christos static PyObject *
699 1.1 christos gdbpy_solib_name (PyObject *self, PyObject *args)
700 1.1 christos {
701 1.6 christos char *soname;
702 1.1 christos PyObject *str_obj;
703 1.6 christos gdb_py_ulongest pc;
704 1.1 christos
705 1.1 christos if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
706 1.1 christos return NULL;
707 1.1 christos
708 1.6 christos soname = solib_name_from_address (current_program_space, pc);
709 1.1 christos if (soname)
710 1.1 christos str_obj = host_string_to_python_string (soname);
711 1.1 christos else
712 1.1 christos {
713 1.1 christos str_obj = Py_None;
714 1.1 christos Py_INCREF (Py_None);
715 1.1 christos }
716 1.1 christos
717 1.1 christos return str_obj;
718 1.1 christos }
719 1.1 christos
720 1.1 christos /* A Python function which is a wrapper for decode_line_1. */
721 1.1 christos
722 1.1 christos static PyObject *
723 1.5 christos gdbpy_decode_line (PyObject *self, PyObject *args)
724 1.1 christos {
725 1.1 christos struct gdb_exception except = exception_none;
726 1.1 christos struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to
727 1.6 christos appease gcc. */
728 1.1 christos struct symtab_and_line sal;
729 1.1 christos char *arg = NULL;
730 1.1 christos struct cleanup *cleanups;
731 1.1 christos PyObject *result = NULL;
732 1.6 christos PyObject *return_result = NULL;
733 1.1 christos PyObject *unparsed = NULL;
734 1.1 christos struct event_location *location = NULL;
735 1.1 christos
736 1.1 christos if (! PyArg_ParseTuple (args, "|s", &arg))
737 1.1 christos return NULL;
738 1.1 christos
739 1.1 christos cleanups = make_cleanup (null_cleanup, NULL);
740 1.5 christos
741 1.6 christos sals.sals = NULL;
742 1.6 christos
743 1.6 christos if (arg != NULL)
744 1.6 christos {
745 1.6 christos location = new_linespec_location (&arg);
746 1.6 christos make_cleanup_delete_event_location (location);
747 1.5 christos }
748 1.1 christos
749 1.6 christos TRY
750 1.6 christos {
751 1.1 christos if (location != NULL)
752 1.1 christos sals = decode_line_1 (location, 0, NULL, NULL, 0);
753 1.1 christos else
754 1.1 christos {
755 1.1 christos set_default_source_symtab_and_line ();
756 1.1 christos sal = get_current_source_symtab_and_line ();
757 1.1 christos sals.sals = &sal;
758 1.1 christos sals.nelts = 1;
759 1.5 christos }
760 1.5 christos }
761 1.5 christos CATCH (ex, RETURN_MASK_ALL)
762 1.5 christos {
763 1.5 christos except = ex;
764 1.1 christos }
765 1.1 christos END_CATCH
766 1.6 christos
767 1.1 christos if (sals.sals != NULL && sals.sals != &sal)
768 1.1 christos make_cleanup (xfree, sals.sals);
769 1.1 christos
770 1.1 christos if (except.reason < 0)
771 1.1 christos {
772 1.1 christos do_cleanups (cleanups);
773 1.1 christos /* We know this will always throw. */
774 1.1 christos gdbpy_convert_exception (except);
775 1.1 christos return NULL;
776 1.1 christos }
777 1.1 christos
778 1.1 christos if (sals.nelts)
779 1.1 christos {
780 1.1 christos int i;
781 1.1 christos
782 1.1 christos result = PyTuple_New (sals.nelts);
783 1.1 christos if (! result)
784 1.1 christos goto error;
785 1.1 christos for (i = 0; i < sals.nelts; ++i)
786 1.1 christos {
787 1.1 christos PyObject *obj;
788 1.1 christos
789 1.1 christos obj = symtab_and_line_to_sal_object (sals.sals[i]);
790 1.1 christos if (! obj)
791 1.1 christos {
792 1.1 christos Py_DECREF (result);
793 1.1 christos goto error;
794 1.1 christos }
795 1.1 christos
796 1.1 christos PyTuple_SetItem (result, i, obj);
797 1.1 christos }
798 1.1 christos }
799 1.1 christos else
800 1.1 christos {
801 1.1 christos result = Py_None;
802 1.1 christos Py_INCREF (Py_None);
803 1.1 christos }
804 1.1 christos
805 1.1 christos return_result = PyTuple_New (2);
806 1.1 christos if (! return_result)
807 1.1 christos {
808 1.1 christos Py_DECREF (result);
809 1.1 christos goto error;
810 1.6 christos }
811 1.1 christos
812 1.6 christos if (arg != NULL && strlen (arg) > 0)
813 1.1 christos {
814 1.1 christos unparsed = PyString_FromString (arg);
815 1.1 christos if (unparsed == NULL)
816 1.1 christos {
817 1.1 christos Py_DECREF (result);
818 1.1 christos Py_DECREF (return_result);
819 1.1 christos return_result = NULL;
820 1.1 christos goto error;
821 1.1 christos }
822 1.1 christos }
823 1.1 christos else
824 1.1 christos {
825 1.1 christos unparsed = Py_None;
826 1.1 christos Py_INCREF (Py_None);
827 1.1 christos }
828 1.1 christos
829 1.1 christos PyTuple_SetItem (return_result, 0, unparsed);
830 1.1 christos PyTuple_SetItem (return_result, 1, result);
831 1.1 christos
832 1.1 christos error:
833 1.1 christos do_cleanups (cleanups);
834 1.1 christos
835 1.1 christos return return_result;
836 1.1 christos }
837 1.1 christos
838 1.1 christos /* Parse a string and evaluate it as an expression. */
839 1.1 christos static PyObject *
840 1.1 christos gdbpy_parse_and_eval (PyObject *self, PyObject *args)
841 1.1 christos {
842 1.1 christos const char *expr_str;
843 1.1 christos struct value *result = NULL;
844 1.1 christos
845 1.1 christos if (!PyArg_ParseTuple (args, "s", &expr_str))
846 1.5 christos return NULL;
847 1.1 christos
848 1.1 christos TRY
849 1.1 christos {
850 1.5 christos result = parse_and_eval (expr_str);
851 1.5 christos }
852 1.5 christos CATCH (except, RETURN_MASK_ALL)
853 1.5 christos {
854 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
855 1.1 christos }
856 1.1 christos END_CATCH
857 1.1 christos
858 1.1 christos return value_to_value_object (result);
859 1.1 christos }
860 1.1 christos
861 1.1 christos /* Implementation of gdb.find_pc_line function.
862 1.1 christos Returns the gdb.Symtab_and_line object corresponding to a PC value. */
863 1.1 christos
864 1.1 christos static PyObject *
865 1.1 christos gdbpy_find_pc_line (PyObject *self, PyObject *args)
866 1.1 christos {
867 1.1 christos gdb_py_ulongest pc_llu;
868 1.1 christos PyObject *result = NULL; /* init for gcc -Wall */
869 1.1 christos
870 1.1 christos if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
871 1.5 christos return NULL;
872 1.1 christos
873 1.1 christos TRY
874 1.1 christos {
875 1.1 christos struct symtab_and_line sal;
876 1.1 christos CORE_ADDR pc;
877 1.1 christos
878 1.1 christos pc = (CORE_ADDR) pc_llu;
879 1.1 christos sal = find_pc_line (pc, 0);
880 1.5 christos result = symtab_and_line_to_sal_object (sal);
881 1.5 christos }
882 1.5 christos CATCH (except, RETURN_MASK_ALL)
883 1.5 christos {
884 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
885 1.1 christos }
886 1.1 christos END_CATCH
887 1.1 christos
888 1.1 christos return result;
889 1.6 christos }
890 1.6 christos
891 1.6 christos /* Implementation of gdb.invalidate_cached_frames. */
892 1.6 christos
893 1.6 christos static PyObject *
894 1.6 christos gdbpy_invalidate_cached_frames (PyObject *self, PyObject *args)
895 1.6 christos {
896 1.6 christos reinit_frame_cache ();
897 1.6 christos Py_RETURN_NONE;
898 1.1 christos }
899 1.3 christos
900 1.3 christos /* Read a file as Python code.
901 1.1 christos This is the extension_language_script_ops.script_sourcer "method".
902 1.1 christos FILE is the file to load. FILENAME is name of the file FILE.
903 1.1 christos This does not throw any errors. If an exception occurs python will print
904 1.3 christos the traceback and clear the error indicator. */
905 1.3 christos
906 1.3 christos static void
907 1.1 christos gdbpy_source_script (const struct extension_language_defn *extlang,
908 1.1 christos FILE *file, const char *filename)
909 1.1 christos {
910 1.1 christos struct cleanup *cleanup;
911 1.1 christos
912 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
913 1.1 christos python_run_simple_file (file, filename);
914 1.1 christos do_cleanups (cleanup);
915 1.1 christos }
916 1.1 christos
917 1.1 christos
918 1.1 christos
920 1.1 christos /* Posting and handling events. */
921 1.1 christos
922 1.1 christos /* A single event. */
923 1.1 christos struct gdbpy_event
924 1.1 christos {
925 1.1 christos /* The Python event. This is just a callable object. */
926 1.1 christos PyObject *event;
927 1.1 christos /* The next event. */
928 1.1 christos struct gdbpy_event *next;
929 1.1 christos };
930 1.1 christos
931 1.1 christos /* All pending events. */
932 1.1 christos static struct gdbpy_event *gdbpy_event_list;
933 1.6 christos /* The final link of the event list. */
934 1.6 christos static struct gdbpy_event **gdbpy_event_list_end;
935 1.6 christos
936 1.1 christos /* So that we can wake up the main thread even when it is blocked in
937 1.1 christos poll(). */
938 1.1 christos static struct serial_event *gdbpy_serial_event;
939 1.1 christos
940 1.1 christos /* The file handler callback. This reads from the internal pipe, and
941 1.1 christos then processes the Python event queue. This will always be run in
942 1.6 christos the main gdb thread. */
943 1.1 christos
944 1.1 christos static void
945 1.1 christos gdbpy_run_events (int error, gdb_client_data client_data)
946 1.1 christos {
947 1.1 christos struct cleanup *cleanup;
948 1.6 christos
949 1.6 christos cleanup = ensure_python_env (get_current_arch (), current_language);
950 1.1 christos
951 1.6 christos /* Clear the event fd. Do this before flushing the events list, so
952 1.1 christos that any new event post afterwards is sure to re-awake the event
953 1.1 christos loop. */
954 1.1 christos serial_event_clear (gdbpy_serial_event);
955 1.1 christos
956 1.1 christos while (gdbpy_event_list)
957 1.1 christos {
958 1.1 christos PyObject *call_result;
959 1.1 christos
960 1.1 christos /* Dispatching the event might push a new element onto the event
961 1.1 christos loop, so we update here "atomically enough". */
962 1.1 christos struct gdbpy_event *item = gdbpy_event_list;
963 1.1 christos gdbpy_event_list = gdbpy_event_list->next;
964 1.1 christos if (gdbpy_event_list == NULL)
965 1.1 christos gdbpy_event_list_end = &gdbpy_event_list;
966 1.1 christos
967 1.1 christos /* Ignore errors. */
968 1.1 christos call_result = PyObject_CallObject (item->event, NULL);
969 1.1 christos if (call_result == NULL)
970 1.1 christos PyErr_Clear ();
971 1.1 christos
972 1.1 christos Py_XDECREF (call_result);
973 1.1 christos Py_DECREF (item->event);
974 1.1 christos xfree (item);
975 1.1 christos }
976 1.1 christos
977 1.1 christos do_cleanups (cleanup);
978 1.1 christos }
979 1.1 christos
980 1.1 christos /* Submit an event to the gdb thread. */
981 1.1 christos static PyObject *
982 1.1 christos gdbpy_post_event (PyObject *self, PyObject *args)
983 1.1 christos {
984 1.1 christos struct gdbpy_event *event;
985 1.1 christos PyObject *func;
986 1.1 christos int wakeup;
987 1.1 christos
988 1.1 christos if (!PyArg_ParseTuple (args, "O", &func))
989 1.1 christos return NULL;
990 1.1 christos
991 1.1 christos if (!PyCallable_Check (func))
992 1.1 christos {
993 1.1 christos PyErr_SetString (PyExc_RuntimeError,
994 1.1 christos _("Posted event is not callable"));
995 1.1 christos return NULL;
996 1.1 christos }
997 1.1 christos
998 1.1 christos Py_INCREF (func);
999 1.1 christos
1000 1.1 christos /* From here until the end of the function, we have the GIL, so we
1001 1.1 christos can operate on our global data structures without worrying. */
1002 1.1 christos wakeup = gdbpy_event_list == NULL;
1003 1.1 christos
1004 1.1 christos event = XNEW (struct gdbpy_event);
1005 1.1 christos event->event = func;
1006 1.1 christos event->next = NULL;
1007 1.1 christos *gdbpy_event_list_end = event;
1008 1.1 christos gdbpy_event_list_end = &event->next;
1009 1.6 christos
1010 1.1 christos /* Wake up gdb when needed. */
1011 1.1 christos if (wakeup)
1012 1.1 christos serial_event_set (gdbpy_serial_event);
1013 1.1 christos
1014 1.1 christos Py_RETURN_NONE;
1015 1.1 christos }
1016 1.1 christos
1017 1.1 christos /* Initialize the Python event handler. */
1018 1.6 christos static int
1019 1.6 christos gdbpy_initialize_events (void)
1020 1.6 christos {
1021 1.6 christos gdbpy_event_list_end = &gdbpy_event_list;
1022 1.6 christos
1023 1.1 christos gdbpy_serial_event = make_serial_event ();
1024 1.1 christos add_file_handler (serial_event_fd (gdbpy_serial_event),
1025 1.1 christos gdbpy_run_events, NULL);
1026 1.1 christos
1027 1.1 christos return 0;
1028 1.1 christos }
1029 1.3 christos
1030 1.3 christos
1031 1.3 christos
1033 1.3 christos /* This is the extension_language_ops.before_prompt "method". */
1034 1.1 christos
1035 1.1 christos static enum ext_lang_rc
1036 1.1 christos gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
1037 1.1 christos const char *current_gdb_prompt)
1038 1.1 christos {
1039 1.3 christos struct cleanup *cleanup;
1040 1.1 christos char *prompt = NULL;
1041 1.1 christos
1042 1.1 christos if (!gdb_python_initialized)
1043 1.1 christos return EXT_LANG_RC_NOP;
1044 1.1 christos
1045 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
1046 1.1 christos
1047 1.1 christos if (gdb_python_module
1048 1.1 christos && PyObject_HasAttrString (gdb_python_module, "prompt_hook"))
1049 1.1 christos {
1050 1.1 christos PyObject *hook;
1051 1.1 christos
1052 1.1 christos hook = PyObject_GetAttrString (gdb_python_module, "prompt_hook");
1053 1.1 christos if (hook == NULL)
1054 1.1 christos goto fail;
1055 1.1 christos
1056 1.1 christos make_cleanup_py_decref (hook);
1057 1.1 christos
1058 1.1 christos if (PyCallable_Check (hook))
1059 1.1 christos {
1060 1.1 christos PyObject *result;
1061 1.1 christos PyObject *current_prompt;
1062 1.1 christos
1063 1.1 christos current_prompt = PyString_FromString (current_gdb_prompt);
1064 1.1 christos if (current_prompt == NULL)
1065 1.1 christos goto fail;
1066 1.1 christos
1067 1.1 christos result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL);
1068 1.1 christos
1069 1.1 christos Py_DECREF (current_prompt);
1070 1.1 christos
1071 1.1 christos if (result == NULL)
1072 1.1 christos goto fail;
1073 1.1 christos
1074 1.1 christos make_cleanup_py_decref (result);
1075 1.1 christos
1076 1.1 christos /* Return type should be None, or a String. If it is None,
1077 1.1 christos fall through, we will not set a prompt. If it is a
1078 1.1 christos string, set PROMPT. Anything else, set an exception. */
1079 1.1 christos if (result != Py_None && ! PyString_Check (result))
1080 1.1 christos {
1081 1.1 christos PyErr_Format (PyExc_RuntimeError,
1082 1.1 christos _("Return from prompt_hook must " \
1083 1.1 christos "be either a Python string, or None"));
1084 1.1 christos goto fail;
1085 1.1 christos }
1086 1.1 christos
1087 1.1 christos if (result != Py_None)
1088 1.1 christos {
1089 1.1 christos prompt = python_string_to_host_string (result);
1090 1.1 christos
1091 1.1 christos if (prompt == NULL)
1092 1.1 christos goto fail;
1093 1.1 christos else
1094 1.1 christos make_cleanup (xfree, prompt);
1095 1.1 christos }
1096 1.1 christos }
1097 1.1 christos }
1098 1.1 christos
1099 1.1 christos /* If a prompt has been set, PROMPT will not be NULL. If it is
1100 1.1 christos NULL, do not set the prompt. */
1101 1.3 christos if (prompt != NULL)
1102 1.1 christos set_prompt (prompt);
1103 1.1 christos
1104 1.1 christos do_cleanups (cleanup);
1105 1.1 christos return prompt != NULL ? EXT_LANG_RC_OK : EXT_LANG_RC_NOP;
1106 1.3 christos
1107 1.1 christos fail:
1108 1.1 christos gdbpy_print_stack ();
1109 1.1 christos do_cleanups (cleanup);
1110 1.1 christos return EXT_LANG_RC_ERROR;
1111 1.1 christos }
1112 1.1 christos
1113 1.1 christos
1114 1.1 christos
1116 1.1 christos /* Printing. */
1117 1.1 christos
1118 1.1 christos /* A python function to write a single string using gdb's filtered
1119 1.1 christos output stream . The optional keyword STREAM can be used to write
1120 1.1 christos to a particular stream. The default stream is to gdb_stdout. */
1121 1.1 christos
1122 1.1 christos static PyObject *
1123 1.1 christos gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
1124 1.1 christos {
1125 1.1 christos const char *arg;
1126 1.1 christos static char *keywords[] = {"text", "stream", NULL };
1127 1.1 christos int stream_type = 0;
1128 1.5 christos
1129 1.1 christos if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
1130 1.1 christos &stream_type))
1131 1.1 christos return NULL;
1132 1.1 christos
1133 1.1 christos TRY
1134 1.1 christos {
1135 1.1 christos switch (stream_type)
1136 1.1 christos {
1137 1.1 christos case 1:
1138 1.1 christos {
1139 1.1 christos fprintf_filtered (gdb_stderr, "%s", arg);
1140 1.1 christos break;
1141 1.1 christos }
1142 1.1 christos case 2:
1143 1.1 christos {
1144 1.1 christos fprintf_filtered (gdb_stdlog, "%s", arg);
1145 1.1 christos break;
1146 1.5 christos }
1147 1.5 christos default:
1148 1.5 christos fprintf_filtered (gdb_stdout, "%s", arg);
1149 1.5 christos }
1150 1.5 christos }
1151 1.1 christos CATCH (except, RETURN_MASK_ALL)
1152 1.1 christos {
1153 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
1154 1.1 christos }
1155 1.1 christos END_CATCH
1156 1.1 christos
1157 1.1 christos Py_RETURN_NONE;
1158 1.1 christos }
1159 1.1 christos
1160 1.1 christos /* A python function to flush a gdb stream. The optional keyword
1161 1.1 christos STREAM can be used to flush a particular stream. The default stream
1162 1.1 christos is gdb_stdout. */
1163 1.1 christos
1164 1.1 christos static PyObject *
1165 1.1 christos gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
1166 1.1 christos {
1167 1.1 christos static char *keywords[] = {"stream", NULL };
1168 1.1 christos int stream_type = 0;
1169 1.1 christos
1170 1.1 christos if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
1171 1.1 christos &stream_type))
1172 1.1 christos return NULL;
1173 1.1 christos
1174 1.1 christos switch (stream_type)
1175 1.1 christos {
1176 1.1 christos case 1:
1177 1.1 christos {
1178 1.1 christos gdb_flush (gdb_stderr);
1179 1.1 christos break;
1180 1.1 christos }
1181 1.1 christos case 2:
1182 1.1 christos {
1183 1.1 christos gdb_flush (gdb_stdlog);
1184 1.1 christos break;
1185 1.1 christos }
1186 1.1 christos default:
1187 1.1 christos gdb_flush (gdb_stdout);
1188 1.5 christos }
1189 1.5 christos
1190 1.5 christos Py_RETURN_NONE;
1191 1.5 christos }
1192 1.5 christos
1193 1.5 christos /* Return non-zero if print-stack is not "none". */
1194 1.5 christos
1195 1.5 christos int
1196 1.1 christos gdbpy_print_python_errors_p (void)
1197 1.1 christos {
1198 1.1 christos return gdbpy_should_print_stack != python_excp_none;
1199 1.1 christos }
1200 1.1 christos
1201 1.1 christos /* Print a python exception trace, print just a message, or print
1202 1.1 christos nothing and clear the python exception, depending on
1203 1.1 christos gdbpy_should_print_stack. Only call this if a python exception is
1204 1.1 christos set. */
1205 1.1 christos void
1206 1.1 christos gdbpy_print_stack (void)
1207 1.1 christos {
1208 1.1 christos
1209 1.1 christos /* Print "none", just clear exception. */
1210 1.1 christos if (gdbpy_should_print_stack == python_excp_none)
1211 1.1 christos {
1212 1.1 christos PyErr_Clear ();
1213 1.1 christos }
1214 1.1 christos /* Print "full" message and backtrace. */
1215 1.1 christos else if (gdbpy_should_print_stack == python_excp_full)
1216 1.5 christos {
1217 1.1 christos PyErr_Print ();
1218 1.1 christos /* PyErr_Print doesn't necessarily end output with a newline.
1219 1.1 christos This works because Python's stdout/stderr is fed through
1220 1.5 christos printf_filtered. */
1221 1.5 christos TRY
1222 1.5 christos {
1223 1.5 christos begin_line ();
1224 1.1 christos }
1225 1.1 christos CATCH (except, RETURN_MASK_ALL)
1226 1.1 christos {
1227 1.1 christos }
1228 1.1 christos END_CATCH
1229 1.1 christos }
1230 1.1 christos /* Print "message", just error print message. */
1231 1.1 christos else
1232 1.1 christos {
1233 1.1 christos PyObject *ptype, *pvalue, *ptraceback;
1234 1.1 christos char *msg = NULL, *type = NULL;
1235 1.1 christos
1236 1.1 christos PyErr_Fetch (&ptype, &pvalue, &ptraceback);
1237 1.5 christos
1238 1.1 christos /* Fetch the error message contained within ptype, pvalue. */
1239 1.1 christos msg = gdbpy_exception_to_string (ptype, pvalue);
1240 1.1 christos type = gdbpy_obj_to_string (ptype);
1241 1.1 christos
1242 1.1 christos TRY
1243 1.1 christos {
1244 1.1 christos if (msg == NULL)
1245 1.1 christos {
1246 1.1 christos /* An error occurred computing the string representation of the
1247 1.1 christos error message. */
1248 1.1 christos fprintf_filtered (gdb_stderr,
1249 1.1 christos _("Error occurred computing Python error" \
1250 1.1 christos "message.\n"));
1251 1.5 christos }
1252 1.5 christos else
1253 1.5 christos fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
1254 1.5 christos type, msg);
1255 1.1 christos }
1256 1.1 christos CATCH (except, RETURN_MASK_ALL)
1257 1.1 christos {
1258 1.1 christos }
1259 1.1 christos END_CATCH
1260 1.1 christos
1261 1.1 christos Py_XDECREF (ptype);
1262 1.1 christos Py_XDECREF (pvalue);
1263 1.1 christos Py_XDECREF (ptraceback);
1264 1.1 christos xfree (msg);
1265 1.1 christos }
1266 1.1 christos }
1267 1.1 christos
1268 1.1 christos
1269 1.1 christos
1271 1.1 christos /* Return the current Progspace.
1272 1.1 christos There always is one. */
1273 1.1 christos
1274 1.1 christos static PyObject *
1275 1.1 christos gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
1276 1.1 christos {
1277 1.1 christos PyObject *result;
1278 1.1 christos
1279 1.1 christos result = pspace_to_pspace_object (current_program_space);
1280 1.1 christos if (result)
1281 1.1 christos Py_INCREF (result);
1282 1.1 christos return result;
1283 1.1 christos }
1284 1.1 christos
1285 1.1 christos /* Return a sequence holding all the Progspaces. */
1286 1.1 christos
1287 1.1 christos static PyObject *
1288 1.1 christos gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
1289 1.1 christos {
1290 1.1 christos struct program_space *ps;
1291 1.1 christos PyObject *list;
1292 1.1 christos
1293 1.1 christos list = PyList_New (0);
1294 1.1 christos if (!list)
1295 1.1 christos return NULL;
1296 1.1 christos
1297 1.1 christos ALL_PSPACES (ps)
1298 1.1 christos {
1299 1.1 christos PyObject *item = pspace_to_pspace_object (ps);
1300 1.1 christos
1301 1.1 christos if (!item || PyList_Append (list, item) == -1)
1302 1.1 christos {
1303 1.1 christos Py_DECREF (list);
1304 1.1 christos return NULL;
1305 1.1 christos }
1306 1.1 christos }
1307 1.1 christos
1308 1.1 christos return list;
1309 1.5 christos }
1310 1.5 christos
1311 1.1 christos
1312 1.1 christos
1314 1.1 christos /* The "current" objfile. This is set when gdb detects that a new
1315 1.3 christos objfile has been loaded. It is only set for the duration of a call to
1316 1.3 christos gdbpy_source_objfile_script and gdbpy_execute_objfile_script; it is NULL
1317 1.3 christos at other times. */
1318 1.1 christos static struct objfile *gdbpy_current_objfile;
1319 1.3 christos
1320 1.3 christos /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1321 1.3 christos as Python code. This does not throw any errors. If an exception
1322 1.3 christos occurs python will print the traceback and clear the error indicator.
1323 1.1 christos This is the extension_language_script_ops.objfile_script_sourcer
1324 1.1 christos "method". */
1325 1.1 christos
1326 1.1 christos static void
1327 1.1 christos gdbpy_source_objfile_script (const struct extension_language_defn *extlang,
1328 1.1 christos struct objfile *objfile, FILE *file,
1329 1.1 christos const char *filename)
1330 1.1 christos {
1331 1.1 christos struct cleanup *cleanups;
1332 1.1 christos
1333 1.1 christos if (!gdb_python_initialized)
1334 1.1 christos return;
1335 1.1 christos
1336 1.1 christos cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
1337 1.1 christos gdbpy_current_objfile = objfile;
1338 1.5 christos
1339 1.5 christos python_run_simple_file (file, filename);
1340 1.5 christos
1341 1.5 christos do_cleanups (cleanups);
1342 1.5 christos gdbpy_current_objfile = NULL;
1343 1.5 christos }
1344 1.5 christos
1345 1.5 christos /* Set the current objfile to OBJFILE and then execute SCRIPT
1346 1.5 christos as Python code. This does not throw any errors. If an exception
1347 1.5 christos occurs python will print the traceback and clear the error indicator.
1348 1.5 christos This is the extension_language_script_ops.objfile_script_executor
1349 1.5 christos "method". */
1350 1.5 christos
1351 1.5 christos static void
1352 1.5 christos gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
1353 1.5 christos struct objfile *objfile, const char *name,
1354 1.5 christos const char *script)
1355 1.5 christos {
1356 1.5 christos struct cleanup *cleanups;
1357 1.5 christos
1358 1.5 christos if (!gdb_python_initialized)
1359 1.5 christos return;
1360 1.5 christos
1361 1.5 christos cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
1362 1.5 christos gdbpy_current_objfile = objfile;
1363 1.1 christos
1364 1.1 christos PyRun_SimpleString (script);
1365 1.1 christos
1366 1.1 christos do_cleanups (cleanups);
1367 1.1 christos gdbpy_current_objfile = NULL;
1368 1.1 christos }
1369 1.1 christos
1370 1.1 christos /* Return the current Objfile, or None if there isn't one. */
1371 1.1 christos
1372 1.1 christos static PyObject *
1373 1.1 christos gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
1374 1.1 christos {
1375 1.1 christos PyObject *result;
1376 1.1 christos
1377 1.1 christos if (! gdbpy_current_objfile)
1378 1.1 christos Py_RETURN_NONE;
1379 1.1 christos
1380 1.1 christos result = objfile_to_objfile_object (gdbpy_current_objfile);
1381 1.1 christos if (result)
1382 1.1 christos Py_INCREF (result);
1383 1.1 christos return result;
1384 1.1 christos }
1385 1.1 christos
1386 1.1 christos /* Return a sequence holding all the Objfiles. */
1387 1.1 christos
1388 1.1 christos static PyObject *
1389 1.1 christos gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
1390 1.1 christos {
1391 1.1 christos struct objfile *objf;
1392 1.1 christos PyObject *list;
1393 1.1 christos
1394 1.1 christos list = PyList_New (0);
1395 1.1 christos if (!list)
1396 1.1 christos return NULL;
1397 1.1 christos
1398 1.1 christos ALL_OBJFILES (objf)
1399 1.1 christos {
1400 1.1 christos PyObject *item = objfile_to_objfile_object (objf);
1401 1.1 christos
1402 1.1 christos if (!item || PyList_Append (list, item) == -1)
1403 1.1 christos {
1404 1.1 christos Py_DECREF (list);
1405 1.3 christos return NULL;
1406 1.3 christos }
1407 1.3 christos }
1408 1.3 christos
1409 1.1 christos return list;
1410 1.3 christos }
1411 1.3 christos
1412 1.3 christos /* Compute the list of active python type printers and store them in
1413 1.1 christos EXT_PRINTERS->py_type_printers. The product of this function is used by
1414 1.1 christos gdbpy_apply_type_printers, and freed by gdbpy_free_type_printers.
1415 1.3 christos This is the extension_language_ops.start_type_printers "method". */
1416 1.1 christos
1417 1.1 christos static void
1418 1.3 christos gdbpy_start_type_printers (const struct extension_language_defn *extlang,
1419 1.1 christos struct ext_lang_type_printers *ext_printers)
1420 1.1 christos {
1421 1.1 christos struct cleanup *cleanups;
1422 1.1 christos PyObject *type_module, *func = NULL, *printers_obj = NULL;
1423 1.1 christos
1424 1.1 christos if (!gdb_python_initialized)
1425 1.1 christos return;
1426 1.1 christos
1427 1.1 christos cleanups = ensure_python_env (get_current_arch (), current_language);
1428 1.1 christos
1429 1.1 christos type_module = PyImport_ImportModule ("gdb.types");
1430 1.1 christos if (type_module == NULL)
1431 1.1 christos {
1432 1.1 christos gdbpy_print_stack ();
1433 1.1 christos goto done;
1434 1.1 christos }
1435 1.1 christos
1436 1.3 christos func = PyObject_GetAttrString (type_module, "get_type_recognizers");
1437 1.3 christos if (func == NULL)
1438 1.1 christos {
1439 1.3 christos gdbpy_print_stack ();
1440 1.3 christos goto done;
1441 1.1 christos }
1442 1.1 christos
1443 1.1 christos printers_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
1444 1.1 christos if (printers_obj == NULL)
1445 1.1 christos gdbpy_print_stack ();
1446 1.1 christos else
1447 1.1 christos ext_printers->py_type_printers = printers_obj;
1448 1.3 christos
1449 1.3 christos done:
1450 1.3 christos Py_XDECREF (type_module);
1451 1.3 christos Py_XDECREF (func);
1452 1.3 christos do_cleanups (cleanups);
1453 1.3 christos }
1454 1.3 christos
1455 1.3 christos /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE
1456 1.3 christos a newly allocated string holding the type's replacement name, and return
1457 1.3 christos EXT_LANG_RC_OK. The caller is responsible for freeing the string.
1458 1.3 christos If there's a Python error return EXT_LANG_RC_ERROR.
1459 1.1 christos Otherwise, return EXT_LANG_RC_NOP.
1460 1.1 christos This is the extension_language_ops.apply_type_printers "method". */
1461 1.1 christos
1462 1.1 christos static enum ext_lang_rc
1463 1.6 christos gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
1464 1.1 christos const struct ext_lang_type_printers *ext_printers,
1465 1.1 christos struct type *type, char **prettied_type)
1466 1.1 christos {
1467 1.3 christos struct cleanup *cleanups;
1468 1.1 christos PyObject *type_obj, *type_module = NULL, *func = NULL;
1469 1.1 christos PyObject *result_obj = NULL;
1470 1.3 christos PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
1471 1.1 christos char *result = NULL;
1472 1.1 christos
1473 1.1 christos if (printers_obj == NULL)
1474 1.1 christos return EXT_LANG_RC_NOP;
1475 1.1 christos
1476 1.1 christos if (!gdb_python_initialized)
1477 1.1 christos return EXT_LANG_RC_NOP;
1478 1.1 christos
1479 1.1 christos cleanups = ensure_python_env (get_current_arch (), current_language);
1480 1.1 christos
1481 1.1 christos type_obj = type_to_type_object (type);
1482 1.1 christos if (type_obj == NULL)
1483 1.1 christos {
1484 1.1 christos gdbpy_print_stack ();
1485 1.1 christos goto done;
1486 1.1 christos }
1487 1.1 christos
1488 1.1 christos type_module = PyImport_ImportModule ("gdb.types");
1489 1.1 christos if (type_module == NULL)
1490 1.1 christos {
1491 1.1 christos gdbpy_print_stack ();
1492 1.1 christos goto done;
1493 1.1 christos }
1494 1.1 christos
1495 1.1 christos func = PyObject_GetAttrString (type_module, "apply_type_recognizers");
1496 1.1 christos if (func == NULL)
1497 1.1 christos {
1498 1.1 christos gdbpy_print_stack ();
1499 1.1 christos goto done;
1500 1.1 christos }
1501 1.1 christos
1502 1.1 christos result_obj = PyObject_CallFunctionObjArgs (func, printers_obj,
1503 1.1 christos type_obj, (char *) NULL);
1504 1.1 christos if (result_obj == NULL)
1505 1.1 christos {
1506 1.1 christos gdbpy_print_stack ();
1507 1.1 christos goto done;
1508 1.1 christos }
1509 1.1 christos
1510 1.1 christos if (result_obj != Py_None)
1511 1.1 christos {
1512 1.1 christos result = python_string_to_host_string (result_obj);
1513 1.1 christos if (result == NULL)
1514 1.1 christos gdbpy_print_stack ();
1515 1.1 christos }
1516 1.3 christos
1517 1.3 christos done:
1518 1.3 christos Py_XDECREF (type_obj);
1519 1.1 christos Py_XDECREF (type_module);
1520 1.1 christos Py_XDECREF (func);
1521 1.3 christos Py_XDECREF (result_obj);
1522 1.3 christos do_cleanups (cleanups);
1523 1.1 christos if (result != NULL)
1524 1.3 christos *prettied_type = result;
1525 1.3 christos return result != NULL ? EXT_LANG_RC_OK : EXT_LANG_RC_ERROR;
1526 1.3 christos }
1527 1.1 christos
1528 1.1 christos /* Free the result of start_type_printers.
1529 1.6 christos This is the extension_language_ops.free_type_printers "method". */
1530 1.1 christos
1531 1.1 christos static void
1532 1.1 christos gdbpy_free_type_printers (const struct extension_language_defn *extlang,
1533 1.1 christos struct ext_lang_type_printers *ext_printers)
1534 1.1 christos {
1535 1.1 christos struct cleanup *cleanups;
1536 1.1 christos PyObject *printers = (PyObject *) ext_printers->py_type_printers;
1537 1.1 christos
1538 1.1 christos if (printers == NULL)
1539 1.1 christos return;
1540 1.1 christos
1541 1.1 christos if (!gdb_python_initialized)
1542 1.1 christos return;
1543 1.1 christos
1544 1.1 christos cleanups = ensure_python_env (get_current_arch (), current_language);
1545 1.1 christos Py_DECREF (printers);
1546 1.1 christos do_cleanups (cleanups);
1547 1.1 christos }
1548 1.1 christos
1549 1.1 christos #else /* HAVE_PYTHON */
1550 1.1 christos
1551 1.1 christos /* Dummy implementation of the gdb "python-interactive" and "python"
1552 1.1 christos command. */
1553 1.1 christos
1554 1.1 christos static void
1555 1.1 christos python_interactive_command (char *arg, int from_tty)
1556 1.1 christos {
1557 1.1 christos arg = skip_spaces (arg);
1558 1.1 christos if (arg && *arg)
1559 1.1 christos error (_("Python scripting is not supported in this copy of GDB."));
1560 1.1 christos else
1561 1.1 christos {
1562 1.1 christos struct command_line *l = get_command_line (python_control, "");
1563 1.1 christos struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
1564 1.1 christos
1565 1.1 christos execute_control_command_untraced (l);
1566 1.1 christos do_cleanups (cleanups);
1567 1.1 christos }
1568 1.1 christos }
1569 1.1 christos
1570 1.1 christos static void
1571 1.1 christos python_command (char *arg, int from_tty)
1572 1.1 christos {
1573 1.1 christos python_interactive_command (arg, from_tty);
1574 1.1 christos }
1575 1.1 christos
1576 1.1 christos #endif /* HAVE_PYTHON */
1577 1.1 christos
1578 1.1 christos
1579 1.1 christos
1581 1.1 christos /* Lists for 'set python' commands. */
1582 1.1 christos
1583 1.1 christos static struct cmd_list_element *user_set_python_list;
1584 1.1 christos static struct cmd_list_element *user_show_python_list;
1585 1.1 christos
1586 1.1 christos /* Function for use by 'set python' prefix command. */
1587 1.1 christos
1588 1.1 christos static void
1589 1.1 christos user_set_python (char *args, int from_tty)
1590 1.1 christos {
1591 1.1 christos help_list (user_set_python_list, "set python ", all_commands,
1592 1.1 christos gdb_stdout);
1593 1.1 christos }
1594 1.1 christos
1595 1.1 christos /* Function for use by 'show python' prefix command. */
1596 1.1 christos
1597 1.1 christos static void
1598 1.1 christos user_show_python (char *args, int from_tty)
1599 1.1 christos {
1600 1.1 christos cmd_show_list (user_show_python_list, from_tty, "");
1601 1.1 christos }
1602 1.1 christos
1603 1.1 christos /* Initialize the Python code. */
1604 1.1 christos
1605 1.3 christos #ifdef HAVE_PYTHON
1606 1.3 christos
1607 1.1 christos /* This is installed as a final cleanup and cleans up the
1608 1.1 christos interpreter. This lets Python's 'atexit' work. */
1609 1.1 christos
1610 1.1 christos static void
1611 1.1 christos finalize_python (void *ignore)
1612 1.3 christos {
1613 1.3 christos struct active_ext_lang_state *previous_active;
1614 1.3 christos
1615 1.3 christos /* We don't use ensure_python_env here because if we ever ran the
1616 1.3 christos cleanup, gdb would crash -- because the cleanup calls into the
1617 1.1 christos Python interpreter, which we are about to destroy. It seems
1618 1.1 christos clearer to make the needed calls explicitly here than to create a
1619 1.1 christos cleanup and then mysteriously discard it. */
1620 1.1 christos
1621 1.1 christos /* This is only called as a final cleanup so we can assume the active
1622 1.3 christos SIGINT handler is gdb's. We still need to tell it to notify Python. */
1623 1.3 christos previous_active = set_active_ext_lang (&extension_language_python);
1624 1.1 christos
1625 1.1 christos (void) PyGILState_Ensure ();
1626 1.1 christos python_gdbarch = target_gdbarch ();
1627 1.1 christos python_language = current_language;
1628 1.1 christos
1629 1.1 christos Py_Finalize ();
1630 1.1 christos
1631 1.1 christos restore_active_ext_lang (previous_active);
1632 1.1 christos }
1633 1.1 christos #endif
1634 1.1 christos
1635 1.1 christos /* Provide a prototype to silence -Wmissing-prototypes. */
1636 1.1 christos extern initialize_file_ftype _initialize_python;
1637 1.1 christos
1638 1.1 christos void
1639 1.1 christos _initialize_python (void)
1640 1.1 christos {
1641 1.1 christos char *progname;
1642 1.1 christos #ifdef IS_PY3K
1643 1.1 christos int i;
1644 1.1 christos size_t progsize, count;
1645 1.1 christos char *oldloc;
1646 1.1 christos wchar_t *progname_copy;
1647 1.1 christos #endif
1648 1.1 christos
1649 1.1 christos add_com ("python-interactive", class_obscure,
1650 1.1 christos python_interactive_command,
1651 1.1 christos #ifdef HAVE_PYTHON
1652 1.1 christos _("\
1653 1.1 christos Start an interactive Python prompt.\n\
1654 1.1 christos \n\
1655 1.1 christos To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
1656 1.1 christos prompt).\n\
1657 1.1 christos \n\
1658 1.1 christos Alternatively, a single-line Python command can be given as an\n\
1659 1.1 christos argument, and if the command is an expression, the result will be\n\
1660 1.1 christos printed. For example:\n\
1661 1.1 christos \n\
1662 1.1 christos (gdb) python-interactive 2 + 3\n\
1663 1.1 christos 5\n\
1664 1.1 christos ")
1665 1.1 christos #else /* HAVE_PYTHON */
1666 1.1 christos _("\
1667 1.1 christos Start a Python interactive prompt.\n\
1668 1.1 christos \n\
1669 1.1 christos Python scripting is not supported in this copy of GDB.\n\
1670 1.1 christos This command is only a placeholder.")
1671 1.1 christos #endif /* HAVE_PYTHON */
1672 1.1 christos );
1673 1.1 christos add_com_alias ("pi", "python-interactive", class_obscure, 1);
1674 1.1 christos
1675 1.1 christos add_com ("python", class_obscure, python_command,
1676 1.1 christos #ifdef HAVE_PYTHON
1677 1.1 christos _("\
1678 1.1 christos Evaluate a Python command.\n\
1679 1.1 christos \n\
1680 1.1 christos The command can be given as an argument, for instance:\n\
1681 1.1 christos \n\
1682 1.1 christos python print 23\n\
1683 1.1 christos \n\
1684 1.1 christos If no argument is given, the following lines are read and used\n\
1685 1.1 christos as the Python commands. Type a line containing \"end\" to indicate\n\
1686 1.1 christos the end of the command.")
1687 1.1 christos #else /* HAVE_PYTHON */
1688 1.1 christos _("\
1689 1.1 christos Evaluate a Python command.\n\
1690 1.1 christos \n\
1691 1.1 christos Python scripting is not supported in this copy of GDB.\n\
1692 1.1 christos This command is only a placeholder.")
1693 1.1 christos #endif /* HAVE_PYTHON */
1694 1.1 christos );
1695 1.1 christos add_com_alias ("py", "python", class_obscure, 1);
1696 1.1 christos
1697 1.1 christos /* Add set/show python print-stack. */
1698 1.1 christos add_prefix_cmd ("python", no_class, user_show_python,
1699 1.1 christos _("Prefix command for python preference settings."),
1700 1.1 christos &user_show_python_list, "show python ", 0,
1701 1.1 christos &showlist);
1702 1.1 christos
1703 1.1 christos add_prefix_cmd ("python", no_class, user_set_python,
1704 1.1 christos _("Prefix command for python preference settings."),
1705 1.1 christos &user_set_python_list, "set python ", 0,
1706 1.1 christos &setlist);
1707 1.1 christos
1708 1.1 christos add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
1709 1.1 christos &gdbpy_should_print_stack, _("\
1710 1.1 christos Set mode for Python stack dump on error."), _("\
1711 1.1 christos Show the mode of Python stack printing on error."), _("\
1712 1.1 christos none == no stack or message will be printed.\n\
1713 1.1 christos full == a message and a stack will be printed.\n\
1714 1.1 christos message == an error message without a stack will be printed."),
1715 1.1 christos NULL, NULL,
1716 1.1 christos &user_set_python_list,
1717 1.1 christos &user_show_python_list);
1718 1.1 christos
1719 1.1 christos #ifdef HAVE_PYTHON
1720 1.6 christos #ifdef WITH_PYTHON_PATH
1721 1.1 christos /* Work around problem where python gets confused about where it is,
1722 1.6 christos and then can't find its libraries, etc.
1723 1.1 christos NOTE: Python assumes the following layout:
1724 1.1 christos /foo/bin/python
1725 1.6 christos /foo/lib/pythonX.Y/...
1726 1.1 christos This must be done before calling Py_Initialize. */
1727 1.1 christos progname = concat (ldirname (python_libdir), SLASH_STRING, "bin",
1728 1.6 christos SLASH_STRING, "python", (char *) NULL);
1729 1.1 christos #ifdef IS_PY3K
1730 1.1 christos oldloc = xstrdup (setlocale (LC_ALL, NULL));
1731 1.1 christos setlocale (LC_ALL, "");
1732 1.1 christos progsize = strlen (progname);
1733 1.1 christos progname_copy = (wchar_t *) PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
1734 1.1 christos if (!progname_copy)
1735 1.6 christos {
1736 1.1 christos xfree (oldloc);
1737 1.1 christos fprintf (stderr, "out of memory\n");
1738 1.1 christos return;
1739 1.1 christos }
1740 1.6 christos count = mbstowcs (progname_copy, progname, progsize + 1);
1741 1.1 christos if (count == (size_t) -1)
1742 1.1 christos {
1743 1.1 christos xfree (oldloc);
1744 1.1 christos fprintf (stderr, "Could not convert python path to string\n");
1745 1.1 christos return;
1746 1.1 christos }
1747 1.1 christos setlocale (LC_ALL, oldloc);
1748 1.1 christos xfree (oldloc);
1749 1.1 christos
1750 1.1 christos /* Note that Py_SetProgramName expects the string it is passed to
1751 1.1 christos remain alive for the duration of the program's execution, so
1752 1.1 christos it is not freed after this call. */
1753 1.1 christos Py_SetProgramName (progname_copy);
1754 1.1 christos #else
1755 1.5 christos Py_SetProgramName (progname);
1756 1.1 christos #endif
1757 1.1 christos #endif
1758 1.1 christos
1759 1.5 christos Py_Initialize ();
1760 1.1 christos PyEval_InitThreads ();
1761 1.1 christos
1762 1.1 christos #ifdef IS_PY3K
1763 1.1 christos gdb_module = PyModule_Create (&python_GdbModuleDef);
1764 1.1 christos /* Add _gdb module to the list of known built-in modules. */
1765 1.1 christos _PyImport_FixupBuiltin (gdb_module, "_gdb");
1766 1.1 christos #else
1767 1.1 christos gdb_module = Py_InitModule ("_gdb", python_GdbMethods);
1768 1.1 christos #endif
1769 1.1 christos if (gdb_module == NULL)
1770 1.1 christos goto fail;
1771 1.1 christos
1772 1.1 christos /* The casts to (char*) are for python 2.4. */
1773 1.1 christos if (PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version) < 0
1774 1.1 christos || PyModule_AddStringConstant (gdb_module, "HOST_CONFIG",
1775 1.1 christos (char*) host_name) < 0
1776 1.1 christos || PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
1777 1.1 christos (char*) target_name) < 0)
1778 1.1 christos goto fail;
1779 1.1 christos
1780 1.1 christos /* Add stream constants. */
1781 1.1 christos if (PyModule_AddIntConstant (gdb_module, "STDOUT", 0) < 0
1782 1.1 christos || PyModule_AddIntConstant (gdb_module, "STDERR", 1) < 0
1783 1.1 christos || PyModule_AddIntConstant (gdb_module, "STDLOG", 2) < 0)
1784 1.1 christos goto fail;
1785 1.1 christos
1786 1.1 christos gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL);
1787 1.1 christos if (gdbpy_gdb_error == NULL
1788 1.1 christos || gdb_pymodule_addobject (gdb_module, "error", gdbpy_gdb_error) < 0)
1789 1.1 christos goto fail;
1790 1.1 christos
1791 1.1 christos gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError",
1792 1.1 christos gdbpy_gdb_error, NULL);
1793 1.1 christos if (gdbpy_gdb_memory_error == NULL
1794 1.1 christos || gdb_pymodule_addobject (gdb_module, "MemoryError",
1795 1.1 christos gdbpy_gdb_memory_error) < 0)
1796 1.1 christos goto fail;
1797 1.1 christos
1798 1.1 christos gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
1799 1.1 christos if (gdbpy_gdberror_exc == NULL
1800 1.1 christos || gdb_pymodule_addobject (gdb_module, "GdbError",
1801 1.1 christos gdbpy_gdberror_exc) < 0)
1802 1.1 christos goto fail;
1803 1.1 christos
1804 1.1 christos gdbpy_initialize_gdb_readline ();
1805 1.1 christos
1806 1.1 christos if (gdbpy_initialize_auto_load () < 0
1807 1.1 christos || gdbpy_initialize_values () < 0
1808 1.1 christos || gdbpy_initialize_frames () < 0
1809 1.1 christos || gdbpy_initialize_commands () < 0
1810 1.1 christos || gdbpy_initialize_symbols () < 0
1811 1.1 christos || gdbpy_initialize_symtabs () < 0
1812 1.1 christos || gdbpy_initialize_blocks () < 0
1813 1.1 christos || gdbpy_initialize_functions () < 0
1814 1.1 christos || gdbpy_initialize_parameters () < 0
1815 1.1 christos || gdbpy_initialize_types () < 0
1816 1.1 christos || gdbpy_initialize_pspace () < 0
1817 1.1 christos || gdbpy_initialize_objfile () < 0
1818 1.1 christos || gdbpy_initialize_breakpoints () < 0
1819 1.1 christos || gdbpy_initialize_finishbreakpoints () < 0
1820 1.1 christos || gdbpy_initialize_lazy_string () < 0
1821 1.1 christos || gdbpy_initialize_linetable () < 0
1822 1.1 christos || gdbpy_initialize_thread () < 0
1823 1.1 christos || gdbpy_initialize_inferior () < 0
1824 1.3 christos || gdbpy_initialize_events () < 0
1825 1.3 christos || gdbpy_initialize_eventregistry () < 0
1826 1.3 christos || gdbpy_initialize_py_events () < 0
1827 1.3 christos || gdbpy_initialize_event () < 0
1828 1.1 christos || gdbpy_initialize_stop_event () < 0
1829 1.1 christos || gdbpy_initialize_signal_event () < 0
1830 1.1 christos || gdbpy_initialize_breakpoint_event () < 0
1831 1.3 christos || gdbpy_initialize_continue_event () < 0
1832 1.3 christos || gdbpy_initialize_inferior_call_pre_event () < 0
1833 1.5 christos || gdbpy_initialize_inferior_call_post_event () < 0
1834 1.5 christos || gdbpy_initialize_register_changed_event () < 0
1835 1.1 christos || gdbpy_initialize_memory_changed_event () < 0
1836 1.1 christos || gdbpy_initialize_exited_event () < 0
1837 1.1 christos || gdbpy_initialize_thread_event () < 0
1838 1.1 christos || gdbpy_initialize_new_objfile_event () < 0
1839 1.1 christos || gdbpy_initialize_clear_objfiles_event () < 0
1840 1.1 christos || gdbpy_initialize_arch () < 0
1841 1.1 christos || gdbpy_initialize_xmethods () < 0
1842 1.1 christos || gdbpy_initialize_unwind () < 0)
1843 1.1 christos goto fail;
1844 1.1 christos
1845 1.1 christos gdbpy_to_string_cst = PyString_FromString ("to_string");
1846 1.1 christos if (gdbpy_to_string_cst == NULL)
1847 1.1 christos goto fail;
1848 1.1 christos gdbpy_children_cst = PyString_FromString ("children");
1849 1.1 christos if (gdbpy_children_cst == NULL)
1850 1.1 christos goto fail;
1851 1.1 christos gdbpy_display_hint_cst = PyString_FromString ("display_hint");
1852 1.1 christos if (gdbpy_display_hint_cst == NULL)
1853 1.1 christos goto fail;
1854 1.1 christos gdbpy_doc_cst = PyString_FromString ("__doc__");
1855 1.1 christos if (gdbpy_doc_cst == NULL)
1856 1.1 christos goto fail;
1857 1.1 christos gdbpy_enabled_cst = PyString_FromString ("enabled");
1858 1.1 christos if (gdbpy_enabled_cst == NULL)
1859 1.1 christos goto fail;
1860 1.1 christos gdbpy_value_cst = PyString_FromString ("value");
1861 1.1 christos if (gdbpy_value_cst == NULL)
1862 1.1 christos goto fail;
1863 1.1 christos
1864 1.1 christos /* Release the GIL while gdb runs. */
1865 1.1 christos PyThreadState_Swap (NULL);
1866 1.1 christos PyEval_ReleaseLock ();
1867 1.1 christos
1868 1.1 christos make_final_cleanup (finalize_python, NULL);
1869 1.1 christos
1870 1.1 christos gdb_python_initialized = 1;
1871 1.1 christos return;
1872 1.1 christos
1873 1.1 christos fail:
1874 1.1 christos gdbpy_print_stack ();
1875 1.1 christos /* Do not set 'gdb_python_initialized'. */
1876 1.1 christos return;
1877 1.1 christos
1878 1.3 christos #endif /* HAVE_PYTHON */
1879 1.3 christos }
1880 1.1 christos
1881 1.3 christos #ifdef HAVE_PYTHON
1882 1.3 christos
1883 1.1 christos /* Perform the remaining python initializations.
1884 1.1 christos These must be done after GDB is at least mostly initialized.
1885 1.1 christos E.g., The "info pretty-printer" command needs the "info" prefix
1886 1.1 christos command installed.
1887 1.1 christos This is the extension_language_ops.finish_initialization "method". */
1888 1.1 christos
1889 1.1 christos static void
1890 1.1 christos gdbpy_finish_initialization (const struct extension_language_defn *extlang)
1891 1.1 christos {
1892 1.1 christos PyObject *m;
1893 1.6 christos char *gdb_pythondir;
1894 1.1 christos PyObject *sys_path;
1895 1.1 christos struct cleanup *cleanup;
1896 1.1 christos
1897 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
1898 1.1 christos
1899 1.1 christos /* Add the initial data-directory to sys.path. */
1900 1.1 christos
1901 1.1 christos gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", (char *) NULL);
1902 1.1 christos make_cleanup (xfree, gdb_pythondir);
1903 1.1 christos
1904 1.1 christos sys_path = PySys_GetObject ("path");
1905 1.1 christos
1906 1.1 christos /* If sys.path is not defined yet, define it first. */
1907 1.1 christos if (!(sys_path && PyList_Check (sys_path)))
1908 1.1 christos {
1909 1.1 christos #ifdef IS_PY3K
1910 1.1 christos PySys_SetPath (L"");
1911 1.1 christos #else
1912 1.1 christos PySys_SetPath ("");
1913 1.1 christos #endif
1914 1.1 christos sys_path = PySys_GetObject ("path");
1915 1.1 christos }
1916 1.1 christos if (sys_path && PyList_Check (sys_path))
1917 1.1 christos {
1918 1.1 christos PyObject *pythondir;
1919 1.1 christos int err;
1920 1.1 christos
1921 1.1 christos pythondir = PyString_FromString (gdb_pythondir);
1922 1.1 christos if (pythondir == NULL)
1923 1.1 christos goto fail;
1924 1.1 christos
1925 1.1 christos err = PyList_Insert (sys_path, 0, pythondir);
1926 1.1 christos Py_DECREF (pythondir);
1927 1.1 christos if (err)
1928 1.1 christos goto fail;
1929 1.1 christos }
1930 1.1 christos else
1931 1.1 christos goto fail;
1932 1.1 christos
1933 1.1 christos /* Import the gdb module to finish the initialization, and
1934 1.1 christos add it to __main__ for convenience. */
1935 1.1 christos m = PyImport_AddModule ("__main__");
1936 1.1 christos if (m == NULL)
1937 1.1 christos goto fail;
1938 1.1 christos
1939 1.1 christos gdb_python_module = PyImport_ImportModule ("gdb");
1940 1.1 christos if (gdb_python_module == NULL)
1941 1.1 christos {
1942 1.1 christos gdbpy_print_stack ();
1943 1.1 christos /* This is passed in one call to warning so that blank lines aren't
1944 1.1 christos inserted between each line of text. */
1945 1.1 christos warning (_("\n"
1946 1.1 christos "Could not load the Python gdb module from `%s'.\n"
1947 1.1 christos "Limited Python support is available from the _gdb module.\n"
1948 1.1 christos "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
1949 1.1 christos gdb_pythondir);
1950 1.1 christos do_cleanups (cleanup);
1951 1.1 christos return;
1952 1.1 christos }
1953 1.1 christos
1954 1.1 christos if (gdb_pymodule_addobject (m, "gdb", gdb_python_module) < 0)
1955 1.1 christos goto fail;
1956 1.1 christos
1957 1.1 christos /* Keep the reference to gdb_python_module since it is in a global
1958 1.1 christos variable. */
1959 1.1 christos
1960 1.1 christos do_cleanups (cleanup);
1961 1.3 christos return;
1962 1.3 christos
1963 1.3 christos fail:
1964 1.3 christos gdbpy_print_stack ();
1965 1.3 christos warning (_("internal error: Unhandled Python exception"));
1966 1.3 christos do_cleanups (cleanup);
1967 1.3 christos }
1968 1.3 christos
1969 1.3 christos /* Return non-zero if Python has successfully initialized.
1970 1.1 christos This is the extension_languages_ops.initialized "method". */
1971 1.1 christos
1972 1.1 christos static int
1973 1.1 christos gdbpy_initialized (const struct extension_language_defn *extlang)
1974 1.1 christos {
1975 1.1 christos return gdb_python_initialized;
1976 1.5 christos }
1977 1.1 christos
1978 1.1 christos #endif /* HAVE_PYTHON */
1979 1.1 christos
1980 1.1 christos
1981 1.1 christos
1983 1.1 christos #ifdef HAVE_PYTHON
1984 1.1 christos
1985 1.1 christos PyMethodDef python_GdbMethods[] =
1986 1.1 christos {
1987 1.1 christos { "history", gdbpy_history, METH_VARARGS,
1988 1.1 christos "Get a value from history" },
1989 1.1 christos { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
1990 1.1 christos "execute (command [, from_tty] [, to_string]) -> [String]\n\
1991 1.1 christos Evaluate command, a string, as a gdb CLI command. Optionally returns\n\
1992 1.1 christos a Python String containing the output of the command if to_string is\n\
1993 1.1 christos set to True." },
1994 1.1 christos { "parameter", gdbpy_parameter, METH_VARARGS,
1995 1.1 christos "Return a gdb parameter's value" },
1996 1.1 christos
1997 1.1 christos { "breakpoints", gdbpy_breakpoints, METH_NOARGS,
1998 1.1 christos "Return a tuple of all breakpoint objects" },
1999 1.1 christos
2000 1.1 christos { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
2001 1.1 christos "Find the default visualizer for a Value." },
2002 1.1 christos
2003 1.1 christos { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS,
2004 1.1 christos "Return the current Progspace." },
2005 1.1 christos { "progspaces", gdbpy_progspaces, METH_NOARGS,
2006 1.1 christos "Return a sequence of all progspaces." },
2007 1.1 christos
2008 1.1 christos { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
2009 1.1 christos "Return the current Objfile being loaded, or None." },
2010 1.1 christos { "objfiles", gdbpy_objfiles, METH_NOARGS,
2011 1.1 christos "Return a sequence of all loaded objfiles." },
2012 1.1 christos
2013 1.1 christos { "newest_frame", gdbpy_newest_frame, METH_NOARGS,
2014 1.1 christos "newest_frame () -> gdb.Frame.\n\
2015 1.1 christos Return the newest frame object." },
2016 1.1 christos { "selected_frame", gdbpy_selected_frame, METH_NOARGS,
2017 1.1 christos "selected_frame () -> gdb.Frame.\n\
2018 1.1 christos Return the selected frame object." },
2019 1.1 christos { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
2020 1.1 christos "stop_reason_string (Integer) -> String.\n\
2021 1.1 christos Return a string explaining unwind stop reason." },
2022 1.1 christos
2023 1.1 christos { "lookup_type", (PyCFunction) gdbpy_lookup_type,
2024 1.1 christos METH_VARARGS | METH_KEYWORDS,
2025 1.1 christos "lookup_type (name [, block]) -> type\n\
2026 1.1 christos Return a Type corresponding to the given name." },
2027 1.1 christos { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol,
2028 1.3 christos METH_VARARGS | METH_KEYWORDS,
2029 1.3 christos "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
2030 1.3 christos Return a tuple with the symbol corresponding to the given name (or None) and\n\
2031 1.3 christos a boolean indicating if name is a field of the current implied argument\n\
2032 1.3 christos `this' (when the current language is object-oriented)." },
2033 1.3 christos { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol,
2034 1.3 christos METH_VARARGS | METH_KEYWORDS,
2035 1.3 christos "lookup_global_symbol (name [, domain]) -> symbol\n\
2036 1.1 christos Return the symbol corresponding to the given name (or None)." },
2037 1.1 christos
2038 1.1 christos { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
2039 1.1 christos METH_VARARGS | METH_KEYWORDS,
2040 1.1 christos "lookup_objfile (name, [by_build_id]) -> objfile\n\
2041 1.1 christos Look up the specified objfile.\n\
2042 1.1 christos If by_build_id is True, the objfile is looked up by using name\n\
2043 1.1 christos as its build id." },
2044 1.1 christos
2045 1.1 christos { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS,
2046 1.1 christos "Return the block containing the given pc value, or None." },
2047 1.1 christos { "solib_name", gdbpy_solib_name, METH_VARARGS,
2048 1.1 christos "solib_name (Long) -> String.\n\
2049 1.1 christos Return the name of the shared library holding a given address, or None." },
2050 1.1 christos { "decode_line", gdbpy_decode_line, METH_VARARGS,
2051 1.1 christos "decode_line (String) -> Tuple. Decode a string argument the way\n\
2052 1.1 christos that 'break' or 'edit' does. Return a tuple containing two elements.\n\
2053 1.1 christos The first element contains any unparsed portion of the String parameter\n\
2054 1.1 christos (or None if the string was fully parsed). The second element contains\n\
2055 1.1 christos a tuple that contains all the locations that match, represented as\n\
2056 1.1 christos gdb.Symtab_and_line objects (or None)."},
2057 1.1 christos { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
2058 1.1 christos "parse_and_eval (String) -> Value.\n\
2059 1.1 christos Parse String as an expression, evaluate it, and return the result as a Value."
2060 1.1 christos },
2061 1.1 christos { "find_pc_line", gdbpy_find_pc_line, METH_VARARGS,
2062 1.1 christos "find_pc_line (pc) -> Symtab_and_line.\n\
2063 1.1 christos Return the gdb.Symtab_and_line object corresponding to the pc value." },
2064 1.1 christos
2065 1.1 christos { "post_event", gdbpy_post_event, METH_VARARGS,
2066 1.1 christos "Post an event into gdb's event loop." },
2067 1.1 christos
2068 1.1 christos { "target_charset", gdbpy_target_charset, METH_NOARGS,
2069 1.1 christos "target_charset () -> string.\n\
2070 1.1 christos Return the name of the current target charset." },
2071 1.1 christos { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
2072 1.1 christos "target_wide_charset () -> string.\n\
2073 1.1 christos Return the name of the current target wide charset." },
2074 1.1 christos
2075 1.1 christos { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS,
2076 1.1 christos "string_to_argv (String) -> Array.\n\
2077 1.1 christos Parse String and return an argv-like array.\n\
2078 1.1 christos Arguments are separate by spaces and may be quoted."
2079 1.1 christos },
2080 1.1 christos { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS,
2081 1.1 christos "Write a string using gdb's filtered stream." },
2082 1.1 christos { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS,
2083 1.1 christos "Flush gdb's filtered stdout stream." },
2084 1.6 christos { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
2085 1.6 christos "selected_thread () -> gdb.InferiorThread.\n\
2086 1.6 christos Return the selected thread object." },
2087 1.6 christos { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
2088 1.6 christos "selected_inferior () -> gdb.Inferior.\n\
2089 1.6 christos Return the selected inferior object." },
2090 1.1 christos { "inferiors", gdbpy_inferiors, METH_NOARGS,
2091 1.1 christos "inferiors () -> (gdb.Inferior, ...).\n\
2092 1.1 christos Return a tuple containing all inferiors." },
2093 1.1 christos
2094 1.5 christos { "invalidate_cached_frames", gdbpy_invalidate_cached_frames, METH_NOARGS,
2095 1.1 christos "invalidate_cached_frames () -> None.\n\
2096 1.1 christos Invalidate any cached frame objects in gdb.\n\
2097 1.1 christos Intended for internal use only." },
2098 1.1 christos
2099 1.1 christos {NULL, NULL, 0, NULL}
2100 1.5 christos };
2101 1.1 christos
2102 1.1 christos #ifdef IS_PY3K
2103 1.1 christos struct PyModuleDef python_GdbModuleDef =
2104 1.1 christos {
2105 1.1 christos PyModuleDef_HEAD_INIT,
2106 1.1 christos "_gdb",
2107 1.1 christos NULL,
2108 -1,
2109 python_GdbMethods,
2110 NULL,
2111 NULL,
2112 NULL,
2113 NULL
2114 };
2115 #endif
2116 #endif /* HAVE_PYTHON */
2117