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