python.c revision 1.5 1 1.1 christos /* General python/gdb code
2 1.1 christos
3 1.3 christos Copyright (C) 2008-2015 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.1 christos
38 1.1 christos /* Declared constants and enum for python stack printing. */
39 1.1 christos static const char python_excp_none[] = "none";
40 1.1 christos static const char python_excp_full[] = "full";
41 1.1 christos static const char python_excp_message[] = "message";
42 1.1 christos
43 1.1 christos /* "set python print-stack" choices. */
44 1.1 christos static const char *const python_excp_enums[] =
45 1.1 christos {
46 1.1 christos python_excp_none,
47 1.1 christos python_excp_full,
48 1.1 christos python_excp_message,
49 1.1 christos NULL
50 1.1 christos };
51 1.1 christos
52 1.1 christos /* The exception printing variable. 'full' if we want to print the
53 1.1 christos error message and stack, 'none' if we want to print nothing, and
54 1.1 christos 'message' if we only want to print the error message. 'message' is
55 1.1 christos the default. */
56 1.1 christos static const char *gdbpy_should_print_stack = python_excp_message;
57 1.1 christos
58 1.1 christos #ifdef HAVE_PYTHON
59 1.3 christos /* Forward decls, these are defined later. */
60 1.5 christos extern const struct extension_language_script_ops python_extension_script_ops;
61 1.5 christos extern const struct extension_language_ops python_extension_ops;
62 1.3 christos #endif
63 1.3 christos
64 1.3 christos /* The main struct describing GDB's interface to the Python
65 1.3 christos extension language. */
66 1.3 christos const struct extension_language_defn extension_language_python =
67 1.3 christos {
68 1.3 christos EXT_LANG_PYTHON,
69 1.3 christos "python",
70 1.3 christos "Python",
71 1.3 christos
72 1.3 christos ".py",
73 1.3 christos "-gdb.py",
74 1.3 christos
75 1.3 christos python_control,
76 1.3 christos
77 1.3 christos #ifdef HAVE_PYTHON
78 1.3 christos &python_extension_script_ops,
79 1.3 christos &python_extension_ops
80 1.3 christos #else
81 1.3 christos NULL,
82 1.3 christos NULL
83 1.3 christos #endif
84 1.3 christos };
85 1.3 christos
86 1.3 christos #ifdef HAVE_PYTHON
88 1.1 christos
89 1.1 christos #include "cli/cli-decode.h"
90 1.1 christos #include "charset.h"
91 1.1 christos #include "top.h"
92 1.1 christos #include "solib.h"
93 1.1 christos #include "python-internal.h"
94 1.1 christos #include "linespec.h"
95 1.1 christos #include "source.h"
96 1.1 christos #include "version.h"
97 1.1 christos #include "target.h"
98 1.1 christos #include "gdbthread.h"
99 1.1 christos #include "interps.h"
100 1.1 christos #include "event-top.h"
101 1.1 christos
102 1.1 christos /* True if Python has been successfully initialized, false
103 1.1 christos otherwise. */
104 1.1 christos
105 1.1 christos int gdb_python_initialized;
106 1.5 christos
107 1.1 christos extern PyMethodDef python_GdbMethods[];
108 1.1 christos
109 1.5 christos #ifdef IS_PY3K
110 1.1 christos extern struct PyModuleDef python_GdbModuleDef;
111 1.1 christos #endif
112 1.1 christos
113 1.1 christos PyObject *gdb_module;
114 1.1 christos PyObject *gdb_python_module;
115 1.1 christos
116 1.1 christos /* Some string constants we may wish to use. */
117 1.1 christos PyObject *gdbpy_to_string_cst;
118 1.1 christos PyObject *gdbpy_children_cst;
119 1.1 christos PyObject *gdbpy_display_hint_cst;
120 1.1 christos PyObject *gdbpy_doc_cst;
121 1.1 christos PyObject *gdbpy_enabled_cst;
122 1.1 christos PyObject *gdbpy_value_cst;
123 1.1 christos
124 1.1 christos /* The GdbError exception. */
125 1.1 christos PyObject *gdbpy_gdberror_exc;
126 1.1 christos
127 1.1 christos /* The `gdb.error' base class. */
128 1.1 christos PyObject *gdbpy_gdb_error;
129 1.1 christos
130 1.1 christos /* The `gdb.MemoryError' exception. */
131 1.1 christos PyObject *gdbpy_gdb_memory_error;
132 1.3 christos
133 1.3 christos static script_sourcer_func gdbpy_source_script;
134 1.5 christos static objfile_script_sourcer_func gdbpy_source_objfile_script;
135 1.3 christos static objfile_script_executor_func gdbpy_execute_objfile_script;
136 1.3 christos static void gdbpy_finish_initialization
137 1.3 christos (const struct extension_language_defn *);
138 1.3 christos static int gdbpy_initialized (const struct extension_language_defn *);
139 1.3 christos static void gdbpy_eval_from_control_command
140 1.3 christos (const struct extension_language_defn *, struct command_line *cmd);
141 1.3 christos static void gdbpy_start_type_printers (const struct extension_language_defn *,
142 1.3 christos struct ext_lang_type_printers *);
143 1.3 christos static enum ext_lang_rc gdbpy_apply_type_printers
144 1.3 christos (const struct extension_language_defn *,
145 1.3 christos const struct ext_lang_type_printers *, struct type *, char **);
146 1.3 christos static void gdbpy_free_type_printers (const struct extension_language_defn *,
147 1.3 christos struct ext_lang_type_printers *);
148 1.3 christos static void gdbpy_clear_quit_flag (const struct extension_language_defn *);
149 1.3 christos static void gdbpy_set_quit_flag (const struct extension_language_defn *);
150 1.3 christos static int gdbpy_check_quit_flag (const struct extension_language_defn *);
151 1.3 christos static enum ext_lang_rc gdbpy_before_prompt_hook
152 1.3 christos (const struct extension_language_defn *, const char *current_gdb_prompt);
153 1.3 christos
154 1.3 christos /* The interface between gdb proper and loading of python scripts. */
155 1.5 christos
156 1.3 christos const struct extension_language_script_ops python_extension_script_ops =
157 1.3 christos {
158 1.3 christos gdbpy_source_script,
159 1.5 christos gdbpy_source_objfile_script,
160 1.3 christos gdbpy_execute_objfile_script,
161 1.3 christos gdbpy_auto_load_enabled
162 1.3 christos };
163 1.3 christos
164 1.3 christos /* The interface between gdb proper and python extensions. */
165 1.5 christos
166 1.3 christos const struct extension_language_ops python_extension_ops =
167 1.3 christos {
168 1.3 christos gdbpy_finish_initialization,
169 1.3 christos gdbpy_initialized,
170 1.3 christos
171 1.3 christos gdbpy_eval_from_control_command,
172 1.3 christos
173 1.3 christos gdbpy_start_type_printers,
174 1.3 christos gdbpy_apply_type_printers,
175 1.3 christos gdbpy_free_type_printers,
176 1.3 christos
177 1.3 christos gdbpy_apply_val_pretty_printer,
178 1.3 christos
179 1.3 christos gdbpy_apply_frame_filter,
180 1.3 christos
181 1.3 christos gdbpy_preserve_values,
182 1.3 christos
183 1.3 christos gdbpy_breakpoint_has_cond,
184 1.3 christos gdbpy_breakpoint_cond_says_stop,
185 1.3 christos
186 1.3 christos gdbpy_clear_quit_flag,
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.1 christos {
251 1.1 christos struct python_env *env = xmalloc (sizeof *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 /* Clear the quit flag. */
273 1.3 christos
274 1.3 christos static void
275 1.1 christos gdbpy_clear_quit_flag (const struct extension_language_defn *extlang)
276 1.1 christos {
277 1.1 christos /* This clears the flag as a side effect. */
278 1.1 christos PyOS_InterruptOccurred ();
279 1.1 christos }
280 1.1 christos
281 1.1 christos /* Set the quit flag. */
282 1.3 christos
283 1.3 christos static void
284 1.1 christos gdbpy_set_quit_flag (const struct extension_language_defn *extlang)
285 1.1 christos {
286 1.1 christos PyErr_SetInterrupt ();
287 1.1 christos }
288 1.1 christos
289 1.1 christos /* Return true if the quit flag has been set, false otherwise. */
290 1.3 christos
291 1.3 christos static int
292 1.1 christos gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
293 1.1 christos {
294 1.1 christos return PyOS_InterruptOccurred ();
295 1.1 christos }
296 1.1 christos
297 1.1 christos /* Evaluate a Python command like PyRun_SimpleString, but uses
298 1.1 christos Py_single_input which prints the result of expressions, and does
299 1.1 christos not automatically print the stack on errors. */
300 1.1 christos
301 1.1 christos static int
302 1.1 christos eval_python_command (const char *command)
303 1.1 christos {
304 1.1 christos PyObject *m, *d, *v;
305 1.1 christos
306 1.1 christos m = PyImport_AddModule ("__main__");
307 1.1 christos if (m == NULL)
308 1.1 christos return -1;
309 1.1 christos
310 1.1 christos d = PyModule_GetDict (m);
311 1.1 christos if (d == NULL)
312 1.1 christos return -1;
313 1.1 christos v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
314 1.1 christos if (v == NULL)
315 1.1 christos return -1;
316 1.1 christos
317 1.1 christos Py_DECREF (v);
318 1.1 christos #ifndef IS_PY3K
319 1.1 christos if (Py_FlushLine ())
320 1.1 christos PyErr_Clear ();
321 1.1 christos #endif
322 1.1 christos
323 1.1 christos return 0;
324 1.1 christos }
325 1.1 christos
326 1.1 christos /* Implementation of the gdb "python-interactive" command. */
327 1.1 christos
328 1.1 christos static void
329 1.1 christos python_interactive_command (char *arg, int from_tty)
330 1.1 christos {
331 1.1 christos struct cleanup *cleanup;
332 1.1 christos int err;
333 1.1 christos
334 1.1 christos cleanup = make_cleanup_restore_integer (&interpreter_async);
335 1.1 christos interpreter_async = 0;
336 1.1 christos
337 1.1 christos arg = skip_spaces (arg);
338 1.1 christos
339 1.1 christos ensure_python_env (get_current_arch (), current_language);
340 1.1 christos
341 1.1 christos if (arg && *arg)
342 1.1 christos {
343 1.1 christos int len = strlen (arg);
344 1.1 christos char *script = xmalloc (len + 2);
345 1.1 christos
346 1.1 christos strcpy (script, arg);
347 1.1 christos script[len] = '\n';
348 1.1 christos script[len + 1] = '\0';
349 1.1 christos err = eval_python_command (script);
350 1.1 christos xfree (script);
351 1.1 christos }
352 1.1 christos else
353 1.1 christos {
354 1.1 christos err = PyRun_InteractiveLoop (instream, "<stdin>");
355 1.1 christos dont_repeat ();
356 1.1 christos }
357 1.1 christos
358 1.1 christos if (err)
359 1.1 christos {
360 1.1 christos gdbpy_print_stack ();
361 1.1 christos error (_("Error while executing Python code."));
362 1.1 christos }
363 1.1 christos
364 1.1 christos do_cleanups (cleanup);
365 1.1 christos }
366 1.1 christos
367 1.1 christos /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
368 1.1 christos named FILENAME.
369 1.1 christos
370 1.1 christos On Windows hosts few users would build Python themselves (this is no
371 1.1 christos trivial task on this platform), and thus use binaries built by
372 1.1 christos someone else instead. There may happen situation where the Python
373 1.1 christos library and GDB are using two different versions of the C runtime
374 1.1 christos library. Python, being built with VC, would use one version of the
375 1.1 christos msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
376 1.1 christos A FILE * from one runtime does not necessarily operate correctly in
377 1.1 christos the other runtime.
378 1.1 christos
379 1.1 christos To work around this potential issue, we create on Windows hosts the
380 1.1 christos FILE object using Python routines, thus making sure that it is
381 1.1 christos compatible with the Python library. */
382 1.1 christos
383 1.1 christos static void
384 1.1 christos python_run_simple_file (FILE *file, const char *filename)
385 1.1 christos {
386 1.1 christos #ifndef _WIN32
387 1.1 christos
388 1.1 christos PyRun_SimpleFile (file, filename);
389 1.1 christos
390 1.1 christos #else /* _WIN32 */
391 1.1 christos
392 1.1 christos char *full_path;
393 1.1 christos PyObject *python_file;
394 1.1 christos struct cleanup *cleanup;
395 1.1 christos
396 1.1 christos /* Because we have a string for a filename, and are using Python to
397 1.1 christos open the file, we need to expand any tilde in the path first. */
398 1.1 christos full_path = tilde_expand (filename);
399 1.1 christos cleanup = make_cleanup (xfree, full_path);
400 1.1 christos python_file = PyFile_FromString (full_path, "r");
401 1.1 christos if (! python_file)
402 1.1 christos {
403 1.1 christos do_cleanups (cleanup);
404 1.1 christos gdbpy_print_stack ();
405 1.1 christos error (_("Error while opening file: %s"), full_path);
406 1.1 christos }
407 1.1 christos
408 1.1 christos make_cleanup_py_decref (python_file);
409 1.1 christos PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
410 1.1 christos do_cleanups (cleanup);
411 1.1 christos
412 1.1 christos #endif /* _WIN32 */
413 1.1 christos }
414 1.1 christos
415 1.1 christos /* Given a command_line, return a command string suitable for passing
416 1.1 christos to Python. Lines in the string are separated by newlines. The
417 1.1 christos return value is allocated using xmalloc and the caller is
418 1.1 christos responsible for freeing it. */
419 1.1 christos
420 1.1 christos static char *
421 1.1 christos compute_python_string (struct command_line *l)
422 1.1 christos {
423 1.1 christos struct command_line *iter;
424 1.1 christos char *script = NULL;
425 1.1 christos int size = 0;
426 1.1 christos int here;
427 1.1 christos
428 1.1 christos for (iter = l; iter; iter = iter->next)
429 1.1 christos size += strlen (iter->line) + 1;
430 1.1 christos
431 1.1 christos script = xmalloc (size + 1);
432 1.1 christos here = 0;
433 1.1 christos for (iter = l; iter; iter = iter->next)
434 1.1 christos {
435 1.1 christos int len = strlen (iter->line);
436 1.1 christos
437 1.1 christos strcpy (&script[here], iter->line);
438 1.1 christos here += len;
439 1.1 christos script[here++] = '\n';
440 1.1 christos }
441 1.1 christos script[here] = '\0';
442 1.1 christos return script;
443 1.1 christos }
444 1.1 christos
445 1.1 christos /* Take a command line structure representing a 'python' command, and
446 1.1 christos evaluate its body using the Python interpreter. */
447 1.3 christos
448 1.3 christos static void
449 1.3 christos gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
450 1.1 christos struct command_line *cmd)
451 1.1 christos {
452 1.1 christos int ret;
453 1.1 christos char *script;
454 1.1 christos struct cleanup *cleanup;
455 1.1 christos
456 1.1 christos if (cmd->body_count != 1)
457 1.1 christos error (_("Invalid \"python\" block structure."));
458 1.1 christos
459 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
460 1.1 christos
461 1.1 christos script = compute_python_string (cmd->body_list[0]);
462 1.1 christos ret = PyRun_SimpleString (script);
463 1.1 christos xfree (script);
464 1.1 christos if (ret)
465 1.1 christos error (_("Error while executing Python code."));
466 1.1 christos
467 1.1 christos do_cleanups (cleanup);
468 1.1 christos }
469 1.1 christos
470 1.1 christos /* Implementation of the gdb "python" command. */
471 1.1 christos
472 1.1 christos static void
473 1.1 christos python_command (char *arg, int from_tty)
474 1.1 christos {
475 1.1 christos struct cleanup *cleanup;
476 1.1 christos
477 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
478 1.1 christos
479 1.1 christos make_cleanup_restore_integer (&interpreter_async);
480 1.1 christos interpreter_async = 0;
481 1.1 christos
482 1.1 christos arg = skip_spaces (arg);
483 1.1 christos if (arg && *arg)
484 1.1 christos {
485 1.1 christos if (PyRun_SimpleString (arg))
486 1.1 christos error (_("Error while executing Python code."));
487 1.1 christos }
488 1.1 christos else
489 1.1 christos {
490 1.1 christos struct command_line *l = get_command_line (python_control, "");
491 1.1 christos
492 1.1 christos make_cleanup_free_command_lines (&l);
493 1.1 christos execute_control_command_untraced (l);
494 1.1 christos }
495 1.1 christos
496 1.1 christos do_cleanups (cleanup);
497 1.1 christos }
498 1.1 christos
499 1.1 christos
500 1.1 christos
502 1.1 christos /* Transform a gdb parameters's value into a Python value. May return
503 1.1 christos NULL (and set a Python exception) on error. Helper function for
504 1.1 christos get_parameter. */
505 1.1 christos PyObject *
506 1.1 christos gdbpy_parameter_value (enum var_types type, void *var)
507 1.1 christos {
508 1.1 christos switch (type)
509 1.1 christos {
510 1.1 christos case var_string:
511 1.1 christos case var_string_noescape:
512 1.1 christos case var_optional_filename:
513 1.1 christos case var_filename:
514 1.1 christos case var_enum:
515 1.1 christos {
516 1.1 christos char *str = * (char **) var;
517 1.1 christos
518 1.1 christos if (! str)
519 1.1 christos str = "";
520 1.1 christos return PyString_Decode (str, strlen (str), host_charset (), NULL);
521 1.1 christos }
522 1.1 christos
523 1.1 christos case var_boolean:
524 1.1 christos {
525 1.1 christos if (* (int *) var)
526 1.1 christos Py_RETURN_TRUE;
527 1.1 christos else
528 1.1 christos Py_RETURN_FALSE;
529 1.1 christos }
530 1.1 christos
531 1.1 christos case var_auto_boolean:
532 1.1 christos {
533 1.1 christos enum auto_boolean ab = * (enum auto_boolean *) var;
534 1.1 christos
535 1.1 christos if (ab == AUTO_BOOLEAN_TRUE)
536 1.1 christos Py_RETURN_TRUE;
537 1.1 christos else if (ab == AUTO_BOOLEAN_FALSE)
538 1.1 christos Py_RETURN_FALSE;
539 1.1 christos else
540 1.1 christos Py_RETURN_NONE;
541 1.1 christos }
542 1.1 christos
543 1.1 christos case var_integer:
544 1.1 christos if ((* (int *) var) == INT_MAX)
545 1.1 christos Py_RETURN_NONE;
546 1.1 christos /* Fall through. */
547 1.1 christos case var_zinteger:
548 1.1 christos return PyLong_FromLong (* (int *) var);
549 1.1 christos
550 1.1 christos case var_uinteger:
551 1.1 christos {
552 1.1 christos unsigned int val = * (unsigned int *) var;
553 1.1 christos
554 1.1 christos if (val == UINT_MAX)
555 1.1 christos Py_RETURN_NONE;
556 1.1 christos return PyLong_FromUnsignedLong (val);
557 1.1 christos }
558 1.1 christos }
559 1.1 christos
560 1.1 christos return PyErr_Format (PyExc_RuntimeError,
561 1.1 christos _("Programmer error: unhandled type."));
562 1.1 christos }
563 1.1 christos
564 1.1 christos /* A Python function which returns a gdb parameter's value as a Python
565 1.1 christos value. */
566 1.1 christos
567 1.1 christos PyObject *
568 1.5 christos gdbpy_parameter (PyObject *self, PyObject *args)
569 1.1 christos {
570 1.1 christos struct gdb_exception except = exception_none;
571 1.1 christos struct cmd_list_element *alias, *prefix, *cmd;
572 1.1 christos const char *arg;
573 1.1 christos char *newarg;
574 1.1 christos int found = -1;
575 1.1 christos
576 1.1 christos if (! PyArg_ParseTuple (args, "s", &arg))
577 1.1 christos return NULL;
578 1.1 christos
579 1.5 christos newarg = concat ("show ", arg, (char *) NULL);
580 1.1 christos
581 1.1 christos TRY
582 1.1 christos {
583 1.5 christos found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd);
584 1.5 christos }
585 1.5 christos CATCH (ex, RETURN_MASK_ALL)
586 1.5 christos {
587 1.5 christos except = ex;
588 1.5 christos }
589 1.1 christos END_CATCH
590 1.1 christos
591 1.1 christos xfree (newarg);
592 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
593 1.1 christos if (!found)
594 1.1 christos return PyErr_Format (PyExc_RuntimeError,
595 1.1 christos _("Could not find parameter `%s'."), arg);
596 1.1 christos
597 1.1 christos if (! cmd->var)
598 1.1 christos return PyErr_Format (PyExc_RuntimeError,
599 1.1 christos _("`%s' is not a parameter."), arg);
600 1.1 christos return gdbpy_parameter_value (cmd->var_type, cmd->var);
601 1.1 christos }
602 1.1 christos
603 1.1 christos /* Wrapper for target_charset. */
604 1.1 christos
605 1.1 christos static PyObject *
606 1.1 christos gdbpy_target_charset (PyObject *self, PyObject *args)
607 1.1 christos {
608 1.1 christos const char *cset = target_charset (python_gdbarch);
609 1.1 christos
610 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
611 1.1 christos }
612 1.1 christos
613 1.1 christos /* Wrapper for target_wide_charset. */
614 1.1 christos
615 1.1 christos static PyObject *
616 1.1 christos gdbpy_target_wide_charset (PyObject *self, PyObject *args)
617 1.1 christos {
618 1.1 christos const char *cset = target_wide_charset (python_gdbarch);
619 1.1 christos
620 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
621 1.1 christos }
622 1.1 christos
623 1.1 christos /* A Python function which evaluates a string using the gdb CLI. */
624 1.1 christos
625 1.1 christos static PyObject *
626 1.1 christos execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
627 1.1 christos {
628 1.1 christos const char *arg;
629 1.1 christos PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
630 1.1 christos int from_tty, to_string;
631 1.1 christos static char *keywords[] = {"command", "from_tty", "to_string", NULL };
632 1.1 christos char *result = NULL;
633 1.1 christos
634 1.1 christos if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
635 1.1 christos &PyBool_Type, &from_tty_obj,
636 1.1 christos &PyBool_Type, &to_string_obj))
637 1.1 christos return NULL;
638 1.1 christos
639 1.1 christos from_tty = 0;
640 1.1 christos if (from_tty_obj)
641 1.1 christos {
642 1.1 christos int cmp = PyObject_IsTrue (from_tty_obj);
643 1.1 christos if (cmp < 0)
644 1.1 christos return NULL;
645 1.1 christos from_tty = cmp;
646 1.1 christos }
647 1.1 christos
648 1.1 christos to_string = 0;
649 1.1 christos if (to_string_obj)
650 1.1 christos {
651 1.1 christos int cmp = PyObject_IsTrue (to_string_obj);
652 1.1 christos if (cmp < 0)
653 1.1 christos return NULL;
654 1.1 christos to_string = cmp;
655 1.5 christos }
656 1.1 christos
657 1.1 christos TRY
658 1.1 christos {
659 1.1 christos /* Copy the argument text in case the command modifies it. */
660 1.1 christos char *copy = xstrdup (arg);
661 1.1 christos struct cleanup *cleanup = make_cleanup (xfree, copy);
662 1.1 christos
663 1.1 christos make_cleanup_restore_integer (&interpreter_async);
664 1.1 christos interpreter_async = 0;
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.1 christos char *soname;
702 1.1 christos PyObject *str_obj;
703 1.1 christos gdb_py_longest pc;
704 1.1 christos
705 1.1 christos if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc))
706 1.1 christos return NULL;
707 1.1 christos
708 1.1 christos soname = solib_name_from_address (current_program_space, pc);
709 1.1 christos if (soname)
710 1.1 christos str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL);
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.1 christos appease gcc. */
728 1.1 christos struct symtab_and_line sal;
729 1.1 christos const char *arg = NULL;
730 1.1 christos char *copy_to_free = NULL, *copy = NULL;
731 1.1 christos struct cleanup *cleanups;
732 1.1 christos PyObject *result = NULL;
733 1.1 christos PyObject *return_result = NULL;
734 1.1 christos PyObject *unparsed = 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.5 christos sals.sals = NULL;
742 1.1 christos
743 1.1 christos TRY
744 1.1 christos {
745 1.1 christos if (arg)
746 1.1 christos {
747 1.1 christos copy = xstrdup (arg);
748 1.1 christos copy_to_free = copy;
749 1.1 christos sals = decode_line_1 (©, 0, 0, 0);
750 1.1 christos }
751 1.1 christos else
752 1.1 christos {
753 1.1 christos set_default_source_symtab_and_line ();
754 1.1 christos sal = get_current_source_symtab_and_line ();
755 1.1 christos sals.sals = &sal;
756 1.1 christos sals.nelts = 1;
757 1.5 christos }
758 1.5 christos }
759 1.5 christos CATCH (ex, RETURN_MASK_ALL)
760 1.5 christos {
761 1.5 christos except = ex;
762 1.1 christos }
763 1.1 christos END_CATCH
764 1.1 christos
765 1.1 christos if (sals.sals != NULL && sals.sals != &sal)
766 1.1 christos {
767 1.1 christos make_cleanup (xfree, copy_to_free);
768 1.1 christos make_cleanup (xfree, sals.sals);
769 1.1 christos }
770 1.1 christos
771 1.1 christos if (except.reason < 0)
772 1.1 christos {
773 1.1 christos do_cleanups (cleanups);
774 1.1 christos /* We know this will always throw. */
775 1.1 christos gdbpy_convert_exception (except);
776 1.1 christos return NULL;
777 1.1 christos }
778 1.1 christos
779 1.1 christos if (sals.nelts)
780 1.1 christos {
781 1.1 christos int i;
782 1.1 christos
783 1.1 christos result = PyTuple_New (sals.nelts);
784 1.1 christos if (! result)
785 1.1 christos goto error;
786 1.1 christos for (i = 0; i < sals.nelts; ++i)
787 1.1 christos {
788 1.1 christos PyObject *obj;
789 1.1 christos
790 1.1 christos obj = symtab_and_line_to_sal_object (sals.sals[i]);
791 1.1 christos if (! obj)
792 1.1 christos {
793 1.1 christos Py_DECREF (result);
794 1.1 christos goto error;
795 1.1 christos }
796 1.1 christos
797 1.1 christos PyTuple_SetItem (result, i, obj);
798 1.1 christos }
799 1.1 christos }
800 1.1 christos else
801 1.1 christos {
802 1.1 christos result = Py_None;
803 1.1 christos Py_INCREF (Py_None);
804 1.1 christos }
805 1.1 christos
806 1.1 christos return_result = PyTuple_New (2);
807 1.1 christos if (! return_result)
808 1.1 christos {
809 1.1 christos Py_DECREF (result);
810 1.1 christos goto error;
811 1.1 christos }
812 1.1 christos
813 1.1 christos if (copy && strlen (copy) > 0)
814 1.1 christos {
815 1.1 christos unparsed = PyString_FromString (copy);
816 1.1 christos if (unparsed == NULL)
817 1.1 christos {
818 1.1 christos Py_DECREF (result);
819 1.1 christos Py_DECREF (return_result);
820 1.1 christos return_result = NULL;
821 1.1 christos goto error;
822 1.1 christos }
823 1.1 christos }
824 1.1 christos else
825 1.1 christos {
826 1.1 christos unparsed = Py_None;
827 1.1 christos Py_INCREF (Py_None);
828 1.1 christos }
829 1.1 christos
830 1.1 christos PyTuple_SetItem (return_result, 0, unparsed);
831 1.1 christos PyTuple_SetItem (return_result, 1, result);
832 1.1 christos
833 1.1 christos error:
834 1.1 christos do_cleanups (cleanups);
835 1.1 christos
836 1.1 christos return return_result;
837 1.1 christos }
838 1.1 christos
839 1.1 christos /* Parse a string and evaluate it as an expression. */
840 1.1 christos static PyObject *
841 1.1 christos gdbpy_parse_and_eval (PyObject *self, PyObject *args)
842 1.1 christos {
843 1.1 christos const char *expr_str;
844 1.1 christos struct value *result = NULL;
845 1.1 christos
846 1.1 christos if (!PyArg_ParseTuple (args, "s", &expr_str))
847 1.5 christos return NULL;
848 1.1 christos
849 1.1 christos TRY
850 1.1 christos {
851 1.5 christos result = parse_and_eval (expr_str);
852 1.5 christos }
853 1.5 christos CATCH (except, RETURN_MASK_ALL)
854 1.5 christos {
855 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
856 1.1 christos }
857 1.1 christos END_CATCH
858 1.1 christos
859 1.1 christos return value_to_value_object (result);
860 1.1 christos }
861 1.1 christos
862 1.1 christos /* Implementation of gdb.find_pc_line function.
863 1.1 christos Returns the gdb.Symtab_and_line object corresponding to a PC value. */
864 1.1 christos
865 1.1 christos static PyObject *
866 1.1 christos gdbpy_find_pc_line (PyObject *self, PyObject *args)
867 1.1 christos {
868 1.1 christos gdb_py_ulongest pc_llu;
869 1.1 christos PyObject *result = NULL; /* init for gcc -Wall */
870 1.1 christos
871 1.1 christos if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
872 1.5 christos return NULL;
873 1.1 christos
874 1.1 christos TRY
875 1.1 christos {
876 1.1 christos struct symtab_and_line sal;
877 1.1 christos CORE_ADDR pc;
878 1.1 christos
879 1.1 christos pc = (CORE_ADDR) pc_llu;
880 1.1 christos sal = find_pc_line (pc, 0);
881 1.5 christos result = symtab_and_line_to_sal_object (sal);
882 1.5 christos }
883 1.5 christos CATCH (except, RETURN_MASK_ALL)
884 1.5 christos {
885 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
886 1.1 christos }
887 1.1 christos END_CATCH
888 1.1 christos
889 1.1 christos return result;
890 1.1 christos }
891 1.3 christos
892 1.3 christos /* Read a file as Python code.
893 1.1 christos This is the extension_language_script_ops.script_sourcer "method".
894 1.1 christos FILE is the file to load. FILENAME is name of the file FILE.
895 1.1 christos This does not throw any errors. If an exception occurs python will print
896 1.3 christos the traceback and clear the error indicator. */
897 1.3 christos
898 1.3 christos static void
899 1.1 christos gdbpy_source_script (const struct extension_language_defn *extlang,
900 1.1 christos FILE *file, const char *filename)
901 1.1 christos {
902 1.1 christos struct cleanup *cleanup;
903 1.1 christos
904 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
905 1.1 christos python_run_simple_file (file, filename);
906 1.1 christos do_cleanups (cleanup);
907 1.1 christos }
908 1.1 christos
909 1.1 christos
910 1.1 christos
912 1.1 christos /* Posting and handling events. */
913 1.1 christos
914 1.1 christos /* A single event. */
915 1.1 christos struct gdbpy_event
916 1.1 christos {
917 1.1 christos /* The Python event. This is just a callable object. */
918 1.1 christos PyObject *event;
919 1.1 christos /* The next event. */
920 1.1 christos struct gdbpy_event *next;
921 1.1 christos };
922 1.1 christos
923 1.1 christos /* All pending events. */
924 1.1 christos static struct gdbpy_event *gdbpy_event_list;
925 1.1 christos /* The final link of the event list. */
926 1.1 christos static struct gdbpy_event **gdbpy_event_list_end;
927 1.1 christos
928 1.1 christos /* We use a file handler, and not an async handler, so that we can
929 1.1 christos wake up the main thread even when it is blocked in poll(). */
930 1.1 christos static struct serial *gdbpy_event_fds[2];
931 1.1 christos
932 1.1 christos /* The file handler callback. This reads from the internal pipe, and
933 1.1 christos then processes the Python event queue. This will always be run in
934 1.1 christos the main gdb thread. */
935 1.1 christos
936 1.1 christos static void
937 1.1 christos gdbpy_run_events (struct serial *scb, void *context)
938 1.1 christos {
939 1.1 christos struct cleanup *cleanup;
940 1.1 christos
941 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
942 1.1 christos
943 1.1 christos /* Flush the fd. Do this before flushing the events list, so that
944 1.1 christos any new event post afterwards is sure to re-awake the event
945 1.1 christos loop. */
946 1.1 christos while (serial_readchar (gdbpy_event_fds[0], 0) >= 0)
947 1.1 christos ;
948 1.1 christos
949 1.1 christos while (gdbpy_event_list)
950 1.1 christos {
951 1.1 christos PyObject *call_result;
952 1.1 christos
953 1.1 christos /* Dispatching the event might push a new element onto the event
954 1.1 christos loop, so we update here "atomically enough". */
955 1.1 christos struct gdbpy_event *item = gdbpy_event_list;
956 1.1 christos gdbpy_event_list = gdbpy_event_list->next;
957 1.1 christos if (gdbpy_event_list == NULL)
958 1.1 christos gdbpy_event_list_end = &gdbpy_event_list;
959 1.1 christos
960 1.1 christos /* Ignore errors. */
961 1.1 christos call_result = PyObject_CallObject (item->event, NULL);
962 1.1 christos if (call_result == NULL)
963 1.1 christos PyErr_Clear ();
964 1.1 christos
965 1.1 christos Py_XDECREF (call_result);
966 1.1 christos Py_DECREF (item->event);
967 1.1 christos xfree (item);
968 1.1 christos }
969 1.1 christos
970 1.1 christos do_cleanups (cleanup);
971 1.1 christos }
972 1.1 christos
973 1.1 christos /* Submit an event to the gdb thread. */
974 1.1 christos static PyObject *
975 1.1 christos gdbpy_post_event (PyObject *self, PyObject *args)
976 1.1 christos {
977 1.1 christos struct gdbpy_event *event;
978 1.1 christos PyObject *func;
979 1.1 christos int wakeup;
980 1.1 christos
981 1.1 christos if (!PyArg_ParseTuple (args, "O", &func))
982 1.1 christos return NULL;
983 1.1 christos
984 1.1 christos if (!PyCallable_Check (func))
985 1.1 christos {
986 1.1 christos PyErr_SetString (PyExc_RuntimeError,
987 1.1 christos _("Posted event is not callable"));
988 1.1 christos return NULL;
989 1.1 christos }
990 1.1 christos
991 1.1 christos Py_INCREF (func);
992 1.1 christos
993 1.1 christos /* From here until the end of the function, we have the GIL, so we
994 1.1 christos can operate on our global data structures without worrying. */
995 1.1 christos wakeup = gdbpy_event_list == NULL;
996 1.1 christos
997 1.1 christos event = XNEW (struct gdbpy_event);
998 1.1 christos event->event = func;
999 1.1 christos event->next = NULL;
1000 1.1 christos *gdbpy_event_list_end = event;
1001 1.1 christos gdbpy_event_list_end = &event->next;
1002 1.1 christos
1003 1.1 christos /* Wake up gdb when needed. */
1004 1.1 christos if (wakeup)
1005 1.1 christos {
1006 1.1 christos char c = 'q'; /* Anything. */
1007 1.1 christos
1008 1.1 christos if (serial_write (gdbpy_event_fds[1], &c, 1))
1009 1.1 christos return PyErr_SetFromErrno (PyExc_IOError);
1010 1.1 christos }
1011 1.1 christos
1012 1.1 christos Py_RETURN_NONE;
1013 1.1 christos }
1014 1.1 christos
1015 1.1 christos /* Initialize the Python event handler. */
1016 1.1 christos static int
1017 1.1 christos gdbpy_initialize_events (void)
1018 1.1 christos {
1019 1.1 christos if (serial_pipe (gdbpy_event_fds) == 0)
1020 1.1 christos {
1021 1.1 christos gdbpy_event_list_end = &gdbpy_event_list;
1022 1.1 christos serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL);
1023 1.1 christos }
1024 1.1 christos
1025 1.1 christos return 0;
1026 1.1 christos }
1027 1.3 christos
1028 1.3 christos
1029 1.3 christos
1031 1.3 christos /* This is the extension_language_ops.before_prompt "method". */
1032 1.1 christos
1033 1.1 christos static enum ext_lang_rc
1034 1.1 christos gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
1035 1.1 christos const char *current_gdb_prompt)
1036 1.1 christos {
1037 1.3 christos struct cleanup *cleanup;
1038 1.1 christos char *prompt = NULL;
1039 1.1 christos
1040 1.1 christos if (!gdb_python_initialized)
1041 1.1 christos return EXT_LANG_RC_NOP;
1042 1.1 christos
1043 1.1 christos cleanup = ensure_python_env (get_current_arch (), current_language);
1044 1.1 christos
1045 1.1 christos if (gdb_python_module
1046 1.1 christos && PyObject_HasAttrString (gdb_python_module, "prompt_hook"))
1047 1.1 christos {
1048 1.1 christos PyObject *hook;
1049 1.1 christos
1050 1.1 christos hook = PyObject_GetAttrString (gdb_python_module, "prompt_hook");
1051 1.1 christos if (hook == NULL)
1052 1.1 christos goto fail;
1053 1.1 christos
1054 1.1 christos make_cleanup_py_decref (hook);
1055 1.1 christos
1056 1.1 christos if (PyCallable_Check (hook))
1057 1.1 christos {
1058 1.1 christos PyObject *result;
1059 1.1 christos PyObject *current_prompt;
1060 1.1 christos
1061 1.1 christos current_prompt = PyString_FromString (current_gdb_prompt);
1062 1.1 christos if (current_prompt == NULL)
1063 1.1 christos goto fail;
1064 1.1 christos
1065 1.1 christos result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL);
1066 1.1 christos
1067 1.1 christos Py_DECREF (current_prompt);
1068 1.1 christos
1069 1.1 christos if (result == NULL)
1070 1.1 christos goto fail;
1071 1.1 christos
1072 1.1 christos make_cleanup_py_decref (result);
1073 1.1 christos
1074 1.1 christos /* Return type should be None, or a String. If it is None,
1075 1.1 christos fall through, we will not set a prompt. If it is a
1076 1.1 christos string, set PROMPT. Anything else, set an exception. */
1077 1.1 christos if (result != Py_None && ! PyString_Check (result))
1078 1.1 christos {
1079 1.1 christos PyErr_Format (PyExc_RuntimeError,
1080 1.1 christos _("Return from prompt_hook must " \
1081 1.1 christos "be either a Python string, or None"));
1082 1.1 christos goto fail;
1083 1.1 christos }
1084 1.1 christos
1085 1.1 christos if (result != Py_None)
1086 1.1 christos {
1087 1.1 christos prompt = python_string_to_host_string (result);
1088 1.1 christos
1089 1.1 christos if (prompt == NULL)
1090 1.1 christos goto fail;
1091 1.1 christos else
1092 1.1 christos make_cleanup (xfree, prompt);
1093 1.1 christos }
1094 1.1 christos }
1095 1.1 christos }
1096 1.1 christos
1097 1.1 christos /* If a prompt has been set, PROMPT will not be NULL. If it is
1098 1.1 christos NULL, do not set the prompt. */
1099 1.3 christos if (prompt != NULL)
1100 1.1 christos set_prompt (prompt);
1101 1.1 christos
1102 1.1 christos do_cleanups (cleanup);
1103 1.1 christos return prompt != NULL ? EXT_LANG_RC_OK : EXT_LANG_RC_NOP;
1104 1.3 christos
1105 1.1 christos fail:
1106 1.1 christos gdbpy_print_stack ();
1107 1.1 christos do_cleanups (cleanup);
1108 1.1 christos return EXT_LANG_RC_ERROR;
1109 1.1 christos }
1110 1.1 christos
1111 1.1 christos
1112 1.1 christos
1114 1.1 christos /* Printing. */
1115 1.1 christos
1116 1.1 christos /* A python function to write a single string using gdb's filtered
1117 1.1 christos output stream . The optional keyword STREAM can be used to write
1118 1.1 christos to a particular stream. The default stream is to gdb_stdout. */
1119 1.1 christos
1120 1.1 christos static PyObject *
1121 1.1 christos gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
1122 1.1 christos {
1123 1.1 christos const char *arg;
1124 1.1 christos static char *keywords[] = {"text", "stream", NULL };
1125 1.1 christos int stream_type = 0;
1126 1.5 christos
1127 1.1 christos if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
1128 1.1 christos &stream_type))
1129 1.1 christos return NULL;
1130 1.1 christos
1131 1.1 christos TRY
1132 1.1 christos {
1133 1.1 christos switch (stream_type)
1134 1.1 christos {
1135 1.1 christos case 1:
1136 1.1 christos {
1137 1.1 christos fprintf_filtered (gdb_stderr, "%s", arg);
1138 1.1 christos break;
1139 1.1 christos }
1140 1.1 christos case 2:
1141 1.1 christos {
1142 1.1 christos fprintf_filtered (gdb_stdlog, "%s", arg);
1143 1.1 christos break;
1144 1.5 christos }
1145 1.5 christos default:
1146 1.5 christos fprintf_filtered (gdb_stdout, "%s", arg);
1147 1.5 christos }
1148 1.5 christos }
1149 1.1 christos CATCH (except, RETURN_MASK_ALL)
1150 1.1 christos {
1151 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
1152 1.1 christos }
1153 1.1 christos END_CATCH
1154 1.1 christos
1155 1.1 christos Py_RETURN_NONE;
1156 1.1 christos }
1157 1.1 christos
1158 1.1 christos /* A python function to flush a gdb stream. The optional keyword
1159 1.1 christos STREAM can be used to flush a particular stream. The default stream
1160 1.1 christos is gdb_stdout. */
1161 1.1 christos
1162 1.1 christos static PyObject *
1163 1.1 christos gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
1164 1.1 christos {
1165 1.1 christos static char *keywords[] = {"stream", NULL };
1166 1.1 christos int stream_type = 0;
1167 1.1 christos
1168 1.1 christos if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
1169 1.1 christos &stream_type))
1170 1.1 christos return NULL;
1171 1.1 christos
1172 1.1 christos switch (stream_type)
1173 1.1 christos {
1174 1.1 christos case 1:
1175 1.1 christos {
1176 1.1 christos gdb_flush (gdb_stderr);
1177 1.1 christos break;
1178 1.1 christos }
1179 1.1 christos case 2:
1180 1.1 christos {
1181 1.1 christos gdb_flush (gdb_stdlog);
1182 1.1 christos break;
1183 1.1 christos }
1184 1.1 christos default:
1185 1.1 christos gdb_flush (gdb_stdout);
1186 1.5 christos }
1187 1.5 christos
1188 1.5 christos Py_RETURN_NONE;
1189 1.5 christos }
1190 1.5 christos
1191 1.5 christos /* Return non-zero if print-stack is not "none". */
1192 1.5 christos
1193 1.5 christos int
1194 1.1 christos gdbpy_print_python_errors_p (void)
1195 1.1 christos {
1196 1.1 christos return gdbpy_should_print_stack != python_excp_none;
1197 1.1 christos }
1198 1.1 christos
1199 1.1 christos /* Print a python exception trace, print just a message, or print
1200 1.1 christos nothing and clear the python exception, depending on
1201 1.1 christos gdbpy_should_print_stack. Only call this if a python exception is
1202 1.1 christos set. */
1203 1.1 christos void
1204 1.1 christos gdbpy_print_stack (void)
1205 1.1 christos {
1206 1.1 christos
1207 1.1 christos /* Print "none", just clear exception. */
1208 1.1 christos if (gdbpy_should_print_stack == python_excp_none)
1209 1.1 christos {
1210 1.1 christos PyErr_Clear ();
1211 1.1 christos }
1212 1.1 christos /* Print "full" message and backtrace. */
1213 1.1 christos else if (gdbpy_should_print_stack == python_excp_full)
1214 1.5 christos {
1215 1.1 christos PyErr_Print ();
1216 1.1 christos /* PyErr_Print doesn't necessarily end output with a newline.
1217 1.1 christos This works because Python's stdout/stderr is fed through
1218 1.5 christos printf_filtered. */
1219 1.5 christos TRY
1220 1.5 christos {
1221 1.5 christos begin_line ();
1222 1.1 christos }
1223 1.1 christos CATCH (except, RETURN_MASK_ALL)
1224 1.1 christos {
1225 1.1 christos }
1226 1.1 christos END_CATCH
1227 1.1 christos }
1228 1.1 christos /* Print "message", just error print message. */
1229 1.1 christos else
1230 1.1 christos {
1231 1.1 christos PyObject *ptype, *pvalue, *ptraceback;
1232 1.1 christos char *msg = NULL, *type = NULL;
1233 1.1 christos
1234 1.1 christos PyErr_Fetch (&ptype, &pvalue, &ptraceback);
1235 1.5 christos
1236 1.1 christos /* Fetch the error message contained within ptype, pvalue. */
1237 1.1 christos msg = gdbpy_exception_to_string (ptype, pvalue);
1238 1.1 christos type = gdbpy_obj_to_string (ptype);
1239 1.1 christos
1240 1.1 christos TRY
1241 1.1 christos {
1242 1.1 christos if (msg == NULL)
1243 1.1 christos {
1244 1.1 christos /* An error occurred computing the string representation of the
1245 1.1 christos error message. */
1246 1.1 christos fprintf_filtered (gdb_stderr,
1247 1.1 christos _("Error occurred computing Python error" \
1248 1.1 christos "message.\n"));
1249 1.5 christos }
1250 1.5 christos else
1251 1.5 christos fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
1252 1.5 christos type, msg);
1253 1.1 christos }
1254 1.1 christos CATCH (except, RETURN_MASK_ALL)
1255 1.1 christos {
1256 1.1 christos }
1257 1.1 christos END_CATCH
1258 1.1 christos
1259 1.1 christos Py_XDECREF (ptype);
1260 1.1 christos Py_XDECREF (pvalue);
1261 1.1 christos Py_XDECREF (ptraceback);
1262 1.1 christos xfree (msg);
1263 1.1 christos }
1264 1.1 christos }
1265 1.1 christos
1266 1.1 christos
1267 1.1 christos
1269 1.1 christos /* Return the current Progspace.
1270 1.1 christos There always is one. */
1271 1.1 christos
1272 1.1 christos static PyObject *
1273 1.1 christos gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
1274 1.1 christos {
1275 1.1 christos PyObject *result;
1276 1.1 christos
1277 1.1 christos result = pspace_to_pspace_object (current_program_space);
1278 1.1 christos if (result)
1279 1.1 christos Py_INCREF (result);
1280 1.1 christos return result;
1281 1.1 christos }
1282 1.1 christos
1283 1.1 christos /* Return a sequence holding all the Progspaces. */
1284 1.1 christos
1285 1.1 christos static PyObject *
1286 1.1 christos gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
1287 1.1 christos {
1288 1.1 christos struct program_space *ps;
1289 1.1 christos PyObject *list;
1290 1.1 christos
1291 1.1 christos list = PyList_New (0);
1292 1.1 christos if (!list)
1293 1.1 christos return NULL;
1294 1.1 christos
1295 1.1 christos ALL_PSPACES (ps)
1296 1.1 christos {
1297 1.1 christos PyObject *item = pspace_to_pspace_object (ps);
1298 1.1 christos
1299 1.1 christos if (!item || PyList_Append (list, item) == -1)
1300 1.1 christos {
1301 1.1 christos Py_DECREF (list);
1302 1.1 christos return NULL;
1303 1.1 christos }
1304 1.1 christos }
1305 1.1 christos
1306 1.1 christos return list;
1307 1.5 christos }
1308 1.5 christos
1309 1.1 christos
1310 1.1 christos
1312 1.1 christos /* The "current" objfile. This is set when gdb detects that a new
1313 1.3 christos objfile has been loaded. It is only set for the duration of a call to
1314 1.3 christos gdbpy_source_objfile_script and gdbpy_execute_objfile_script; it is NULL
1315 1.3 christos at other times. */
1316 1.1 christos static struct objfile *gdbpy_current_objfile;
1317 1.3 christos
1318 1.3 christos /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1319 1.3 christos as Python code. This does not throw any errors. If an exception
1320 1.3 christos occurs python will print the traceback and clear the error indicator.
1321 1.1 christos This is the extension_language_script_ops.objfile_script_sourcer
1322 1.1 christos "method". */
1323 1.1 christos
1324 1.1 christos static void
1325 1.1 christos gdbpy_source_objfile_script (const struct extension_language_defn *extlang,
1326 1.1 christos struct objfile *objfile, FILE *file,
1327 1.1 christos const char *filename)
1328 1.1 christos {
1329 1.1 christos struct cleanup *cleanups;
1330 1.1 christos
1331 1.1 christos if (!gdb_python_initialized)
1332 1.1 christos return;
1333 1.1 christos
1334 1.1 christos cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
1335 1.1 christos gdbpy_current_objfile = objfile;
1336 1.5 christos
1337 1.5 christos python_run_simple_file (file, filename);
1338 1.5 christos
1339 1.5 christos do_cleanups (cleanups);
1340 1.5 christos gdbpy_current_objfile = NULL;
1341 1.5 christos }
1342 1.5 christos
1343 1.5 christos /* Set the current objfile to OBJFILE and then execute SCRIPT
1344 1.5 christos as Python code. This does not throw any errors. If an exception
1345 1.5 christos occurs python will print the traceback and clear the error indicator.
1346 1.5 christos This is the extension_language_script_ops.objfile_script_executor
1347 1.5 christos "method". */
1348 1.5 christos
1349 1.5 christos static void
1350 1.5 christos gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
1351 1.5 christos struct objfile *objfile, const char *name,
1352 1.5 christos const char *script)
1353 1.5 christos {
1354 1.5 christos struct cleanup *cleanups;
1355 1.5 christos
1356 1.5 christos if (!gdb_python_initialized)
1357 1.5 christos return;
1358 1.5 christos
1359 1.5 christos cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
1360 1.5 christos gdbpy_current_objfile = objfile;
1361 1.1 christos
1362 1.1 christos PyRun_SimpleString (script);
1363 1.1 christos
1364 1.1 christos do_cleanups (cleanups);
1365 1.1 christos gdbpy_current_objfile = NULL;
1366 1.1 christos }
1367 1.1 christos
1368 1.1 christos /* Return the current Objfile, or None if there isn't one. */
1369 1.1 christos
1370 1.1 christos static PyObject *
1371 1.1 christos gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
1372 1.1 christos {
1373 1.1 christos PyObject *result;
1374 1.1 christos
1375 1.1 christos if (! gdbpy_current_objfile)
1376 1.1 christos Py_RETURN_NONE;
1377 1.1 christos
1378 1.1 christos result = objfile_to_objfile_object (gdbpy_current_objfile);
1379 1.1 christos if (result)
1380 1.1 christos Py_INCREF (result);
1381 1.1 christos return result;
1382 1.1 christos }
1383 1.1 christos
1384 1.1 christos /* Return a sequence holding all the Objfiles. */
1385 1.1 christos
1386 1.1 christos static PyObject *
1387 1.1 christos gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
1388 1.1 christos {
1389 1.1 christos struct objfile *objf;
1390 1.1 christos PyObject *list;
1391 1.1 christos
1392 1.1 christos list = PyList_New (0);
1393 1.1 christos if (!list)
1394 1.1 christos return NULL;
1395 1.1 christos
1396 1.1 christos ALL_OBJFILES (objf)
1397 1.1 christos {
1398 1.1 christos PyObject *item = objfile_to_objfile_object (objf);
1399 1.1 christos
1400 1.1 christos if (!item || PyList_Append (list, item) == -1)
1401 1.1 christos {
1402 1.1 christos Py_DECREF (list);
1403 1.3 christos return NULL;
1404 1.3 christos }
1405 1.3 christos }
1406 1.3 christos
1407 1.1 christos return list;
1408 1.3 christos }
1409 1.3 christos
1410 1.3 christos /* Compute the list of active python type printers and store them in
1411 1.1 christos EXT_PRINTERS->py_type_printers. The product of this function is used by
1412 1.1 christos gdbpy_apply_type_printers, and freed by gdbpy_free_type_printers.
1413 1.3 christos This is the extension_language_ops.start_type_printers "method". */
1414 1.1 christos
1415 1.1 christos static void
1416 1.3 christos gdbpy_start_type_printers (const struct extension_language_defn *extlang,
1417 1.1 christos struct ext_lang_type_printers *ext_printers)
1418 1.1 christos {
1419 1.1 christos struct cleanup *cleanups;
1420 1.1 christos PyObject *type_module, *func = NULL, *printers_obj = NULL;
1421 1.1 christos
1422 1.1 christos if (!gdb_python_initialized)
1423 1.1 christos return;
1424 1.1 christos
1425 1.1 christos cleanups = ensure_python_env (get_current_arch (), current_language);
1426 1.1 christos
1427 1.1 christos type_module = PyImport_ImportModule ("gdb.types");
1428 1.1 christos if (type_module == NULL)
1429 1.1 christos {
1430 1.1 christos gdbpy_print_stack ();
1431 1.1 christos goto done;
1432 1.1 christos }
1433 1.1 christos
1434 1.3 christos func = PyObject_GetAttrString (type_module, "get_type_recognizers");
1435 1.3 christos if (func == NULL)
1436 1.1 christos {
1437 1.3 christos gdbpy_print_stack ();
1438 1.3 christos goto done;
1439 1.1 christos }
1440 1.1 christos
1441 1.1 christos printers_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
1442 1.1 christos if (printers_obj == NULL)
1443 1.1 christos gdbpy_print_stack ();
1444 1.1 christos else
1445 1.1 christos ext_printers->py_type_printers = printers_obj;
1446 1.3 christos
1447 1.3 christos done:
1448 1.3 christos Py_XDECREF (type_module);
1449 1.3 christos Py_XDECREF (func);
1450 1.3 christos do_cleanups (cleanups);
1451 1.3 christos }
1452 1.3 christos
1453 1.3 christos /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE
1454 1.3 christos a newly allocated string holding the type's replacement name, and return
1455 1.3 christos EXT_LANG_RC_OK. The caller is responsible for freeing the string.
1456 1.3 christos If there's a Python error return EXT_LANG_RC_ERROR.
1457 1.1 christos Otherwise, return EXT_LANG_RC_NOP.
1458 1.1 christos This is the extension_language_ops.apply_type_printers "method". */
1459 1.1 christos
1460 1.1 christos static enum ext_lang_rc
1461 1.3 christos gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
1462 1.1 christos const struct ext_lang_type_printers *ext_printers,
1463 1.1 christos struct type *type, char **prettied_type)
1464 1.1 christos {
1465 1.3 christos struct cleanup *cleanups;
1466 1.1 christos PyObject *type_obj, *type_module = NULL, *func = NULL;
1467 1.1 christos PyObject *result_obj = NULL;
1468 1.3 christos PyObject *printers_obj = ext_printers->py_type_printers;
1469 1.1 christos char *result = NULL;
1470 1.1 christos
1471 1.1 christos if (printers_obj == NULL)
1472 1.1 christos return EXT_LANG_RC_NOP;
1473 1.1 christos
1474 1.1 christos if (!gdb_python_initialized)
1475 1.1 christos return EXT_LANG_RC_NOP;
1476 1.1 christos
1477 1.1 christos cleanups = ensure_python_env (get_current_arch (), current_language);
1478 1.1 christos
1479 1.1 christos type_obj = type_to_type_object (type);
1480 1.1 christos if (type_obj == NULL)
1481 1.1 christos {
1482 1.1 christos gdbpy_print_stack ();
1483 1.1 christos goto done;
1484 1.1 christos }
1485 1.1 christos
1486 1.1 christos type_module = PyImport_ImportModule ("gdb.types");
1487 1.1 christos if (type_module == NULL)
1488 1.1 christos {
1489 1.1 christos gdbpy_print_stack ();
1490 1.1 christos goto done;
1491 1.1 christos }
1492 1.1 christos
1493 1.1 christos func = PyObject_GetAttrString (type_module, "apply_type_recognizers");
1494 1.1 christos if (func == NULL)
1495 1.1 christos {
1496 1.1 christos gdbpy_print_stack ();
1497 1.1 christos goto done;
1498 1.1 christos }
1499 1.1 christos
1500 1.1 christos result_obj = PyObject_CallFunctionObjArgs (func, printers_obj,
1501 1.1 christos type_obj, (char *) NULL);
1502 1.1 christos if (result_obj == NULL)
1503 1.1 christos {
1504 1.1 christos gdbpy_print_stack ();
1505 1.1 christos goto done;
1506 1.1 christos }
1507 1.1 christos
1508 1.1 christos if (result_obj != Py_None)
1509 1.1 christos {
1510 1.1 christos result = python_string_to_host_string (result_obj);
1511 1.1 christos if (result == NULL)
1512 1.1 christos gdbpy_print_stack ();
1513 1.1 christos }
1514 1.3 christos
1515 1.3 christos done:
1516 1.3 christos Py_XDECREF (type_obj);
1517 1.1 christos Py_XDECREF (type_module);
1518 1.1 christos Py_XDECREF (func);
1519 1.3 christos Py_XDECREF (result_obj);
1520 1.3 christos do_cleanups (cleanups);
1521 1.1 christos if (result != NULL)
1522 1.3 christos *prettied_type = result;
1523 1.3 christos return result != NULL ? EXT_LANG_RC_OK : EXT_LANG_RC_ERROR;
1524 1.3 christos }
1525 1.1 christos
1526 1.1 christos /* Free the result of start_type_printers.
1527 1.3 christos This is the extension_language_ops.free_type_printers "method". */
1528 1.1 christos
1529 1.1 christos static void
1530 1.1 christos gdbpy_free_type_printers (const struct extension_language_defn *extlang,
1531 1.1 christos struct ext_lang_type_printers *ext_printers)
1532 1.1 christos {
1533 1.1 christos struct cleanup *cleanups;
1534 1.1 christos PyObject *printers = ext_printers->py_type_printers;
1535 1.1 christos
1536 1.1 christos if (printers == NULL)
1537 1.1 christos return;
1538 1.1 christos
1539 1.1 christos if (!gdb_python_initialized)
1540 1.1 christos return;
1541 1.1 christos
1542 1.1 christos cleanups = ensure_python_env (get_current_arch (), current_language);
1543 1.1 christos Py_DECREF (printers);
1544 1.1 christos do_cleanups (cleanups);
1545 1.1 christos }
1546 1.1 christos
1547 1.1 christos #else /* HAVE_PYTHON */
1548 1.1 christos
1549 1.1 christos /* Dummy implementation of the gdb "python-interactive" and "python"
1550 1.1 christos command. */
1551 1.1 christos
1552 1.1 christos static void
1553 1.1 christos python_interactive_command (char *arg, int from_tty)
1554 1.1 christos {
1555 1.1 christos arg = skip_spaces (arg);
1556 1.1 christos if (arg && *arg)
1557 1.1 christos error (_("Python scripting is not supported in this copy of GDB."));
1558 1.1 christos else
1559 1.1 christos {
1560 1.1 christos struct command_line *l = get_command_line (python_control, "");
1561 1.1 christos struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
1562 1.1 christos
1563 1.1 christos execute_control_command_untraced (l);
1564 1.1 christos do_cleanups (cleanups);
1565 1.1 christos }
1566 1.1 christos }
1567 1.1 christos
1568 1.1 christos static void
1569 1.1 christos python_command (char *arg, int from_tty)
1570 1.1 christos {
1571 1.1 christos python_interactive_command (arg, from_tty);
1572 1.1 christos }
1573 1.1 christos
1574 1.1 christos #endif /* HAVE_PYTHON */
1575 1.1 christos
1576 1.1 christos
1577 1.1 christos
1579 1.1 christos /* Lists for 'set python' commands. */
1580 1.1 christos
1581 1.1 christos static struct cmd_list_element *user_set_python_list;
1582 1.1 christos static struct cmd_list_element *user_show_python_list;
1583 1.1 christos
1584 1.1 christos /* Function for use by 'set python' prefix command. */
1585 1.1 christos
1586 1.1 christos static void
1587 1.1 christos user_set_python (char *args, int from_tty)
1588 1.1 christos {
1589 1.1 christos help_list (user_set_python_list, "set python ", all_commands,
1590 1.1 christos gdb_stdout);
1591 1.1 christos }
1592 1.1 christos
1593 1.1 christos /* Function for use by 'show python' prefix command. */
1594 1.1 christos
1595 1.1 christos static void
1596 1.1 christos user_show_python (char *args, int from_tty)
1597 1.1 christos {
1598 1.1 christos cmd_show_list (user_show_python_list, from_tty, "");
1599 1.1 christos }
1600 1.1 christos
1601 1.1 christos /* Initialize the Python code. */
1602 1.1 christos
1603 1.3 christos #ifdef HAVE_PYTHON
1604 1.3 christos
1605 1.1 christos /* This is installed as a final cleanup and cleans up the
1606 1.1 christos interpreter. This lets Python's 'atexit' work. */
1607 1.1 christos
1608 1.1 christos static void
1609 1.1 christos finalize_python (void *ignore)
1610 1.3 christos {
1611 1.3 christos struct active_ext_lang_state *previous_active;
1612 1.3 christos
1613 1.3 christos /* We don't use ensure_python_env here because if we ever ran the
1614 1.3 christos cleanup, gdb would crash -- because the cleanup calls into the
1615 1.1 christos Python interpreter, which we are about to destroy. It seems
1616 1.1 christos clearer to make the needed calls explicitly here than to create a
1617 1.1 christos cleanup and then mysteriously discard it. */
1618 1.1 christos
1619 1.1 christos /* This is only called as a final cleanup so we can assume the active
1620 1.3 christos SIGINT handler is gdb's. We still need to tell it to notify Python. */
1621 1.3 christos previous_active = set_active_ext_lang (&extension_language_python);
1622 1.1 christos
1623 1.1 christos (void) PyGILState_Ensure ();
1624 1.1 christos python_gdbarch = target_gdbarch ();
1625 1.1 christos python_language = current_language;
1626 1.1 christos
1627 1.1 christos Py_Finalize ();
1628 1.1 christos
1629 1.1 christos restore_active_ext_lang (previous_active);
1630 1.1 christos }
1631 1.1 christos #endif
1632 1.1 christos
1633 1.1 christos /* Provide a prototype to silence -Wmissing-prototypes. */
1634 1.1 christos extern initialize_file_ftype _initialize_python;
1635 1.1 christos
1636 1.1 christos void
1637 1.1 christos _initialize_python (void)
1638 1.1 christos {
1639 1.1 christos char *progname;
1640 1.1 christos #ifdef IS_PY3K
1641 1.1 christos int i;
1642 1.1 christos size_t progsize, count;
1643 1.1 christos char *oldloc;
1644 1.1 christos wchar_t *progname_copy;
1645 1.1 christos #endif
1646 1.1 christos
1647 1.1 christos add_com ("python-interactive", class_obscure,
1648 1.1 christos python_interactive_command,
1649 1.1 christos #ifdef HAVE_PYTHON
1650 1.1 christos _("\
1651 1.1 christos Start an interactive Python prompt.\n\
1652 1.1 christos \n\
1653 1.1 christos To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
1654 1.1 christos prompt).\n\
1655 1.1 christos \n\
1656 1.1 christos Alternatively, a single-line Python command can be given as an\n\
1657 1.1 christos argument, and if the command is an expression, the result will be\n\
1658 1.1 christos printed. For example:\n\
1659 1.1 christos \n\
1660 1.1 christos (gdb) python-interactive 2 + 3\n\
1661 1.1 christos 5\n\
1662 1.1 christos ")
1663 1.1 christos #else /* HAVE_PYTHON */
1664 1.1 christos _("\
1665 1.1 christos Start a Python interactive prompt.\n\
1666 1.1 christos \n\
1667 1.1 christos Python scripting is not supported in this copy of GDB.\n\
1668 1.1 christos This command is only a placeholder.")
1669 1.1 christos #endif /* HAVE_PYTHON */
1670 1.1 christos );
1671 1.1 christos add_com_alias ("pi", "python-interactive", class_obscure, 1);
1672 1.1 christos
1673 1.1 christos add_com ("python", class_obscure, python_command,
1674 1.1 christos #ifdef HAVE_PYTHON
1675 1.1 christos _("\
1676 1.1 christos Evaluate a Python command.\n\
1677 1.1 christos \n\
1678 1.1 christos The command can be given as an argument, for instance:\n\
1679 1.1 christos \n\
1680 1.1 christos python print 23\n\
1681 1.1 christos \n\
1682 1.1 christos If no argument is given, the following lines are read and used\n\
1683 1.1 christos as the Python commands. Type a line containing \"end\" to indicate\n\
1684 1.1 christos the end of the command.")
1685 1.1 christos #else /* HAVE_PYTHON */
1686 1.1 christos _("\
1687 1.1 christos Evaluate a Python command.\n\
1688 1.1 christos \n\
1689 1.1 christos Python scripting is not supported in this copy of GDB.\n\
1690 1.1 christos This command is only a placeholder.")
1691 1.1 christos #endif /* HAVE_PYTHON */
1692 1.1 christos );
1693 1.1 christos add_com_alias ("py", "python", class_obscure, 1);
1694 1.1 christos
1695 1.1 christos /* Add set/show python print-stack. */
1696 1.1 christos add_prefix_cmd ("python", no_class, user_show_python,
1697 1.1 christos _("Prefix command for python preference settings."),
1698 1.1 christos &user_show_python_list, "show python ", 0,
1699 1.1 christos &showlist);
1700 1.1 christos
1701 1.1 christos add_prefix_cmd ("python", no_class, user_set_python,
1702 1.1 christos _("Prefix command for python preference settings."),
1703 1.1 christos &user_set_python_list, "set python ", 0,
1704 1.1 christos &setlist);
1705 1.1 christos
1706 1.1 christos add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
1707 1.1 christos &gdbpy_should_print_stack, _("\
1708 1.1 christos Set mode for Python stack dump on error."), _("\
1709 1.1 christos Show the mode of Python stack printing on error."), _("\
1710 1.1 christos none == no stack or message will be printed.\n\
1711 1.1 christos full == a message and a stack will be printed.\n\
1712 1.1 christos message == an error message without a stack will be printed."),
1713 1.1 christos NULL, NULL,
1714 1.1 christos &user_set_python_list,
1715 1.1 christos &user_show_python_list);
1716 1.1 christos
1717 1.1 christos #ifdef HAVE_PYTHON
1718 1.1 christos #ifdef WITH_PYTHON_PATH
1719 1.1 christos /* Work around problem where python gets confused about where it is,
1720 1.1 christos and then can't find its libraries, etc.
1721 1.1 christos NOTE: Python assumes the following layout:
1722 1.1 christos /foo/bin/python
1723 1.1 christos /foo/lib/pythonX.Y/...
1724 1.1 christos This must be done before calling Py_Initialize. */
1725 1.1 christos progname = concat (ldirname (python_libdir), SLASH_STRING, "bin",
1726 1.1 christos SLASH_STRING, "python", NULL);
1727 1.1 christos #ifdef IS_PY3K
1728 1.1 christos oldloc = setlocale (LC_ALL, NULL);
1729 1.1 christos setlocale (LC_ALL, "");
1730 1.1 christos progsize = strlen (progname);
1731 1.1 christos if (progsize == (size_t) -1)
1732 1.1 christos {
1733 1.1 christos fprintf (stderr, "Could not convert python path to string\n");
1734 1.1 christos return;
1735 1.1 christos }
1736 1.1 christos progname_copy = PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
1737 1.1 christos if (!progname_copy)
1738 1.1 christos {
1739 1.1 christos fprintf (stderr, "out of memory\n");
1740 1.1 christos return;
1741 1.1 christos }
1742 1.1 christos count = mbstowcs (progname_copy, progname, progsize + 1);
1743 1.1 christos if (count == (size_t) -1)
1744 1.1 christos {
1745 1.1 christos fprintf (stderr, "Could not convert python path to string\n");
1746 1.1 christos return;
1747 1.1 christos }
1748 1.1 christos setlocale (LC_ALL, 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.1 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", 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.1 christos { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
2085 1.1 christos "selected_thread () -> gdb.InferiorThread.\n\
2086 1.1 christos Return the selected thread object." },
2087 1.1 christos { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
2088 1.5 christos "selected_inferior () -> gdb.Inferior.\n\
2089 1.1 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 {NULL, NULL, 0, NULL}
2094 1.5 christos };
2095 1.1 christos
2096 1.1 christos #ifdef IS_PY3K
2097 1.1 christos struct PyModuleDef python_GdbModuleDef =
2098 1.1 christos {
2099 1.1 christos PyModuleDef_HEAD_INIT,
2100 1.1 christos "_gdb",
2101 1.1 christos NULL,
2102 -1,
2103 python_GdbMethods,
2104 NULL,
2105 NULL,
2106 NULL,
2107 NULL
2108 };
2109 #endif
2110 #endif /* HAVE_PYTHON */
2111