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