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