Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* General python/gdb code |
2 | ||
4c38e0a4 | 3 | Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc. |
d57a3c85 TJB |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
d452c4bc | 21 | #include "arch-utils.h" |
d57a3c85 TJB |
22 | #include "command.h" |
23 | #include "ui-out.h" | |
24 | #include "cli/cli-script.h" | |
25 | #include "gdbcmd.h" | |
89c73ade TT |
26 | #include "objfiles.h" |
27 | #include "observer.h" | |
d452c4bc UW |
28 | #include "value.h" |
29 | #include "language.h" | |
d9c57d9f | 30 | #include "exceptions.h" |
d57a3c85 TJB |
31 | |
32 | #include <ctype.h> | |
33 | ||
34 | /* True if we should print the stack when catching a Python error, | |
35 | false otherwise. */ | |
36 | static int gdbpy_should_print_stack = 1; | |
37 | ||
89c73ade TT |
38 | /* This is true if we should auto-load python code when an objfile is |
39 | opened, false otherwise. */ | |
40 | static int gdbpy_auto_load = 1; | |
41 | ||
d57a3c85 TJB |
42 | #ifdef HAVE_PYTHON |
43 | ||
44 | #include "python.h" | |
45 | #include "libiberty.h" | |
46 | #include "cli/cli-decode.h" | |
47 | #include "charset.h" | |
48 | #include "top.h" | |
d57a3c85 TJB |
49 | #include "python-internal.h" |
50 | #include "version.h" | |
51 | #include "target.h" | |
52 | #include "gdbthread.h" | |
53 | ||
12453b93 | 54 | static PyMethodDef GdbMethods[]; |
d57a3c85 TJB |
55 | |
56 | PyObject *gdb_module; | |
57 | ||
a6bac58e TT |
58 | /* Some string constants we may wish to use. */ |
59 | PyObject *gdbpy_to_string_cst; | |
60 | PyObject *gdbpy_children_cst; | |
61 | PyObject *gdbpy_display_hint_cst; | |
d8906c6f TJB |
62 | PyObject *gdbpy_doc_cst; |
63 | ||
d452c4bc UW |
64 | |
65 | /* Architecture and language to be used in callbacks from | |
66 | the Python interpreter. */ | |
67 | struct gdbarch *python_gdbarch; | |
68 | const struct language_defn *python_language; | |
69 | ||
70 | /* Restore global language and architecture and Python GIL state | |
71 | when leaving the Python interpreter. */ | |
72 | ||
73 | struct python_env | |
74 | { | |
75 | PyGILState_STATE state; | |
76 | struct gdbarch *gdbarch; | |
77 | const struct language_defn *language; | |
78 | }; | |
79 | ||
80 | static void | |
81 | restore_python_env (void *p) | |
82 | { | |
83 | struct python_env *env = (struct python_env *)p; | |
84 | PyGILState_Release (env->state); | |
85 | python_gdbarch = env->gdbarch; | |
86 | python_language = env->language; | |
87 | xfree (env); | |
88 | } | |
89 | ||
90 | /* Called before entering the Python interpreter to install the | |
91 | current language and architecture to be used for Python values. */ | |
92 | ||
93 | struct cleanup * | |
94 | ensure_python_env (struct gdbarch *gdbarch, | |
95 | const struct language_defn *language) | |
96 | { | |
97 | struct python_env *env = xmalloc (sizeof *env); | |
98 | ||
99 | env->state = PyGILState_Ensure (); | |
100 | env->gdbarch = python_gdbarch; | |
101 | env->language = python_language; | |
102 | ||
103 | python_gdbarch = gdbarch; | |
104 | python_language = language; | |
105 | ||
106 | return make_cleanup (restore_python_env, env); | |
107 | } | |
108 | ||
109 | ||
d57a3c85 TJB |
110 | /* Given a command_line, return a command string suitable for passing |
111 | to Python. Lines in the string are separated by newlines. The | |
112 | return value is allocated using xmalloc and the caller is | |
113 | responsible for freeing it. */ | |
114 | ||
115 | static char * | |
116 | compute_python_string (struct command_line *l) | |
117 | { | |
118 | struct command_line *iter; | |
119 | char *script = NULL; | |
120 | int size = 0; | |
121 | int here; | |
122 | ||
123 | for (iter = l; iter; iter = iter->next) | |
124 | size += strlen (iter->line) + 1; | |
125 | ||
126 | script = xmalloc (size + 1); | |
127 | here = 0; | |
128 | for (iter = l; iter; iter = iter->next) | |
129 | { | |
130 | int len = strlen (iter->line); | |
131 | strcpy (&script[here], iter->line); | |
132 | here += len; | |
133 | script[here++] = '\n'; | |
134 | } | |
135 | script[here] = '\0'; | |
136 | return script; | |
137 | } | |
138 | ||
139 | /* Take a command line structure representing a 'python' command, and | |
140 | evaluate its body using the Python interpreter. */ | |
141 | ||
142 | void | |
143 | eval_python_from_control_command (struct command_line *cmd) | |
144 | { | |
12453b93 | 145 | int ret; |
d57a3c85 | 146 | char *script; |
ca30a762 | 147 | struct cleanup *cleanup; |
d57a3c85 TJB |
148 | |
149 | if (cmd->body_count != 1) | |
150 | error (_("Invalid \"python\" block structure.")); | |
151 | ||
d452c4bc | 152 | cleanup = ensure_python_env (get_current_arch (), current_language); |
ca30a762 | 153 | |
d57a3c85 | 154 | script = compute_python_string (cmd->body_list[0]); |
12453b93 | 155 | ret = PyRun_SimpleString (script); |
d57a3c85 | 156 | xfree (script); |
12453b93 | 157 | if (ret) |
d57a3c85 TJB |
158 | { |
159 | gdbpy_print_stack (); | |
12453b93 | 160 | error (_("Error while executing Python code.")); |
d57a3c85 | 161 | } |
ca30a762 TT |
162 | |
163 | do_cleanups (cleanup); | |
d57a3c85 TJB |
164 | } |
165 | ||
166 | /* Implementation of the gdb "python" command. */ | |
167 | ||
168 | static void | |
169 | python_command (char *arg, int from_tty) | |
170 | { | |
ca30a762 | 171 | struct cleanup *cleanup; |
d452c4bc | 172 | cleanup = ensure_python_env (get_current_arch (), current_language); |
ca30a762 | 173 | |
d57a3c85 TJB |
174 | while (arg && *arg && isspace (*arg)) |
175 | ++arg; | |
176 | if (arg && *arg) | |
177 | { | |
12453b93 | 178 | if (PyRun_SimpleString (arg)) |
d57a3c85 TJB |
179 | { |
180 | gdbpy_print_stack (); | |
12453b93 | 181 | error (_("Error while executing Python code.")); |
d57a3c85 TJB |
182 | } |
183 | } | |
184 | else | |
185 | { | |
186 | struct command_line *l = get_command_line (python_control, ""); | |
ca30a762 | 187 | make_cleanup_free_command_lines (&l); |
d57a3c85 | 188 | execute_control_command_untraced (l); |
d57a3c85 | 189 | } |
ca30a762 TT |
190 | |
191 | do_cleanups (cleanup); | |
d57a3c85 TJB |
192 | } |
193 | ||
194 | \f | |
195 | ||
196 | /* Transform a gdb parameters's value into a Python value. May return | |
197 | NULL (and set a Python exception) on error. Helper function for | |
198 | get_parameter. */ | |
199 | ||
200 | static PyObject * | |
201 | parameter_to_python (struct cmd_list_element *cmd) | |
202 | { | |
203 | switch (cmd->var_type) | |
204 | { | |
205 | case var_string: | |
206 | case var_string_noescape: | |
207 | case var_optional_filename: | |
208 | case var_filename: | |
209 | case var_enum: | |
210 | { | |
211 | char *str = * (char **) cmd->var; | |
212 | if (! str) | |
213 | str = ""; | |
214 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
215 | } | |
216 | ||
217 | case var_boolean: | |
218 | { | |
219 | if (* (int *) cmd->var) | |
220 | Py_RETURN_TRUE; | |
221 | else | |
222 | Py_RETURN_FALSE; | |
223 | } | |
224 | ||
225 | case var_auto_boolean: | |
226 | { | |
227 | enum auto_boolean ab = * (enum auto_boolean *) cmd->var; | |
228 | if (ab == AUTO_BOOLEAN_TRUE) | |
229 | Py_RETURN_TRUE; | |
230 | else if (ab == AUTO_BOOLEAN_FALSE) | |
231 | Py_RETURN_FALSE; | |
232 | else | |
233 | Py_RETURN_NONE; | |
234 | } | |
235 | ||
236 | case var_integer: | |
237 | if ((* (int *) cmd->var) == INT_MAX) | |
238 | Py_RETURN_NONE; | |
239 | /* Fall through. */ | |
240 | case var_zinteger: | |
241 | return PyLong_FromLong (* (int *) cmd->var); | |
242 | ||
243 | case var_uinteger: | |
244 | { | |
245 | unsigned int val = * (unsigned int *) cmd->var; | |
246 | if (val == UINT_MAX) | |
247 | Py_RETURN_NONE; | |
248 | return PyLong_FromUnsignedLong (val); | |
249 | } | |
250 | } | |
251 | ||
252 | return PyErr_Format (PyExc_RuntimeError, "programmer error: unhandled type"); | |
253 | } | |
254 | ||
255 | /* A Python function which returns a gdb parameter's value as a Python | |
256 | value. */ | |
257 | ||
258 | static PyObject * | |
8f500870 | 259 | gdbpy_parameter (PyObject *self, PyObject *args) |
d57a3c85 TJB |
260 | { |
261 | struct cmd_list_element *alias, *prefix, *cmd; | |
262 | char *arg, *newarg; | |
cc924cad | 263 | int found = -1; |
d57a3c85 TJB |
264 | volatile struct gdb_exception except; |
265 | ||
266 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
267 | return NULL; | |
268 | ||
269 | newarg = concat ("show ", arg, (char *) NULL); | |
270 | ||
271 | TRY_CATCH (except, RETURN_MASK_ALL) | |
272 | { | |
cc924cad | 273 | found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); |
d57a3c85 TJB |
274 | } |
275 | xfree (newarg); | |
276 | GDB_PY_HANDLE_EXCEPTION (except); | |
cc924cad TJB |
277 | if (!found) |
278 | return PyErr_Format (PyExc_RuntimeError, | |
279 | "could not find parameter `%s'", arg); | |
d57a3c85 TJB |
280 | |
281 | if (! cmd->var) | |
8f500870 | 282 | return PyErr_Format (PyExc_RuntimeError, "`%s' is not a parameter", arg); |
d57a3c85 TJB |
283 | return parameter_to_python (cmd); |
284 | } | |
285 | ||
286 | /* A Python function which evaluates a string using the gdb CLI. */ | |
287 | ||
288 | static PyObject * | |
289 | execute_gdb_command (PyObject *self, PyObject *args) | |
290 | { | |
291 | struct cmd_list_element *alias, *prefix, *cmd; | |
292 | char *arg, *newarg; | |
12453b93 TJB |
293 | PyObject *from_tty_obj = NULL; |
294 | int from_tty; | |
295 | int cmp; | |
d57a3c85 | 296 | volatile struct gdb_exception except; |
d57a3c85 | 297 | |
12453b93 | 298 | if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj)) |
d57a3c85 TJB |
299 | return NULL; |
300 | ||
12453b93 TJB |
301 | from_tty = 0; |
302 | if (from_tty_obj) | |
303 | { | |
304 | cmp = PyObject_IsTrue (from_tty_obj); | |
305 | if (cmp < 0) | |
306 | return NULL; | |
307 | from_tty = cmp; | |
308 | } | |
309 | ||
d57a3c85 TJB |
310 | TRY_CATCH (except, RETURN_MASK_ALL) |
311 | { | |
86c6265d TT |
312 | /* Copy the argument text in case the command modifies it. */ |
313 | char *copy = xstrdup (arg); | |
314 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
315 | execute_command (copy, from_tty); | |
316 | do_cleanups (cleanup); | |
d57a3c85 TJB |
317 | } |
318 | GDB_PY_HANDLE_EXCEPTION (except); | |
319 | ||
347bddb7 PA |
320 | /* Do any commands attached to breakpoint we stopped at. */ |
321 | bpstat_do_actions (); | |
d57a3c85 TJB |
322 | |
323 | Py_RETURN_NONE; | |
324 | } | |
325 | ||
57a1d736 TT |
326 | /* Parse a string and evaluate it as an expression. */ |
327 | static PyObject * | |
328 | gdbpy_parse_and_eval (PyObject *self, PyObject *args) | |
329 | { | |
330 | char *expr_str; | |
331 | struct value *result = NULL; | |
332 | volatile struct gdb_exception except; | |
333 | ||
334 | if (!PyArg_ParseTuple (args, "s", &expr_str)) | |
335 | return NULL; | |
336 | ||
337 | TRY_CATCH (except, RETURN_MASK_ALL) | |
338 | { | |
339 | result = parse_and_eval (expr_str); | |
340 | } | |
341 | GDB_PY_HANDLE_EXCEPTION (except); | |
342 | ||
343 | return value_to_value_object (result); | |
344 | } | |
345 | ||
973817a3 JB |
346 | /* Read a file as Python code. STREAM is the input file; FILE is the |
347 | name of the file. */ | |
348 | ||
349 | void | |
350 | source_python_script (FILE *stream, char *file) | |
351 | { | |
352 | PyGILState_STATE state; | |
353 | ||
354 | state = PyGILState_Ensure (); | |
355 | ||
356 | PyRun_SimpleFile (stream, file); | |
357 | ||
358 | fclose (stream); | |
359 | PyGILState_Release (state); | |
360 | } | |
361 | ||
d57a3c85 TJB |
362 | \f |
363 | ||
364 | /* Printing. */ | |
365 | ||
366 | /* A python function to write a single string using gdb's filtered | |
367 | output stream. */ | |
368 | static PyObject * | |
369 | gdbpy_write (PyObject *self, PyObject *args) | |
370 | { | |
371 | char *arg; | |
372 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
373 | return NULL; | |
374 | printf_filtered ("%s", arg); | |
375 | Py_RETURN_NONE; | |
376 | } | |
377 | ||
378 | /* A python function to flush gdb's filtered output stream. */ | |
379 | static PyObject * | |
380 | gdbpy_flush (PyObject *self, PyObject *args) | |
381 | { | |
382 | gdb_flush (gdb_stdout); | |
383 | Py_RETURN_NONE; | |
384 | } | |
385 | ||
386 | /* Print a python exception trace, or print nothing and clear the | |
387 | python exception, depending on gdbpy_should_print_stack. Only call | |
388 | this if a python exception is set. */ | |
389 | void | |
390 | gdbpy_print_stack (void) | |
391 | { | |
392 | if (gdbpy_should_print_stack) | |
393 | PyErr_Print (); | |
394 | else | |
395 | PyErr_Clear (); | |
396 | } | |
397 | ||
89c73ade TT |
398 | \f |
399 | ||
400 | /* The "current" objfile. This is set when gdb detects that a new | |
401 | objfile has been loaded. It is only set for the duration of a call | |
402 | to gdbpy_new_objfile; it is NULL at other times. */ | |
403 | static struct objfile *gdbpy_current_objfile; | |
404 | ||
405 | /* The file name we attempt to read. */ | |
406 | #define GDBPY_AUTO_FILENAME "-gdb.py" | |
407 | ||
408 | /* This is a new_objfile observer callback which loads python code | |
409 | based on the path to the objfile. */ | |
410 | static void | |
411 | gdbpy_new_objfile (struct objfile *objfile) | |
412 | { | |
413 | char *realname; | |
414 | char *filename, *debugfile; | |
415 | int len; | |
416 | FILE *input; | |
89c73ade TT |
417 | struct cleanup *cleanups; |
418 | ||
419 | if (!gdbpy_auto_load || !objfile || !objfile->name) | |
420 | return; | |
421 | ||
d452c4bc | 422 | cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
423 | |
424 | gdbpy_current_objfile = objfile; | |
425 | ||
426 | realname = gdb_realpath (objfile->name); | |
427 | len = strlen (realname); | |
428 | filename = xmalloc (len + sizeof (GDBPY_AUTO_FILENAME)); | |
429 | memcpy (filename, realname, len); | |
430 | strcpy (filename + len, GDBPY_AUTO_FILENAME); | |
431 | ||
432 | input = fopen (filename, "r"); | |
433 | debugfile = filename; | |
434 | ||
d452c4bc | 435 | make_cleanup (xfree, filename); |
89c73ade TT |
436 | make_cleanup (xfree, realname); |
437 | ||
438 | if (!input && debug_file_directory) | |
439 | { | |
440 | /* Also try the same file in the separate debug info directory. */ | |
441 | debugfile = xmalloc (strlen (filename) | |
442 | + strlen (debug_file_directory) + 1); | |
443 | strcpy (debugfile, debug_file_directory); | |
444 | /* FILENAME is absolute, so we don't need a "/" here. */ | |
445 | strcat (debugfile, filename); | |
446 | ||
447 | make_cleanup (xfree, debugfile); | |
448 | input = fopen (debugfile, "r"); | |
449 | } | |
450 | ||
451 | if (!input && gdb_datadir) | |
452 | { | |
453 | /* Also try the same file in a subdirectory of gdb's data | |
454 | directory. */ | |
455 | debugfile = xmalloc (strlen (gdb_datadir) + strlen (filename) | |
456 | + strlen ("/auto-load") + 1); | |
457 | strcpy (debugfile, gdb_datadir); | |
458 | strcat (debugfile, "/auto-load"); | |
459 | /* FILENAME is absolute, so we don't need a "/" here. */ | |
460 | strcat (debugfile, filename); | |
461 | ||
462 | make_cleanup (xfree, debugfile); | |
463 | input = fopen (debugfile, "r"); | |
464 | } | |
465 | ||
466 | if (input) | |
467 | { | |
468 | /* We don't want to throw an exception here -- but the user | |
469 | would like to know that something went wrong. */ | |
470 | if (PyRun_SimpleFile (input, debugfile)) | |
471 | gdbpy_print_stack (); | |
472 | fclose (input); | |
473 | } | |
474 | ||
475 | do_cleanups (cleanups); | |
476 | gdbpy_current_objfile = NULL; | |
89c73ade TT |
477 | } |
478 | ||
479 | /* Return the current Objfile, or None if there isn't one. */ | |
480 | static PyObject * | |
481 | gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) | |
482 | { | |
483 | PyObject *result; | |
484 | ||
485 | if (! gdbpy_current_objfile) | |
486 | Py_RETURN_NONE; | |
487 | ||
488 | result = objfile_to_objfile_object (gdbpy_current_objfile); | |
489 | if (result) | |
490 | Py_INCREF (result); | |
491 | return result; | |
492 | } | |
493 | ||
494 | /* Return a sequence holding all the Objfiles. */ | |
495 | static PyObject * | |
496 | gdbpy_objfiles (PyObject *unused1, PyObject *unused2) | |
497 | { | |
498 | struct objfile *objf; | |
499 | PyObject *list; | |
500 | ||
501 | list = PyList_New (0); | |
502 | if (!list) | |
503 | return NULL; | |
504 | ||
505 | ALL_OBJFILES (objf) | |
506 | { | |
507 | PyObject *item = objfile_to_objfile_object (objf); | |
508 | if (!item || PyList_Append (list, item) == -1) | |
509 | { | |
510 | Py_DECREF (list); | |
511 | return NULL; | |
512 | } | |
513 | } | |
514 | ||
515 | return list; | |
516 | } | |
517 | ||
d57a3c85 TJB |
518 | #else /* HAVE_PYTHON */ |
519 | ||
520 | /* Dummy implementation of the gdb "python" command. */ | |
521 | ||
522 | static void | |
523 | python_command (char *arg, int from_tty) | |
524 | { | |
525 | while (arg && *arg && isspace (*arg)) | |
526 | ++arg; | |
527 | if (arg && *arg) | |
528 | error (_("Python scripting is not supported in this copy of GDB.")); | |
529 | else | |
530 | { | |
531 | struct command_line *l = get_command_line (python_control, ""); | |
532 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
533 | execute_control_command_untraced (l); | |
534 | do_cleanups (cleanups); | |
535 | } | |
536 | } | |
537 | ||
538 | void | |
539 | eval_python_from_control_command (struct command_line *cmd) | |
540 | { | |
541 | error (_("Python scripting is not supported in this copy of GDB.")); | |
542 | } | |
543 | ||
973817a3 JB |
544 | void |
545 | source_python_script (FILE *stream, char *file) | |
546 | { | |
547 | fclose (stream); | |
548 | throw_error (UNSUPPORTED_ERROR, | |
549 | _("Python scripting is not supported in this copy of GDB.")); | |
550 | } | |
551 | ||
d57a3c85 TJB |
552 | #endif /* HAVE_PYTHON */ |
553 | ||
554 | \f | |
555 | ||
556 | /* Lists for 'maint set python' commands. */ | |
557 | ||
558 | static struct cmd_list_element *set_python_list; | |
559 | static struct cmd_list_element *show_python_list; | |
560 | ||
561 | /* Function for use by 'maint set python' prefix command. */ | |
562 | ||
563 | static void | |
564 | set_python (char *args, int from_tty) | |
565 | { | |
566 | help_list (set_python_list, "maintenance set python ", -1, gdb_stdout); | |
567 | } | |
568 | ||
569 | /* Function for use by 'maint show python' prefix command. */ | |
570 | ||
571 | static void | |
572 | show_python (char *args, int from_tty) | |
573 | { | |
574 | cmd_show_list (show_python_list, from_tty, ""); | |
575 | } | |
576 | ||
577 | /* Initialize the Python code. */ | |
578 | ||
2c0b251b PA |
579 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
580 | extern initialize_file_ftype _initialize_python; | |
581 | ||
d57a3c85 TJB |
582 | void |
583 | _initialize_python (void) | |
584 | { | |
585 | add_com ("python", class_obscure, python_command, | |
586 | #ifdef HAVE_PYTHON | |
587 | _("\ | |
588 | Evaluate a Python command.\n\ | |
589 | \n\ | |
590 | The command can be given as an argument, for instance:\n\ | |
591 | \n\ | |
592 | python print 23\n\ | |
593 | \n\ | |
594 | If no argument is given, the following lines are read and used\n\ | |
595 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
596 | the end of the command.") | |
597 | #else /* HAVE_PYTHON */ | |
598 | _("\ | |
599 | Evaluate a Python command.\n\ | |
600 | \n\ | |
601 | Python scripting is not supported in this copy of GDB.\n\ | |
602 | This command is only a placeholder.") | |
603 | #endif /* HAVE_PYTHON */ | |
604 | ); | |
605 | ||
606 | add_prefix_cmd ("python", no_class, show_python, | |
607 | _("Prefix command for python maintenance settings."), | |
509238d6 | 608 | &show_python_list, "maintenance show python ", 0, |
d57a3c85 TJB |
609 | &maintenance_show_cmdlist); |
610 | add_prefix_cmd ("python", no_class, set_python, | |
611 | _("Prefix command for python maintenance settings."), | |
509238d6 | 612 | &set_python_list, "maintenance set python ", 0, |
d57a3c85 TJB |
613 | &maintenance_set_cmdlist); |
614 | ||
615 | add_setshow_boolean_cmd ("print-stack", class_maintenance, | |
616 | &gdbpy_should_print_stack, _("\ | |
617 | Enable or disable printing of Python stack dump on error."), _("\ | |
618 | Show whether Python stack will be printed on error."), _("\ | |
619 | Enables or disables printing of Python stack traces."), | |
620 | NULL, NULL, | |
621 | &set_python_list, | |
622 | &show_python_list); | |
623 | ||
89c73ade TT |
624 | add_setshow_boolean_cmd ("auto-load", class_maintenance, |
625 | &gdbpy_auto_load, _("\ | |
626 | Enable or disable auto-loading of Python code when an object is opened."), _("\ | |
627 | Show whether Python code will be auto-loaded when an object is opened."), _("\ | |
628 | Enables or disables auto-loading of Python code when an object is opened."), | |
629 | NULL, NULL, | |
630 | &set_python_list, | |
631 | &show_python_list); | |
632 | ||
d57a3c85 TJB |
633 | #ifdef HAVE_PYTHON |
634 | Py_Initialize (); | |
ca30a762 | 635 | PyEval_InitThreads (); |
d57a3c85 TJB |
636 | |
637 | gdb_module = Py_InitModule ("gdb", GdbMethods); | |
638 | ||
639 | /* The casts to (char*) are for python 2.4. */ | |
640 | PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version); | |
641 | PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name); | |
642 | PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name); | |
643 | ||
a08702d6 | 644 | gdbpy_initialize_values (); |
f8f6f20b | 645 | gdbpy_initialize_frames (); |
d8906c6f | 646 | gdbpy_initialize_commands (); |
f3e9a817 PM |
647 | gdbpy_initialize_symbols (); |
648 | gdbpy_initialize_symtabs (); | |
649 | gdbpy_initialize_blocks (); | |
bc3b79fd | 650 | gdbpy_initialize_functions (); |
2c74e833 | 651 | gdbpy_initialize_types (); |
89c73ade | 652 | gdbpy_initialize_objfile (); |
be759fcf | 653 | gdbpy_initialize_lazy_string (); |
a08702d6 | 654 | |
d57a3c85 | 655 | PyRun_SimpleString ("import gdb"); |
a6bac58e | 656 | PyRun_SimpleString ("gdb.pretty_printers = []"); |
d57a3c85 | 657 | |
89c73ade TT |
658 | observer_attach_new_objfile (gdbpy_new_objfile); |
659 | ||
a6bac58e TT |
660 | gdbpy_to_string_cst = PyString_FromString ("to_string"); |
661 | gdbpy_children_cst = PyString_FromString ("children"); | |
662 | gdbpy_display_hint_cst = PyString_FromString ("display_hint"); | |
d8906c6f TJB |
663 | gdbpy_doc_cst = PyString_FromString ("__doc__"); |
664 | ||
d57a3c85 TJB |
665 | /* Create a couple objects which are used for Python's stdout and |
666 | stderr. */ | |
667 | PyRun_SimpleString ("\ | |
668 | import sys\n\ | |
669 | class GdbOutputFile:\n\ | |
670 | def close(self):\n\ | |
671 | # Do nothing.\n\ | |
672 | return None\n\ | |
673 | \n\ | |
674 | def isatty(self):\n\ | |
675 | return False\n\ | |
676 | \n\ | |
677 | def write(self, s):\n\ | |
678 | gdb.write(s)\n\ | |
679 | \n\ | |
680 | def writelines(self, iterable):\n\ | |
681 | for line in iterable:\n\ | |
682 | self.write(line)\n\ | |
683 | \n\ | |
684 | def flush(self):\n\ | |
685 | gdb.flush()\n\ | |
686 | \n\ | |
687 | sys.stderr = GdbOutputFile()\n\ | |
688 | sys.stdout = GdbOutputFile()\n\ | |
689 | "); | |
ca30a762 TT |
690 | |
691 | /* Release the GIL while gdb runs. */ | |
692 | PyThreadState_Swap (NULL); | |
693 | PyEval_ReleaseLock (); | |
694 | ||
d57a3c85 TJB |
695 | #endif /* HAVE_PYTHON */ |
696 | } | |
12453b93 TJB |
697 | |
698 | \f | |
699 | ||
700 | #if HAVE_PYTHON | |
701 | ||
702 | static PyMethodDef GdbMethods[] = | |
703 | { | |
704 | { "history", gdbpy_history, METH_VARARGS, | |
705 | "Get a value from history" }, | |
706 | { "execute", execute_gdb_command, METH_VARARGS, | |
707 | "Execute a gdb command" }, | |
8f500870 | 708 | { "parameter", gdbpy_parameter, METH_VARARGS, |
12453b93 TJB |
709 | "Return a gdb parameter's value" }, |
710 | ||
b6313243 TT |
711 | { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS, |
712 | "Find the default visualizer for a Value." }, | |
713 | ||
89c73ade TT |
714 | { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, |
715 | "Return the current Objfile being loaded, or None." }, | |
716 | { "objfiles", gdbpy_objfiles, METH_NOARGS, | |
717 | "Return a sequence of all loaded objfiles." }, | |
718 | ||
f8f6f20b TJB |
719 | { "selected_frame", gdbpy_selected_frame, METH_NOARGS, |
720 | "selected_frame () -> gdb.Frame.\n\ | |
721 | Return the selected frame object." }, | |
722 | { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS, | |
723 | "stop_reason_string (Integer) -> String.\n\ | |
724 | Return a string explaining unwind stop reason." }, | |
725 | ||
2c74e833 TT |
726 | { "lookup_type", (PyCFunction) gdbpy_lookup_type, |
727 | METH_VARARGS | METH_KEYWORDS, | |
728 | "lookup_type (name [, block]) -> type\n\ | |
729 | Return a Type corresponding to the given name." }, | |
f3e9a817 PM |
730 | { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol, |
731 | METH_VARARGS | METH_KEYWORDS, | |
732 | "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\ | |
733 | Return a tuple with the symbol corresponding to the given name (or None) and\n\ | |
734 | a boolean indicating if name is a field of the current implied argument\n\ | |
735 | `this' (when the current language is object-oriented)." }, | |
736 | { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS, | |
737 | "Return the block containing the given pc value, or None." }, | |
57a1d736 TT |
738 | { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, |
739 | "parse_and_eval (String) -> Value.\n\ | |
740 | Parse String as an expression, evaluate it, and return the result as a Value." | |
741 | }, | |
742 | ||
12453b93 TJB |
743 | { "write", gdbpy_write, METH_VARARGS, |
744 | "Write a string using gdb's filtered stream." }, | |
745 | { "flush", gdbpy_flush, METH_NOARGS, | |
746 | "Flush gdb's filtered stdout stream." }, | |
747 | ||
748 | {NULL, NULL, 0, NULL} | |
749 | }; | |
750 | ||
751 | #endif /* HAVE_PYTHON */ |