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" |
3ab1ec27 | 33 | #include "readline/tilde.h" |
7371cf6d | 34 | #include "python.h" |
d57a3c85 TJB |
35 | |
36 | #include <ctype.h> | |
37 | ||
80b6e756 PM |
38 | /* Declared constants and enum for python stack printing. */ |
39 | static const char python_excp_none[] = "none"; | |
40 | static const char python_excp_full[] = "full"; | |
41 | static const char python_excp_message[] = "message"; | |
42 | ||
43 | /* "set python print-stack" choices. */ | |
44 | static const char *python_excp_enums[] = | |
45 | { | |
46 | python_excp_none, | |
47 | python_excp_full, | |
48 | python_excp_message, | |
49 | NULL | |
50 | }; | |
51 | ||
52 | /* The exception printing variable. 'full' if we want to print the | |
53 | error message and stack, 'none' if we want to print nothing, and | |
54 | 'message' if we only want to print the error message. 'message' is | |
55 | the default. */ | |
56 | static const char *gdbpy_should_print_stack = python_excp_message; | |
d57a3c85 TJB |
57 | |
58 | #ifdef HAVE_PYTHON | |
59 | ||
d57a3c85 TJB |
60 | #include "libiberty.h" |
61 | #include "cli/cli-decode.h" | |
62 | #include "charset.h" | |
63 | #include "top.h" | |
cb2e07a6 | 64 | #include "solib.h" |
d57a3c85 | 65 | #include "python-internal.h" |
cb2e07a6 PM |
66 | #include "linespec.h" |
67 | #include "source.h" | |
d57a3c85 TJB |
68 | #include "version.h" |
69 | #include "target.h" | |
70 | #include "gdbthread.h" | |
d17b6f81 | 71 | #include "observer.h" |
b4a14fd0 | 72 | #include "interps.h" |
d57a3c85 | 73 | |
12453b93 | 74 | static PyMethodDef GdbMethods[]; |
d57a3c85 TJB |
75 | |
76 | PyObject *gdb_module; | |
77 | ||
a6bac58e TT |
78 | /* Some string constants we may wish to use. */ |
79 | PyObject *gdbpy_to_string_cst; | |
80 | PyObject *gdbpy_children_cst; | |
81 | PyObject *gdbpy_display_hint_cst; | |
d8906c6f | 82 | PyObject *gdbpy_doc_cst; |
967cf477 | 83 | PyObject *gdbpy_enabled_cst; |
fb6a3ed3 | 84 | PyObject *gdbpy_value_cst; |
d8906c6f | 85 | |
07ca107c DE |
86 | /* The GdbError exception. */ |
87 | PyObject *gdbpy_gdberror_exc; | |
d452c4bc | 88 | |
621c8364 TT |
89 | /* The `gdb.error' base class. */ |
90 | PyObject *gdbpy_gdb_error; | |
91 | ||
92 | /* The `gdb.MemoryError' exception. */ | |
93 | PyObject *gdbpy_gdb_memory_error; | |
94 | ||
d452c4bc UW |
95 | /* Architecture and language to be used in callbacks from |
96 | the Python interpreter. */ | |
97 | struct gdbarch *python_gdbarch; | |
98 | const struct language_defn *python_language; | |
99 | ||
100 | /* Restore global language and architecture and Python GIL state | |
101 | when leaving the Python interpreter. */ | |
102 | ||
103 | struct python_env | |
104 | { | |
105 | PyGILState_STATE state; | |
106 | struct gdbarch *gdbarch; | |
107 | const struct language_defn *language; | |
8dc78533 | 108 | PyObject *error_type, *error_value, *error_traceback; |
d452c4bc UW |
109 | }; |
110 | ||
111 | static void | |
112 | restore_python_env (void *p) | |
113 | { | |
114 | struct python_env *env = (struct python_env *)p; | |
d59b6f6c | 115 | |
8dc78533 JK |
116 | /* Leftover Python error is forbidden by Python Exception Handling. */ |
117 | if (PyErr_Occurred ()) | |
118 | { | |
119 | /* This order is similar to the one calling error afterwards. */ | |
120 | gdbpy_print_stack (); | |
121 | warning (_("internal error: Unhandled Python exception")); | |
122 | } | |
123 | ||
124 | PyErr_Restore (env->error_type, env->error_value, env->error_traceback); | |
125 | ||
d452c4bc UW |
126 | PyGILState_Release (env->state); |
127 | python_gdbarch = env->gdbarch; | |
128 | python_language = env->language; | |
129 | xfree (env); | |
130 | } | |
131 | ||
132 | /* Called before entering the Python interpreter to install the | |
133 | current language and architecture to be used for Python values. */ | |
134 | ||
135 | struct cleanup * | |
136 | ensure_python_env (struct gdbarch *gdbarch, | |
137 | const struct language_defn *language) | |
138 | { | |
139 | struct python_env *env = xmalloc (sizeof *env); | |
140 | ||
141 | env->state = PyGILState_Ensure (); | |
142 | env->gdbarch = python_gdbarch; | |
143 | env->language = python_language; | |
144 | ||
145 | python_gdbarch = gdbarch; | |
146 | python_language = language; | |
147 | ||
8dc78533 JK |
148 | /* Save it and ensure ! PyErr_Occurred () afterwards. */ |
149 | PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback); | |
150 | ||
d452c4bc UW |
151 | return make_cleanup (restore_python_env, env); |
152 | } | |
153 | ||
7ed7d719 JB |
154 | /* A wrapper around PyRun_SimpleFile. FILENAME is the name of |
155 | the Python script to run. | |
156 | ||
157 | One of the parameters of PyRun_SimpleFile is a FILE *. | |
158 | The problem is that type FILE is extremely system and compiler | |
159 | dependent. So, unless the Python library has been compiled using | |
160 | the same build environment as GDB, we run the risk of getting | |
161 | a crash due to inconsistencies between the definition used by GDB, | |
162 | and the definition used by Python. A mismatch can very likely | |
163 | lead to a crash. | |
164 | ||
165 | There is also the situation where the Python library and GDB | |
166 | are using two different versions of the C runtime library. | |
167 | This is particularly visible on Windows, where few users would | |
168 | build Python themselves (this is no trivial task on this platform), | |
169 | and thus use binaries built by someone else instead. Python, | |
170 | being built with VC, would use one version of the msvcr DLL | |
171 | (Eg. msvcr100.dll), while MinGW uses msvcrt.dll. A FILE * | |
172 | from one runtime does not necessarily operate correctly in | |
173 | the other runtime. | |
174 | ||
175 | To work around this potential issue, we create the FILE object | |
176 | using Python routines, thus making sure that it is compatible | |
177 | with the Python library. */ | |
178 | ||
179 | static void | |
180 | python_run_simple_file (const char *filename) | |
181 | { | |
3ab1ec27 | 182 | char *full_path; |
7ed7d719 JB |
183 | PyObject *python_file; |
184 | struct cleanup *cleanup; | |
185 | ||
3ab1ec27 PM |
186 | /* Because we have a string for a filename, and are using Python to |
187 | open the file, we need to expand any tilde in the path first. */ | |
188 | full_path = tilde_expand (filename); | |
189 | cleanup = make_cleanup (xfree, full_path); | |
190 | python_file = PyFile_FromString (full_path, "r"); | |
191 | if (! python_file) | |
192 | { | |
193 | do_cleanups (cleanup); | |
194 | gdbpy_print_stack (); | |
195 | error (_("Error while opening file: %s"), full_path); | |
196 | } | |
197 | ||
7ed7d719 JB |
198 | make_cleanup_py_decref (python_file); |
199 | PyRun_SimpleFile (PyFile_AsFile (python_file), filename); | |
200 | do_cleanups (cleanup); | |
201 | } | |
d452c4bc | 202 | |
d57a3c85 TJB |
203 | /* Given a command_line, return a command string suitable for passing |
204 | to Python. Lines in the string are separated by newlines. The | |
205 | return value is allocated using xmalloc and the caller is | |
206 | responsible for freeing it. */ | |
207 | ||
208 | static char * | |
209 | compute_python_string (struct command_line *l) | |
210 | { | |
211 | struct command_line *iter; | |
212 | char *script = NULL; | |
213 | int size = 0; | |
214 | int here; | |
215 | ||
216 | for (iter = l; iter; iter = iter->next) | |
217 | size += strlen (iter->line) + 1; | |
218 | ||
219 | script = xmalloc (size + 1); | |
220 | here = 0; | |
221 | for (iter = l; iter; iter = iter->next) | |
222 | { | |
223 | int len = strlen (iter->line); | |
d59b6f6c | 224 | |
d57a3c85 TJB |
225 | strcpy (&script[here], iter->line); |
226 | here += len; | |
227 | script[here++] = '\n'; | |
228 | } | |
229 | script[here] = '\0'; | |
230 | return script; | |
231 | } | |
232 | ||
233 | /* Take a command line structure representing a 'python' command, and | |
234 | evaluate its body using the Python interpreter. */ | |
235 | ||
236 | void | |
237 | eval_python_from_control_command (struct command_line *cmd) | |
238 | { | |
12453b93 | 239 | int ret; |
d57a3c85 | 240 | char *script; |
ca30a762 | 241 | struct cleanup *cleanup; |
d57a3c85 TJB |
242 | |
243 | if (cmd->body_count != 1) | |
244 | error (_("Invalid \"python\" block structure.")); | |
245 | ||
d452c4bc | 246 | cleanup = ensure_python_env (get_current_arch (), current_language); |
ca30a762 | 247 | |
d57a3c85 | 248 | script = compute_python_string (cmd->body_list[0]); |
12453b93 | 249 | ret = PyRun_SimpleString (script); |
d57a3c85 | 250 | xfree (script); |
12453b93 | 251 | if (ret) |
80b6e756 | 252 | error (_("Error while executing Python code.")); |
ca30a762 TT |
253 | |
254 | do_cleanups (cleanup); | |
d57a3c85 TJB |
255 | } |
256 | ||
257 | /* Implementation of the gdb "python" command. */ | |
258 | ||
259 | static void | |
260 | python_command (char *arg, int from_tty) | |
261 | { | |
ca30a762 | 262 | struct cleanup *cleanup; |
ca30a762 | 263 | |
d59b6f6c | 264 | cleanup = ensure_python_env (get_current_arch (), current_language); |
b4a14fd0 PA |
265 | |
266 | make_cleanup_restore_integer (&interpreter_async); | |
267 | interpreter_async = 0; | |
268 | ||
d57a3c85 TJB |
269 | while (arg && *arg && isspace (*arg)) |
270 | ++arg; | |
271 | if (arg && *arg) | |
272 | { | |
12453b93 | 273 | if (PyRun_SimpleString (arg)) |
80b6e756 | 274 | error (_("Error while executing Python code.")); |
d57a3c85 TJB |
275 | } |
276 | else | |
277 | { | |
278 | struct command_line *l = get_command_line (python_control, ""); | |
d59b6f6c | 279 | |
ca30a762 | 280 | make_cleanup_free_command_lines (&l); |
d57a3c85 | 281 | execute_control_command_untraced (l); |
d57a3c85 | 282 | } |
ca30a762 TT |
283 | |
284 | do_cleanups (cleanup); | |
d57a3c85 TJB |
285 | } |
286 | ||
287 | \f | |
288 | ||
289 | /* Transform a gdb parameters's value into a Python value. May return | |
290 | NULL (and set a Python exception) on error. Helper function for | |
291 | get_parameter. */ | |
d7b32ed3 PM |
292 | PyObject * |
293 | gdbpy_parameter_value (enum var_types type, void *var) | |
d57a3c85 | 294 | { |
d7b32ed3 | 295 | switch (type) |
d57a3c85 TJB |
296 | { |
297 | case var_string: | |
298 | case var_string_noescape: | |
299 | case var_optional_filename: | |
300 | case var_filename: | |
301 | case var_enum: | |
302 | { | |
d7b32ed3 | 303 | char *str = * (char **) var; |
d59b6f6c | 304 | |
d57a3c85 TJB |
305 | if (! str) |
306 | str = ""; | |
307 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
308 | } | |
309 | ||
310 | case var_boolean: | |
311 | { | |
d7b32ed3 | 312 | if (* (int *) var) |
d57a3c85 TJB |
313 | Py_RETURN_TRUE; |
314 | else | |
315 | Py_RETURN_FALSE; | |
316 | } | |
317 | ||
318 | case var_auto_boolean: | |
319 | { | |
d7b32ed3 | 320 | enum auto_boolean ab = * (enum auto_boolean *) var; |
d59b6f6c | 321 | |
d57a3c85 TJB |
322 | if (ab == AUTO_BOOLEAN_TRUE) |
323 | Py_RETURN_TRUE; | |
324 | else if (ab == AUTO_BOOLEAN_FALSE) | |
325 | Py_RETURN_FALSE; | |
326 | else | |
327 | Py_RETURN_NONE; | |
328 | } | |
329 | ||
330 | case var_integer: | |
d7b32ed3 | 331 | if ((* (int *) var) == INT_MAX) |
d57a3c85 TJB |
332 | Py_RETURN_NONE; |
333 | /* Fall through. */ | |
334 | case var_zinteger: | |
d7b32ed3 | 335 | return PyLong_FromLong (* (int *) var); |
d57a3c85 TJB |
336 | |
337 | case var_uinteger: | |
338 | { | |
d7b32ed3 | 339 | unsigned int val = * (unsigned int *) var; |
d59b6f6c | 340 | |
d57a3c85 TJB |
341 | if (val == UINT_MAX) |
342 | Py_RETURN_NONE; | |
343 | return PyLong_FromUnsignedLong (val); | |
344 | } | |
345 | } | |
346 | ||
044c0f87 PM |
347 | return PyErr_Format (PyExc_RuntimeError, |
348 | _("Programmer error: unhandled type.")); | |
d57a3c85 TJB |
349 | } |
350 | ||
351 | /* A Python function which returns a gdb parameter's value as a Python | |
352 | value. */ | |
353 | ||
d7b32ed3 | 354 | PyObject * |
8f500870 | 355 | gdbpy_parameter (PyObject *self, PyObject *args) |
d57a3c85 TJB |
356 | { |
357 | struct cmd_list_element *alias, *prefix, *cmd; | |
ddd49eee TT |
358 | const char *arg; |
359 | char *newarg; | |
cc924cad | 360 | int found = -1; |
d57a3c85 TJB |
361 | volatile struct gdb_exception except; |
362 | ||
363 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
364 | return NULL; | |
365 | ||
366 | newarg = concat ("show ", arg, (char *) NULL); | |
367 | ||
368 | TRY_CATCH (except, RETURN_MASK_ALL) | |
369 | { | |
cc924cad | 370 | found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); |
d57a3c85 TJB |
371 | } |
372 | xfree (newarg); | |
373 | GDB_PY_HANDLE_EXCEPTION (except); | |
cc924cad TJB |
374 | if (!found) |
375 | return PyErr_Format (PyExc_RuntimeError, | |
044c0f87 | 376 | _("Could not find parameter `%s'."), arg); |
d57a3c85 TJB |
377 | |
378 | if (! cmd->var) | |
044c0f87 PM |
379 | return PyErr_Format (PyExc_RuntimeError, |
380 | _("`%s' is not a parameter."), arg); | |
d7b32ed3 | 381 | return gdbpy_parameter_value (cmd->var_type, cmd->var); |
d57a3c85 TJB |
382 | } |
383 | ||
f870a310 TT |
384 | /* Wrapper for target_charset. */ |
385 | ||
386 | static PyObject * | |
387 | gdbpy_target_charset (PyObject *self, PyObject *args) | |
388 | { | |
389 | const char *cset = target_charset (python_gdbarch); | |
d59b6f6c | 390 | |
f870a310 TT |
391 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
392 | } | |
393 | ||
394 | /* Wrapper for target_wide_charset. */ | |
395 | ||
396 | static PyObject * | |
397 | gdbpy_target_wide_charset (PyObject *self, PyObject *args) | |
398 | { | |
399 | const char *cset = target_wide_charset (python_gdbarch); | |
d59b6f6c | 400 | |
f870a310 TT |
401 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
402 | } | |
403 | ||
d57a3c85 TJB |
404 | /* A Python function which evaluates a string using the gdb CLI. */ |
405 | ||
406 | static PyObject * | |
bc9f0842 | 407 | execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 408 | { |
ddd49eee | 409 | const char *arg; |
bc9f0842 TT |
410 | PyObject *from_tty_obj = NULL, *to_string_obj = NULL; |
411 | int from_tty, to_string; | |
d57a3c85 | 412 | volatile struct gdb_exception except; |
bc9f0842 TT |
413 | static char *keywords[] = {"command", "from_tty", "to_string", NULL }; |
414 | char *result = NULL; | |
d57a3c85 | 415 | |
bc9f0842 TT |
416 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg, |
417 | &PyBool_Type, &from_tty_obj, | |
418 | &PyBool_Type, &to_string_obj)) | |
d57a3c85 TJB |
419 | return NULL; |
420 | ||
12453b93 TJB |
421 | from_tty = 0; |
422 | if (from_tty_obj) | |
423 | { | |
bc9f0842 | 424 | int cmp = PyObject_IsTrue (from_tty_obj); |
12453b93 | 425 | if (cmp < 0) |
bc9f0842 | 426 | return NULL; |
12453b93 TJB |
427 | from_tty = cmp; |
428 | } | |
429 | ||
bc9f0842 TT |
430 | to_string = 0; |
431 | if (to_string_obj) | |
432 | { | |
433 | int cmp = PyObject_IsTrue (to_string_obj); | |
434 | if (cmp < 0) | |
435 | return NULL; | |
436 | to_string = cmp; | |
437 | } | |
438 | ||
d57a3c85 TJB |
439 | TRY_CATCH (except, RETURN_MASK_ALL) |
440 | { | |
86c6265d TT |
441 | /* Copy the argument text in case the command modifies it. */ |
442 | char *copy = xstrdup (arg); | |
443 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
bc9f0842 | 444 | |
b4a14fd0 PA |
445 | make_cleanup_restore_integer (&interpreter_async); |
446 | interpreter_async = 0; | |
447 | ||
47a80e90 | 448 | prevent_dont_repeat (); |
bc9f0842 | 449 | if (to_string) |
5da1313b JK |
450 | result = execute_command_to_string (copy, from_tty); |
451 | else | |
bc9f0842 | 452 | { |
5da1313b JK |
453 | result = NULL; |
454 | execute_command (copy, from_tty); | |
bc9f0842 | 455 | } |
d59b6f6c | 456 | |
86c6265d | 457 | do_cleanups (cleanup); |
d57a3c85 TJB |
458 | } |
459 | GDB_PY_HANDLE_EXCEPTION (except); | |
460 | ||
347bddb7 PA |
461 | /* Do any commands attached to breakpoint we stopped at. */ |
462 | bpstat_do_actions (); | |
d57a3c85 | 463 | |
bc9f0842 TT |
464 | if (result) |
465 | { | |
466 | PyObject *r = PyString_FromString (result); | |
467 | xfree (result); | |
468 | return r; | |
469 | } | |
d57a3c85 TJB |
470 | Py_RETURN_NONE; |
471 | } | |
472 | ||
cb2e07a6 PM |
473 | /* Implementation of gdb.solib_name (Long) -> String. |
474 | Returns the name of the shared library holding a given address, or None. */ | |
475 | ||
476 | static PyObject * | |
477 | gdbpy_solib_name (PyObject *self, PyObject *args) | |
478 | { | |
479 | char *soname; | |
480 | PyObject *str_obj; | |
74aedc46 TT |
481 | gdb_py_longest pc; |
482 | ||
483 | if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc)) | |
cb2e07a6 PM |
484 | return NULL; |
485 | ||
486 | soname = solib_name_from_address (current_program_space, pc); | |
487 | if (soname) | |
488 | str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL); | |
489 | else | |
490 | { | |
491 | str_obj = Py_None; | |
492 | Py_INCREF (Py_None); | |
493 | } | |
494 | ||
495 | return str_obj; | |
496 | } | |
497 | ||
498 | /* A Python function which is a wrapper for decode_line_1. */ | |
499 | ||
500 | static PyObject * | |
501 | gdbpy_decode_line (PyObject *self, PyObject *args) | |
502 | { | |
9a2b4c1b MS |
503 | struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to |
504 | appease gcc. */ | |
cb2e07a6 | 505 | struct symtab_and_line sal; |
ddd49eee | 506 | const char *arg = NULL; |
cb2e07a6 PM |
507 | char *copy = NULL; |
508 | struct cleanup *cleanups; | |
509 | PyObject *result = NULL; | |
510 | PyObject *return_result = NULL; | |
511 | PyObject *unparsed = NULL; | |
512 | volatile struct gdb_exception except; | |
513 | ||
514 | if (! PyArg_ParseTuple (args, "|s", &arg)) | |
515 | return NULL; | |
516 | ||
5d9c5995 | 517 | cleanups = make_cleanup (null_cleanup, NULL); |
cb2e07a6 PM |
518 | |
519 | TRY_CATCH (except, RETURN_MASK_ALL) | |
520 | { | |
521 | if (arg) | |
522 | { | |
ddd49eee TT |
523 | copy = xstrdup (arg); |
524 | make_cleanup (xfree, copy); | |
f8eba3c6 | 525 | sals = decode_line_1 (©, 0, 0, 0); |
cb2e07a6 PM |
526 | make_cleanup (xfree, sals.sals); |
527 | } | |
528 | else | |
529 | { | |
530 | set_default_source_symtab_and_line (); | |
531 | sal = get_current_source_symtab_and_line (); | |
532 | sals.sals = &sal; | |
533 | sals.nelts = 1; | |
534 | } | |
535 | } | |
536 | if (except.reason < 0) | |
537 | { | |
538 | do_cleanups (cleanups); | |
539 | /* We know this will always throw. */ | |
540 | GDB_PY_HANDLE_EXCEPTION (except); | |
541 | } | |
542 | ||
543 | if (sals.nelts) | |
544 | { | |
545 | int i; | |
546 | ||
547 | result = PyTuple_New (sals.nelts); | |
548 | if (! result) | |
549 | goto error; | |
550 | for (i = 0; i < sals.nelts; ++i) | |
551 | { | |
552 | PyObject *obj; | |
553 | char *str; | |
554 | ||
555 | obj = symtab_and_line_to_sal_object (sals.sals[i]); | |
556 | if (! obj) | |
557 | { | |
558 | Py_DECREF (result); | |
559 | goto error; | |
560 | } | |
561 | ||
562 | PyTuple_SetItem (result, i, obj); | |
563 | } | |
564 | } | |
565 | else | |
566 | { | |
567 | result = Py_None; | |
568 | Py_INCREF (Py_None); | |
569 | } | |
570 | ||
571 | return_result = PyTuple_New (2); | |
572 | if (! return_result) | |
573 | { | |
574 | Py_DECREF (result); | |
575 | goto error; | |
576 | } | |
577 | ||
578 | if (copy && strlen (copy) > 0) | |
579 | unparsed = PyString_FromString (copy); | |
580 | else | |
581 | { | |
582 | unparsed = Py_None; | |
583 | Py_INCREF (Py_None); | |
584 | } | |
585 | ||
586 | PyTuple_SetItem (return_result, 0, unparsed); | |
587 | PyTuple_SetItem (return_result, 1, result); | |
588 | ||
589 | do_cleanups (cleanups); | |
590 | ||
591 | return return_result; | |
592 | ||
593 | error: | |
594 | do_cleanups (cleanups); | |
595 | return NULL; | |
596 | } | |
597 | ||
57a1d736 TT |
598 | /* Parse a string and evaluate it as an expression. */ |
599 | static PyObject * | |
600 | gdbpy_parse_and_eval (PyObject *self, PyObject *args) | |
601 | { | |
ddd49eee | 602 | const char *expr_str; |
57a1d736 TT |
603 | struct value *result = NULL; |
604 | volatile struct gdb_exception except; | |
605 | ||
606 | if (!PyArg_ParseTuple (args, "s", &expr_str)) | |
607 | return NULL; | |
608 | ||
609 | TRY_CATCH (except, RETURN_MASK_ALL) | |
610 | { | |
ddd49eee TT |
611 | char *copy = xstrdup (expr_str); |
612 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
613 | ||
614 | result = parse_and_eval (copy); | |
615 | do_cleanups (cleanup); | |
57a1d736 TT |
616 | } |
617 | GDB_PY_HANDLE_EXCEPTION (except); | |
618 | ||
619 | return value_to_value_object (result); | |
620 | } | |
621 | ||
d234ef5c DE |
622 | /* Read a file as Python code. |
623 | FILE is the name of the file. | |
624 | This does not throw any errors. If an exception occurs python will print | |
625 | the traceback and clear the error indicator. */ | |
973817a3 JB |
626 | |
627 | void | |
d234ef5c | 628 | source_python_script (const char *file) |
973817a3 | 629 | { |
eb5cda86 | 630 | struct cleanup *cleanup; |
973817a3 | 631 | |
eb5cda86 | 632 | cleanup = ensure_python_env (get_current_arch (), current_language); |
7ed7d719 | 633 | python_run_simple_file (file); |
eb5cda86 | 634 | do_cleanups (cleanup); |
973817a3 JB |
635 | } |
636 | ||
d57a3c85 TJB |
637 | \f |
638 | ||
ca5c20b6 PM |
639 | /* Posting and handling events. */ |
640 | ||
641 | /* A single event. */ | |
642 | struct gdbpy_event | |
643 | { | |
644 | /* The Python event. This is just a callable object. */ | |
645 | PyObject *event; | |
646 | /* The next event. */ | |
647 | struct gdbpy_event *next; | |
648 | }; | |
649 | ||
650 | /* All pending events. */ | |
651 | static struct gdbpy_event *gdbpy_event_list; | |
652 | /* The final link of the event list. */ | |
653 | static struct gdbpy_event **gdbpy_event_list_end; | |
654 | ||
655 | /* We use a file handler, and not an async handler, so that we can | |
656 | wake up the main thread even when it is blocked in poll(). */ | |
4a532131 | 657 | static struct serial *gdbpy_event_fds[2]; |
ca5c20b6 PM |
658 | |
659 | /* The file handler callback. This reads from the internal pipe, and | |
660 | then processes the Python event queue. This will always be run in | |
661 | the main gdb thread. */ | |
4a532131 | 662 | |
ca5c20b6 | 663 | static void |
4a532131 | 664 | gdbpy_run_events (struct serial *scb, void *context) |
ca5c20b6 PM |
665 | { |
666 | struct cleanup *cleanup; | |
ca5c20b6 PM |
667 | int r; |
668 | ||
669 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
670 | ||
4a532131 PA |
671 | /* Flush the fd. Do this before flushing the events list, so that |
672 | any new event post afterwards is sure to re-awake the event | |
673 | loop. */ | |
674 | while (serial_readchar (gdbpy_event_fds[0], 0) >= 0) | |
675 | ; | |
ca5c20b6 PM |
676 | |
677 | while (gdbpy_event_list) | |
678 | { | |
679 | /* Dispatching the event might push a new element onto the event | |
680 | loop, so we update here "atomically enough". */ | |
681 | struct gdbpy_event *item = gdbpy_event_list; | |
682 | gdbpy_event_list = gdbpy_event_list->next; | |
683 | if (gdbpy_event_list == NULL) | |
684 | gdbpy_event_list_end = &gdbpy_event_list; | |
685 | ||
686 | /* Ignore errors. */ | |
687 | if (PyObject_CallObject (item->event, NULL) == NULL) | |
688 | PyErr_Clear (); | |
689 | ||
690 | Py_DECREF (item->event); | |
691 | xfree (item); | |
692 | } | |
693 | ||
694 | do_cleanups (cleanup); | |
695 | } | |
696 | ||
697 | /* Submit an event to the gdb thread. */ | |
698 | static PyObject * | |
699 | gdbpy_post_event (PyObject *self, PyObject *args) | |
700 | { | |
701 | struct gdbpy_event *event; | |
702 | PyObject *func; | |
703 | int wakeup; | |
704 | ||
705 | if (!PyArg_ParseTuple (args, "O", &func)) | |
706 | return NULL; | |
707 | ||
708 | if (!PyCallable_Check (func)) | |
709 | { | |
710 | PyErr_SetString (PyExc_RuntimeError, | |
711 | _("Posted event is not callable")); | |
712 | return NULL; | |
713 | } | |
714 | ||
715 | Py_INCREF (func); | |
716 | ||
717 | /* From here until the end of the function, we have the GIL, so we | |
718 | can operate on our global data structures without worrying. */ | |
719 | wakeup = gdbpy_event_list == NULL; | |
720 | ||
721 | event = XNEW (struct gdbpy_event); | |
722 | event->event = func; | |
723 | event->next = NULL; | |
724 | *gdbpy_event_list_end = event; | |
725 | gdbpy_event_list_end = &event->next; | |
726 | ||
727 | /* Wake up gdb when needed. */ | |
728 | if (wakeup) | |
729 | { | |
730 | char c = 'q'; /* Anything. */ | |
4a532131 PA |
731 | |
732 | if (serial_write (gdbpy_event_fds[1], &c, 1)) | |
ca5c20b6 PM |
733 | return PyErr_SetFromErrno (PyExc_IOError); |
734 | } | |
735 | ||
736 | Py_RETURN_NONE; | |
737 | } | |
738 | ||
739 | /* Initialize the Python event handler. */ | |
740 | static void | |
741 | gdbpy_initialize_events (void) | |
742 | { | |
4a532131 | 743 | if (serial_pipe (gdbpy_event_fds) == 0) |
ca5c20b6 PM |
744 | { |
745 | gdbpy_event_list_end = &gdbpy_event_list; | |
4a532131 | 746 | serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL); |
ca5c20b6 PM |
747 | } |
748 | } | |
749 | ||
d17b6f81 PM |
750 | \f |
751 | ||
752 | static void | |
753 | before_prompt_hook (const char *current_gdb_prompt) | |
754 | { | |
755 | struct cleanup *cleanup; | |
756 | char *prompt = NULL; | |
757 | ||
758 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
759 | ||
760 | if (PyObject_HasAttrString (gdb_module, "prompt_hook")) | |
761 | { | |
762 | PyObject *hook; | |
763 | ||
764 | hook = PyObject_GetAttrString (gdb_module, "prompt_hook"); | |
765 | if (hook == NULL) | |
766 | goto fail; | |
767 | ||
768 | if (PyCallable_Check (hook)) | |
769 | { | |
770 | PyObject *result; | |
771 | PyObject *current_prompt; | |
772 | ||
773 | current_prompt = PyString_FromString (current_gdb_prompt); | |
774 | if (current_prompt == NULL) | |
775 | goto fail; | |
776 | ||
777 | result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL); | |
778 | ||
779 | Py_DECREF (current_prompt); | |
780 | ||
781 | if (result == NULL) | |
782 | goto fail; | |
783 | ||
784 | make_cleanup_py_decref (result); | |
785 | ||
786 | /* Return type should be None, or a String. If it is None, | |
787 | fall through, we will not set a prompt. If it is a | |
788 | string, set PROMPT. Anything else, set an exception. */ | |
789 | if (result != Py_None && ! PyString_Check (result)) | |
790 | { | |
791 | PyErr_Format (PyExc_RuntimeError, | |
792 | _("Return from prompt_hook must " \ | |
793 | "be either a Python string, or None")); | |
794 | goto fail; | |
795 | } | |
796 | ||
797 | if (result != Py_None) | |
798 | { | |
799 | prompt = python_string_to_host_string (result); | |
800 | ||
801 | if (prompt == NULL) | |
802 | goto fail; | |
803 | else | |
804 | make_cleanup (xfree, prompt); | |
805 | } | |
806 | } | |
807 | } | |
808 | ||
809 | /* If a prompt has been set, PROMPT will not be NULL. If it is | |
810 | NULL, do not set the prompt. */ | |
811 | if (prompt != NULL) | |
ab821bc6 | 812 | set_prompt (prompt); |
d17b6f81 PM |
813 | |
814 | do_cleanups (cleanup); | |
815 | return; | |
816 | ||
817 | fail: | |
818 | gdbpy_print_stack (); | |
819 | do_cleanups (cleanup); | |
820 | return; | |
821 | } | |
822 | ||
823 | \f | |
824 | ||
d57a3c85 TJB |
825 | /* Printing. */ |
826 | ||
827 | /* A python function to write a single string using gdb's filtered | |
99c3dc11 PM |
828 | output stream . The optional keyword STREAM can be used to write |
829 | to a particular stream. The default stream is to gdb_stdout. */ | |
830 | ||
d57a3c85 | 831 | static PyObject * |
99c3dc11 | 832 | gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 833 | { |
ddd49eee | 834 | const char *arg; |
99c3dc11 PM |
835 | static char *keywords[] = {"text", "stream", NULL }; |
836 | int stream_type = 0; | |
837 | ||
838 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg, | |
839 | &stream_type)) | |
d57a3c85 | 840 | return NULL; |
99c3dc11 PM |
841 | |
842 | switch (stream_type) | |
843 | { | |
844 | case 1: | |
845 | { | |
846 | fprintf_filtered (gdb_stderr, "%s", arg); | |
847 | break; | |
848 | } | |
849 | case 2: | |
850 | { | |
851 | fprintf_filtered (gdb_stdlog, "%s", arg); | |
852 | break; | |
853 | } | |
854 | default: | |
855 | fprintf_filtered (gdb_stdout, "%s", arg); | |
856 | } | |
857 | ||
d57a3c85 TJB |
858 | Py_RETURN_NONE; |
859 | } | |
860 | ||
99c3dc11 PM |
861 | /* A python function to flush a gdb stream. The optional keyword |
862 | STREAM can be used to flush a particular stream. The default stream | |
863 | is gdb_stdout. */ | |
864 | ||
d57a3c85 | 865 | static PyObject * |
99c3dc11 | 866 | gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 867 | { |
99c3dc11 PM |
868 | static char *keywords[] = {"stream", NULL }; |
869 | int stream_type = 0; | |
870 | ||
871 | if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords, | |
872 | &stream_type)) | |
873 | return NULL; | |
874 | ||
875 | switch (stream_type) | |
876 | { | |
877 | case 1: | |
878 | { | |
879 | gdb_flush (gdb_stderr); | |
880 | break; | |
881 | } | |
882 | case 2: | |
883 | { | |
884 | gdb_flush (gdb_stdlog); | |
885 | break; | |
886 | } | |
887 | default: | |
888 | gdb_flush (gdb_stdout); | |
889 | } | |
890 | ||
d57a3c85 TJB |
891 | Py_RETURN_NONE; |
892 | } | |
893 | ||
80b6e756 PM |
894 | /* Print a python exception trace, print just a message, or print |
895 | nothing and clear the python exception, depending on | |
896 | gdbpy_should_print_stack. Only call this if a python exception is | |
897 | set. */ | |
d57a3c85 TJB |
898 | void |
899 | gdbpy_print_stack (void) | |
900 | { | |
80b6e756 PM |
901 | /* Print "none", just clear exception. */ |
902 | if (gdbpy_should_print_stack == python_excp_none) | |
903 | { | |
904 | PyErr_Clear (); | |
905 | } | |
906 | /* Print "full" message and backtrace. */ | |
907 | else if (gdbpy_should_print_stack == python_excp_full) | |
0bf0f8c4 DE |
908 | { |
909 | PyErr_Print (); | |
910 | /* PyErr_Print doesn't necessarily end output with a newline. | |
911 | This works because Python's stdout/stderr is fed through | |
912 | printf_filtered. */ | |
913 | begin_line (); | |
914 | } | |
80b6e756 | 915 | /* Print "message", just error print message. */ |
d57a3c85 | 916 | else |
80b6e756 PM |
917 | { |
918 | PyObject *ptype, *pvalue, *ptraceback; | |
919 | char *msg = NULL, *type = NULL; | |
920 | ||
921 | PyErr_Fetch (&ptype, &pvalue, &ptraceback); | |
922 | ||
923 | /* Fetch the error message contained within ptype, pvalue. */ | |
924 | msg = gdbpy_exception_to_string (ptype, pvalue); | |
925 | type = gdbpy_obj_to_string (ptype); | |
926 | if (msg == NULL) | |
927 | { | |
928 | /* An error occurred computing the string representation of the | |
929 | error message. */ | |
930 | fprintf_filtered (gdb_stderr, | |
931 | _("Error occurred computing Python error" \ | |
932 | "message.\n")); | |
933 | } | |
934 | else | |
935 | fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n", | |
936 | type, msg); | |
937 | ||
938 | Py_XDECREF (ptype); | |
939 | Py_XDECREF (pvalue); | |
940 | Py_XDECREF (ptraceback); | |
941 | xfree (msg); | |
942 | } | |
d57a3c85 TJB |
943 | } |
944 | ||
89c73ade TT |
945 | \f |
946 | ||
fa33c3cd DE |
947 | /* Return the current Progspace. |
948 | There always is one. */ | |
949 | ||
950 | static PyObject * | |
951 | gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2) | |
952 | { | |
953 | PyObject *result; | |
954 | ||
955 | result = pspace_to_pspace_object (current_program_space); | |
956 | if (result) | |
957 | Py_INCREF (result); | |
958 | return result; | |
959 | } | |
960 | ||
961 | /* Return a sequence holding all the Progspaces. */ | |
962 | ||
963 | static PyObject * | |
964 | gdbpy_progspaces (PyObject *unused1, PyObject *unused2) | |
965 | { | |
966 | struct program_space *ps; | |
967 | PyObject *list; | |
968 | ||
969 | list = PyList_New (0); | |
970 | if (!list) | |
971 | return NULL; | |
972 | ||
973 | ALL_PSPACES (ps) | |
974 | { | |
975 | PyObject *item = pspace_to_pspace_object (ps); | |
d59b6f6c | 976 | |
fa33c3cd DE |
977 | if (!item || PyList_Append (list, item) == -1) |
978 | { | |
979 | Py_DECREF (list); | |
980 | return NULL; | |
981 | } | |
982 | } | |
983 | ||
984 | return list; | |
985 | } | |
986 | ||
987 | \f | |
988 | ||
89c73ade | 989 | /* The "current" objfile. This is set when gdb detects that a new |
8a1ea21f DE |
990 | objfile has been loaded. It is only set for the duration of a call to |
991 | source_python_script_for_objfile; it is NULL at other times. */ | |
89c73ade TT |
992 | static struct objfile *gdbpy_current_objfile; |
993 | ||
d234ef5c DE |
994 | /* Set the current objfile to OBJFILE and then read FILE as Python code. |
995 | This does not throw any errors. If an exception occurs python will print | |
996 | the traceback and clear the error indicator. */ | |
89c73ade | 997 | |
8a1ea21f | 998 | void |
d234ef5c | 999 | source_python_script_for_objfile (struct objfile *objfile, const char *file) |
89c73ade | 1000 | { |
89c73ade TT |
1001 | struct cleanup *cleanups; |
1002 | ||
d452c4bc | 1003 | cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
1004 | gdbpy_current_objfile = objfile; |
1005 | ||
7ed7d719 | 1006 | python_run_simple_file (file); |
89c73ade TT |
1007 | |
1008 | do_cleanups (cleanups); | |
1009 | gdbpy_current_objfile = NULL; | |
89c73ade TT |
1010 | } |
1011 | ||
1012 | /* Return the current Objfile, or None if there isn't one. */ | |
8a1ea21f | 1013 | |
89c73ade TT |
1014 | static PyObject * |
1015 | gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) | |
1016 | { | |
1017 | PyObject *result; | |
1018 | ||
1019 | if (! gdbpy_current_objfile) | |
1020 | Py_RETURN_NONE; | |
1021 | ||
1022 | result = objfile_to_objfile_object (gdbpy_current_objfile); | |
1023 | if (result) | |
1024 | Py_INCREF (result); | |
1025 | return result; | |
1026 | } | |
1027 | ||
1028 | /* Return a sequence holding all the Objfiles. */ | |
fa33c3cd | 1029 | |
89c73ade TT |
1030 | static PyObject * |
1031 | gdbpy_objfiles (PyObject *unused1, PyObject *unused2) | |
1032 | { | |
1033 | struct objfile *objf; | |
1034 | PyObject *list; | |
1035 | ||
1036 | list = PyList_New (0); | |
1037 | if (!list) | |
1038 | return NULL; | |
1039 | ||
1040 | ALL_OBJFILES (objf) | |
1041 | { | |
1042 | PyObject *item = objfile_to_objfile_object (objf); | |
d59b6f6c | 1043 | |
89c73ade TT |
1044 | if (!item || PyList_Append (list, item) == -1) |
1045 | { | |
1046 | Py_DECREF (list); | |
1047 | return NULL; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | return list; | |
1052 | } | |
1053 | ||
d57a3c85 TJB |
1054 | #else /* HAVE_PYTHON */ |
1055 | ||
1056 | /* Dummy implementation of the gdb "python" command. */ | |
1057 | ||
1058 | static void | |
1059 | python_command (char *arg, int from_tty) | |
1060 | { | |
1061 | while (arg && *arg && isspace (*arg)) | |
1062 | ++arg; | |
1063 | if (arg && *arg) | |
1064 | error (_("Python scripting is not supported in this copy of GDB.")); | |
1065 | else | |
1066 | { | |
1067 | struct command_line *l = get_command_line (python_control, ""); | |
1068 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
d59b6f6c | 1069 | |
d57a3c85 TJB |
1070 | execute_control_command_untraced (l); |
1071 | do_cleanups (cleanups); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | void | |
1076 | eval_python_from_control_command (struct command_line *cmd) | |
1077 | { | |
1078 | error (_("Python scripting is not supported in this copy of GDB.")); | |
1079 | } | |
1080 | ||
973817a3 | 1081 | void |
d234ef5c | 1082 | source_python_script (const char *file) |
973817a3 | 1083 | { |
973817a3 JB |
1084 | throw_error (UNSUPPORTED_ERROR, |
1085 | _("Python scripting is not supported in this copy of GDB.")); | |
1086 | } | |
1087 | ||
7371cf6d PM |
1088 | int |
1089 | gdbpy_should_stop (struct breakpoint_object *bp_obj) | |
1090 | { | |
1091 | internal_error (__FILE__, __LINE__, | |
1092 | _("gdbpy_should_stop called when Python scripting is " \ | |
1093 | "not supported.")); | |
1094 | } | |
1095 | ||
1096 | int | |
1097 | gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj) | |
1098 | { | |
1099 | internal_error (__FILE__, __LINE__, | |
1100 | _("gdbpy_breakpoint_has_py_cond called when Python " \ | |
1101 | "scripting is not supported.")); | |
1102 | } | |
1103 | ||
d57a3c85 TJB |
1104 | #endif /* HAVE_PYTHON */ |
1105 | ||
1106 | \f | |
1107 | ||
713389e0 PM |
1108 | /* Lists for 'set python' commands. */ |
1109 | ||
1110 | static struct cmd_list_element *user_set_python_list; | |
1111 | static struct cmd_list_element *user_show_python_list; | |
d57a3c85 | 1112 | |
713389e0 PM |
1113 | /* Function for use by 'set python' prefix command. */ |
1114 | ||
1115 | static void | |
1116 | user_set_python (char *args, int from_tty) | |
1117 | { | |
1118 | help_list (user_set_python_list, "set python ", all_commands, | |
1119 | gdb_stdout); | |
1120 | } | |
1121 | ||
1122 | /* Function for use by 'show python' prefix command. */ | |
1123 | ||
1124 | static void | |
1125 | user_show_python (char *args, int from_tty) | |
d57a3c85 | 1126 | { |
713389e0 | 1127 | cmd_show_list (user_show_python_list, from_tty, ""); |
d57a3c85 TJB |
1128 | } |
1129 | ||
1130 | /* Initialize the Python code. */ | |
1131 | ||
2c0b251b PA |
1132 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1133 | extern initialize_file_ftype _initialize_python; | |
1134 | ||
d57a3c85 TJB |
1135 | void |
1136 | _initialize_python (void) | |
1137 | { | |
713389e0 PM |
1138 | char *cmd_name; |
1139 | struct cmd_list_element *cmd; | |
1140 | ||
d57a3c85 TJB |
1141 | add_com ("python", class_obscure, python_command, |
1142 | #ifdef HAVE_PYTHON | |
1143 | _("\ | |
1144 | Evaluate a Python command.\n\ | |
1145 | \n\ | |
1146 | The command can be given as an argument, for instance:\n\ | |
1147 | \n\ | |
1148 | python print 23\n\ | |
1149 | \n\ | |
1150 | If no argument is given, the following lines are read and used\n\ | |
1151 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
1152 | the end of the command.") | |
1153 | #else /* HAVE_PYTHON */ | |
1154 | _("\ | |
1155 | Evaluate a Python command.\n\ | |
1156 | \n\ | |
1157 | Python scripting is not supported in this copy of GDB.\n\ | |
1158 | This command is only a placeholder.") | |
1159 | #endif /* HAVE_PYTHON */ | |
1160 | ); | |
1161 | ||
713389e0 PM |
1162 | /* Add set/show python print-stack. */ |
1163 | add_prefix_cmd ("python", no_class, user_show_python, | |
1164 | _("Prefix command for python preference settings."), | |
1165 | &user_show_python_list, "show python ", 0, | |
1166 | &showlist); | |
1167 | ||
1168 | add_prefix_cmd ("python", no_class, user_set_python, | |
1169 | _("Prefix command for python preference settings."), | |
1170 | &user_set_python_list, "set python ", 0, | |
1171 | &setlist); | |
1172 | ||
80b6e756 PM |
1173 | add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, |
1174 | &gdbpy_should_print_stack, _("\ | |
1175 | Set mode for Python stack dump on error."), _("\ | |
1176 | Show the mode of Python stack printing on error."), _("\ | |
1177 | none == no stack or message will be printed.\n\ | |
1178 | full == a message and a stack will be printed.\n\ | |
1179 | message == an error message without a stack will be printed."), | |
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 */ |