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" | |
fa33c3cd | 26 | #include "progspace.h" |
89c73ade | 27 | #include "objfiles.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 | ||
38 | #ifdef HAVE_PYTHON | |
39 | ||
40 | #include "python.h" | |
41 | #include "libiberty.h" | |
42 | #include "cli/cli-decode.h" | |
43 | #include "charset.h" | |
44 | #include "top.h" | |
d57a3c85 TJB |
45 | #include "python-internal.h" |
46 | #include "version.h" | |
47 | #include "target.h" | |
48 | #include "gdbthread.h" | |
49 | ||
12453b93 | 50 | static PyMethodDef GdbMethods[]; |
d57a3c85 TJB |
51 | |
52 | PyObject *gdb_module; | |
53 | ||
a6bac58e TT |
54 | /* Some string constants we may wish to use. */ |
55 | PyObject *gdbpy_to_string_cst; | |
56 | PyObject *gdbpy_children_cst; | |
57 | PyObject *gdbpy_display_hint_cst; | |
d8906c6f | 58 | PyObject *gdbpy_doc_cst; |
967cf477 | 59 | PyObject *gdbpy_enabled_cst; |
d8906c6f | 60 | |
07ca107c DE |
61 | /* The GdbError exception. */ |
62 | PyObject *gdbpy_gdberror_exc; | |
d452c4bc UW |
63 | |
64 | /* Architecture and language to be used in callbacks from | |
65 | the Python interpreter. */ | |
66 | struct gdbarch *python_gdbarch; | |
67 | const struct language_defn *python_language; | |
68 | ||
69 | /* Restore global language and architecture and Python GIL state | |
70 | when leaving the Python interpreter. */ | |
71 | ||
72 | struct python_env | |
73 | { | |
74 | PyGILState_STATE state; | |
75 | struct gdbarch *gdbarch; | |
76 | const struct language_defn *language; | |
77 | }; | |
78 | ||
79 | static void | |
80 | restore_python_env (void *p) | |
81 | { | |
82 | struct python_env *env = (struct python_env *)p; | |
d59b6f6c | 83 | |
d452c4bc UW |
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); | |
d59b6f6c | 131 | |
d57a3c85 TJB |
132 | strcpy (&script[here], iter->line); |
133 | here += len; | |
134 | script[here++] = '\n'; | |
135 | } | |
136 | script[here] = '\0'; | |
137 | return script; | |
138 | } | |
139 | ||
140 | /* Take a command line structure representing a 'python' command, and | |
141 | evaluate its body using the Python interpreter. */ | |
142 | ||
143 | void | |
144 | eval_python_from_control_command (struct command_line *cmd) | |
145 | { | |
12453b93 | 146 | int ret; |
d57a3c85 | 147 | char *script; |
ca30a762 | 148 | struct cleanup *cleanup; |
d57a3c85 TJB |
149 | |
150 | if (cmd->body_count != 1) | |
151 | error (_("Invalid \"python\" block structure.")); | |
152 | ||
d452c4bc | 153 | cleanup = ensure_python_env (get_current_arch (), current_language); |
ca30a762 | 154 | |
d57a3c85 | 155 | script = compute_python_string (cmd->body_list[0]); |
12453b93 | 156 | ret = PyRun_SimpleString (script); |
d57a3c85 | 157 | xfree (script); |
12453b93 | 158 | if (ret) |
d57a3c85 TJB |
159 | { |
160 | gdbpy_print_stack (); | |
12453b93 | 161 | error (_("Error while executing Python code.")); |
d57a3c85 | 162 | } |
ca30a762 TT |
163 | |
164 | do_cleanups (cleanup); | |
d57a3c85 TJB |
165 | } |
166 | ||
167 | /* Implementation of the gdb "python" command. */ | |
168 | ||
169 | static void | |
170 | python_command (char *arg, int from_tty) | |
171 | { | |
ca30a762 | 172 | struct cleanup *cleanup; |
ca30a762 | 173 | |
d59b6f6c | 174 | cleanup = ensure_python_env (get_current_arch (), current_language); |
d57a3c85 TJB |
175 | while (arg && *arg && isspace (*arg)) |
176 | ++arg; | |
177 | if (arg && *arg) | |
178 | { | |
12453b93 | 179 | if (PyRun_SimpleString (arg)) |
d57a3c85 TJB |
180 | { |
181 | gdbpy_print_stack (); | |
12453b93 | 182 | error (_("Error while executing Python code.")); |
d57a3c85 TJB |
183 | } |
184 | } | |
185 | else | |
186 | { | |
187 | struct command_line *l = get_command_line (python_control, ""); | |
d59b6f6c | 188 | |
ca30a762 | 189 | make_cleanup_free_command_lines (&l); |
d57a3c85 | 190 | execute_control_command_untraced (l); |
d57a3c85 | 191 | } |
ca30a762 TT |
192 | |
193 | do_cleanups (cleanup); | |
d57a3c85 TJB |
194 | } |
195 | ||
196 | \f | |
197 | ||
198 | /* Transform a gdb parameters's value into a Python value. May return | |
199 | NULL (and set a Python exception) on error. Helper function for | |
200 | get_parameter. */ | |
d7b32ed3 PM |
201 | PyObject * |
202 | gdbpy_parameter_value (enum var_types type, void *var) | |
d57a3c85 | 203 | { |
d7b32ed3 | 204 | switch (type) |
d57a3c85 TJB |
205 | { |
206 | case var_string: | |
207 | case var_string_noescape: | |
208 | case var_optional_filename: | |
209 | case var_filename: | |
210 | case var_enum: | |
211 | { | |
d7b32ed3 | 212 | char *str = * (char **) var; |
d59b6f6c | 213 | |
d57a3c85 TJB |
214 | if (! str) |
215 | str = ""; | |
216 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
217 | } | |
218 | ||
219 | case var_boolean: | |
220 | { | |
d7b32ed3 | 221 | if (* (int *) var) |
d57a3c85 TJB |
222 | Py_RETURN_TRUE; |
223 | else | |
224 | Py_RETURN_FALSE; | |
225 | } | |
226 | ||
227 | case var_auto_boolean: | |
228 | { | |
d7b32ed3 | 229 | enum auto_boolean ab = * (enum auto_boolean *) var; |
d59b6f6c | 230 | |
d57a3c85 TJB |
231 | if (ab == AUTO_BOOLEAN_TRUE) |
232 | Py_RETURN_TRUE; | |
233 | else if (ab == AUTO_BOOLEAN_FALSE) | |
234 | Py_RETURN_FALSE; | |
235 | else | |
236 | Py_RETURN_NONE; | |
237 | } | |
238 | ||
239 | case var_integer: | |
d7b32ed3 | 240 | if ((* (int *) var) == INT_MAX) |
d57a3c85 TJB |
241 | Py_RETURN_NONE; |
242 | /* Fall through. */ | |
243 | case var_zinteger: | |
d7b32ed3 | 244 | return PyLong_FromLong (* (int *) var); |
d57a3c85 TJB |
245 | |
246 | case var_uinteger: | |
247 | { | |
d7b32ed3 | 248 | unsigned int val = * (unsigned int *) var; |
d59b6f6c | 249 | |
d57a3c85 TJB |
250 | if (val == UINT_MAX) |
251 | Py_RETURN_NONE; | |
252 | return PyLong_FromUnsignedLong (val); | |
253 | } | |
254 | } | |
255 | ||
044c0f87 PM |
256 | return PyErr_Format (PyExc_RuntimeError, |
257 | _("Programmer error: unhandled type.")); | |
d57a3c85 TJB |
258 | } |
259 | ||
260 | /* A Python function which returns a gdb parameter's value as a Python | |
261 | value. */ | |
262 | ||
d7b32ed3 | 263 | PyObject * |
8f500870 | 264 | gdbpy_parameter (PyObject *self, PyObject *args) |
d57a3c85 TJB |
265 | { |
266 | struct cmd_list_element *alias, *prefix, *cmd; | |
267 | char *arg, *newarg; | |
cc924cad | 268 | int found = -1; |
d57a3c85 TJB |
269 | volatile struct gdb_exception except; |
270 | ||
271 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
272 | return NULL; | |
273 | ||
274 | newarg = concat ("show ", arg, (char *) NULL); | |
275 | ||
276 | TRY_CATCH (except, RETURN_MASK_ALL) | |
277 | { | |
cc924cad | 278 | found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); |
d57a3c85 TJB |
279 | } |
280 | xfree (newarg); | |
281 | GDB_PY_HANDLE_EXCEPTION (except); | |
cc924cad TJB |
282 | if (!found) |
283 | return PyErr_Format (PyExc_RuntimeError, | |
044c0f87 | 284 | _("Could not find parameter `%s'."), arg); |
d57a3c85 TJB |
285 | |
286 | if (! cmd->var) | |
044c0f87 PM |
287 | return PyErr_Format (PyExc_RuntimeError, |
288 | _("`%s' is not a parameter."), arg); | |
d7b32ed3 | 289 | return gdbpy_parameter_value (cmd->var_type, cmd->var); |
d57a3c85 TJB |
290 | } |
291 | ||
f870a310 TT |
292 | /* Wrapper for target_charset. */ |
293 | ||
294 | static PyObject * | |
295 | gdbpy_target_charset (PyObject *self, PyObject *args) | |
296 | { | |
297 | const char *cset = target_charset (python_gdbarch); | |
d59b6f6c | 298 | |
f870a310 TT |
299 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
300 | } | |
301 | ||
302 | /* Wrapper for target_wide_charset. */ | |
303 | ||
304 | static PyObject * | |
305 | gdbpy_target_wide_charset (PyObject *self, PyObject *args) | |
306 | { | |
307 | const char *cset = target_wide_charset (python_gdbarch); | |
d59b6f6c | 308 | |
f870a310 TT |
309 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
310 | } | |
311 | ||
d57a3c85 TJB |
312 | /* A Python function which evaluates a string using the gdb CLI. */ |
313 | ||
314 | static PyObject * | |
315 | execute_gdb_command (PyObject *self, PyObject *args) | |
316 | { | |
f92adf3c | 317 | char *arg; |
12453b93 TJB |
318 | PyObject *from_tty_obj = NULL; |
319 | int from_tty; | |
320 | int cmp; | |
d57a3c85 | 321 | volatile struct gdb_exception except; |
d57a3c85 | 322 | |
12453b93 | 323 | if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj)) |
d57a3c85 TJB |
324 | return NULL; |
325 | ||
12453b93 TJB |
326 | from_tty = 0; |
327 | if (from_tty_obj) | |
328 | { | |
329 | cmp = PyObject_IsTrue (from_tty_obj); | |
330 | if (cmp < 0) | |
331 | return NULL; | |
332 | from_tty = cmp; | |
333 | } | |
334 | ||
d57a3c85 TJB |
335 | TRY_CATCH (except, RETURN_MASK_ALL) |
336 | { | |
86c6265d TT |
337 | /* Copy the argument text in case the command modifies it. */ |
338 | char *copy = xstrdup (arg); | |
339 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
d59b6f6c | 340 | |
86c6265d TT |
341 | execute_command (copy, from_tty); |
342 | do_cleanups (cleanup); | |
d57a3c85 TJB |
343 | } |
344 | GDB_PY_HANDLE_EXCEPTION (except); | |
345 | ||
347bddb7 PA |
346 | /* Do any commands attached to breakpoint we stopped at. */ |
347 | bpstat_do_actions (); | |
d57a3c85 TJB |
348 | |
349 | Py_RETURN_NONE; | |
350 | } | |
351 | ||
57a1d736 TT |
352 | /* Parse a string and evaluate it as an expression. */ |
353 | static PyObject * | |
354 | gdbpy_parse_and_eval (PyObject *self, PyObject *args) | |
355 | { | |
356 | char *expr_str; | |
357 | struct value *result = NULL; | |
358 | volatile struct gdb_exception except; | |
359 | ||
360 | if (!PyArg_ParseTuple (args, "s", &expr_str)) | |
361 | return NULL; | |
362 | ||
363 | TRY_CATCH (except, RETURN_MASK_ALL) | |
364 | { | |
365 | result = parse_and_eval (expr_str); | |
366 | } | |
367 | GDB_PY_HANDLE_EXCEPTION (except); | |
368 | ||
369 | return value_to_value_object (result); | |
370 | } | |
371 | ||
973817a3 | 372 | /* Read a file as Python code. STREAM is the input file; FILE is the |
3f7b2faa DE |
373 | name of the file. |
374 | STREAM is not closed, that is the caller's responsibility. */ | |
973817a3 JB |
375 | |
376 | void | |
3f7b2faa | 377 | source_python_script (FILE *stream, const char *file) |
973817a3 | 378 | { |
eb5cda86 | 379 | struct cleanup *cleanup; |
973817a3 | 380 | |
eb5cda86 | 381 | cleanup = ensure_python_env (get_current_arch (), current_language); |
973817a3 | 382 | |
3a1d4620 DE |
383 | /* Note: If an exception occurs python will print the traceback and |
384 | clear the error indicator. */ | |
973817a3 JB |
385 | PyRun_SimpleFile (stream, file); |
386 | ||
eb5cda86 | 387 | do_cleanups (cleanup); |
973817a3 JB |
388 | } |
389 | ||
d57a3c85 TJB |
390 | \f |
391 | ||
392 | /* Printing. */ | |
393 | ||
394 | /* A python function to write a single string using gdb's filtered | |
395 | output stream. */ | |
396 | static PyObject * | |
397 | gdbpy_write (PyObject *self, PyObject *args) | |
398 | { | |
399 | char *arg; | |
d59b6f6c | 400 | |
d57a3c85 TJB |
401 | if (! PyArg_ParseTuple (args, "s", &arg)) |
402 | return NULL; | |
403 | printf_filtered ("%s", arg); | |
404 | Py_RETURN_NONE; | |
405 | } | |
406 | ||
407 | /* A python function to flush gdb's filtered output stream. */ | |
408 | static PyObject * | |
409 | gdbpy_flush (PyObject *self, PyObject *args) | |
410 | { | |
411 | gdb_flush (gdb_stdout); | |
412 | Py_RETURN_NONE; | |
413 | } | |
414 | ||
415 | /* Print a python exception trace, or print nothing and clear the | |
416 | python exception, depending on gdbpy_should_print_stack. Only call | |
417 | this if a python exception is set. */ | |
418 | void | |
419 | gdbpy_print_stack (void) | |
420 | { | |
421 | if (gdbpy_should_print_stack) | |
0bf0f8c4 DE |
422 | { |
423 | PyErr_Print (); | |
424 | /* PyErr_Print doesn't necessarily end output with a newline. | |
425 | This works because Python's stdout/stderr is fed through | |
426 | printf_filtered. */ | |
427 | begin_line (); | |
428 | } | |
d57a3c85 TJB |
429 | else |
430 | PyErr_Clear (); | |
431 | } | |
432 | ||
89c73ade TT |
433 | \f |
434 | ||
fa33c3cd DE |
435 | /* Return the current Progspace. |
436 | There always is one. */ | |
437 | ||
438 | static PyObject * | |
439 | gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2) | |
440 | { | |
441 | PyObject *result; | |
442 | ||
443 | result = pspace_to_pspace_object (current_program_space); | |
444 | if (result) | |
445 | Py_INCREF (result); | |
446 | return result; | |
447 | } | |
448 | ||
449 | /* Return a sequence holding all the Progspaces. */ | |
450 | ||
451 | static PyObject * | |
452 | gdbpy_progspaces (PyObject *unused1, PyObject *unused2) | |
453 | { | |
454 | struct program_space *ps; | |
455 | PyObject *list; | |
456 | ||
457 | list = PyList_New (0); | |
458 | if (!list) | |
459 | return NULL; | |
460 | ||
461 | ALL_PSPACES (ps) | |
462 | { | |
463 | PyObject *item = pspace_to_pspace_object (ps); | |
d59b6f6c | 464 | |
fa33c3cd DE |
465 | if (!item || PyList_Append (list, item) == -1) |
466 | { | |
467 | Py_DECREF (list); | |
468 | return NULL; | |
469 | } | |
470 | } | |
471 | ||
472 | return list; | |
473 | } | |
474 | ||
475 | \f | |
476 | ||
89c73ade | 477 | /* The "current" objfile. This is set when gdb detects that a new |
8a1ea21f DE |
478 | objfile has been loaded. It is only set for the duration of a call to |
479 | source_python_script_for_objfile; it is NULL at other times. */ | |
89c73ade TT |
480 | static struct objfile *gdbpy_current_objfile; |
481 | ||
8a1ea21f DE |
482 | /* Set the current objfile to OBJFILE and then read STREAM,FILE as |
483 | Python code. */ | |
89c73ade | 484 | |
8a1ea21f DE |
485 | void |
486 | source_python_script_for_objfile (struct objfile *objfile, | |
487 | FILE *stream, const char *file) | |
89c73ade | 488 | { |
89c73ade TT |
489 | struct cleanup *cleanups; |
490 | ||
d452c4bc | 491 | cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
492 | gdbpy_current_objfile = objfile; |
493 | ||
3a1d4620 DE |
494 | /* Note: If an exception occurs python will print the traceback and |
495 | clear the error indicator. */ | |
496 | PyRun_SimpleFile (stream, file); | |
89c73ade TT |
497 | |
498 | do_cleanups (cleanups); | |
499 | gdbpy_current_objfile = NULL; | |
89c73ade TT |
500 | } |
501 | ||
502 | /* Return the current Objfile, or None if there isn't one. */ | |
8a1ea21f | 503 | |
89c73ade TT |
504 | static PyObject * |
505 | gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) | |
506 | { | |
507 | PyObject *result; | |
508 | ||
509 | if (! gdbpy_current_objfile) | |
510 | Py_RETURN_NONE; | |
511 | ||
512 | result = objfile_to_objfile_object (gdbpy_current_objfile); | |
513 | if (result) | |
514 | Py_INCREF (result); | |
515 | return result; | |
516 | } | |
517 | ||
518 | /* Return a sequence holding all the Objfiles. */ | |
fa33c3cd | 519 | |
89c73ade TT |
520 | static PyObject * |
521 | gdbpy_objfiles (PyObject *unused1, PyObject *unused2) | |
522 | { | |
523 | struct objfile *objf; | |
524 | PyObject *list; | |
525 | ||
526 | list = PyList_New (0); | |
527 | if (!list) | |
528 | return NULL; | |
529 | ||
530 | ALL_OBJFILES (objf) | |
531 | { | |
532 | PyObject *item = objfile_to_objfile_object (objf); | |
d59b6f6c | 533 | |
89c73ade TT |
534 | if (!item || PyList_Append (list, item) == -1) |
535 | { | |
536 | Py_DECREF (list); | |
537 | return NULL; | |
538 | } | |
539 | } | |
540 | ||
541 | return list; | |
542 | } | |
543 | ||
d57a3c85 TJB |
544 | #else /* HAVE_PYTHON */ |
545 | ||
546 | /* Dummy implementation of the gdb "python" command. */ | |
547 | ||
548 | static void | |
549 | python_command (char *arg, int from_tty) | |
550 | { | |
551 | while (arg && *arg && isspace (*arg)) | |
552 | ++arg; | |
553 | if (arg && *arg) | |
554 | error (_("Python scripting is not supported in this copy of GDB.")); | |
555 | else | |
556 | { | |
557 | struct command_line *l = get_command_line (python_control, ""); | |
558 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
d59b6f6c | 559 | |
d57a3c85 TJB |
560 | execute_control_command_untraced (l); |
561 | do_cleanups (cleanups); | |
562 | } | |
563 | } | |
564 | ||
565 | void | |
566 | eval_python_from_control_command (struct command_line *cmd) | |
567 | { | |
568 | error (_("Python scripting is not supported in this copy of GDB.")); | |
569 | } | |
570 | ||
973817a3 | 571 | void |
3f7b2faa | 572 | source_python_script (FILE *stream, const char *file) |
973817a3 | 573 | { |
973817a3 JB |
574 | throw_error (UNSUPPORTED_ERROR, |
575 | _("Python scripting is not supported in this copy of GDB.")); | |
576 | } | |
577 | ||
d57a3c85 TJB |
578 | #endif /* HAVE_PYTHON */ |
579 | ||
580 | \f | |
581 | ||
582 | /* Lists for 'maint set python' commands. */ | |
583 | ||
8a1ea21f DE |
584 | struct cmd_list_element *set_python_list; |
585 | struct cmd_list_element *show_python_list; | |
d57a3c85 TJB |
586 | |
587 | /* Function for use by 'maint set python' prefix command. */ | |
588 | ||
589 | static void | |
590 | set_python (char *args, int from_tty) | |
591 | { | |
592 | help_list (set_python_list, "maintenance set python ", -1, gdb_stdout); | |
593 | } | |
594 | ||
595 | /* Function for use by 'maint show python' prefix command. */ | |
596 | ||
597 | static void | |
598 | show_python (char *args, int from_tty) | |
599 | { | |
600 | cmd_show_list (show_python_list, from_tty, ""); | |
601 | } | |
602 | ||
603 | /* Initialize the Python code. */ | |
604 | ||
2c0b251b PA |
605 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
606 | extern initialize_file_ftype _initialize_python; | |
607 | ||
d57a3c85 TJB |
608 | void |
609 | _initialize_python (void) | |
610 | { | |
611 | add_com ("python", class_obscure, python_command, | |
612 | #ifdef HAVE_PYTHON | |
613 | _("\ | |
614 | Evaluate a Python command.\n\ | |
615 | \n\ | |
616 | The command can be given as an argument, for instance:\n\ | |
617 | \n\ | |
618 | python print 23\n\ | |
619 | \n\ | |
620 | If no argument is given, the following lines are read and used\n\ | |
621 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
622 | the end of the command.") | |
623 | #else /* HAVE_PYTHON */ | |
624 | _("\ | |
625 | Evaluate a Python command.\n\ | |
626 | \n\ | |
627 | Python scripting is not supported in this copy of GDB.\n\ | |
628 | This command is only a placeholder.") | |
629 | #endif /* HAVE_PYTHON */ | |
630 | ); | |
631 | ||
632 | add_prefix_cmd ("python", no_class, show_python, | |
633 | _("Prefix command for python maintenance settings."), | |
509238d6 | 634 | &show_python_list, "maintenance show python ", 0, |
d57a3c85 TJB |
635 | &maintenance_show_cmdlist); |
636 | add_prefix_cmd ("python", no_class, set_python, | |
637 | _("Prefix command for python maintenance settings."), | |
509238d6 | 638 | &set_python_list, "maintenance set python ", 0, |
d57a3c85 TJB |
639 | &maintenance_set_cmdlist); |
640 | ||
641 | add_setshow_boolean_cmd ("print-stack", class_maintenance, | |
642 | &gdbpy_should_print_stack, _("\ | |
643 | Enable or disable printing of Python stack dump on error."), _("\ | |
644 | Show whether Python stack will be printed on error."), _("\ | |
645 | Enables or disables printing of Python stack traces."), | |
646 | NULL, NULL, | |
647 | &set_python_list, | |
648 | &show_python_list); | |
649 | ||
650 | #ifdef HAVE_PYTHON | |
0c4a4063 DE |
651 | #ifdef WITH_PYTHON_PATH |
652 | /* Work around problem where python gets confused about where it is, | |
653 | and then can't find its libraries, etc. | |
654 | NOTE: Python assumes the following layout: | |
655 | /foo/bin/python | |
656 | /foo/lib/pythonX.Y/... | |
657 | This must be done before calling Py_Initialize. */ | |
658 | Py_SetProgramName (concat (ldirname (python_libdir), SLASH_STRING, "bin", | |
659 | SLASH_STRING, "python", NULL)); | |
660 | #endif | |
661 | ||
d57a3c85 | 662 | Py_Initialize (); |
ca30a762 | 663 | PyEval_InitThreads (); |
d57a3c85 TJB |
664 | |
665 | gdb_module = Py_InitModule ("gdb", GdbMethods); | |
666 | ||
667 | /* The casts to (char*) are for python 2.4. */ | |
668 | PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version); | |
669 | PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name); | |
670 | PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name); | |
671 | ||
07ca107c DE |
672 | gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL); |
673 | PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc); | |
674 | ||
8a1ea21f | 675 | gdbpy_initialize_auto_load (); |
a08702d6 | 676 | gdbpy_initialize_values (); |
f8f6f20b | 677 | gdbpy_initialize_frames (); |
d8906c6f | 678 | gdbpy_initialize_commands (); |
f3e9a817 PM |
679 | gdbpy_initialize_symbols (); |
680 | gdbpy_initialize_symtabs (); | |
681 | gdbpy_initialize_blocks (); | |
bc3b79fd | 682 | gdbpy_initialize_functions (); |
d7b32ed3 | 683 | gdbpy_initialize_parameters (); |
2c74e833 | 684 | gdbpy_initialize_types (); |
fa33c3cd | 685 | gdbpy_initialize_pspace (); |
89c73ade | 686 | gdbpy_initialize_objfile (); |
adc36818 | 687 | gdbpy_initialize_breakpoints (); |
be759fcf | 688 | gdbpy_initialize_lazy_string (); |
a08702d6 | 689 | |
d57a3c85 | 690 | PyRun_SimpleString ("import gdb"); |
a6bac58e | 691 | PyRun_SimpleString ("gdb.pretty_printers = []"); |
d57a3c85 | 692 | |
a6bac58e TT |
693 | gdbpy_to_string_cst = PyString_FromString ("to_string"); |
694 | gdbpy_children_cst = PyString_FromString ("children"); | |
695 | gdbpy_display_hint_cst = PyString_FromString ("display_hint"); | |
d8906c6f | 696 | gdbpy_doc_cst = PyString_FromString ("__doc__"); |
967cf477 | 697 | gdbpy_enabled_cst = PyString_FromString ("enabled"); |
d8906c6f | 698 | |
d57a3c85 TJB |
699 | /* Create a couple objects which are used for Python's stdout and |
700 | stderr. */ | |
701 | PyRun_SimpleString ("\ | |
702 | import sys\n\ | |
703 | class GdbOutputFile:\n\ | |
704 | def close(self):\n\ | |
705 | # Do nothing.\n\ | |
706 | return None\n\ | |
707 | \n\ | |
708 | def isatty(self):\n\ | |
709 | return False\n\ | |
710 | \n\ | |
711 | def write(self, s):\n\ | |
712 | gdb.write(s)\n\ | |
713 | \n\ | |
714 | def writelines(self, iterable):\n\ | |
715 | for line in iterable:\n\ | |
716 | self.write(line)\n\ | |
717 | \n\ | |
718 | def flush(self):\n\ | |
719 | gdb.flush()\n\ | |
720 | \n\ | |
721 | sys.stderr = GdbOutputFile()\n\ | |
722 | sys.stdout = GdbOutputFile()\n\ | |
723 | "); | |
ca30a762 TT |
724 | |
725 | /* Release the GIL while gdb runs. */ | |
726 | PyThreadState_Swap (NULL); | |
727 | PyEval_ReleaseLock (); | |
728 | ||
d57a3c85 TJB |
729 | #endif /* HAVE_PYTHON */ |
730 | } | |
12453b93 TJB |
731 | |
732 | \f | |
733 | ||
734 | #if HAVE_PYTHON | |
735 | ||
736 | static PyMethodDef GdbMethods[] = | |
737 | { | |
738 | { "history", gdbpy_history, METH_VARARGS, | |
739 | "Get a value from history" }, | |
740 | { "execute", execute_gdb_command, METH_VARARGS, | |
741 | "Execute a gdb command" }, | |
8f500870 | 742 | { "parameter", gdbpy_parameter, METH_VARARGS, |
12453b93 TJB |
743 | "Return a gdb parameter's value" }, |
744 | ||
adc36818 PM |
745 | { "breakpoints", gdbpy_breakpoints, METH_NOARGS, |
746 | "Return a tuple of all breakpoint objects" }, | |
747 | ||
b6313243 TT |
748 | { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS, |
749 | "Find the default visualizer for a Value." }, | |
750 | ||
fa33c3cd DE |
751 | { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS, |
752 | "Return the current Progspace." }, | |
753 | { "progspaces", gdbpy_progspaces, METH_NOARGS, | |
754 | "Return a sequence of all progspaces." }, | |
755 | ||
89c73ade TT |
756 | { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, |
757 | "Return the current Objfile being loaded, or None." }, | |
758 | { "objfiles", gdbpy_objfiles, METH_NOARGS, | |
759 | "Return a sequence of all loaded objfiles." }, | |
760 | ||
f8f6f20b TJB |
761 | { "selected_frame", gdbpy_selected_frame, METH_NOARGS, |
762 | "selected_frame () -> gdb.Frame.\n\ | |
763 | Return the selected frame object." }, | |
764 | { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS, | |
765 | "stop_reason_string (Integer) -> String.\n\ | |
766 | Return a string explaining unwind stop reason." }, | |
767 | ||
2c74e833 TT |
768 | { "lookup_type", (PyCFunction) gdbpy_lookup_type, |
769 | METH_VARARGS | METH_KEYWORDS, | |
770 | "lookup_type (name [, block]) -> type\n\ | |
771 | Return a Type corresponding to the given name." }, | |
f3e9a817 PM |
772 | { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol, |
773 | METH_VARARGS | METH_KEYWORDS, | |
774 | "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\ | |
775 | Return a tuple with the symbol corresponding to the given name (or None) and\n\ | |
776 | a boolean indicating if name is a field of the current implied argument\n\ | |
777 | `this' (when the current language is object-oriented)." }, | |
778 | { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS, | |
779 | "Return the block containing the given pc value, or None." }, | |
57a1d736 TT |
780 | { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, |
781 | "parse_and_eval (String) -> Value.\n\ | |
782 | Parse String as an expression, evaluate it, and return the result as a Value." | |
783 | }, | |
784 | ||
f870a310 TT |
785 | { "target_charset", gdbpy_target_charset, METH_NOARGS, |
786 | "target_charset () -> string.\n\ | |
787 | Return the name of the current target charset." }, | |
788 | { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS, | |
789 | "target_wide_charset () -> string.\n\ | |
790 | Return the name of the current target wide charset." }, | |
791 | ||
07ca107c DE |
792 | { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS, |
793 | "string_to_argv (String) -> Array.\n\ | |
794 | Parse String and return an argv-like array.\n\ | |
795 | Arguments are separate by spaces and may be quoted." | |
796 | }, | |
797 | ||
12453b93 TJB |
798 | { "write", gdbpy_write, METH_VARARGS, |
799 | "Write a string using gdb's filtered stream." }, | |
800 | { "flush", gdbpy_flush, METH_NOARGS, | |
801 | "Flush gdb's filtered stdout stream." }, | |
802 | ||
803 | {NULL, NULL, 0, NULL} | |
804 | }; | |
805 | ||
806 | #endif /* HAVE_PYTHON */ |