Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* General python/gdb code |
2 | ||
7b6bb8da | 3 | Copyright (C) 2008, 2009, 2010, 2011 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" |
ca5c20b6 | 31 | #include "event-loop.h" |
4a532131 | 32 | #include "serial.h" |
7371cf6d | 33 | #include "python.h" |
d57a3c85 TJB |
34 | |
35 | #include <ctype.h> | |
36 | ||
37 | /* True if we should print the stack when catching a Python error, | |
38 | false otherwise. */ | |
39 | static int gdbpy_should_print_stack = 1; | |
40 | ||
41 | #ifdef HAVE_PYTHON | |
42 | ||
d57a3c85 TJB |
43 | #include "libiberty.h" |
44 | #include "cli/cli-decode.h" | |
45 | #include "charset.h" | |
46 | #include "top.h" | |
cb2e07a6 | 47 | #include "solib.h" |
d57a3c85 | 48 | #include "python-internal.h" |
cb2e07a6 PM |
49 | #include "linespec.h" |
50 | #include "source.h" | |
d57a3c85 TJB |
51 | #include "version.h" |
52 | #include "target.h" | |
53 | #include "gdbthread.h" | |
54 | ||
12453b93 | 55 | static PyMethodDef GdbMethods[]; |
d57a3c85 TJB |
56 | |
57 | PyObject *gdb_module; | |
58 | ||
a6bac58e TT |
59 | /* Some string constants we may wish to use. */ |
60 | PyObject *gdbpy_to_string_cst; | |
61 | PyObject *gdbpy_children_cst; | |
62 | PyObject *gdbpy_display_hint_cst; | |
d8906c6f | 63 | PyObject *gdbpy_doc_cst; |
967cf477 | 64 | PyObject *gdbpy_enabled_cst; |
fb6a3ed3 | 65 | PyObject *gdbpy_value_cst; |
d8906c6f | 66 | |
07ca107c DE |
67 | /* The GdbError exception. */ |
68 | PyObject *gdbpy_gdberror_exc; | |
d452c4bc | 69 | |
621c8364 TT |
70 | /* The `gdb.error' base class. */ |
71 | PyObject *gdbpy_gdb_error; | |
72 | ||
73 | /* The `gdb.MemoryError' exception. */ | |
74 | PyObject *gdbpy_gdb_memory_error; | |
75 | ||
d452c4bc UW |
76 | /* Architecture and language to be used in callbacks from |
77 | the Python interpreter. */ | |
78 | struct gdbarch *python_gdbarch; | |
79 | const struct language_defn *python_language; | |
80 | ||
81 | /* Restore global language and architecture and Python GIL state | |
82 | when leaving the Python interpreter. */ | |
83 | ||
84 | struct python_env | |
85 | { | |
86 | PyGILState_STATE state; | |
87 | struct gdbarch *gdbarch; | |
88 | const struct language_defn *language; | |
8dc78533 | 89 | PyObject *error_type, *error_value, *error_traceback; |
d452c4bc UW |
90 | }; |
91 | ||
92 | static void | |
93 | restore_python_env (void *p) | |
94 | { | |
95 | struct python_env *env = (struct python_env *)p; | |
d59b6f6c | 96 | |
8dc78533 JK |
97 | /* Leftover Python error is forbidden by Python Exception Handling. */ |
98 | if (PyErr_Occurred ()) | |
99 | { | |
100 | /* This order is similar to the one calling error afterwards. */ | |
101 | gdbpy_print_stack (); | |
102 | warning (_("internal error: Unhandled Python exception")); | |
103 | } | |
104 | ||
105 | PyErr_Restore (env->error_type, env->error_value, env->error_traceback); | |
106 | ||
d452c4bc UW |
107 | PyGILState_Release (env->state); |
108 | python_gdbarch = env->gdbarch; | |
109 | python_language = env->language; | |
110 | xfree (env); | |
111 | } | |
112 | ||
113 | /* Called before entering the Python interpreter to install the | |
114 | current language and architecture to be used for Python values. */ | |
115 | ||
116 | struct cleanup * | |
117 | ensure_python_env (struct gdbarch *gdbarch, | |
118 | const struct language_defn *language) | |
119 | { | |
120 | struct python_env *env = xmalloc (sizeof *env); | |
121 | ||
122 | env->state = PyGILState_Ensure (); | |
123 | env->gdbarch = python_gdbarch; | |
124 | env->language = python_language; | |
125 | ||
126 | python_gdbarch = gdbarch; | |
127 | python_language = language; | |
128 | ||
8dc78533 JK |
129 | /* Save it and ensure ! PyErr_Occurred () afterwards. */ |
130 | PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback); | |
131 | ||
d452c4bc UW |
132 | return make_cleanup (restore_python_env, env); |
133 | } | |
134 | ||
135 | ||
d57a3c85 TJB |
136 | /* Given a command_line, return a command string suitable for passing |
137 | to Python. Lines in the string are separated by newlines. The | |
138 | return value is allocated using xmalloc and the caller is | |
139 | responsible for freeing it. */ | |
140 | ||
141 | static char * | |
142 | compute_python_string (struct command_line *l) | |
143 | { | |
144 | struct command_line *iter; | |
145 | char *script = NULL; | |
146 | int size = 0; | |
147 | int here; | |
148 | ||
149 | for (iter = l; iter; iter = iter->next) | |
150 | size += strlen (iter->line) + 1; | |
151 | ||
152 | script = xmalloc (size + 1); | |
153 | here = 0; | |
154 | for (iter = l; iter; iter = iter->next) | |
155 | { | |
156 | int len = strlen (iter->line); | |
d59b6f6c | 157 | |
d57a3c85 TJB |
158 | strcpy (&script[here], iter->line); |
159 | here += len; | |
160 | script[here++] = '\n'; | |
161 | } | |
162 | script[here] = '\0'; | |
163 | return script; | |
164 | } | |
165 | ||
166 | /* Take a command line structure representing a 'python' command, and | |
167 | evaluate its body using the Python interpreter. */ | |
168 | ||
169 | void | |
170 | eval_python_from_control_command (struct command_line *cmd) | |
171 | { | |
12453b93 | 172 | int ret; |
d57a3c85 | 173 | char *script; |
ca30a762 | 174 | struct cleanup *cleanup; |
d57a3c85 TJB |
175 | |
176 | if (cmd->body_count != 1) | |
177 | error (_("Invalid \"python\" block structure.")); | |
178 | ||
d452c4bc | 179 | cleanup = ensure_python_env (get_current_arch (), current_language); |
ca30a762 | 180 | |
d57a3c85 | 181 | script = compute_python_string (cmd->body_list[0]); |
12453b93 | 182 | ret = PyRun_SimpleString (script); |
d57a3c85 | 183 | xfree (script); |
12453b93 | 184 | if (ret) |
d57a3c85 TJB |
185 | { |
186 | gdbpy_print_stack (); | |
12453b93 | 187 | error (_("Error while executing Python code.")); |
d57a3c85 | 188 | } |
ca30a762 TT |
189 | |
190 | do_cleanups (cleanup); | |
d57a3c85 TJB |
191 | } |
192 | ||
193 | /* Implementation of the gdb "python" command. */ | |
194 | ||
195 | static void | |
196 | python_command (char *arg, int from_tty) | |
197 | { | |
ca30a762 | 198 | struct cleanup *cleanup; |
ca30a762 | 199 | |
d59b6f6c | 200 | cleanup = ensure_python_env (get_current_arch (), current_language); |
d57a3c85 TJB |
201 | while (arg && *arg && isspace (*arg)) |
202 | ++arg; | |
203 | if (arg && *arg) | |
204 | { | |
12453b93 | 205 | if (PyRun_SimpleString (arg)) |
d57a3c85 TJB |
206 | { |
207 | gdbpy_print_stack (); | |
12453b93 | 208 | error (_("Error while executing Python code.")); |
d57a3c85 TJB |
209 | } |
210 | } | |
211 | else | |
212 | { | |
213 | struct command_line *l = get_command_line (python_control, ""); | |
d59b6f6c | 214 | |
ca30a762 | 215 | make_cleanup_free_command_lines (&l); |
d57a3c85 | 216 | execute_control_command_untraced (l); |
d57a3c85 | 217 | } |
ca30a762 TT |
218 | |
219 | do_cleanups (cleanup); | |
d57a3c85 TJB |
220 | } |
221 | ||
222 | \f | |
223 | ||
224 | /* Transform a gdb parameters's value into a Python value. May return | |
225 | NULL (and set a Python exception) on error. Helper function for | |
226 | get_parameter. */ | |
d7b32ed3 PM |
227 | PyObject * |
228 | gdbpy_parameter_value (enum var_types type, void *var) | |
d57a3c85 | 229 | { |
d7b32ed3 | 230 | switch (type) |
d57a3c85 TJB |
231 | { |
232 | case var_string: | |
233 | case var_string_noescape: | |
234 | case var_optional_filename: | |
235 | case var_filename: | |
236 | case var_enum: | |
237 | { | |
d7b32ed3 | 238 | char *str = * (char **) var; |
d59b6f6c | 239 | |
d57a3c85 TJB |
240 | if (! str) |
241 | str = ""; | |
242 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
243 | } | |
244 | ||
245 | case var_boolean: | |
246 | { | |
d7b32ed3 | 247 | if (* (int *) var) |
d57a3c85 TJB |
248 | Py_RETURN_TRUE; |
249 | else | |
250 | Py_RETURN_FALSE; | |
251 | } | |
252 | ||
253 | case var_auto_boolean: | |
254 | { | |
d7b32ed3 | 255 | enum auto_boolean ab = * (enum auto_boolean *) var; |
d59b6f6c | 256 | |
d57a3c85 TJB |
257 | if (ab == AUTO_BOOLEAN_TRUE) |
258 | Py_RETURN_TRUE; | |
259 | else if (ab == AUTO_BOOLEAN_FALSE) | |
260 | Py_RETURN_FALSE; | |
261 | else | |
262 | Py_RETURN_NONE; | |
263 | } | |
264 | ||
265 | case var_integer: | |
d7b32ed3 | 266 | if ((* (int *) var) == INT_MAX) |
d57a3c85 TJB |
267 | Py_RETURN_NONE; |
268 | /* Fall through. */ | |
269 | case var_zinteger: | |
d7b32ed3 | 270 | return PyLong_FromLong (* (int *) var); |
d57a3c85 TJB |
271 | |
272 | case var_uinteger: | |
273 | { | |
d7b32ed3 | 274 | unsigned int val = * (unsigned int *) var; |
d59b6f6c | 275 | |
d57a3c85 TJB |
276 | if (val == UINT_MAX) |
277 | Py_RETURN_NONE; | |
278 | return PyLong_FromUnsignedLong (val); | |
279 | } | |
280 | } | |
281 | ||
044c0f87 PM |
282 | return PyErr_Format (PyExc_RuntimeError, |
283 | _("Programmer error: unhandled type.")); | |
d57a3c85 TJB |
284 | } |
285 | ||
286 | /* A Python function which returns a gdb parameter's value as a Python | |
287 | value. */ | |
288 | ||
d7b32ed3 | 289 | PyObject * |
8f500870 | 290 | gdbpy_parameter (PyObject *self, PyObject *args) |
d57a3c85 TJB |
291 | { |
292 | struct cmd_list_element *alias, *prefix, *cmd; | |
293 | char *arg, *newarg; | |
cc924cad | 294 | int found = -1; |
d57a3c85 TJB |
295 | volatile struct gdb_exception except; |
296 | ||
297 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
298 | return NULL; | |
299 | ||
300 | newarg = concat ("show ", arg, (char *) NULL); | |
301 | ||
302 | TRY_CATCH (except, RETURN_MASK_ALL) | |
303 | { | |
cc924cad | 304 | found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); |
d57a3c85 TJB |
305 | } |
306 | xfree (newarg); | |
307 | GDB_PY_HANDLE_EXCEPTION (except); | |
cc924cad TJB |
308 | if (!found) |
309 | return PyErr_Format (PyExc_RuntimeError, | |
044c0f87 | 310 | _("Could not find parameter `%s'."), arg); |
d57a3c85 TJB |
311 | |
312 | if (! cmd->var) | |
044c0f87 PM |
313 | return PyErr_Format (PyExc_RuntimeError, |
314 | _("`%s' is not a parameter."), arg); | |
d7b32ed3 | 315 | return gdbpy_parameter_value (cmd->var_type, cmd->var); |
d57a3c85 TJB |
316 | } |
317 | ||
f870a310 TT |
318 | /* Wrapper for target_charset. */ |
319 | ||
320 | static PyObject * | |
321 | gdbpy_target_charset (PyObject *self, PyObject *args) | |
322 | { | |
323 | const char *cset = target_charset (python_gdbarch); | |
d59b6f6c | 324 | |
f870a310 TT |
325 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
326 | } | |
327 | ||
328 | /* Wrapper for target_wide_charset. */ | |
329 | ||
330 | static PyObject * | |
331 | gdbpy_target_wide_charset (PyObject *self, PyObject *args) | |
332 | { | |
333 | const char *cset = target_wide_charset (python_gdbarch); | |
d59b6f6c | 334 | |
f870a310 TT |
335 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
336 | } | |
337 | ||
d57a3c85 TJB |
338 | /* A Python function which evaluates a string using the gdb CLI. */ |
339 | ||
340 | static PyObject * | |
bc9f0842 | 341 | execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 342 | { |
f92adf3c | 343 | char *arg; |
bc9f0842 TT |
344 | PyObject *from_tty_obj = NULL, *to_string_obj = NULL; |
345 | int from_tty, to_string; | |
d57a3c85 | 346 | volatile struct gdb_exception except; |
bc9f0842 TT |
347 | static char *keywords[] = {"command", "from_tty", "to_string", NULL }; |
348 | char *result = NULL; | |
d57a3c85 | 349 | |
bc9f0842 TT |
350 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg, |
351 | &PyBool_Type, &from_tty_obj, | |
352 | &PyBool_Type, &to_string_obj)) | |
d57a3c85 TJB |
353 | return NULL; |
354 | ||
12453b93 TJB |
355 | from_tty = 0; |
356 | if (from_tty_obj) | |
357 | { | |
bc9f0842 | 358 | int cmp = PyObject_IsTrue (from_tty_obj); |
12453b93 | 359 | if (cmp < 0) |
bc9f0842 | 360 | return NULL; |
12453b93 TJB |
361 | from_tty = cmp; |
362 | } | |
363 | ||
bc9f0842 TT |
364 | to_string = 0; |
365 | if (to_string_obj) | |
366 | { | |
367 | int cmp = PyObject_IsTrue (to_string_obj); | |
368 | if (cmp < 0) | |
369 | return NULL; | |
370 | to_string = cmp; | |
371 | } | |
372 | ||
d57a3c85 TJB |
373 | TRY_CATCH (except, RETURN_MASK_ALL) |
374 | { | |
86c6265d TT |
375 | /* Copy the argument text in case the command modifies it. */ |
376 | char *copy = xstrdup (arg); | |
377 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
bc9f0842 | 378 | |
47a80e90 | 379 | prevent_dont_repeat (); |
bc9f0842 | 380 | if (to_string) |
5da1313b JK |
381 | result = execute_command_to_string (copy, from_tty); |
382 | else | |
bc9f0842 | 383 | { |
5da1313b JK |
384 | result = NULL; |
385 | execute_command (copy, from_tty); | |
bc9f0842 | 386 | } |
d59b6f6c | 387 | |
86c6265d | 388 | do_cleanups (cleanup); |
d57a3c85 TJB |
389 | } |
390 | GDB_PY_HANDLE_EXCEPTION (except); | |
391 | ||
347bddb7 PA |
392 | /* Do any commands attached to breakpoint we stopped at. */ |
393 | bpstat_do_actions (); | |
d57a3c85 | 394 | |
bc9f0842 TT |
395 | if (result) |
396 | { | |
397 | PyObject *r = PyString_FromString (result); | |
398 | xfree (result); | |
399 | return r; | |
400 | } | |
d57a3c85 TJB |
401 | Py_RETURN_NONE; |
402 | } | |
403 | ||
cb2e07a6 PM |
404 | /* Implementation of gdb.solib_name (Long) -> String. |
405 | Returns the name of the shared library holding a given address, or None. */ | |
406 | ||
407 | static PyObject * | |
408 | gdbpy_solib_name (PyObject *self, PyObject *args) | |
409 | { | |
410 | char *soname; | |
411 | PyObject *str_obj; | |
74aedc46 TT |
412 | gdb_py_longest pc; |
413 | ||
414 | if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc)) | |
cb2e07a6 PM |
415 | return NULL; |
416 | ||
417 | soname = solib_name_from_address (current_program_space, pc); | |
418 | if (soname) | |
419 | str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL); | |
420 | else | |
421 | { | |
422 | str_obj = Py_None; | |
423 | Py_INCREF (Py_None); | |
424 | } | |
425 | ||
426 | return str_obj; | |
427 | } | |
428 | ||
429 | /* A Python function which is a wrapper for decode_line_1. */ | |
430 | ||
431 | static PyObject * | |
432 | gdbpy_decode_line (PyObject *self, PyObject *args) | |
433 | { | |
9a2b4c1b MS |
434 | struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to |
435 | appease gcc. */ | |
cb2e07a6 PM |
436 | struct symtab_and_line sal; |
437 | char *arg = NULL; | |
438 | char *copy = NULL; | |
439 | struct cleanup *cleanups; | |
440 | PyObject *result = NULL; | |
441 | PyObject *return_result = NULL; | |
442 | PyObject *unparsed = NULL; | |
443 | volatile struct gdb_exception except; | |
444 | ||
445 | if (! PyArg_ParseTuple (args, "|s", &arg)) | |
446 | return NULL; | |
447 | ||
448 | cleanups = ensure_python_env (get_current_arch (), current_language); | |
449 | ||
450 | TRY_CATCH (except, RETURN_MASK_ALL) | |
451 | { | |
452 | if (arg) | |
453 | { | |
454 | arg = xstrdup (arg); | |
455 | make_cleanup (xfree, arg); | |
456 | copy = arg; | |
58438ac1 | 457 | sals = decode_line_1 (©, 0, 0, 0, 0); |
cb2e07a6 PM |
458 | make_cleanup (xfree, sals.sals); |
459 | } | |
460 | else | |
461 | { | |
462 | set_default_source_symtab_and_line (); | |
463 | sal = get_current_source_symtab_and_line (); | |
464 | sals.sals = &sal; | |
465 | sals.nelts = 1; | |
466 | } | |
467 | } | |
468 | if (except.reason < 0) | |
469 | { | |
470 | do_cleanups (cleanups); | |
471 | /* We know this will always throw. */ | |
472 | GDB_PY_HANDLE_EXCEPTION (except); | |
473 | } | |
474 | ||
475 | if (sals.nelts) | |
476 | { | |
477 | int i; | |
478 | ||
479 | result = PyTuple_New (sals.nelts); | |
480 | if (! result) | |
481 | goto error; | |
482 | for (i = 0; i < sals.nelts; ++i) | |
483 | { | |
484 | PyObject *obj; | |
485 | char *str; | |
486 | ||
487 | obj = symtab_and_line_to_sal_object (sals.sals[i]); | |
488 | if (! obj) | |
489 | { | |
490 | Py_DECREF (result); | |
491 | goto error; | |
492 | } | |
493 | ||
494 | PyTuple_SetItem (result, i, obj); | |
495 | } | |
496 | } | |
497 | else | |
498 | { | |
499 | result = Py_None; | |
500 | Py_INCREF (Py_None); | |
501 | } | |
502 | ||
503 | return_result = PyTuple_New (2); | |
504 | if (! return_result) | |
505 | { | |
506 | Py_DECREF (result); | |
507 | goto error; | |
508 | } | |
509 | ||
510 | if (copy && strlen (copy) > 0) | |
511 | unparsed = PyString_FromString (copy); | |
512 | else | |
513 | { | |
514 | unparsed = Py_None; | |
515 | Py_INCREF (Py_None); | |
516 | } | |
517 | ||
518 | PyTuple_SetItem (return_result, 0, unparsed); | |
519 | PyTuple_SetItem (return_result, 1, result); | |
520 | ||
521 | do_cleanups (cleanups); | |
522 | ||
523 | return return_result; | |
524 | ||
525 | error: | |
526 | do_cleanups (cleanups); | |
527 | return NULL; | |
528 | } | |
529 | ||
57a1d736 TT |
530 | /* Parse a string and evaluate it as an expression. */ |
531 | static PyObject * | |
532 | gdbpy_parse_and_eval (PyObject *self, PyObject *args) | |
533 | { | |
534 | char *expr_str; | |
535 | struct value *result = NULL; | |
536 | volatile struct gdb_exception except; | |
537 | ||
538 | if (!PyArg_ParseTuple (args, "s", &expr_str)) | |
539 | return NULL; | |
540 | ||
541 | TRY_CATCH (except, RETURN_MASK_ALL) | |
542 | { | |
543 | result = parse_and_eval (expr_str); | |
544 | } | |
545 | GDB_PY_HANDLE_EXCEPTION (except); | |
546 | ||
547 | return value_to_value_object (result); | |
548 | } | |
549 | ||
973817a3 | 550 | /* Read a file as Python code. STREAM is the input file; FILE is the |
3f7b2faa DE |
551 | name of the file. |
552 | STREAM is not closed, that is the caller's responsibility. */ | |
973817a3 JB |
553 | |
554 | void | |
3f7b2faa | 555 | source_python_script (FILE *stream, const char *file) |
973817a3 | 556 | { |
eb5cda86 | 557 | struct cleanup *cleanup; |
973817a3 | 558 | |
eb5cda86 | 559 | cleanup = ensure_python_env (get_current_arch (), current_language); |
973817a3 | 560 | |
3a1d4620 DE |
561 | /* Note: If an exception occurs python will print the traceback and |
562 | clear the error indicator. */ | |
973817a3 JB |
563 | PyRun_SimpleFile (stream, file); |
564 | ||
eb5cda86 | 565 | do_cleanups (cleanup); |
973817a3 JB |
566 | } |
567 | ||
d57a3c85 TJB |
568 | \f |
569 | ||
ca5c20b6 PM |
570 | /* Posting and handling events. */ |
571 | ||
572 | /* A single event. */ | |
573 | struct gdbpy_event | |
574 | { | |
575 | /* The Python event. This is just a callable object. */ | |
576 | PyObject *event; | |
577 | /* The next event. */ | |
578 | struct gdbpy_event *next; | |
579 | }; | |
580 | ||
581 | /* All pending events. */ | |
582 | static struct gdbpy_event *gdbpy_event_list; | |
583 | /* The final link of the event list. */ | |
584 | static struct gdbpy_event **gdbpy_event_list_end; | |
585 | ||
586 | /* We use a file handler, and not an async handler, so that we can | |
587 | wake up the main thread even when it is blocked in poll(). */ | |
4a532131 | 588 | static struct serial *gdbpy_event_fds[2]; |
ca5c20b6 PM |
589 | |
590 | /* The file handler callback. This reads from the internal pipe, and | |
591 | then processes the Python event queue. This will always be run in | |
592 | the main gdb thread. */ | |
4a532131 | 593 | |
ca5c20b6 | 594 | static void |
4a532131 | 595 | gdbpy_run_events (struct serial *scb, void *context) |
ca5c20b6 PM |
596 | { |
597 | struct cleanup *cleanup; | |
ca5c20b6 PM |
598 | int r; |
599 | ||
600 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
601 | ||
4a532131 PA |
602 | /* Flush the fd. Do this before flushing the events list, so that |
603 | any new event post afterwards is sure to re-awake the event | |
604 | loop. */ | |
605 | while (serial_readchar (gdbpy_event_fds[0], 0) >= 0) | |
606 | ; | |
ca5c20b6 PM |
607 | |
608 | while (gdbpy_event_list) | |
609 | { | |
610 | /* Dispatching the event might push a new element onto the event | |
611 | loop, so we update here "atomically enough". */ | |
612 | struct gdbpy_event *item = gdbpy_event_list; | |
613 | gdbpy_event_list = gdbpy_event_list->next; | |
614 | if (gdbpy_event_list == NULL) | |
615 | gdbpy_event_list_end = &gdbpy_event_list; | |
616 | ||
617 | /* Ignore errors. */ | |
618 | if (PyObject_CallObject (item->event, NULL) == NULL) | |
619 | PyErr_Clear (); | |
620 | ||
621 | Py_DECREF (item->event); | |
622 | xfree (item); | |
623 | } | |
624 | ||
625 | do_cleanups (cleanup); | |
626 | } | |
627 | ||
628 | /* Submit an event to the gdb thread. */ | |
629 | static PyObject * | |
630 | gdbpy_post_event (PyObject *self, PyObject *args) | |
631 | { | |
632 | struct gdbpy_event *event; | |
633 | PyObject *func; | |
634 | int wakeup; | |
635 | ||
636 | if (!PyArg_ParseTuple (args, "O", &func)) | |
637 | return NULL; | |
638 | ||
639 | if (!PyCallable_Check (func)) | |
640 | { | |
641 | PyErr_SetString (PyExc_RuntimeError, | |
642 | _("Posted event is not callable")); | |
643 | return NULL; | |
644 | } | |
645 | ||
646 | Py_INCREF (func); | |
647 | ||
648 | /* From here until the end of the function, we have the GIL, so we | |
649 | can operate on our global data structures without worrying. */ | |
650 | wakeup = gdbpy_event_list == NULL; | |
651 | ||
652 | event = XNEW (struct gdbpy_event); | |
653 | event->event = func; | |
654 | event->next = NULL; | |
655 | *gdbpy_event_list_end = event; | |
656 | gdbpy_event_list_end = &event->next; | |
657 | ||
658 | /* Wake up gdb when needed. */ | |
659 | if (wakeup) | |
660 | { | |
661 | char c = 'q'; /* Anything. */ | |
4a532131 PA |
662 | |
663 | if (serial_write (gdbpy_event_fds[1], &c, 1)) | |
ca5c20b6 PM |
664 | return PyErr_SetFromErrno (PyExc_IOError); |
665 | } | |
666 | ||
667 | Py_RETURN_NONE; | |
668 | } | |
669 | ||
670 | /* Initialize the Python event handler. */ | |
671 | static void | |
672 | gdbpy_initialize_events (void) | |
673 | { | |
4a532131 | 674 | if (serial_pipe (gdbpy_event_fds) == 0) |
ca5c20b6 PM |
675 | { |
676 | gdbpy_event_list_end = &gdbpy_event_list; | |
4a532131 | 677 | serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL); |
ca5c20b6 PM |
678 | } |
679 | } | |
680 | ||
d57a3c85 TJB |
681 | /* Printing. */ |
682 | ||
683 | /* A python function to write a single string using gdb's filtered | |
99c3dc11 PM |
684 | output stream . The optional keyword STREAM can be used to write |
685 | to a particular stream. The default stream is to gdb_stdout. */ | |
686 | ||
d57a3c85 | 687 | static PyObject * |
99c3dc11 | 688 | gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 TJB |
689 | { |
690 | char *arg; | |
99c3dc11 PM |
691 | static char *keywords[] = {"text", "stream", NULL }; |
692 | int stream_type = 0; | |
693 | ||
694 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg, | |
695 | &stream_type)) | |
d57a3c85 | 696 | return NULL; |
99c3dc11 PM |
697 | |
698 | switch (stream_type) | |
699 | { | |
700 | case 1: | |
701 | { | |
702 | fprintf_filtered (gdb_stderr, "%s", arg); | |
703 | break; | |
704 | } | |
705 | case 2: | |
706 | { | |
707 | fprintf_filtered (gdb_stdlog, "%s", arg); | |
708 | break; | |
709 | } | |
710 | default: | |
711 | fprintf_filtered (gdb_stdout, "%s", arg); | |
712 | } | |
713 | ||
d57a3c85 TJB |
714 | Py_RETURN_NONE; |
715 | } | |
716 | ||
99c3dc11 PM |
717 | /* A python function to flush a gdb stream. The optional keyword |
718 | STREAM can be used to flush a particular stream. The default stream | |
719 | is gdb_stdout. */ | |
720 | ||
d57a3c85 | 721 | static PyObject * |
99c3dc11 | 722 | gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 723 | { |
99c3dc11 PM |
724 | static char *keywords[] = {"stream", NULL }; |
725 | int stream_type = 0; | |
726 | ||
727 | if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords, | |
728 | &stream_type)) | |
729 | return NULL; | |
730 | ||
731 | switch (stream_type) | |
732 | { | |
733 | case 1: | |
734 | { | |
735 | gdb_flush (gdb_stderr); | |
736 | break; | |
737 | } | |
738 | case 2: | |
739 | { | |
740 | gdb_flush (gdb_stdlog); | |
741 | break; | |
742 | } | |
743 | default: | |
744 | gdb_flush (gdb_stdout); | |
745 | } | |
746 | ||
d57a3c85 TJB |
747 | Py_RETURN_NONE; |
748 | } | |
749 | ||
750 | /* Print a python exception trace, or print nothing and clear the | |
751 | python exception, depending on gdbpy_should_print_stack. Only call | |
752 | this if a python exception is set. */ | |
753 | void | |
754 | gdbpy_print_stack (void) | |
755 | { | |
756 | if (gdbpy_should_print_stack) | |
0bf0f8c4 DE |
757 | { |
758 | PyErr_Print (); | |
759 | /* PyErr_Print doesn't necessarily end output with a newline. | |
760 | This works because Python's stdout/stderr is fed through | |
761 | printf_filtered. */ | |
762 | begin_line (); | |
763 | } | |
d57a3c85 TJB |
764 | else |
765 | PyErr_Clear (); | |
766 | } | |
767 | ||
89c73ade TT |
768 | \f |
769 | ||
fa33c3cd DE |
770 | /* Return the current Progspace. |
771 | There always is one. */ | |
772 | ||
773 | static PyObject * | |
774 | gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2) | |
775 | { | |
776 | PyObject *result; | |
777 | ||
778 | result = pspace_to_pspace_object (current_program_space); | |
779 | if (result) | |
780 | Py_INCREF (result); | |
781 | return result; | |
782 | } | |
783 | ||
784 | /* Return a sequence holding all the Progspaces. */ | |
785 | ||
786 | static PyObject * | |
787 | gdbpy_progspaces (PyObject *unused1, PyObject *unused2) | |
788 | { | |
789 | struct program_space *ps; | |
790 | PyObject *list; | |
791 | ||
792 | list = PyList_New (0); | |
793 | if (!list) | |
794 | return NULL; | |
795 | ||
796 | ALL_PSPACES (ps) | |
797 | { | |
798 | PyObject *item = pspace_to_pspace_object (ps); | |
d59b6f6c | 799 | |
fa33c3cd DE |
800 | if (!item || PyList_Append (list, item) == -1) |
801 | { | |
802 | Py_DECREF (list); | |
803 | return NULL; | |
804 | } | |
805 | } | |
806 | ||
807 | return list; | |
808 | } | |
809 | ||
810 | \f | |
811 | ||
89c73ade | 812 | /* The "current" objfile. This is set when gdb detects that a new |
8a1ea21f DE |
813 | objfile has been loaded. It is only set for the duration of a call to |
814 | source_python_script_for_objfile; it is NULL at other times. */ | |
89c73ade TT |
815 | static struct objfile *gdbpy_current_objfile; |
816 | ||
8a1ea21f DE |
817 | /* Set the current objfile to OBJFILE and then read STREAM,FILE as |
818 | Python code. */ | |
89c73ade | 819 | |
8a1ea21f DE |
820 | void |
821 | source_python_script_for_objfile (struct objfile *objfile, | |
822 | FILE *stream, const char *file) | |
89c73ade | 823 | { |
89c73ade TT |
824 | struct cleanup *cleanups; |
825 | ||
d452c4bc | 826 | cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
827 | gdbpy_current_objfile = objfile; |
828 | ||
3a1d4620 DE |
829 | /* Note: If an exception occurs python will print the traceback and |
830 | clear the error indicator. */ | |
831 | PyRun_SimpleFile (stream, file); | |
89c73ade TT |
832 | |
833 | do_cleanups (cleanups); | |
834 | gdbpy_current_objfile = NULL; | |
89c73ade TT |
835 | } |
836 | ||
837 | /* Return the current Objfile, or None if there isn't one. */ | |
8a1ea21f | 838 | |
89c73ade TT |
839 | static PyObject * |
840 | gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) | |
841 | { | |
842 | PyObject *result; | |
843 | ||
844 | if (! gdbpy_current_objfile) | |
845 | Py_RETURN_NONE; | |
846 | ||
847 | result = objfile_to_objfile_object (gdbpy_current_objfile); | |
848 | if (result) | |
849 | Py_INCREF (result); | |
850 | return result; | |
851 | } | |
852 | ||
853 | /* Return a sequence holding all the Objfiles. */ | |
fa33c3cd | 854 | |
89c73ade TT |
855 | static PyObject * |
856 | gdbpy_objfiles (PyObject *unused1, PyObject *unused2) | |
857 | { | |
858 | struct objfile *objf; | |
859 | PyObject *list; | |
860 | ||
861 | list = PyList_New (0); | |
862 | if (!list) | |
863 | return NULL; | |
864 | ||
865 | ALL_OBJFILES (objf) | |
866 | { | |
867 | PyObject *item = objfile_to_objfile_object (objf); | |
d59b6f6c | 868 | |
89c73ade TT |
869 | if (!item || PyList_Append (list, item) == -1) |
870 | { | |
871 | Py_DECREF (list); | |
872 | return NULL; | |
873 | } | |
874 | } | |
875 | ||
876 | return list; | |
877 | } | |
878 | ||
d57a3c85 TJB |
879 | #else /* HAVE_PYTHON */ |
880 | ||
881 | /* Dummy implementation of the gdb "python" command. */ | |
882 | ||
883 | static void | |
884 | python_command (char *arg, int from_tty) | |
885 | { | |
886 | while (arg && *arg && isspace (*arg)) | |
887 | ++arg; | |
888 | if (arg && *arg) | |
889 | error (_("Python scripting is not supported in this copy of GDB.")); | |
890 | else | |
891 | { | |
892 | struct command_line *l = get_command_line (python_control, ""); | |
893 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
d59b6f6c | 894 | |
d57a3c85 TJB |
895 | execute_control_command_untraced (l); |
896 | do_cleanups (cleanups); | |
897 | } | |
898 | } | |
899 | ||
900 | void | |
901 | eval_python_from_control_command (struct command_line *cmd) | |
902 | { | |
903 | error (_("Python scripting is not supported in this copy of GDB.")); | |
904 | } | |
905 | ||
973817a3 | 906 | void |
3f7b2faa | 907 | source_python_script (FILE *stream, const char *file) |
973817a3 | 908 | { |
973817a3 JB |
909 | throw_error (UNSUPPORTED_ERROR, |
910 | _("Python scripting is not supported in this copy of GDB.")); | |
911 | } | |
912 | ||
7371cf6d PM |
913 | int |
914 | gdbpy_should_stop (struct breakpoint_object *bp_obj) | |
915 | { | |
916 | internal_error (__FILE__, __LINE__, | |
917 | _("gdbpy_should_stop called when Python scripting is " \ | |
918 | "not supported.")); | |
919 | } | |
920 | ||
921 | int | |
922 | gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj) | |
923 | { | |
924 | internal_error (__FILE__, __LINE__, | |
925 | _("gdbpy_breakpoint_has_py_cond called when Python " \ | |
926 | "scripting is not supported.")); | |
927 | } | |
928 | ||
d57a3c85 TJB |
929 | #endif /* HAVE_PYTHON */ |
930 | ||
931 | \f | |
932 | ||
933 | /* Lists for 'maint set python' commands. */ | |
934 | ||
8a1ea21f DE |
935 | struct cmd_list_element *set_python_list; |
936 | struct cmd_list_element *show_python_list; | |
d57a3c85 TJB |
937 | |
938 | /* Function for use by 'maint set python' prefix command. */ | |
939 | ||
940 | static void | |
941 | set_python (char *args, int from_tty) | |
942 | { | |
943 | help_list (set_python_list, "maintenance set python ", -1, gdb_stdout); | |
944 | } | |
945 | ||
946 | /* Function for use by 'maint show python' prefix command. */ | |
947 | ||
948 | static void | |
949 | show_python (char *args, int from_tty) | |
950 | { | |
951 | cmd_show_list (show_python_list, from_tty, ""); | |
952 | } | |
953 | ||
954 | /* Initialize the Python code. */ | |
955 | ||
2c0b251b PA |
956 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
957 | extern initialize_file_ftype _initialize_python; | |
958 | ||
d57a3c85 TJB |
959 | void |
960 | _initialize_python (void) | |
961 | { | |
962 | add_com ("python", class_obscure, python_command, | |
963 | #ifdef HAVE_PYTHON | |
964 | _("\ | |
965 | Evaluate a Python command.\n\ | |
966 | \n\ | |
967 | The command can be given as an argument, for instance:\n\ | |
968 | \n\ | |
969 | python print 23\n\ | |
970 | \n\ | |
971 | If no argument is given, the following lines are read and used\n\ | |
972 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
973 | the end of the command.") | |
974 | #else /* HAVE_PYTHON */ | |
975 | _("\ | |
976 | Evaluate a Python command.\n\ | |
977 | \n\ | |
978 | Python scripting is not supported in this copy of GDB.\n\ | |
979 | This command is only a placeholder.") | |
980 | #endif /* HAVE_PYTHON */ | |
981 | ); | |
982 | ||
983 | add_prefix_cmd ("python", no_class, show_python, | |
984 | _("Prefix command for python maintenance settings."), | |
509238d6 | 985 | &show_python_list, "maintenance show python ", 0, |
d57a3c85 TJB |
986 | &maintenance_show_cmdlist); |
987 | add_prefix_cmd ("python", no_class, set_python, | |
988 | _("Prefix command for python maintenance settings."), | |
509238d6 | 989 | &set_python_list, "maintenance set python ", 0, |
d57a3c85 TJB |
990 | &maintenance_set_cmdlist); |
991 | ||
992 | add_setshow_boolean_cmd ("print-stack", class_maintenance, | |
993 | &gdbpy_should_print_stack, _("\ | |
994 | Enable or disable printing of Python stack dump on error."), _("\ | |
995 | Show whether Python stack will be printed on error."), _("\ | |
996 | Enables or disables printing of Python stack traces."), | |
997 | NULL, NULL, | |
998 | &set_python_list, | |
999 | &show_python_list); | |
1000 | ||
1001 | #ifdef HAVE_PYTHON | |
0c4a4063 DE |
1002 | #ifdef WITH_PYTHON_PATH |
1003 | /* Work around problem where python gets confused about where it is, | |
1004 | and then can't find its libraries, etc. | |
1005 | NOTE: Python assumes the following layout: | |
1006 | /foo/bin/python | |
1007 | /foo/lib/pythonX.Y/... | |
1008 | This must be done before calling Py_Initialize. */ | |
1009 | Py_SetProgramName (concat (ldirname (python_libdir), SLASH_STRING, "bin", | |
1010 | SLASH_STRING, "python", NULL)); | |
1011 | #endif | |
1012 | ||
d57a3c85 | 1013 | Py_Initialize (); |
ca30a762 | 1014 | PyEval_InitThreads (); |
d57a3c85 TJB |
1015 | |
1016 | gdb_module = Py_InitModule ("gdb", GdbMethods); | |
1017 | ||
1018 | /* The casts to (char*) are for python 2.4. */ | |
1019 | PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version); | |
1020 | PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name); | |
9a2b4c1b MS |
1021 | PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", |
1022 | (char*) target_name); | |
f17618ea | 1023 | |
99c3dc11 PM |
1024 | /* Add stream constants. */ |
1025 | PyModule_AddIntConstant (gdb_module, "STDOUT", 0); | |
1026 | PyModule_AddIntConstant (gdb_module, "STDERR", 1); | |
1027 | PyModule_AddIntConstant (gdb_module, "STDLOG", 2); | |
1028 | ||
f17618ea DE |
1029 | /* gdb.parameter ("data-directory") doesn't necessarily exist when the python |
1030 | script below is run (depending on order of _initialize_* functions). | |
1031 | Define the initial value of gdb.PYTHONDIR here. */ | |
b14285f6 JB |
1032 | { |
1033 | char *gdb_pythondir; | |
1034 | ||
1035 | gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL); | |
1036 | PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir); | |
1037 | xfree (gdb_pythondir); | |
1038 | } | |
d57a3c85 | 1039 | |
621c8364 TT |
1040 | gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL); |
1041 | PyModule_AddObject (gdb_module, "error", gdbpy_gdb_error); | |
1042 | ||
1043 | gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError", | |
1044 | gdbpy_gdb_error, NULL); | |
1045 | PyModule_AddObject (gdb_module, "MemoryError", gdbpy_gdb_memory_error); | |
1046 | ||
07ca107c DE |
1047 | gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL); |
1048 | PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc); | |
1049 | ||
8a1ea21f | 1050 | gdbpy_initialize_auto_load (); |
a08702d6 | 1051 | gdbpy_initialize_values (); |
f8f6f20b | 1052 | gdbpy_initialize_frames (); |
d8906c6f | 1053 | gdbpy_initialize_commands (); |
f3e9a817 PM |
1054 | gdbpy_initialize_symbols (); |
1055 | gdbpy_initialize_symtabs (); | |
1056 | gdbpy_initialize_blocks (); | |
bc3b79fd | 1057 | gdbpy_initialize_functions (); |
d7b32ed3 | 1058 | gdbpy_initialize_parameters (); |
2c74e833 | 1059 | gdbpy_initialize_types (); |
fa33c3cd | 1060 | gdbpy_initialize_pspace (); |
89c73ade | 1061 | gdbpy_initialize_objfile (); |
adc36818 | 1062 | gdbpy_initialize_breakpoints (); |
be759fcf | 1063 | gdbpy_initialize_lazy_string (); |
595939de PM |
1064 | gdbpy_initialize_thread (); |
1065 | gdbpy_initialize_inferior (); | |
ca5c20b6 | 1066 | gdbpy_initialize_events (); |
a08702d6 | 1067 | |
505500db SW |
1068 | gdbpy_initialize_eventregistry (); |
1069 | gdbpy_initialize_py_events (); | |
1070 | gdbpy_initialize_event (); | |
1071 | gdbpy_initialize_stop_event (); | |
1072 | gdbpy_initialize_signal_event (); | |
1073 | gdbpy_initialize_breakpoint_event (); | |
1074 | gdbpy_initialize_continue_event (); | |
1075 | gdbpy_initialize_exited_event (); | |
1076 | gdbpy_initialize_thread_event (); | |
1077 | ||
d57a3c85 | 1078 | PyRun_SimpleString ("import gdb"); |
a6bac58e | 1079 | PyRun_SimpleString ("gdb.pretty_printers = []"); |
d57a3c85 | 1080 | |
a6bac58e TT |
1081 | gdbpy_to_string_cst = PyString_FromString ("to_string"); |
1082 | gdbpy_children_cst = PyString_FromString ("children"); | |
1083 | gdbpy_display_hint_cst = PyString_FromString ("display_hint"); | |
d8906c6f | 1084 | gdbpy_doc_cst = PyString_FromString ("__doc__"); |
967cf477 | 1085 | gdbpy_enabled_cst = PyString_FromString ("enabled"); |
fb6a3ed3 | 1086 | gdbpy_value_cst = PyString_FromString ("value"); |
d8906c6f | 1087 | |
9dea9163 DE |
1088 | /* Release the GIL while gdb runs. */ |
1089 | PyThreadState_Swap (NULL); | |
1090 | PyEval_ReleaseLock (); | |
1091 | ||
1092 | #endif /* HAVE_PYTHON */ | |
1093 | } | |
1094 | ||
1095 | #ifdef HAVE_PYTHON | |
1096 | ||
1097 | /* Perform the remaining python initializations. | |
1098 | These must be done after GDB is at least mostly initialized. | |
1099 | E.g., The "info pretty-printer" command needs the "info" prefix | |
1100 | command installed. */ | |
1101 | ||
1102 | void | |
1103 | finish_python_initialization (void) | |
1104 | { | |
1105 | struct cleanup *cleanup; | |
1106 | ||
1107 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
f17618ea | 1108 | |
d57a3c85 | 1109 | PyRun_SimpleString ("\ |
f17618ea | 1110 | import os\n\ |
d57a3c85 | 1111 | import sys\n\ |
f17618ea | 1112 | \n\ |
d57a3c85 TJB |
1113 | class GdbOutputFile:\n\ |
1114 | def close(self):\n\ | |
1115 | # Do nothing.\n\ | |
1116 | return None\n\ | |
1117 | \n\ | |
1118 | def isatty(self):\n\ | |
1119 | return False\n\ | |
1120 | \n\ | |
1121 | def write(self, s):\n\ | |
99c3dc11 | 1122 | gdb.write(s, stream=gdb.STDOUT)\n \ |
d57a3c85 TJB |
1123 | \n\ |
1124 | def writelines(self, iterable):\n\ | |
1125 | for line in iterable:\n\ | |
1126 | self.write(line)\n\ | |
1127 | \n\ | |
1128 | def flush(self):\n\ | |
1129 | gdb.flush()\n\ | |
1130 | \n\ | |
d57a3c85 | 1131 | sys.stdout = GdbOutputFile()\n\ |
b14285f6 | 1132 | \n\ |
99c3dc11 PM |
1133 | class GdbOutputErrorFile:\n\ |
1134 | def close(self):\n\ | |
1135 | # Do nothing.\n\ | |
1136 | return None\n\ | |
1137 | \n\ | |
1138 | def isatty(self):\n\ | |
1139 | return False\n\ | |
1140 | \n\ | |
1141 | def write(self, s):\n\ | |
1142 | gdb.write(s, stream=gdb.STDERR)\n \ | |
1143 | \n\ | |
1144 | def writelines(self, iterable):\n\ | |
1145 | for line in iterable:\n\ | |
1146 | self.write(line)\n \ | |
1147 | \n\ | |
1148 | def flush(self):\n\ | |
1149 | gdb.flush()\n\ | |
1150 | \n\ | |
1151 | sys.stderr = GdbOutputErrorFile()\n\ | |
1152 | \n\ | |
f17618ea DE |
1153 | # Ideally this would live in the gdb module, but it's intentionally written\n\ |
1154 | # in python, and we need this to bootstrap the gdb module.\n\ | |
1155 | \n\ | |
1156 | def GdbSetPythonDirectory (dir):\n\ | |
1157 | \"Set gdb.PYTHONDIR and update sys.path,etc.\"\n\ | |
1158 | old_dir = gdb.PYTHONDIR\n\ | |
1159 | gdb.PYTHONDIR = dir\n\ | |
1160 | # GDB's python scripts are stored inside gdb.PYTHONDIR. So insert\n\ | |
1161 | # that directory name at the start of sys.path to allow the Python\n\ | |
1162 | # interpreter to find them.\n\ | |
1163 | if old_dir in sys.path:\n\ | |
1164 | sys.path.remove (old_dir)\n\ | |
1165 | sys.path.insert (0, gdb.PYTHONDIR)\n\ | |
1166 | \n\ | |
1167 | # Tell python where to find submodules of gdb.\n\ | |
1168 | gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\ | |
1169 | \n\ | |
1170 | # The gdb module is implemented in C rather than in Python. As a result,\n\ | |
1171 | # the associated __init.py__ script is not not executed by default when\n\ | |
1172 | # the gdb module gets imported. Execute that script manually if it\n\ | |
1173 | # exists.\n\ | |
1174 | ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\ | |
1175 | if os.path.exists (ipy):\n\ | |
1176 | execfile (ipy)\n\ | |
b14285f6 | 1177 | \n\ |
f17618ea DE |
1178 | # Install the default gdb.PYTHONDIR.\n\ |
1179 | GdbSetPythonDirectory (gdb.PYTHONDIR)\n\ | |
d57a3c85 | 1180 | "); |
ca30a762 | 1181 | |
9dea9163 DE |
1182 | do_cleanups (cleanup); |
1183 | } | |
ca30a762 | 1184 | |
d57a3c85 | 1185 | #endif /* HAVE_PYTHON */ |
12453b93 TJB |
1186 | |
1187 | \f | |
1188 | ||
9dea9163 | 1189 | #ifdef HAVE_PYTHON |
12453b93 TJB |
1190 | |
1191 | static PyMethodDef GdbMethods[] = | |
1192 | { | |
1193 | { "history", gdbpy_history, METH_VARARGS, | |
1194 | "Get a value from history" }, | |
bc9f0842 | 1195 | { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 1196 | "Execute a gdb command" }, |
8f500870 | 1197 | { "parameter", gdbpy_parameter, METH_VARARGS, |
12453b93 TJB |
1198 | "Return a gdb parameter's value" }, |
1199 | ||
adc36818 PM |
1200 | { "breakpoints", gdbpy_breakpoints, METH_NOARGS, |
1201 | "Return a tuple of all breakpoint objects" }, | |
1202 | ||
b6313243 TT |
1203 | { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS, |
1204 | "Find the default visualizer for a Value." }, | |
1205 | ||
fa33c3cd DE |
1206 | { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS, |
1207 | "Return the current Progspace." }, | |
1208 | { "progspaces", gdbpy_progspaces, METH_NOARGS, | |
1209 | "Return a sequence of all progspaces." }, | |
1210 | ||
89c73ade TT |
1211 | { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, |
1212 | "Return the current Objfile being loaded, or None." }, | |
1213 | { "objfiles", gdbpy_objfiles, METH_NOARGS, | |
1214 | "Return a sequence of all loaded objfiles." }, | |
1215 | ||
d8e22779 TT |
1216 | { "newest_frame", gdbpy_newest_frame, METH_NOARGS, |
1217 | "newest_frame () -> gdb.Frame.\n\ | |
1218 | Return the newest frame object." }, | |
f8f6f20b TJB |
1219 | { "selected_frame", gdbpy_selected_frame, METH_NOARGS, |
1220 | "selected_frame () -> gdb.Frame.\n\ | |
1221 | Return the selected frame object." }, | |
1222 | { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS, | |
1223 | "stop_reason_string (Integer) -> String.\n\ | |
1224 | Return a string explaining unwind stop reason." }, | |
1225 | ||
2c74e833 TT |
1226 | { "lookup_type", (PyCFunction) gdbpy_lookup_type, |
1227 | METH_VARARGS | METH_KEYWORDS, | |
1228 | "lookup_type (name [, block]) -> type\n\ | |
1229 | Return a Type corresponding to the given name." }, | |
f3e9a817 PM |
1230 | { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol, |
1231 | METH_VARARGS | METH_KEYWORDS, | |
1232 | "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\ | |
1233 | Return a tuple with the symbol corresponding to the given name (or None) and\n\ | |
1234 | a boolean indicating if name is a field of the current implied argument\n\ | |
1235 | `this' (when the current language is object-oriented)." }, | |
6e6fbe60 DE |
1236 | { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol, |
1237 | METH_VARARGS | METH_KEYWORDS, | |
1238 | "lookup_global_symbol (name [, domain]) -> symbol\n\ | |
1239 | Return the symbol corresponding to the given name (or None)." }, | |
f3e9a817 PM |
1240 | { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS, |
1241 | "Return the block containing the given pc value, or None." }, | |
cb2e07a6 PM |
1242 | { "solib_name", gdbpy_solib_name, METH_VARARGS, |
1243 | "solib_name (Long) -> String.\n\ | |
1244 | Return the name of the shared library holding a given address, or None." }, | |
1245 | { "decode_line", gdbpy_decode_line, METH_VARARGS, | |
1246 | "decode_line (String) -> Tuple. Decode a string argument the way\n\ | |
1247 | that 'break' or 'edit' does. Return a tuple containing two elements.\n\ | |
1248 | The first element contains any unparsed portion of the String parameter\n\ | |
1249 | (or None if the string was fully parsed). The second element contains\n\ | |
1250 | a tuple that contains all the locations that match, represented as\n\ | |
1251 | gdb.Symtab_and_line objects (or None)."}, | |
57a1d736 TT |
1252 | { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, |
1253 | "parse_and_eval (String) -> Value.\n\ | |
1254 | Parse String as an expression, evaluate it, and return the result as a Value." | |
1255 | }, | |
1256 | ||
ca5c20b6 PM |
1257 | { "post_event", gdbpy_post_event, METH_VARARGS, |
1258 | "Post an event into gdb's event loop." }, | |
1259 | ||
f870a310 TT |
1260 | { "target_charset", gdbpy_target_charset, METH_NOARGS, |
1261 | "target_charset () -> string.\n\ | |
1262 | Return the name of the current target charset." }, | |
1263 | { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS, | |
1264 | "target_wide_charset () -> string.\n\ | |
1265 | Return the name of the current target wide charset." }, | |
1266 | ||
07ca107c DE |
1267 | { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS, |
1268 | "string_to_argv (String) -> Array.\n\ | |
1269 | Parse String and return an argv-like array.\n\ | |
1270 | Arguments are separate by spaces and may be quoted." | |
1271 | }, | |
99c3dc11 | 1272 | { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 1273 | "Write a string using gdb's filtered stream." }, |
99c3dc11 | 1274 | { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 1275 | "Flush gdb's filtered stdout stream." }, |
595939de PM |
1276 | { "selected_thread", gdbpy_selected_thread, METH_NOARGS, |
1277 | "selected_thread () -> gdb.InferiorThread.\n\ | |
1278 | Return the selected thread object." }, | |
1279 | { "inferiors", gdbpy_inferiors, METH_NOARGS, | |
1280 | "inferiors () -> (gdb.Inferior, ...).\n\ | |
1281 | Return a tuple containing all inferiors." }, | |
12453b93 TJB |
1282 | {NULL, NULL, 0, NULL} |
1283 | }; | |
1284 | ||
1285 | #endif /* HAVE_PYTHON */ |