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 | ||
bc9f0842 TT |
312 | struct restore_ui_file_closure |
313 | { | |
314 | struct ui_file **variable; | |
315 | struct ui_file *value; | |
316 | }; | |
317 | ||
318 | static void | |
319 | restore_ui_file (void *p) | |
320 | { | |
321 | struct restore_ui_file_closure *closure = p; | |
322 | ||
323 | *(closure->variable) = closure->value; | |
324 | } | |
325 | ||
326 | /* Remember the current value of *VARIABLE and make it restored when | |
327 | the cleanup is run. */ | |
328 | struct cleanup * | |
329 | make_cleanup_restore_ui_file (struct ui_file **variable) | |
330 | { | |
331 | struct restore_ui_file_closure *c = XNEW (struct restore_ui_file_closure); | |
332 | ||
333 | c->variable = variable; | |
334 | c->value = *variable; | |
335 | ||
336 | return make_cleanup_dtor (restore_ui_file, (void *) c, xfree); | |
337 | } | |
338 | ||
d57a3c85 TJB |
339 | /* A Python function which evaluates a string using the gdb CLI. */ |
340 | ||
341 | static PyObject * | |
bc9f0842 | 342 | execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 343 | { |
f92adf3c | 344 | char *arg; |
bc9f0842 TT |
345 | PyObject *from_tty_obj = NULL, *to_string_obj = NULL; |
346 | int from_tty, to_string; | |
d57a3c85 | 347 | volatile struct gdb_exception except; |
bc9f0842 TT |
348 | static char *keywords[] = {"command", "from_tty", "to_string", NULL }; |
349 | char *result = NULL; | |
d57a3c85 | 350 | |
bc9f0842 TT |
351 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg, |
352 | &PyBool_Type, &from_tty_obj, | |
353 | &PyBool_Type, &to_string_obj)) | |
d57a3c85 TJB |
354 | return NULL; |
355 | ||
12453b93 TJB |
356 | from_tty = 0; |
357 | if (from_tty_obj) | |
358 | { | |
bc9f0842 | 359 | int cmp = PyObject_IsTrue (from_tty_obj); |
12453b93 | 360 | if (cmp < 0) |
bc9f0842 | 361 | return NULL; |
12453b93 TJB |
362 | from_tty = cmp; |
363 | } | |
364 | ||
bc9f0842 TT |
365 | to_string = 0; |
366 | if (to_string_obj) | |
367 | { | |
368 | int cmp = PyObject_IsTrue (to_string_obj); | |
369 | if (cmp < 0) | |
370 | return NULL; | |
371 | to_string = cmp; | |
372 | } | |
373 | ||
d57a3c85 TJB |
374 | TRY_CATCH (except, RETURN_MASK_ALL) |
375 | { | |
86c6265d TT |
376 | /* Copy the argument text in case the command modifies it. */ |
377 | char *copy = xstrdup (arg); | |
378 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
bc9f0842 TT |
379 | struct ui_file *str_file = NULL; |
380 | ||
381 | if (to_string) | |
382 | { | |
383 | str_file = mem_fileopen (); | |
384 | ||
385 | make_cleanup_restore_ui_file (&gdb_stdout); | |
386 | make_cleanup_restore_ui_file (&gdb_stderr); | |
387 | make_cleanup_ui_file_delete (str_file); | |
388 | ||
389 | gdb_stdout = str_file; | |
390 | gdb_stderr = str_file; | |
391 | } | |
d59b6f6c | 392 | |
86c6265d | 393 | execute_command (copy, from_tty); |
bc9f0842 TT |
394 | |
395 | if (str_file) | |
396 | result = ui_file_xstrdup (str_file, NULL); | |
397 | else | |
398 | result = NULL; | |
399 | ||
86c6265d | 400 | do_cleanups (cleanup); |
d57a3c85 TJB |
401 | } |
402 | GDB_PY_HANDLE_EXCEPTION (except); | |
403 | ||
347bddb7 PA |
404 | /* Do any commands attached to breakpoint we stopped at. */ |
405 | bpstat_do_actions (); | |
d57a3c85 | 406 | |
bc9f0842 TT |
407 | if (result) |
408 | { | |
409 | PyObject *r = PyString_FromString (result); | |
410 | xfree (result); | |
411 | return r; | |
412 | } | |
d57a3c85 TJB |
413 | Py_RETURN_NONE; |
414 | } | |
415 | ||
57a1d736 TT |
416 | /* Parse a string and evaluate it as an expression. */ |
417 | static PyObject * | |
418 | gdbpy_parse_and_eval (PyObject *self, PyObject *args) | |
419 | { | |
420 | char *expr_str; | |
421 | struct value *result = NULL; | |
422 | volatile struct gdb_exception except; | |
423 | ||
424 | if (!PyArg_ParseTuple (args, "s", &expr_str)) | |
425 | return NULL; | |
426 | ||
427 | TRY_CATCH (except, RETURN_MASK_ALL) | |
428 | { | |
429 | result = parse_and_eval (expr_str); | |
430 | } | |
431 | GDB_PY_HANDLE_EXCEPTION (except); | |
432 | ||
433 | return value_to_value_object (result); | |
434 | } | |
435 | ||
973817a3 | 436 | /* Read a file as Python code. STREAM is the input file; FILE is the |
3f7b2faa DE |
437 | name of the file. |
438 | STREAM is not closed, that is the caller's responsibility. */ | |
973817a3 JB |
439 | |
440 | void | |
3f7b2faa | 441 | source_python_script (FILE *stream, const char *file) |
973817a3 | 442 | { |
eb5cda86 | 443 | struct cleanup *cleanup; |
973817a3 | 444 | |
eb5cda86 | 445 | cleanup = ensure_python_env (get_current_arch (), current_language); |
973817a3 | 446 | |
3a1d4620 DE |
447 | /* Note: If an exception occurs python will print the traceback and |
448 | clear the error indicator. */ | |
973817a3 JB |
449 | PyRun_SimpleFile (stream, file); |
450 | ||
eb5cda86 | 451 | do_cleanups (cleanup); |
973817a3 JB |
452 | } |
453 | ||
d57a3c85 TJB |
454 | \f |
455 | ||
456 | /* Printing. */ | |
457 | ||
458 | /* A python function to write a single string using gdb's filtered | |
459 | output stream. */ | |
460 | static PyObject * | |
461 | gdbpy_write (PyObject *self, PyObject *args) | |
462 | { | |
463 | char *arg; | |
d59b6f6c | 464 | |
d57a3c85 TJB |
465 | if (! PyArg_ParseTuple (args, "s", &arg)) |
466 | return NULL; | |
467 | printf_filtered ("%s", arg); | |
468 | Py_RETURN_NONE; | |
469 | } | |
470 | ||
471 | /* A python function to flush gdb's filtered output stream. */ | |
472 | static PyObject * | |
473 | gdbpy_flush (PyObject *self, PyObject *args) | |
474 | { | |
475 | gdb_flush (gdb_stdout); | |
476 | Py_RETURN_NONE; | |
477 | } | |
478 | ||
479 | /* Print a python exception trace, or print nothing and clear the | |
480 | python exception, depending on gdbpy_should_print_stack. Only call | |
481 | this if a python exception is set. */ | |
482 | void | |
483 | gdbpy_print_stack (void) | |
484 | { | |
485 | if (gdbpy_should_print_stack) | |
0bf0f8c4 DE |
486 | { |
487 | PyErr_Print (); | |
488 | /* PyErr_Print doesn't necessarily end output with a newline. | |
489 | This works because Python's stdout/stderr is fed through | |
490 | printf_filtered. */ | |
491 | begin_line (); | |
492 | } | |
d57a3c85 TJB |
493 | else |
494 | PyErr_Clear (); | |
495 | } | |
496 | ||
89c73ade TT |
497 | \f |
498 | ||
fa33c3cd DE |
499 | /* Return the current Progspace. |
500 | There always is one. */ | |
501 | ||
502 | static PyObject * | |
503 | gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2) | |
504 | { | |
505 | PyObject *result; | |
506 | ||
507 | result = pspace_to_pspace_object (current_program_space); | |
508 | if (result) | |
509 | Py_INCREF (result); | |
510 | return result; | |
511 | } | |
512 | ||
513 | /* Return a sequence holding all the Progspaces. */ | |
514 | ||
515 | static PyObject * | |
516 | gdbpy_progspaces (PyObject *unused1, PyObject *unused2) | |
517 | { | |
518 | struct program_space *ps; | |
519 | PyObject *list; | |
520 | ||
521 | list = PyList_New (0); | |
522 | if (!list) | |
523 | return NULL; | |
524 | ||
525 | ALL_PSPACES (ps) | |
526 | { | |
527 | PyObject *item = pspace_to_pspace_object (ps); | |
d59b6f6c | 528 | |
fa33c3cd DE |
529 | if (!item || PyList_Append (list, item) == -1) |
530 | { | |
531 | Py_DECREF (list); | |
532 | return NULL; | |
533 | } | |
534 | } | |
535 | ||
536 | return list; | |
537 | } | |
538 | ||
539 | \f | |
540 | ||
89c73ade | 541 | /* The "current" objfile. This is set when gdb detects that a new |
8a1ea21f DE |
542 | objfile has been loaded. It is only set for the duration of a call to |
543 | source_python_script_for_objfile; it is NULL at other times. */ | |
89c73ade TT |
544 | static struct objfile *gdbpy_current_objfile; |
545 | ||
8a1ea21f DE |
546 | /* Set the current objfile to OBJFILE and then read STREAM,FILE as |
547 | Python code. */ | |
89c73ade | 548 | |
8a1ea21f DE |
549 | void |
550 | source_python_script_for_objfile (struct objfile *objfile, | |
551 | FILE *stream, const char *file) | |
89c73ade | 552 | { |
89c73ade TT |
553 | struct cleanup *cleanups; |
554 | ||
d452c4bc | 555 | cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
556 | gdbpy_current_objfile = objfile; |
557 | ||
3a1d4620 DE |
558 | /* Note: If an exception occurs python will print the traceback and |
559 | clear the error indicator. */ | |
560 | PyRun_SimpleFile (stream, file); | |
89c73ade TT |
561 | |
562 | do_cleanups (cleanups); | |
563 | gdbpy_current_objfile = NULL; | |
89c73ade TT |
564 | } |
565 | ||
566 | /* Return the current Objfile, or None if there isn't one. */ | |
8a1ea21f | 567 | |
89c73ade TT |
568 | static PyObject * |
569 | gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) | |
570 | { | |
571 | PyObject *result; | |
572 | ||
573 | if (! gdbpy_current_objfile) | |
574 | Py_RETURN_NONE; | |
575 | ||
576 | result = objfile_to_objfile_object (gdbpy_current_objfile); | |
577 | if (result) | |
578 | Py_INCREF (result); | |
579 | return result; | |
580 | } | |
581 | ||
582 | /* Return a sequence holding all the Objfiles. */ | |
fa33c3cd | 583 | |
89c73ade TT |
584 | static PyObject * |
585 | gdbpy_objfiles (PyObject *unused1, PyObject *unused2) | |
586 | { | |
587 | struct objfile *objf; | |
588 | PyObject *list; | |
589 | ||
590 | list = PyList_New (0); | |
591 | if (!list) | |
592 | return NULL; | |
593 | ||
594 | ALL_OBJFILES (objf) | |
595 | { | |
596 | PyObject *item = objfile_to_objfile_object (objf); | |
d59b6f6c | 597 | |
89c73ade TT |
598 | if (!item || PyList_Append (list, item) == -1) |
599 | { | |
600 | Py_DECREF (list); | |
601 | return NULL; | |
602 | } | |
603 | } | |
604 | ||
605 | return list; | |
606 | } | |
607 | ||
d57a3c85 TJB |
608 | #else /* HAVE_PYTHON */ |
609 | ||
610 | /* Dummy implementation of the gdb "python" command. */ | |
611 | ||
612 | static void | |
613 | python_command (char *arg, int from_tty) | |
614 | { | |
615 | while (arg && *arg && isspace (*arg)) | |
616 | ++arg; | |
617 | if (arg && *arg) | |
618 | error (_("Python scripting is not supported in this copy of GDB.")); | |
619 | else | |
620 | { | |
621 | struct command_line *l = get_command_line (python_control, ""); | |
622 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
d59b6f6c | 623 | |
d57a3c85 TJB |
624 | execute_control_command_untraced (l); |
625 | do_cleanups (cleanups); | |
626 | } | |
627 | } | |
628 | ||
629 | void | |
630 | eval_python_from_control_command (struct command_line *cmd) | |
631 | { | |
632 | error (_("Python scripting is not supported in this copy of GDB.")); | |
633 | } | |
634 | ||
973817a3 | 635 | void |
3f7b2faa | 636 | source_python_script (FILE *stream, const char *file) |
973817a3 | 637 | { |
973817a3 JB |
638 | throw_error (UNSUPPORTED_ERROR, |
639 | _("Python scripting is not supported in this copy of GDB.")); | |
640 | } | |
641 | ||
d57a3c85 TJB |
642 | #endif /* HAVE_PYTHON */ |
643 | ||
644 | \f | |
645 | ||
646 | /* Lists for 'maint set python' commands. */ | |
647 | ||
8a1ea21f DE |
648 | struct cmd_list_element *set_python_list; |
649 | struct cmd_list_element *show_python_list; | |
d57a3c85 TJB |
650 | |
651 | /* Function for use by 'maint set python' prefix command. */ | |
652 | ||
653 | static void | |
654 | set_python (char *args, int from_tty) | |
655 | { | |
656 | help_list (set_python_list, "maintenance set python ", -1, gdb_stdout); | |
657 | } | |
658 | ||
659 | /* Function for use by 'maint show python' prefix command. */ | |
660 | ||
661 | static void | |
662 | show_python (char *args, int from_tty) | |
663 | { | |
664 | cmd_show_list (show_python_list, from_tty, ""); | |
665 | } | |
666 | ||
667 | /* Initialize the Python code. */ | |
668 | ||
2c0b251b PA |
669 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
670 | extern initialize_file_ftype _initialize_python; | |
671 | ||
d57a3c85 TJB |
672 | void |
673 | _initialize_python (void) | |
674 | { | |
675 | add_com ("python", class_obscure, python_command, | |
676 | #ifdef HAVE_PYTHON | |
677 | _("\ | |
678 | Evaluate a Python command.\n\ | |
679 | \n\ | |
680 | The command can be given as an argument, for instance:\n\ | |
681 | \n\ | |
682 | python print 23\n\ | |
683 | \n\ | |
684 | If no argument is given, the following lines are read and used\n\ | |
685 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
686 | the end of the command.") | |
687 | #else /* HAVE_PYTHON */ | |
688 | _("\ | |
689 | Evaluate a Python command.\n\ | |
690 | \n\ | |
691 | Python scripting is not supported in this copy of GDB.\n\ | |
692 | This command is only a placeholder.") | |
693 | #endif /* HAVE_PYTHON */ | |
694 | ); | |
695 | ||
696 | add_prefix_cmd ("python", no_class, show_python, | |
697 | _("Prefix command for python maintenance settings."), | |
509238d6 | 698 | &show_python_list, "maintenance show python ", 0, |
d57a3c85 TJB |
699 | &maintenance_show_cmdlist); |
700 | add_prefix_cmd ("python", no_class, set_python, | |
701 | _("Prefix command for python maintenance settings."), | |
509238d6 | 702 | &set_python_list, "maintenance set python ", 0, |
d57a3c85 TJB |
703 | &maintenance_set_cmdlist); |
704 | ||
705 | add_setshow_boolean_cmd ("print-stack", class_maintenance, | |
706 | &gdbpy_should_print_stack, _("\ | |
707 | Enable or disable printing of Python stack dump on error."), _("\ | |
708 | Show whether Python stack will be printed on error."), _("\ | |
709 | Enables or disables printing of Python stack traces."), | |
710 | NULL, NULL, | |
711 | &set_python_list, | |
712 | &show_python_list); | |
713 | ||
714 | #ifdef HAVE_PYTHON | |
0c4a4063 DE |
715 | #ifdef WITH_PYTHON_PATH |
716 | /* Work around problem where python gets confused about where it is, | |
717 | and then can't find its libraries, etc. | |
718 | NOTE: Python assumes the following layout: | |
719 | /foo/bin/python | |
720 | /foo/lib/pythonX.Y/... | |
721 | This must be done before calling Py_Initialize. */ | |
722 | Py_SetProgramName (concat (ldirname (python_libdir), SLASH_STRING, "bin", | |
723 | SLASH_STRING, "python", NULL)); | |
724 | #endif | |
725 | ||
d57a3c85 | 726 | Py_Initialize (); |
ca30a762 | 727 | PyEval_InitThreads (); |
d57a3c85 TJB |
728 | |
729 | gdb_module = Py_InitModule ("gdb", GdbMethods); | |
730 | ||
731 | /* The casts to (char*) are for python 2.4. */ | |
732 | PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version); | |
733 | PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name); | |
734 | PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name); | |
b14285f6 JB |
735 | { |
736 | char *gdb_pythondir; | |
737 | ||
738 | gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL); | |
739 | PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir); | |
740 | xfree (gdb_pythondir); | |
741 | } | |
d57a3c85 | 742 | |
07ca107c DE |
743 | gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL); |
744 | PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc); | |
745 | ||
8a1ea21f | 746 | gdbpy_initialize_auto_load (); |
a08702d6 | 747 | gdbpy_initialize_values (); |
f8f6f20b | 748 | gdbpy_initialize_frames (); |
d8906c6f | 749 | gdbpy_initialize_commands (); |
f3e9a817 PM |
750 | gdbpy_initialize_symbols (); |
751 | gdbpy_initialize_symtabs (); | |
752 | gdbpy_initialize_blocks (); | |
bc3b79fd | 753 | gdbpy_initialize_functions (); |
d7b32ed3 | 754 | gdbpy_initialize_parameters (); |
2c74e833 | 755 | gdbpy_initialize_types (); |
fa33c3cd | 756 | gdbpy_initialize_pspace (); |
89c73ade | 757 | gdbpy_initialize_objfile (); |
adc36818 | 758 | gdbpy_initialize_breakpoints (); |
be759fcf | 759 | gdbpy_initialize_lazy_string (); |
595939de PM |
760 | gdbpy_initialize_thread (); |
761 | gdbpy_initialize_inferior (); | |
a08702d6 | 762 | |
d57a3c85 | 763 | PyRun_SimpleString ("import gdb"); |
a6bac58e | 764 | PyRun_SimpleString ("gdb.pretty_printers = []"); |
d57a3c85 | 765 | |
a6bac58e TT |
766 | gdbpy_to_string_cst = PyString_FromString ("to_string"); |
767 | gdbpy_children_cst = PyString_FromString ("children"); | |
768 | gdbpy_display_hint_cst = PyString_FromString ("display_hint"); | |
d8906c6f | 769 | gdbpy_doc_cst = PyString_FromString ("__doc__"); |
967cf477 | 770 | gdbpy_enabled_cst = PyString_FromString ("enabled"); |
d8906c6f | 771 | |
d57a3c85 TJB |
772 | /* Create a couple objects which are used for Python's stdout and |
773 | stderr. */ | |
774 | PyRun_SimpleString ("\ | |
775 | import sys\n\ | |
776 | class GdbOutputFile:\n\ | |
777 | def close(self):\n\ | |
778 | # Do nothing.\n\ | |
779 | return None\n\ | |
780 | \n\ | |
781 | def isatty(self):\n\ | |
782 | return False\n\ | |
783 | \n\ | |
784 | def write(self, s):\n\ | |
785 | gdb.write(s)\n\ | |
786 | \n\ | |
787 | def writelines(self, iterable):\n\ | |
788 | for line in iterable:\n\ | |
789 | self.write(line)\n\ | |
790 | \n\ | |
791 | def flush(self):\n\ | |
792 | gdb.flush()\n\ | |
793 | \n\ | |
794 | sys.stderr = GdbOutputFile()\n\ | |
795 | sys.stdout = GdbOutputFile()\n\ | |
b14285f6 JB |
796 | \n\ |
797 | # GDB's python scripts are stored inside gdb.PYTHONDIR. So insert\n\ | |
798 | # that directory name at the start of sys.path to allow the Python\n\ | |
799 | # interpreter to find them.\n\ | |
800 | sys.path.insert(0, gdb.PYTHONDIR)\n\ | |
801 | \n\ | |
802 | # The gdb module is implemented in C rather than in Python. As a result,\n\ | |
803 | # the associated __init.py__ script is not not executed by default when\n\ | |
804 | # the gdb module gets imported. Execute that script manually if it exists.\n\ | |
805 | gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\ | |
806 | from os.path import exists\n\ | |
807 | ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\ | |
808 | if exists (ipy):\n\ | |
809 | execfile (ipy)\n\ | |
d57a3c85 | 810 | "); |
ca30a762 TT |
811 | |
812 | /* Release the GIL while gdb runs. */ | |
813 | PyThreadState_Swap (NULL); | |
814 | PyEval_ReleaseLock (); | |
815 | ||
d57a3c85 TJB |
816 | #endif /* HAVE_PYTHON */ |
817 | } | |
12453b93 TJB |
818 | |
819 | \f | |
820 | ||
821 | #if HAVE_PYTHON | |
822 | ||
823 | static PyMethodDef GdbMethods[] = | |
824 | { | |
825 | { "history", gdbpy_history, METH_VARARGS, | |
826 | "Get a value from history" }, | |
bc9f0842 | 827 | { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 828 | "Execute a gdb command" }, |
8f500870 | 829 | { "parameter", gdbpy_parameter, METH_VARARGS, |
12453b93 TJB |
830 | "Return a gdb parameter's value" }, |
831 | ||
adc36818 PM |
832 | { "breakpoints", gdbpy_breakpoints, METH_NOARGS, |
833 | "Return a tuple of all breakpoint objects" }, | |
834 | ||
b6313243 TT |
835 | { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS, |
836 | "Find the default visualizer for a Value." }, | |
837 | ||
fa33c3cd DE |
838 | { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS, |
839 | "Return the current Progspace." }, | |
840 | { "progspaces", gdbpy_progspaces, METH_NOARGS, | |
841 | "Return a sequence of all progspaces." }, | |
842 | ||
89c73ade TT |
843 | { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, |
844 | "Return the current Objfile being loaded, or None." }, | |
845 | { "objfiles", gdbpy_objfiles, METH_NOARGS, | |
846 | "Return a sequence of all loaded objfiles." }, | |
847 | ||
f8f6f20b TJB |
848 | { "selected_frame", gdbpy_selected_frame, METH_NOARGS, |
849 | "selected_frame () -> gdb.Frame.\n\ | |
850 | Return the selected frame object." }, | |
851 | { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS, | |
852 | "stop_reason_string (Integer) -> String.\n\ | |
853 | Return a string explaining unwind stop reason." }, | |
854 | ||
2c74e833 TT |
855 | { "lookup_type", (PyCFunction) gdbpy_lookup_type, |
856 | METH_VARARGS | METH_KEYWORDS, | |
857 | "lookup_type (name [, block]) -> type\n\ | |
858 | Return a Type corresponding to the given name." }, | |
f3e9a817 PM |
859 | { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol, |
860 | METH_VARARGS | METH_KEYWORDS, | |
861 | "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\ | |
862 | Return a tuple with the symbol corresponding to the given name (or None) and\n\ | |
863 | a boolean indicating if name is a field of the current implied argument\n\ | |
864 | `this' (when the current language is object-oriented)." }, | |
865 | { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS, | |
866 | "Return the block containing the given pc value, or None." }, | |
57a1d736 TT |
867 | { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, |
868 | "parse_and_eval (String) -> Value.\n\ | |
869 | Parse String as an expression, evaluate it, and return the result as a Value." | |
870 | }, | |
871 | ||
f870a310 TT |
872 | { "target_charset", gdbpy_target_charset, METH_NOARGS, |
873 | "target_charset () -> string.\n\ | |
874 | Return the name of the current target charset." }, | |
875 | { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS, | |
876 | "target_wide_charset () -> string.\n\ | |
877 | Return the name of the current target wide charset." }, | |
878 | ||
07ca107c DE |
879 | { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS, |
880 | "string_to_argv (String) -> Array.\n\ | |
881 | Parse String and return an argv-like array.\n\ | |
882 | Arguments are separate by spaces and may be quoted." | |
883 | }, | |
884 | ||
12453b93 TJB |
885 | { "write", gdbpy_write, METH_VARARGS, |
886 | "Write a string using gdb's filtered stream." }, | |
887 | { "flush", gdbpy_flush, METH_NOARGS, | |
888 | "Flush gdb's filtered stdout stream." }, | |
595939de PM |
889 | { "selected_thread", gdbpy_selected_thread, METH_NOARGS, |
890 | "selected_thread () -> gdb.InferiorThread.\n\ | |
891 | Return the selected thread object." }, | |
892 | { "inferiors", gdbpy_inferiors, METH_NOARGS, | |
893 | "inferiors () -> (gdb.Inferior, ...).\n\ | |
894 | Return a tuple containing all inferiors." }, | |
12453b93 TJB |
895 | {NULL, NULL, 0, NULL} |
896 | }; | |
897 | ||
898 | #endif /* HAVE_PYTHON */ |