Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* General python/gdb code |
2 | ||
28e7fd62 | 3 | Copyright (C) 2008-2013 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" |
9a27f2c6 | 73 | #include "event-top.h" |
d57a3c85 | 74 | |
12453b93 | 75 | static PyMethodDef GdbMethods[]; |
d57a3c85 | 76 | |
9a27f2c6 PK |
77 | #ifdef IS_PY3K |
78 | static struct PyModuleDef GdbModuleDef; | |
79 | #endif | |
80 | ||
d57a3c85 | 81 | PyObject *gdb_module; |
b9516fa1 | 82 | PyObject *gdb_python_module; |
d57a3c85 | 83 | |
a6bac58e TT |
84 | /* Some string constants we may wish to use. */ |
85 | PyObject *gdbpy_to_string_cst; | |
86 | PyObject *gdbpy_children_cst; | |
87 | PyObject *gdbpy_display_hint_cst; | |
d8906c6f | 88 | PyObject *gdbpy_doc_cst; |
967cf477 | 89 | PyObject *gdbpy_enabled_cst; |
fb6a3ed3 | 90 | PyObject *gdbpy_value_cst; |
d8906c6f | 91 | |
07ca107c DE |
92 | /* The GdbError exception. */ |
93 | PyObject *gdbpy_gdberror_exc; | |
d452c4bc | 94 | |
621c8364 TT |
95 | /* The `gdb.error' base class. */ |
96 | PyObject *gdbpy_gdb_error; | |
97 | ||
98 | /* The `gdb.MemoryError' exception. */ | |
99 | PyObject *gdbpy_gdb_memory_error; | |
100 | ||
d452c4bc UW |
101 | /* Architecture and language to be used in callbacks from |
102 | the Python interpreter. */ | |
103 | struct gdbarch *python_gdbarch; | |
104 | const struct language_defn *python_language; | |
105 | ||
106 | /* Restore global language and architecture and Python GIL state | |
107 | when leaving the Python interpreter. */ | |
108 | ||
109 | struct python_env | |
110 | { | |
111 | PyGILState_STATE state; | |
112 | struct gdbarch *gdbarch; | |
113 | const struct language_defn *language; | |
8dc78533 | 114 | PyObject *error_type, *error_value, *error_traceback; |
d452c4bc UW |
115 | }; |
116 | ||
117 | static void | |
118 | restore_python_env (void *p) | |
119 | { | |
120 | struct python_env *env = (struct python_env *)p; | |
d59b6f6c | 121 | |
8dc78533 JK |
122 | /* Leftover Python error is forbidden by Python Exception Handling. */ |
123 | if (PyErr_Occurred ()) | |
124 | { | |
125 | /* This order is similar to the one calling error afterwards. */ | |
126 | gdbpy_print_stack (); | |
127 | warning (_("internal error: Unhandled Python exception")); | |
128 | } | |
129 | ||
130 | PyErr_Restore (env->error_type, env->error_value, env->error_traceback); | |
131 | ||
d452c4bc UW |
132 | PyGILState_Release (env->state); |
133 | python_gdbarch = env->gdbarch; | |
134 | python_language = env->language; | |
135 | xfree (env); | |
136 | } | |
137 | ||
138 | /* Called before entering the Python interpreter to install the | |
139 | current language and architecture to be used for Python values. */ | |
140 | ||
141 | struct cleanup * | |
142 | ensure_python_env (struct gdbarch *gdbarch, | |
143 | const struct language_defn *language) | |
144 | { | |
145 | struct python_env *env = xmalloc (sizeof *env); | |
146 | ||
147 | env->state = PyGILState_Ensure (); | |
148 | env->gdbarch = python_gdbarch; | |
149 | env->language = python_language; | |
150 | ||
151 | python_gdbarch = gdbarch; | |
152 | python_language = language; | |
153 | ||
8dc78533 JK |
154 | /* Save it and ensure ! PyErr_Occurred () afterwards. */ |
155 | PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback); | |
156 | ||
d452c4bc UW |
157 | return make_cleanup (restore_python_env, env); |
158 | } | |
159 | ||
522002f9 TT |
160 | /* Clear the quit flag. */ |
161 | ||
162 | void | |
163 | clear_quit_flag (void) | |
164 | { | |
165 | /* This clears the flag as a side effect. */ | |
166 | PyOS_InterruptOccurred (); | |
167 | } | |
168 | ||
169 | /* Set the quit flag. */ | |
170 | ||
171 | void | |
172 | set_quit_flag (void) | |
173 | { | |
174 | PyErr_SetInterrupt (); | |
175 | } | |
176 | ||
177 | /* Return true if the quit flag has been set, false otherwise. */ | |
178 | ||
179 | int | |
180 | check_quit_flag (void) | |
181 | { | |
182 | return PyOS_InterruptOccurred (); | |
183 | } | |
184 | ||
8315665e YPK |
185 | /* Evaluate a Python command like PyRun_SimpleString, but uses |
186 | Py_single_input which prints the result of expressions, and does | |
187 | not automatically print the stack on errors. */ | |
188 | ||
189 | static int | |
190 | eval_python_command (const char *command) | |
191 | { | |
192 | PyObject *m, *d, *v; | |
193 | ||
194 | m = PyImport_AddModule ("__main__"); | |
195 | if (m == NULL) | |
196 | return -1; | |
197 | ||
198 | d = PyModule_GetDict (m); | |
199 | if (d == NULL) | |
200 | return -1; | |
201 | v = PyRun_StringFlags (command, Py_single_input, d, d, NULL); | |
202 | if (v == NULL) | |
203 | return -1; | |
204 | ||
205 | Py_DECREF (v); | |
9a27f2c6 | 206 | #ifndef IS_PY3K |
8315665e YPK |
207 | if (Py_FlushLine ()) |
208 | PyErr_Clear (); | |
9a27f2c6 | 209 | #endif |
8315665e YPK |
210 | |
211 | return 0; | |
212 | } | |
213 | ||
214 | /* Implementation of the gdb "python-interactive" command. */ | |
215 | ||
216 | static void | |
217 | python_interactive_command (char *arg, int from_tty) | |
218 | { | |
219 | struct cleanup *cleanup; | |
220 | int err; | |
221 | ||
222 | cleanup = make_cleanup_restore_integer (&interpreter_async); | |
223 | interpreter_async = 0; | |
224 | ||
225 | while (arg && *arg && isspace (*arg)) | |
226 | ++arg; | |
227 | ||
228 | ensure_python_env (get_current_arch (), current_language); | |
229 | ||
230 | if (arg && *arg) | |
231 | { | |
232 | int len = strlen (arg); | |
233 | char *script = xmalloc (len + 2); | |
234 | ||
235 | strcpy (script, arg); | |
236 | script[len] = '\n'; | |
237 | script[len + 1] = '\0'; | |
238 | err = eval_python_command (script); | |
239 | xfree (script); | |
240 | } | |
241 | else | |
242 | { | |
243 | err = PyRun_InteractiveLoop (instream, "<stdin>"); | |
244 | dont_repeat (); | |
245 | } | |
246 | ||
247 | if (err) | |
248 | { | |
249 | gdbpy_print_stack (); | |
250 | error (_("Error while executing Python code.")); | |
251 | } | |
252 | ||
253 | do_cleanups (cleanup); | |
254 | } | |
255 | ||
4c63965b JK |
256 | /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run |
257 | named FILENAME. | |
258 | ||
259 | On Windows hosts few users would build Python themselves (this is no | |
260 | trivial task on this platform), and thus use binaries built by | |
261 | someone else instead. There may happen situation where the Python | |
262 | library and GDB are using two different versions of the C runtime | |
263 | library. Python, being built with VC, would use one version of the | |
264 | msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll. | |
265 | A FILE * from one runtime does not necessarily operate correctly in | |
7ed7d719 JB |
266 | the other runtime. |
267 | ||
4c63965b JK |
268 | To work around this potential issue, we create on Windows hosts the |
269 | FILE object using Python routines, thus making sure that it is | |
270 | compatible with the Python library. */ | |
7ed7d719 JB |
271 | |
272 | static void | |
4c63965b | 273 | python_run_simple_file (FILE *file, const char *filename) |
7ed7d719 | 274 | { |
4c63965b JK |
275 | #ifndef _WIN32 |
276 | ||
277 | PyRun_SimpleFile (file, filename); | |
278 | ||
279 | #else /* _WIN32 */ | |
280 | ||
3ab1ec27 | 281 | char *full_path; |
7ed7d719 JB |
282 | PyObject *python_file; |
283 | struct cleanup *cleanup; | |
284 | ||
3ab1ec27 PM |
285 | /* Because we have a string for a filename, and are using Python to |
286 | open the file, we need to expand any tilde in the path first. */ | |
287 | full_path = tilde_expand (filename); | |
288 | cleanup = make_cleanup (xfree, full_path); | |
289 | python_file = PyFile_FromString (full_path, "r"); | |
290 | if (! python_file) | |
291 | { | |
292 | do_cleanups (cleanup); | |
293 | gdbpy_print_stack (); | |
294 | error (_("Error while opening file: %s"), full_path); | |
295 | } | |
296 | ||
7ed7d719 JB |
297 | make_cleanup_py_decref (python_file); |
298 | PyRun_SimpleFile (PyFile_AsFile (python_file), filename); | |
299 | do_cleanups (cleanup); | |
4c63965b JK |
300 | |
301 | #endif /* _WIN32 */ | |
7ed7d719 | 302 | } |
d452c4bc | 303 | |
d57a3c85 TJB |
304 | /* Given a command_line, return a command string suitable for passing |
305 | to Python. Lines in the string are separated by newlines. The | |
306 | return value is allocated using xmalloc and the caller is | |
307 | responsible for freeing it. */ | |
308 | ||
309 | static char * | |
310 | compute_python_string (struct command_line *l) | |
311 | { | |
312 | struct command_line *iter; | |
313 | char *script = NULL; | |
314 | int size = 0; | |
315 | int here; | |
316 | ||
317 | for (iter = l; iter; iter = iter->next) | |
318 | size += strlen (iter->line) + 1; | |
319 | ||
320 | script = xmalloc (size + 1); | |
321 | here = 0; | |
322 | for (iter = l; iter; iter = iter->next) | |
323 | { | |
324 | int len = strlen (iter->line); | |
d59b6f6c | 325 | |
d57a3c85 TJB |
326 | strcpy (&script[here], iter->line); |
327 | here += len; | |
328 | script[here++] = '\n'; | |
329 | } | |
330 | script[here] = '\0'; | |
331 | return script; | |
332 | } | |
333 | ||
334 | /* Take a command line structure representing a 'python' command, and | |
335 | evaluate its body using the Python interpreter. */ | |
336 | ||
337 | void | |
338 | eval_python_from_control_command (struct command_line *cmd) | |
339 | { | |
12453b93 | 340 | int ret; |
d57a3c85 | 341 | char *script; |
ca30a762 | 342 | struct cleanup *cleanup; |
d57a3c85 TJB |
343 | |
344 | if (cmd->body_count != 1) | |
345 | error (_("Invalid \"python\" block structure.")); | |
346 | ||
d452c4bc | 347 | cleanup = ensure_python_env (get_current_arch (), current_language); |
ca30a762 | 348 | |
d57a3c85 | 349 | script = compute_python_string (cmd->body_list[0]); |
12453b93 | 350 | ret = PyRun_SimpleString (script); |
d57a3c85 | 351 | xfree (script); |
12453b93 | 352 | if (ret) |
80b6e756 | 353 | error (_("Error while executing Python code.")); |
ca30a762 TT |
354 | |
355 | do_cleanups (cleanup); | |
d57a3c85 TJB |
356 | } |
357 | ||
358 | /* Implementation of the gdb "python" command. */ | |
359 | ||
360 | static void | |
361 | python_command (char *arg, int from_tty) | |
362 | { | |
ca30a762 | 363 | struct cleanup *cleanup; |
ca30a762 | 364 | |
d59b6f6c | 365 | cleanup = ensure_python_env (get_current_arch (), current_language); |
b4a14fd0 PA |
366 | |
367 | make_cleanup_restore_integer (&interpreter_async); | |
368 | interpreter_async = 0; | |
369 | ||
d57a3c85 TJB |
370 | while (arg && *arg && isspace (*arg)) |
371 | ++arg; | |
372 | if (arg && *arg) | |
373 | { | |
12453b93 | 374 | if (PyRun_SimpleString (arg)) |
80b6e756 | 375 | error (_("Error while executing Python code.")); |
d57a3c85 TJB |
376 | } |
377 | else | |
378 | { | |
379 | struct command_line *l = get_command_line (python_control, ""); | |
d59b6f6c | 380 | |
ca30a762 | 381 | make_cleanup_free_command_lines (&l); |
d57a3c85 | 382 | execute_control_command_untraced (l); |
d57a3c85 | 383 | } |
ca30a762 TT |
384 | |
385 | do_cleanups (cleanup); | |
d57a3c85 TJB |
386 | } |
387 | ||
388 | \f | |
389 | ||
390 | /* Transform a gdb parameters's value into a Python value. May return | |
391 | NULL (and set a Python exception) on error. Helper function for | |
392 | get_parameter. */ | |
d7b32ed3 PM |
393 | PyObject * |
394 | gdbpy_parameter_value (enum var_types type, void *var) | |
d57a3c85 | 395 | { |
d7b32ed3 | 396 | switch (type) |
d57a3c85 TJB |
397 | { |
398 | case var_string: | |
399 | case var_string_noescape: | |
400 | case var_optional_filename: | |
401 | case var_filename: | |
402 | case var_enum: | |
403 | { | |
d7b32ed3 | 404 | char *str = * (char **) var; |
d59b6f6c | 405 | |
d57a3c85 TJB |
406 | if (! str) |
407 | str = ""; | |
408 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
409 | } | |
410 | ||
411 | case var_boolean: | |
412 | { | |
d7b32ed3 | 413 | if (* (int *) var) |
d57a3c85 TJB |
414 | Py_RETURN_TRUE; |
415 | else | |
416 | Py_RETURN_FALSE; | |
417 | } | |
418 | ||
419 | case var_auto_boolean: | |
420 | { | |
d7b32ed3 | 421 | enum auto_boolean ab = * (enum auto_boolean *) var; |
d59b6f6c | 422 | |
d57a3c85 TJB |
423 | if (ab == AUTO_BOOLEAN_TRUE) |
424 | Py_RETURN_TRUE; | |
425 | else if (ab == AUTO_BOOLEAN_FALSE) | |
426 | Py_RETURN_FALSE; | |
427 | else | |
428 | Py_RETURN_NONE; | |
429 | } | |
430 | ||
431 | case var_integer: | |
d7b32ed3 | 432 | if ((* (int *) var) == INT_MAX) |
d57a3c85 TJB |
433 | Py_RETURN_NONE; |
434 | /* Fall through. */ | |
435 | case var_zinteger: | |
d7b32ed3 | 436 | return PyLong_FromLong (* (int *) var); |
d57a3c85 TJB |
437 | |
438 | case var_uinteger: | |
439 | { | |
d7b32ed3 | 440 | unsigned int val = * (unsigned int *) var; |
d59b6f6c | 441 | |
d57a3c85 TJB |
442 | if (val == UINT_MAX) |
443 | Py_RETURN_NONE; | |
444 | return PyLong_FromUnsignedLong (val); | |
445 | } | |
446 | } | |
447 | ||
044c0f87 PM |
448 | return PyErr_Format (PyExc_RuntimeError, |
449 | _("Programmer error: unhandled type.")); | |
d57a3c85 TJB |
450 | } |
451 | ||
452 | /* A Python function which returns a gdb parameter's value as a Python | |
453 | value. */ | |
454 | ||
d7b32ed3 | 455 | PyObject * |
8f500870 | 456 | gdbpy_parameter (PyObject *self, PyObject *args) |
d57a3c85 TJB |
457 | { |
458 | struct cmd_list_element *alias, *prefix, *cmd; | |
ddd49eee TT |
459 | const char *arg; |
460 | char *newarg; | |
cc924cad | 461 | int found = -1; |
d57a3c85 TJB |
462 | volatile struct gdb_exception except; |
463 | ||
464 | if (! PyArg_ParseTuple (args, "s", &arg)) | |
465 | return NULL; | |
466 | ||
467 | newarg = concat ("show ", arg, (char *) NULL); | |
468 | ||
469 | TRY_CATCH (except, RETURN_MASK_ALL) | |
470 | { | |
cc924cad | 471 | found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); |
d57a3c85 TJB |
472 | } |
473 | xfree (newarg); | |
474 | GDB_PY_HANDLE_EXCEPTION (except); | |
cc924cad TJB |
475 | if (!found) |
476 | return PyErr_Format (PyExc_RuntimeError, | |
044c0f87 | 477 | _("Could not find parameter `%s'."), arg); |
d57a3c85 TJB |
478 | |
479 | if (! cmd->var) | |
044c0f87 PM |
480 | return PyErr_Format (PyExc_RuntimeError, |
481 | _("`%s' is not a parameter."), arg); | |
d7b32ed3 | 482 | return gdbpy_parameter_value (cmd->var_type, cmd->var); |
d57a3c85 TJB |
483 | } |
484 | ||
f870a310 TT |
485 | /* Wrapper for target_charset. */ |
486 | ||
487 | static PyObject * | |
488 | gdbpy_target_charset (PyObject *self, PyObject *args) | |
489 | { | |
490 | const char *cset = target_charset (python_gdbarch); | |
d59b6f6c | 491 | |
f870a310 TT |
492 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
493 | } | |
494 | ||
495 | /* Wrapper for target_wide_charset. */ | |
496 | ||
497 | static PyObject * | |
498 | gdbpy_target_wide_charset (PyObject *self, PyObject *args) | |
499 | { | |
500 | const char *cset = target_wide_charset (python_gdbarch); | |
d59b6f6c | 501 | |
f870a310 TT |
502 | return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); |
503 | } | |
504 | ||
d57a3c85 TJB |
505 | /* A Python function which evaluates a string using the gdb CLI. */ |
506 | ||
507 | static PyObject * | |
bc9f0842 | 508 | execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 509 | { |
ddd49eee | 510 | const char *arg; |
bc9f0842 TT |
511 | PyObject *from_tty_obj = NULL, *to_string_obj = NULL; |
512 | int from_tty, to_string; | |
d57a3c85 | 513 | volatile struct gdb_exception except; |
bc9f0842 TT |
514 | static char *keywords[] = {"command", "from_tty", "to_string", NULL }; |
515 | char *result = NULL; | |
d57a3c85 | 516 | |
bc9f0842 TT |
517 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg, |
518 | &PyBool_Type, &from_tty_obj, | |
519 | &PyBool_Type, &to_string_obj)) | |
d57a3c85 TJB |
520 | return NULL; |
521 | ||
12453b93 TJB |
522 | from_tty = 0; |
523 | if (from_tty_obj) | |
524 | { | |
bc9f0842 | 525 | int cmp = PyObject_IsTrue (from_tty_obj); |
12453b93 | 526 | if (cmp < 0) |
bc9f0842 | 527 | return NULL; |
12453b93 TJB |
528 | from_tty = cmp; |
529 | } | |
530 | ||
bc9f0842 TT |
531 | to_string = 0; |
532 | if (to_string_obj) | |
533 | { | |
534 | int cmp = PyObject_IsTrue (to_string_obj); | |
535 | if (cmp < 0) | |
536 | return NULL; | |
537 | to_string = cmp; | |
538 | } | |
539 | ||
d57a3c85 TJB |
540 | TRY_CATCH (except, RETURN_MASK_ALL) |
541 | { | |
86c6265d TT |
542 | /* Copy the argument text in case the command modifies it. */ |
543 | char *copy = xstrdup (arg); | |
544 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
bc9f0842 | 545 | |
b4a14fd0 PA |
546 | make_cleanup_restore_integer (&interpreter_async); |
547 | interpreter_async = 0; | |
548 | ||
47a80e90 | 549 | prevent_dont_repeat (); |
bc9f0842 | 550 | if (to_string) |
5da1313b JK |
551 | result = execute_command_to_string (copy, from_tty); |
552 | else | |
bc9f0842 | 553 | { |
5da1313b JK |
554 | result = NULL; |
555 | execute_command (copy, from_tty); | |
bc9f0842 | 556 | } |
d59b6f6c | 557 | |
86c6265d | 558 | do_cleanups (cleanup); |
d57a3c85 TJB |
559 | } |
560 | GDB_PY_HANDLE_EXCEPTION (except); | |
561 | ||
347bddb7 PA |
562 | /* Do any commands attached to breakpoint we stopped at. */ |
563 | bpstat_do_actions (); | |
d57a3c85 | 564 | |
bc9f0842 TT |
565 | if (result) |
566 | { | |
567 | PyObject *r = PyString_FromString (result); | |
568 | xfree (result); | |
569 | return r; | |
570 | } | |
d57a3c85 TJB |
571 | Py_RETURN_NONE; |
572 | } | |
573 | ||
cb2e07a6 PM |
574 | /* Implementation of gdb.solib_name (Long) -> String. |
575 | Returns the name of the shared library holding a given address, or None. */ | |
576 | ||
577 | static PyObject * | |
578 | gdbpy_solib_name (PyObject *self, PyObject *args) | |
579 | { | |
580 | char *soname; | |
581 | PyObject *str_obj; | |
74aedc46 TT |
582 | gdb_py_longest pc; |
583 | ||
584 | if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc)) | |
cb2e07a6 PM |
585 | return NULL; |
586 | ||
587 | soname = solib_name_from_address (current_program_space, pc); | |
588 | if (soname) | |
589 | str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL); | |
590 | else | |
591 | { | |
592 | str_obj = Py_None; | |
593 | Py_INCREF (Py_None); | |
594 | } | |
595 | ||
596 | return str_obj; | |
597 | } | |
598 | ||
599 | /* A Python function which is a wrapper for decode_line_1. */ | |
600 | ||
601 | static PyObject * | |
602 | gdbpy_decode_line (PyObject *self, PyObject *args) | |
603 | { | |
9a2b4c1b MS |
604 | struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to |
605 | appease gcc. */ | |
cb2e07a6 | 606 | struct symtab_and_line sal; |
ddd49eee | 607 | const char *arg = NULL; |
9bc3523d | 608 | char *copy_to_free = NULL, *copy = NULL; |
cb2e07a6 PM |
609 | struct cleanup *cleanups; |
610 | PyObject *result = NULL; | |
611 | PyObject *return_result = NULL; | |
612 | PyObject *unparsed = NULL; | |
613 | volatile struct gdb_exception except; | |
614 | ||
615 | if (! PyArg_ParseTuple (args, "|s", &arg)) | |
616 | return NULL; | |
617 | ||
5d9c5995 | 618 | cleanups = make_cleanup (null_cleanup, NULL); |
cb2e07a6 | 619 | |
9bc3523d | 620 | sals.sals = NULL; |
cb2e07a6 PM |
621 | TRY_CATCH (except, RETURN_MASK_ALL) |
622 | { | |
623 | if (arg) | |
624 | { | |
ddd49eee | 625 | copy = xstrdup (arg); |
9bc3523d | 626 | copy_to_free = copy; |
f8eba3c6 | 627 | sals = decode_line_1 (©, 0, 0, 0); |
cb2e07a6 PM |
628 | } |
629 | else | |
630 | { | |
631 | set_default_source_symtab_and_line (); | |
632 | sal = get_current_source_symtab_and_line (); | |
633 | sals.sals = &sal; | |
634 | sals.nelts = 1; | |
635 | } | |
636 | } | |
9bc3523d TT |
637 | |
638 | if (sals.sals != NULL && sals.sals != &sal) | |
639 | { | |
640 | make_cleanup (xfree, copy_to_free); | |
641 | make_cleanup (xfree, sals.sals); | |
642 | } | |
643 | ||
cb2e07a6 PM |
644 | if (except.reason < 0) |
645 | { | |
646 | do_cleanups (cleanups); | |
647 | /* We know this will always throw. */ | |
648 | GDB_PY_HANDLE_EXCEPTION (except); | |
649 | } | |
650 | ||
651 | if (sals.nelts) | |
652 | { | |
653 | int i; | |
654 | ||
655 | result = PyTuple_New (sals.nelts); | |
656 | if (! result) | |
657 | goto error; | |
658 | for (i = 0; i < sals.nelts; ++i) | |
659 | { | |
660 | PyObject *obj; | |
cb2e07a6 PM |
661 | |
662 | obj = symtab_and_line_to_sal_object (sals.sals[i]); | |
663 | if (! obj) | |
664 | { | |
665 | Py_DECREF (result); | |
666 | goto error; | |
667 | } | |
668 | ||
669 | PyTuple_SetItem (result, i, obj); | |
670 | } | |
671 | } | |
672 | else | |
673 | { | |
674 | result = Py_None; | |
675 | Py_INCREF (Py_None); | |
676 | } | |
677 | ||
678 | return_result = PyTuple_New (2); | |
679 | if (! return_result) | |
680 | { | |
681 | Py_DECREF (result); | |
682 | goto error; | |
683 | } | |
684 | ||
685 | if (copy && strlen (copy) > 0) | |
9bc3523d TT |
686 | { |
687 | unparsed = PyString_FromString (copy); | |
688 | if (unparsed == NULL) | |
689 | { | |
690 | Py_DECREF (result); | |
691 | Py_DECREF (return_result); | |
692 | return_result = NULL; | |
693 | goto error; | |
694 | } | |
695 | } | |
cb2e07a6 PM |
696 | else |
697 | { | |
698 | unparsed = Py_None; | |
699 | Py_INCREF (Py_None); | |
700 | } | |
701 | ||
702 | PyTuple_SetItem (return_result, 0, unparsed); | |
703 | PyTuple_SetItem (return_result, 1, result); | |
704 | ||
9bc3523d | 705 | error: |
cb2e07a6 PM |
706 | do_cleanups (cleanups); |
707 | ||
708 | return return_result; | |
cb2e07a6 PM |
709 | } |
710 | ||
57a1d736 TT |
711 | /* Parse a string and evaluate it as an expression. */ |
712 | static PyObject * | |
713 | gdbpy_parse_and_eval (PyObject *self, PyObject *args) | |
714 | { | |
ddd49eee | 715 | const char *expr_str; |
57a1d736 TT |
716 | struct value *result = NULL; |
717 | volatile struct gdb_exception except; | |
718 | ||
719 | if (!PyArg_ParseTuple (args, "s", &expr_str)) | |
720 | return NULL; | |
721 | ||
722 | TRY_CATCH (except, RETURN_MASK_ALL) | |
723 | { | |
ddd49eee TT |
724 | char *copy = xstrdup (expr_str); |
725 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
726 | ||
727 | result = parse_and_eval (copy); | |
728 | do_cleanups (cleanup); | |
57a1d736 TT |
729 | } |
730 | GDB_PY_HANDLE_EXCEPTION (except); | |
731 | ||
732 | return value_to_value_object (result); | |
733 | } | |
734 | ||
7efc75aa SCR |
735 | /* Implementation of gdb.find_pc_line function. |
736 | Returns the gdb.Symtab_and_line object corresponding to a PC value. */ | |
737 | ||
738 | static PyObject * | |
739 | gdbpy_find_pc_line (PyObject *self, PyObject *args) | |
740 | { | |
741 | struct symtab_and_line sal; | |
742 | CORE_ADDR pc; | |
29ca12b3 | 743 | gdb_py_ulongest pc_llu; |
7efc75aa SCR |
744 | |
745 | if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu)) | |
746 | return NULL; | |
747 | ||
748 | pc = (CORE_ADDR) pc_llu; | |
749 | sal = find_pc_line (pc, 0); | |
750 | return symtab_and_line_to_sal_object (sal); | |
751 | } | |
752 | ||
d234ef5c | 753 | /* Read a file as Python code. |
4c63965b | 754 | FILE is the file to run. FILENAME is name of the file FILE. |
d234ef5c DE |
755 | This does not throw any errors. If an exception occurs python will print |
756 | the traceback and clear the error indicator. */ | |
973817a3 JB |
757 | |
758 | void | |
4c63965b | 759 | source_python_script (FILE *file, const char *filename) |
973817a3 | 760 | { |
eb5cda86 | 761 | struct cleanup *cleanup; |
973817a3 | 762 | |
eb5cda86 | 763 | cleanup = ensure_python_env (get_current_arch (), current_language); |
4c63965b | 764 | python_run_simple_file (file, filename); |
eb5cda86 | 765 | do_cleanups (cleanup); |
973817a3 JB |
766 | } |
767 | ||
d57a3c85 TJB |
768 | \f |
769 | ||
ca5c20b6 PM |
770 | /* Posting and handling events. */ |
771 | ||
772 | /* A single event. */ | |
773 | struct gdbpy_event | |
774 | { | |
775 | /* The Python event. This is just a callable object. */ | |
776 | PyObject *event; | |
777 | /* The next event. */ | |
778 | struct gdbpy_event *next; | |
779 | }; | |
780 | ||
781 | /* All pending events. */ | |
782 | static struct gdbpy_event *gdbpy_event_list; | |
783 | /* The final link of the event list. */ | |
784 | static struct gdbpy_event **gdbpy_event_list_end; | |
785 | ||
786 | /* We use a file handler, and not an async handler, so that we can | |
787 | wake up the main thread even when it is blocked in poll(). */ | |
4a532131 | 788 | static struct serial *gdbpy_event_fds[2]; |
ca5c20b6 PM |
789 | |
790 | /* The file handler callback. This reads from the internal pipe, and | |
791 | then processes the Python event queue. This will always be run in | |
792 | the main gdb thread. */ | |
4a532131 | 793 | |
ca5c20b6 | 794 | static void |
4a532131 | 795 | gdbpy_run_events (struct serial *scb, void *context) |
ca5c20b6 PM |
796 | { |
797 | struct cleanup *cleanup; | |
ca5c20b6 PM |
798 | |
799 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
800 | ||
4a532131 PA |
801 | /* Flush the fd. Do this before flushing the events list, so that |
802 | any new event post afterwards is sure to re-awake the event | |
803 | loop. */ | |
804 | while (serial_readchar (gdbpy_event_fds[0], 0) >= 0) | |
805 | ; | |
ca5c20b6 PM |
806 | |
807 | while (gdbpy_event_list) | |
808 | { | |
809 | /* Dispatching the event might push a new element onto the event | |
810 | loop, so we update here "atomically enough". */ | |
811 | struct gdbpy_event *item = gdbpy_event_list; | |
812 | gdbpy_event_list = gdbpy_event_list->next; | |
813 | if (gdbpy_event_list == NULL) | |
814 | gdbpy_event_list_end = &gdbpy_event_list; | |
815 | ||
816 | /* Ignore errors. */ | |
817 | if (PyObject_CallObject (item->event, NULL) == NULL) | |
818 | PyErr_Clear (); | |
819 | ||
820 | Py_DECREF (item->event); | |
821 | xfree (item); | |
822 | } | |
823 | ||
824 | do_cleanups (cleanup); | |
825 | } | |
826 | ||
827 | /* Submit an event to the gdb thread. */ | |
828 | static PyObject * | |
829 | gdbpy_post_event (PyObject *self, PyObject *args) | |
830 | { | |
831 | struct gdbpy_event *event; | |
832 | PyObject *func; | |
833 | int wakeup; | |
834 | ||
835 | if (!PyArg_ParseTuple (args, "O", &func)) | |
836 | return NULL; | |
837 | ||
838 | if (!PyCallable_Check (func)) | |
839 | { | |
840 | PyErr_SetString (PyExc_RuntimeError, | |
841 | _("Posted event is not callable")); | |
842 | return NULL; | |
843 | } | |
844 | ||
845 | Py_INCREF (func); | |
846 | ||
847 | /* From here until the end of the function, we have the GIL, so we | |
848 | can operate on our global data structures without worrying. */ | |
849 | wakeup = gdbpy_event_list == NULL; | |
850 | ||
851 | event = XNEW (struct gdbpy_event); | |
852 | event->event = func; | |
853 | event->next = NULL; | |
854 | *gdbpy_event_list_end = event; | |
855 | gdbpy_event_list_end = &event->next; | |
856 | ||
857 | /* Wake up gdb when needed. */ | |
858 | if (wakeup) | |
859 | { | |
860 | char c = 'q'; /* Anything. */ | |
4a532131 PA |
861 | |
862 | if (serial_write (gdbpy_event_fds[1], &c, 1)) | |
ca5c20b6 PM |
863 | return PyErr_SetFromErrno (PyExc_IOError); |
864 | } | |
865 | ||
866 | Py_RETURN_NONE; | |
867 | } | |
868 | ||
869 | /* Initialize the Python event handler. */ | |
870 | static void | |
871 | gdbpy_initialize_events (void) | |
872 | { | |
4a532131 | 873 | if (serial_pipe (gdbpy_event_fds) == 0) |
ca5c20b6 PM |
874 | { |
875 | gdbpy_event_list_end = &gdbpy_event_list; | |
4a532131 | 876 | serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL); |
ca5c20b6 PM |
877 | } |
878 | } | |
879 | ||
d17b6f81 PM |
880 | \f |
881 | ||
882 | static void | |
883 | before_prompt_hook (const char *current_gdb_prompt) | |
884 | { | |
885 | struct cleanup *cleanup; | |
886 | char *prompt = NULL; | |
887 | ||
888 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
889 | ||
b9516fa1 YPK |
890 | if (gdb_python_module |
891 | && PyObject_HasAttrString (gdb_python_module, "prompt_hook")) | |
d17b6f81 PM |
892 | { |
893 | PyObject *hook; | |
894 | ||
b9516fa1 | 895 | hook = PyObject_GetAttrString (gdb_python_module, "prompt_hook"); |
d17b6f81 PM |
896 | if (hook == NULL) |
897 | goto fail; | |
898 | ||
899 | if (PyCallable_Check (hook)) | |
900 | { | |
901 | PyObject *result; | |
902 | PyObject *current_prompt; | |
903 | ||
904 | current_prompt = PyString_FromString (current_gdb_prompt); | |
905 | if (current_prompt == NULL) | |
906 | goto fail; | |
907 | ||
908 | result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL); | |
909 | ||
910 | Py_DECREF (current_prompt); | |
911 | ||
912 | if (result == NULL) | |
913 | goto fail; | |
914 | ||
915 | make_cleanup_py_decref (result); | |
916 | ||
917 | /* Return type should be None, or a String. If it is None, | |
918 | fall through, we will not set a prompt. If it is a | |
919 | string, set PROMPT. Anything else, set an exception. */ | |
920 | if (result != Py_None && ! PyString_Check (result)) | |
921 | { | |
922 | PyErr_Format (PyExc_RuntimeError, | |
923 | _("Return from prompt_hook must " \ | |
924 | "be either a Python string, or None")); | |
925 | goto fail; | |
926 | } | |
927 | ||
928 | if (result != Py_None) | |
929 | { | |
930 | prompt = python_string_to_host_string (result); | |
931 | ||
932 | if (prompt == NULL) | |
933 | goto fail; | |
934 | else | |
935 | make_cleanup (xfree, prompt); | |
936 | } | |
937 | } | |
938 | } | |
939 | ||
940 | /* If a prompt has been set, PROMPT will not be NULL. If it is | |
941 | NULL, do not set the prompt. */ | |
942 | if (prompt != NULL) | |
ab821bc6 | 943 | set_prompt (prompt); |
d17b6f81 PM |
944 | |
945 | do_cleanups (cleanup); | |
946 | return; | |
947 | ||
948 | fail: | |
949 | gdbpy_print_stack (); | |
950 | do_cleanups (cleanup); | |
951 | return; | |
952 | } | |
953 | ||
954 | \f | |
955 | ||
d57a3c85 TJB |
956 | /* Printing. */ |
957 | ||
958 | /* A python function to write a single string using gdb's filtered | |
99c3dc11 PM |
959 | output stream . The optional keyword STREAM can be used to write |
960 | to a particular stream. The default stream is to gdb_stdout. */ | |
961 | ||
d57a3c85 | 962 | static PyObject * |
99c3dc11 | 963 | gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 964 | { |
ddd49eee | 965 | const char *arg; |
99c3dc11 PM |
966 | static char *keywords[] = {"text", "stream", NULL }; |
967 | int stream_type = 0; | |
adb4fe3b | 968 | volatile struct gdb_exception except; |
99c3dc11 PM |
969 | |
970 | if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg, | |
971 | &stream_type)) | |
d57a3c85 | 972 | return NULL; |
99c3dc11 | 973 | |
adb4fe3b | 974 | TRY_CATCH (except, RETURN_MASK_ALL) |
99c3dc11 | 975 | { |
adb4fe3b ME |
976 | switch (stream_type) |
977 | { | |
978 | case 1: | |
979 | { | |
980 | fprintf_filtered (gdb_stderr, "%s", arg); | |
981 | break; | |
982 | } | |
983 | case 2: | |
984 | { | |
985 | fprintf_filtered (gdb_stdlog, "%s", arg); | |
986 | break; | |
987 | } | |
988 | default: | |
989 | fprintf_filtered (gdb_stdout, "%s", arg); | |
990 | } | |
99c3dc11 | 991 | } |
adb4fe3b | 992 | GDB_PY_HANDLE_EXCEPTION (except); |
99c3dc11 | 993 | |
d57a3c85 TJB |
994 | Py_RETURN_NONE; |
995 | } | |
996 | ||
99c3dc11 PM |
997 | /* A python function to flush a gdb stream. The optional keyword |
998 | STREAM can be used to flush a particular stream. The default stream | |
999 | is gdb_stdout. */ | |
1000 | ||
d57a3c85 | 1001 | static PyObject * |
99c3dc11 | 1002 | gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw) |
d57a3c85 | 1003 | { |
99c3dc11 PM |
1004 | static char *keywords[] = {"stream", NULL }; |
1005 | int stream_type = 0; | |
1006 | ||
1007 | if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords, | |
1008 | &stream_type)) | |
1009 | return NULL; | |
1010 | ||
1011 | switch (stream_type) | |
1012 | { | |
1013 | case 1: | |
1014 | { | |
1015 | gdb_flush (gdb_stderr); | |
1016 | break; | |
1017 | } | |
1018 | case 2: | |
1019 | { | |
1020 | gdb_flush (gdb_stdlog); | |
1021 | break; | |
1022 | } | |
1023 | default: | |
1024 | gdb_flush (gdb_stdout); | |
1025 | } | |
1026 | ||
d57a3c85 TJB |
1027 | Py_RETURN_NONE; |
1028 | } | |
1029 | ||
80b6e756 PM |
1030 | /* Print a python exception trace, print just a message, or print |
1031 | nothing and clear the python exception, depending on | |
1032 | gdbpy_should_print_stack. Only call this if a python exception is | |
1033 | set. */ | |
d57a3c85 TJB |
1034 | void |
1035 | gdbpy_print_stack (void) | |
1036 | { | |
80b6e756 PM |
1037 | /* Print "none", just clear exception. */ |
1038 | if (gdbpy_should_print_stack == python_excp_none) | |
1039 | { | |
1040 | PyErr_Clear (); | |
1041 | } | |
1042 | /* Print "full" message and backtrace. */ | |
1043 | else if (gdbpy_should_print_stack == python_excp_full) | |
0bf0f8c4 DE |
1044 | { |
1045 | PyErr_Print (); | |
1046 | /* PyErr_Print doesn't necessarily end output with a newline. | |
1047 | This works because Python's stdout/stderr is fed through | |
1048 | printf_filtered. */ | |
1049 | begin_line (); | |
1050 | } | |
80b6e756 | 1051 | /* Print "message", just error print message. */ |
d57a3c85 | 1052 | else |
80b6e756 PM |
1053 | { |
1054 | PyObject *ptype, *pvalue, *ptraceback; | |
1055 | char *msg = NULL, *type = NULL; | |
1056 | ||
1057 | PyErr_Fetch (&ptype, &pvalue, &ptraceback); | |
1058 | ||
1059 | /* Fetch the error message contained within ptype, pvalue. */ | |
1060 | msg = gdbpy_exception_to_string (ptype, pvalue); | |
1061 | type = gdbpy_obj_to_string (ptype); | |
1062 | if (msg == NULL) | |
1063 | { | |
1064 | /* An error occurred computing the string representation of the | |
1065 | error message. */ | |
1066 | fprintf_filtered (gdb_stderr, | |
1067 | _("Error occurred computing Python error" \ | |
1068 | "message.\n")); | |
1069 | } | |
1070 | else | |
1071 | fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n", | |
1072 | type, msg); | |
1073 | ||
1074 | Py_XDECREF (ptype); | |
1075 | Py_XDECREF (pvalue); | |
1076 | Py_XDECREF (ptraceback); | |
1077 | xfree (msg); | |
1078 | } | |
d57a3c85 TJB |
1079 | } |
1080 | ||
89c73ade TT |
1081 | \f |
1082 | ||
fa33c3cd DE |
1083 | /* Return the current Progspace. |
1084 | There always is one. */ | |
1085 | ||
1086 | static PyObject * | |
1087 | gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2) | |
1088 | { | |
1089 | PyObject *result; | |
1090 | ||
1091 | result = pspace_to_pspace_object (current_program_space); | |
1092 | if (result) | |
1093 | Py_INCREF (result); | |
1094 | return result; | |
1095 | } | |
1096 | ||
1097 | /* Return a sequence holding all the Progspaces. */ | |
1098 | ||
1099 | static PyObject * | |
1100 | gdbpy_progspaces (PyObject *unused1, PyObject *unused2) | |
1101 | { | |
1102 | struct program_space *ps; | |
1103 | PyObject *list; | |
1104 | ||
1105 | list = PyList_New (0); | |
1106 | if (!list) | |
1107 | return NULL; | |
1108 | ||
1109 | ALL_PSPACES (ps) | |
1110 | { | |
1111 | PyObject *item = pspace_to_pspace_object (ps); | |
d59b6f6c | 1112 | |
fa33c3cd DE |
1113 | if (!item || PyList_Append (list, item) == -1) |
1114 | { | |
1115 | Py_DECREF (list); | |
1116 | return NULL; | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | return list; | |
1121 | } | |
1122 | ||
1123 | \f | |
1124 | ||
89c73ade | 1125 | /* The "current" objfile. This is set when gdb detects that a new |
8a1ea21f DE |
1126 | objfile has been loaded. It is only set for the duration of a call to |
1127 | source_python_script_for_objfile; it is NULL at other times. */ | |
89c73ade TT |
1128 | static struct objfile *gdbpy_current_objfile; |
1129 | ||
4c63965b JK |
1130 | /* Set the current objfile to OBJFILE and then read FILE named FILENAME |
1131 | as Python code. This does not throw any errors. If an exception | |
1132 | occurs python will print the traceback and clear the error indicator. */ | |
89c73ade | 1133 | |
8a1ea21f | 1134 | void |
4c63965b JK |
1135 | source_python_script_for_objfile (struct objfile *objfile, FILE *file, |
1136 | const char *filename) | |
89c73ade | 1137 | { |
89c73ade TT |
1138 | struct cleanup *cleanups; |
1139 | ||
d452c4bc | 1140 | cleanups = ensure_python_env (get_objfile_arch (objfile), current_language); |
89c73ade TT |
1141 | gdbpy_current_objfile = objfile; |
1142 | ||
4c63965b | 1143 | python_run_simple_file (file, filename); |
89c73ade TT |
1144 | |
1145 | do_cleanups (cleanups); | |
1146 | gdbpy_current_objfile = NULL; | |
89c73ade TT |
1147 | } |
1148 | ||
1149 | /* Return the current Objfile, or None if there isn't one. */ | |
8a1ea21f | 1150 | |
89c73ade TT |
1151 | static PyObject * |
1152 | gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) | |
1153 | { | |
1154 | PyObject *result; | |
1155 | ||
1156 | if (! gdbpy_current_objfile) | |
1157 | Py_RETURN_NONE; | |
1158 | ||
1159 | result = objfile_to_objfile_object (gdbpy_current_objfile); | |
1160 | if (result) | |
1161 | Py_INCREF (result); | |
1162 | return result; | |
1163 | } | |
1164 | ||
1165 | /* Return a sequence holding all the Objfiles. */ | |
fa33c3cd | 1166 | |
89c73ade TT |
1167 | static PyObject * |
1168 | gdbpy_objfiles (PyObject *unused1, PyObject *unused2) | |
1169 | { | |
1170 | struct objfile *objf; | |
1171 | PyObject *list; | |
1172 | ||
1173 | list = PyList_New (0); | |
1174 | if (!list) | |
1175 | return NULL; | |
1176 | ||
1177 | ALL_OBJFILES (objf) | |
1178 | { | |
1179 | PyObject *item = objfile_to_objfile_object (objf); | |
d59b6f6c | 1180 | |
89c73ade TT |
1181 | if (!item || PyList_Append (list, item) == -1) |
1182 | { | |
1183 | Py_DECREF (list); | |
1184 | return NULL; | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | return list; | |
1189 | } | |
1190 | ||
18a9fc12 TT |
1191 | /* Compute the list of active type printers and return it. The result |
1192 | of this function can be passed to apply_type_printers, and should | |
1193 | be freed by free_type_printers. */ | |
1194 | ||
1195 | void * | |
1196 | start_type_printers (void) | |
1197 | { | |
1198 | struct cleanup *cleanups; | |
31594462 | 1199 | PyObject *type_module, *func, *result_obj = NULL; |
18a9fc12 TT |
1200 | |
1201 | cleanups = ensure_python_env (get_current_arch (), current_language); | |
1202 | ||
1203 | type_module = PyImport_ImportModule ("gdb.types"); | |
1204 | if (type_module == NULL) | |
1205 | { | |
1206 | gdbpy_print_stack (); | |
1207 | goto done; | |
1208 | } | |
1209 | make_cleanup_py_decref (type_module); | |
1210 | ||
1211 | func = PyObject_GetAttrString (type_module, "get_type_recognizers"); | |
1212 | if (func == NULL) | |
1213 | { | |
1214 | gdbpy_print_stack (); | |
1215 | goto done; | |
1216 | } | |
1217 | make_cleanup_py_decref (func); | |
1218 | ||
1219 | result_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL); | |
1220 | if (result_obj == NULL) | |
1221 | gdbpy_print_stack (); | |
1222 | ||
1223 | done: | |
1224 | do_cleanups (cleanups); | |
1225 | return result_obj; | |
1226 | } | |
1227 | ||
1228 | /* If TYPE is recognized by some type printer, return a newly | |
1229 | allocated string holding the type's replacement name. The caller | |
1230 | is responsible for freeing the string. Otherwise, return NULL. | |
1231 | ||
1232 | This function has a bit of a funny name, since it actually applies | |
1233 | recognizers, but this seemed clearer given the start_type_printers | |
1234 | and free_type_printers functions. */ | |
1235 | ||
1236 | char * | |
1237 | apply_type_printers (void *printers, struct type *type) | |
1238 | { | |
1239 | struct cleanup *cleanups; | |
1240 | PyObject *type_obj, *type_module, *func, *result_obj; | |
1241 | PyObject *printers_obj = printers; | |
1242 | char *result = NULL; | |
1243 | ||
1244 | if (printers_obj == NULL) | |
1245 | return NULL; | |
1246 | ||
1247 | cleanups = ensure_python_env (get_current_arch (), current_language); | |
1248 | ||
1249 | type_obj = type_to_type_object (type); | |
1250 | if (type_obj == NULL) | |
1251 | { | |
1252 | gdbpy_print_stack (); | |
1253 | goto done; | |
1254 | } | |
1255 | make_cleanup_py_decref (type_obj); | |
1256 | ||
1257 | type_module = PyImport_ImportModule ("gdb.types"); | |
1258 | if (type_module == NULL) | |
1259 | { | |
1260 | gdbpy_print_stack (); | |
1261 | goto done; | |
1262 | } | |
1263 | make_cleanup_py_decref (type_module); | |
1264 | ||
1265 | func = PyObject_GetAttrString (type_module, "apply_type_recognizers"); | |
1266 | if (func == NULL) | |
1267 | { | |
1268 | gdbpy_print_stack (); | |
1269 | goto done; | |
1270 | } | |
1271 | make_cleanup_py_decref (func); | |
1272 | ||
1273 | result_obj = PyObject_CallFunctionObjArgs (func, printers_obj, | |
1274 | type_obj, (char *) NULL); | |
1275 | if (result_obj == NULL) | |
1276 | { | |
1277 | gdbpy_print_stack (); | |
1278 | goto done; | |
1279 | } | |
1280 | make_cleanup_py_decref (result_obj); | |
1281 | ||
1282 | if (result_obj != Py_None) | |
1283 | { | |
1284 | result = python_string_to_host_string (result_obj); | |
1285 | if (result == NULL) | |
1286 | gdbpy_print_stack (); | |
1287 | } | |
1288 | ||
1289 | done: | |
1290 | do_cleanups (cleanups); | |
1291 | return result; | |
1292 | } | |
1293 | ||
1294 | /* Free the result of start_type_printers. */ | |
1295 | ||
1296 | void | |
1297 | free_type_printers (void *arg) | |
1298 | { | |
1299 | struct cleanup *cleanups; | |
1300 | PyObject *printers = arg; | |
1301 | ||
1302 | if (printers == NULL) | |
1303 | return; | |
1304 | ||
1305 | cleanups = ensure_python_env (get_current_arch (), current_language); | |
1306 | Py_DECREF (printers); | |
1307 | do_cleanups (cleanups); | |
1308 | } | |
1309 | ||
d57a3c85 TJB |
1310 | #else /* HAVE_PYTHON */ |
1311 | ||
8315665e YPK |
1312 | /* Dummy implementation of the gdb "python-interactive" and "python" |
1313 | command. */ | |
d57a3c85 TJB |
1314 | |
1315 | static void | |
8315665e | 1316 | python_interactive_command (char *arg, int from_tty) |
d57a3c85 TJB |
1317 | { |
1318 | while (arg && *arg && isspace (*arg)) | |
1319 | ++arg; | |
1320 | if (arg && *arg) | |
1321 | error (_("Python scripting is not supported in this copy of GDB.")); | |
1322 | else | |
1323 | { | |
1324 | struct command_line *l = get_command_line (python_control, ""); | |
1325 | struct cleanup *cleanups = make_cleanup_free_command_lines (&l); | |
d59b6f6c | 1326 | |
d57a3c85 TJB |
1327 | execute_control_command_untraced (l); |
1328 | do_cleanups (cleanups); | |
1329 | } | |
1330 | } | |
1331 | ||
8315665e YPK |
1332 | static void |
1333 | python_command (char *arg, int from_tty) | |
1334 | { | |
1335 | python_interactive_command (arg, from_tty); | |
1336 | } | |
1337 | ||
d57a3c85 TJB |
1338 | void |
1339 | eval_python_from_control_command (struct command_line *cmd) | |
1340 | { | |
1341 | error (_("Python scripting is not supported in this copy of GDB.")); | |
1342 | } | |
1343 | ||
973817a3 | 1344 | void |
4c63965b | 1345 | source_python_script (FILE *file, const char *filename) |
973817a3 | 1346 | { |
973817a3 JB |
1347 | throw_error (UNSUPPORTED_ERROR, |
1348 | _("Python scripting is not supported in this copy of GDB.")); | |
1349 | } | |
1350 | ||
7371cf6d PM |
1351 | int |
1352 | gdbpy_should_stop (struct breakpoint_object *bp_obj) | |
1353 | { | |
1354 | internal_error (__FILE__, __LINE__, | |
1355 | _("gdbpy_should_stop called when Python scripting is " \ | |
1356 | "not supported.")); | |
1357 | } | |
1358 | ||
1359 | int | |
1360 | gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj) | |
1361 | { | |
1362 | internal_error (__FILE__, __LINE__, | |
1363 | _("gdbpy_breakpoint_has_py_cond called when Python " \ | |
1364 | "scripting is not supported.")); | |
1365 | } | |
1366 | ||
18a9fc12 TT |
1367 | void * |
1368 | start_type_printers (void) | |
1369 | { | |
1370 | return NULL; | |
1371 | } | |
1372 | ||
1373 | char * | |
1374 | apply_type_printers (void *ignore, struct type *type) | |
1375 | { | |
1376 | return NULL; | |
1377 | } | |
1378 | ||
1379 | void | |
1380 | free_type_printers (void *arg) | |
1381 | { | |
1382 | } | |
1383 | ||
d57a3c85 TJB |
1384 | #endif /* HAVE_PYTHON */ |
1385 | ||
1386 | \f | |
1387 | ||
713389e0 PM |
1388 | /* Lists for 'set python' commands. */ |
1389 | ||
1390 | static struct cmd_list_element *user_set_python_list; | |
1391 | static struct cmd_list_element *user_show_python_list; | |
d57a3c85 | 1392 | |
713389e0 PM |
1393 | /* Function for use by 'set python' prefix command. */ |
1394 | ||
1395 | static void | |
1396 | user_set_python (char *args, int from_tty) | |
1397 | { | |
1398 | help_list (user_set_python_list, "set python ", all_commands, | |
1399 | gdb_stdout); | |
1400 | } | |
1401 | ||
1402 | /* Function for use by 'show python' prefix command. */ | |
1403 | ||
1404 | static void | |
1405 | user_show_python (char *args, int from_tty) | |
d57a3c85 | 1406 | { |
713389e0 | 1407 | cmd_show_list (user_show_python_list, from_tty, ""); |
d57a3c85 TJB |
1408 | } |
1409 | ||
1410 | /* Initialize the Python code. */ | |
1411 | ||
810849a3 AS |
1412 | #ifdef HAVE_PYTHON |
1413 | ||
d7de8e3c TT |
1414 | /* This is installed as a final cleanup and cleans up the |
1415 | interpreter. This lets Python's 'atexit' work. */ | |
1416 | ||
1417 | static void | |
1418 | finalize_python (void *ignore) | |
1419 | { | |
1420 | /* We don't use ensure_python_env here because if we ever ran the | |
1421 | cleanup, gdb would crash -- because the cleanup calls into the | |
1422 | Python interpreter, which we are about to destroy. It seems | |
1423 | clearer to make the needed calls explicitly here than to create a | |
1424 | cleanup and then mysteriously discard it. */ | |
b1209b03 | 1425 | (void) PyGILState_Ensure (); |
f5656ead | 1426 | python_gdbarch = target_gdbarch (); |
d7de8e3c TT |
1427 | python_language = current_language; |
1428 | ||
1429 | Py_Finalize (); | |
1430 | } | |
810849a3 | 1431 | #endif |
d7de8e3c | 1432 | |
2c0b251b PA |
1433 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1434 | extern initialize_file_ftype _initialize_python; | |
1435 | ||
d57a3c85 TJB |
1436 | void |
1437 | _initialize_python (void) | |
1438 | { | |
9a27f2c6 PK |
1439 | char *progname; |
1440 | #ifdef IS_PY3K | |
1441 | int i; | |
1442 | size_t progsize, count; | |
1443 | char *oldloc; | |
1444 | wchar_t *progname_copy; | |
1445 | #endif | |
713389e0 | 1446 | |
8315665e YPK |
1447 | add_com ("python-interactive", class_obscure, |
1448 | python_interactive_command, | |
1449 | #ifdef HAVE_PYTHON | |
1450 | _("\ | |
e3480f4a YPK |
1451 | Start an interactive Python prompt.\n\ |
1452 | \n\ | |
1453 | To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\ | |
1454 | prompt).\n\ | |
8315665e YPK |
1455 | \n\ |
1456 | Alternatively, a single-line Python command can be given as an\n\ | |
1457 | argument, and if the command is an expression, the result will be\n\ | |
1458 | printed. For example:\n\ | |
1459 | \n\ | |
1460 | (gdb) python-interactive 2 + 3\n\ | |
1461 | 5\n\ | |
1462 | ") | |
1463 | #else /* HAVE_PYTHON */ | |
1464 | _("\ | |
1465 | Start a Python interactive prompt.\n\ | |
1466 | \n\ | |
1467 | Python scripting is not supported in this copy of GDB.\n\ | |
1468 | This command is only a placeholder.") | |
1469 | #endif /* HAVE_PYTHON */ | |
1470 | ); | |
1471 | add_com_alias ("pi", "python-interactive", class_obscure, 1); | |
1472 | ||
d57a3c85 TJB |
1473 | add_com ("python", class_obscure, python_command, |
1474 | #ifdef HAVE_PYTHON | |
1475 | _("\ | |
1476 | Evaluate a Python command.\n\ | |
1477 | \n\ | |
1478 | The command can be given as an argument, for instance:\n\ | |
1479 | \n\ | |
1480 | python print 23\n\ | |
1481 | \n\ | |
1482 | If no argument is given, the following lines are read and used\n\ | |
1483 | as the Python commands. Type a line containing \"end\" to indicate\n\ | |
1484 | the end of the command.") | |
1485 | #else /* HAVE_PYTHON */ | |
1486 | _("\ | |
1487 | Evaluate a Python command.\n\ | |
1488 | \n\ | |
1489 | Python scripting is not supported in this copy of GDB.\n\ | |
1490 | This command is only a placeholder.") | |
1491 | #endif /* HAVE_PYTHON */ | |
1492 | ); | |
8315665e | 1493 | add_com_alias ("py", "python", class_obscure, 1); |
d57a3c85 | 1494 | |
713389e0 PM |
1495 | /* Add set/show python print-stack. */ |
1496 | add_prefix_cmd ("python", no_class, user_show_python, | |
1497 | _("Prefix command for python preference settings."), | |
1498 | &user_show_python_list, "show python ", 0, | |
1499 | &showlist); | |
1500 | ||
1501 | add_prefix_cmd ("python", no_class, user_set_python, | |
1502 | _("Prefix command for python preference settings."), | |
1503 | &user_set_python_list, "set python ", 0, | |
1504 | &setlist); | |
1505 | ||
80b6e756 PM |
1506 | add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, |
1507 | &gdbpy_should_print_stack, _("\ | |
1508 | Set mode for Python stack dump on error."), _("\ | |
1509 | Show the mode of Python stack printing on error."), _("\ | |
1510 | none == no stack or message will be printed.\n\ | |
1511 | full == a message and a stack will be printed.\n\ | |
1512 | message == an error message without a stack will be printed."), | |
1513 | NULL, NULL, | |
1514 | &user_set_python_list, | |
1515 | &user_show_python_list); | |
d57a3c85 TJB |
1516 | |
1517 | #ifdef HAVE_PYTHON | |
0c4a4063 DE |
1518 | #ifdef WITH_PYTHON_PATH |
1519 | /* Work around problem where python gets confused about where it is, | |
1520 | and then can't find its libraries, etc. | |
1521 | NOTE: Python assumes the following layout: | |
1522 | /foo/bin/python | |
1523 | /foo/lib/pythonX.Y/... | |
1524 | This must be done before calling Py_Initialize. */ | |
9a27f2c6 PK |
1525 | progname = concat (ldirname (python_libdir), SLASH_STRING, "bin", |
1526 | SLASH_STRING, "python", NULL); | |
1527 | #ifdef IS_PY3K | |
1528 | oldloc = setlocale (LC_ALL, NULL); | |
1529 | setlocale (LC_ALL, ""); | |
1530 | progsize = strlen (progname); | |
1531 | if (progsize == (size_t) -1) | |
1532 | { | |
1533 | fprintf (stderr, "Could not convert python path to string\n"); | |
1534 | return; | |
1535 | } | |
1536 | progname_copy = PyMem_Malloc ((progsize + 1) * sizeof (wchar_t)); | |
1537 | if (!progname_copy) | |
1538 | { | |
1539 | fprintf (stderr, "out of memory\n"); | |
1540 | return; | |
1541 | } | |
1542 | count = mbstowcs (progname_copy, progname, progsize + 1); | |
1543 | if (count == (size_t) -1) | |
1544 | { | |
1545 | fprintf (stderr, "Could not convert python path to string\n"); | |
1546 | return; | |
1547 | } | |
1548 | setlocale (LC_ALL, oldloc); | |
1549 | ||
1550 | /* Note that Py_SetProgramName expects the string it is passed to | |
1551 | remain alive for the duration of the program's execution, so | |
1552 | it is not freed after this call. */ | |
1553 | Py_SetProgramName (progname_copy); | |
1554 | #else | |
1555 | Py_SetProgramName (progname); | |
1556 | #endif | |
0c4a4063 DE |
1557 | #endif |
1558 | ||
d57a3c85 | 1559 | Py_Initialize (); |
ca30a762 | 1560 | PyEval_InitThreads (); |
d57a3c85 | 1561 | |
9a27f2c6 PK |
1562 | #ifdef IS_PY3K |
1563 | gdb_module = PyModule_Create (&GdbModuleDef); | |
1564 | /* Add _gdb module to the list of known built-in modules. */ | |
1565 | _PyImport_FixupBuiltin (gdb_module, "_gdb"); | |
1566 | #else | |
b9516fa1 | 1567 | gdb_module = Py_InitModule ("_gdb", GdbMethods); |
9a27f2c6 | 1568 | #endif |
d57a3c85 TJB |
1569 | |
1570 | /* The casts to (char*) are for python 2.4. */ | |
1571 | PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version); | |
1572 | PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name); | |
9a2b4c1b MS |
1573 | PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", |
1574 | (char*) target_name); | |
f17618ea | 1575 | |
99c3dc11 PM |
1576 | /* Add stream constants. */ |
1577 | PyModule_AddIntConstant (gdb_module, "STDOUT", 0); | |
1578 | PyModule_AddIntConstant (gdb_module, "STDERR", 1); | |
1579 | PyModule_AddIntConstant (gdb_module, "STDLOG", 2); | |
d57a3c85 | 1580 | |
621c8364 TT |
1581 | gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL); |
1582 | PyModule_AddObject (gdb_module, "error", gdbpy_gdb_error); | |
1583 | ||
1584 | gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError", | |
1585 | gdbpy_gdb_error, NULL); | |
1586 | PyModule_AddObject (gdb_module, "MemoryError", gdbpy_gdb_memory_error); | |
1587 | ||
07ca107c DE |
1588 | gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL); |
1589 | PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc); | |
1590 | ||
037bbc8e | 1591 | gdbpy_initialize_gdb_readline (); |
8a1ea21f | 1592 | gdbpy_initialize_auto_load (); |
a08702d6 | 1593 | gdbpy_initialize_values (); |
f8f6f20b | 1594 | gdbpy_initialize_frames (); |
d8906c6f | 1595 | gdbpy_initialize_commands (); |
f3e9a817 PM |
1596 | gdbpy_initialize_symbols (); |
1597 | gdbpy_initialize_symtabs (); | |
1598 | gdbpy_initialize_blocks (); | |
bc3b79fd | 1599 | gdbpy_initialize_functions (); |
d7b32ed3 | 1600 | gdbpy_initialize_parameters (); |
2c74e833 | 1601 | gdbpy_initialize_types (); |
fa33c3cd | 1602 | gdbpy_initialize_pspace (); |
89c73ade | 1603 | gdbpy_initialize_objfile (); |
adc36818 | 1604 | gdbpy_initialize_breakpoints (); |
cc72b2a2 | 1605 | gdbpy_initialize_finishbreakpoints (); |
be759fcf | 1606 | gdbpy_initialize_lazy_string (); |
595939de PM |
1607 | gdbpy_initialize_thread (); |
1608 | gdbpy_initialize_inferior (); | |
ca5c20b6 | 1609 | gdbpy_initialize_events (); |
a08702d6 | 1610 | |
505500db SW |
1611 | gdbpy_initialize_eventregistry (); |
1612 | gdbpy_initialize_py_events (); | |
1613 | gdbpy_initialize_event (); | |
1614 | gdbpy_initialize_stop_event (); | |
1615 | gdbpy_initialize_signal_event (); | |
1616 | gdbpy_initialize_breakpoint_event (); | |
1617 | gdbpy_initialize_continue_event (); | |
1618 | gdbpy_initialize_exited_event (); | |
1619 | gdbpy_initialize_thread_event (); | |
20c168b5 | 1620 | gdbpy_initialize_new_objfile_event () ; |
bea883fd | 1621 | gdbpy_initialize_arch (); |
505500db | 1622 | |
d17b6f81 PM |
1623 | observer_attach_before_prompt (before_prompt_hook); |
1624 | ||
a6bac58e TT |
1625 | gdbpy_to_string_cst = PyString_FromString ("to_string"); |
1626 | gdbpy_children_cst = PyString_FromString ("children"); | |
1627 | gdbpy_display_hint_cst = PyString_FromString ("display_hint"); | |
d8906c6f | 1628 | gdbpy_doc_cst = PyString_FromString ("__doc__"); |
967cf477 | 1629 | gdbpy_enabled_cst = PyString_FromString ("enabled"); |
fb6a3ed3 | 1630 | gdbpy_value_cst = PyString_FromString ("value"); |
d8906c6f | 1631 | |
9dea9163 DE |
1632 | /* Release the GIL while gdb runs. */ |
1633 | PyThreadState_Swap (NULL); | |
1634 | PyEval_ReleaseLock (); | |
1635 | ||
d7de8e3c | 1636 | make_final_cleanup (finalize_python, NULL); |
9dea9163 DE |
1637 | #endif /* HAVE_PYTHON */ |
1638 | } | |
1639 | ||
1640 | #ifdef HAVE_PYTHON | |
1641 | ||
1642 | /* Perform the remaining python initializations. | |
1643 | These must be done after GDB is at least mostly initialized. | |
1644 | E.g., The "info pretty-printer" command needs the "info" prefix | |
1645 | command installed. */ | |
1646 | ||
1647 | void | |
1648 | finish_python_initialization (void) | |
1649 | { | |
b9516fa1 YPK |
1650 | PyObject *m; |
1651 | char *gdb_pythondir; | |
1652 | PyObject *sys_path; | |
9dea9163 DE |
1653 | struct cleanup *cleanup; |
1654 | ||
1655 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
f17618ea | 1656 | |
b9516fa1 YPK |
1657 | /* Add the initial data-directory to sys.path. */ |
1658 | ||
1659 | gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL); | |
1660 | make_cleanup (xfree, gdb_pythondir); | |
1661 | ||
1662 | sys_path = PySys_GetObject ("path"); | |
ca30a762 | 1663 | |
9a27f2c6 PK |
1664 | /* If sys.path is not defined yet, define it first. */ |
1665 | if (!(sys_path && PyList_Check (sys_path))) | |
1666 | { | |
1667 | #ifdef IS_PY3K | |
1668 | PySys_SetPath (L""); | |
1669 | #else | |
1670 | PySys_SetPath (""); | |
1671 | #endif | |
1672 | sys_path = PySys_GetObject ("path"); | |
1673 | } | |
1674 | if (sys_path && PyList_Check (sys_path)) | |
b9516fa1 YPK |
1675 | { |
1676 | PyObject *pythondir; | |
1677 | int err; | |
1678 | ||
1679 | pythondir = PyString_FromString (gdb_pythondir); | |
1680 | if (pythondir == NULL) | |
1681 | goto fail; | |
1682 | ||
1683 | err = PyList_Insert (sys_path, 0, pythondir); | |
1684 | if (err) | |
1685 | goto fail; | |
1686 | ||
1687 | Py_DECREF (pythondir); | |
1688 | } | |
1689 | else | |
9a27f2c6 | 1690 | goto fail; |
b9516fa1 YPK |
1691 | |
1692 | /* Import the gdb module to finish the initialization, and | |
1693 | add it to __main__ for convenience. */ | |
1694 | m = PyImport_AddModule ("__main__"); | |
1695 | if (m == NULL) | |
1696 | goto fail; | |
1697 | ||
1698 | gdb_python_module = PyImport_ImportModule ("gdb"); | |
1699 | if (gdb_python_module == NULL) | |
1700 | { | |
1701 | gdbpy_print_stack (); | |
1702 | warning (_("Could not load the Python gdb module from `%s'."), | |
1703 | gdb_pythondir); | |
1704 | warning (_("Limited Python support is available from the _gdb module.")); | |
1705 | do_cleanups (cleanup); | |
1706 | return; | |
1707 | } | |
1708 | ||
1709 | if (PyModule_AddObject (m, "gdb", gdb_python_module)) | |
1710 | goto fail; | |
1711 | ||
1712 | /* Keep the reference to gdb_python_module since it is in a global | |
1713 | variable. */ | |
1714 | ||
1715 | do_cleanups (cleanup); | |
1716 | return; | |
1717 | ||
1718 | fail: | |
1719 | gdbpy_print_stack (); | |
1720 | warning (_("internal error: Unhandled Python exception")); | |
9dea9163 DE |
1721 | do_cleanups (cleanup); |
1722 | } | |
ca30a762 | 1723 | |
d57a3c85 | 1724 | #endif /* HAVE_PYTHON */ |
12453b93 TJB |
1725 | |
1726 | \f | |
1727 | ||
9dea9163 | 1728 | #ifdef HAVE_PYTHON |
12453b93 TJB |
1729 | |
1730 | static PyMethodDef GdbMethods[] = | |
1731 | { | |
1732 | { "history", gdbpy_history, METH_VARARGS, | |
1733 | "Get a value from history" }, | |
bc9f0842 | 1734 | { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 1735 | "Execute a gdb command" }, |
8f500870 | 1736 | { "parameter", gdbpy_parameter, METH_VARARGS, |
12453b93 TJB |
1737 | "Return a gdb parameter's value" }, |
1738 | ||
adc36818 PM |
1739 | { "breakpoints", gdbpy_breakpoints, METH_NOARGS, |
1740 | "Return a tuple of all breakpoint objects" }, | |
1741 | ||
b6313243 TT |
1742 | { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS, |
1743 | "Find the default visualizer for a Value." }, | |
1744 | ||
fa33c3cd DE |
1745 | { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS, |
1746 | "Return the current Progspace." }, | |
1747 | { "progspaces", gdbpy_progspaces, METH_NOARGS, | |
1748 | "Return a sequence of all progspaces." }, | |
1749 | ||
89c73ade TT |
1750 | { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS, |
1751 | "Return the current Objfile being loaded, or None." }, | |
1752 | { "objfiles", gdbpy_objfiles, METH_NOARGS, | |
1753 | "Return a sequence of all loaded objfiles." }, | |
1754 | ||
d8e22779 TT |
1755 | { "newest_frame", gdbpy_newest_frame, METH_NOARGS, |
1756 | "newest_frame () -> gdb.Frame.\n\ | |
1757 | Return the newest frame object." }, | |
f8f6f20b TJB |
1758 | { "selected_frame", gdbpy_selected_frame, METH_NOARGS, |
1759 | "selected_frame () -> gdb.Frame.\n\ | |
1760 | Return the selected frame object." }, | |
1761 | { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS, | |
1762 | "stop_reason_string (Integer) -> String.\n\ | |
1763 | Return a string explaining unwind stop reason." }, | |
1764 | ||
2c74e833 TT |
1765 | { "lookup_type", (PyCFunction) gdbpy_lookup_type, |
1766 | METH_VARARGS | METH_KEYWORDS, | |
1767 | "lookup_type (name [, block]) -> type\n\ | |
1768 | Return a Type corresponding to the given name." }, | |
f3e9a817 PM |
1769 | { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol, |
1770 | METH_VARARGS | METH_KEYWORDS, | |
1771 | "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\ | |
1772 | Return a tuple with the symbol corresponding to the given name (or None) and\n\ | |
1773 | a boolean indicating if name is a field of the current implied argument\n\ | |
1774 | `this' (when the current language is object-oriented)." }, | |
6e6fbe60 DE |
1775 | { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol, |
1776 | METH_VARARGS | METH_KEYWORDS, | |
1777 | "lookup_global_symbol (name [, domain]) -> symbol\n\ | |
1778 | Return the symbol corresponding to the given name (or None)." }, | |
f3e9a817 PM |
1779 | { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS, |
1780 | "Return the block containing the given pc value, or None." }, | |
cb2e07a6 PM |
1781 | { "solib_name", gdbpy_solib_name, METH_VARARGS, |
1782 | "solib_name (Long) -> String.\n\ | |
1783 | Return the name of the shared library holding a given address, or None." }, | |
1784 | { "decode_line", gdbpy_decode_line, METH_VARARGS, | |
1785 | "decode_line (String) -> Tuple. Decode a string argument the way\n\ | |
1786 | that 'break' or 'edit' does. Return a tuple containing two elements.\n\ | |
1787 | The first element contains any unparsed portion of the String parameter\n\ | |
1788 | (or None if the string was fully parsed). The second element contains\n\ | |
1789 | a tuple that contains all the locations that match, represented as\n\ | |
1790 | gdb.Symtab_and_line objects (or None)."}, | |
57a1d736 TT |
1791 | { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, |
1792 | "parse_and_eval (String) -> Value.\n\ | |
1793 | Parse String as an expression, evaluate it, and return the result as a Value." | |
1794 | }, | |
7efc75aa SCR |
1795 | { "find_pc_line", gdbpy_find_pc_line, METH_VARARGS, |
1796 | "find_pc_line (pc) -> Symtab_and_line.\n\ | |
1797 | Return the gdb.Symtab_and_line object corresponding to the pc value." }, | |
57a1d736 | 1798 | |
ca5c20b6 PM |
1799 | { "post_event", gdbpy_post_event, METH_VARARGS, |
1800 | "Post an event into gdb's event loop." }, | |
1801 | ||
f870a310 TT |
1802 | { "target_charset", gdbpy_target_charset, METH_NOARGS, |
1803 | "target_charset () -> string.\n\ | |
1804 | Return the name of the current target charset." }, | |
1805 | { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS, | |
1806 | "target_wide_charset () -> string.\n\ | |
1807 | Return the name of the current target wide charset." }, | |
1808 | ||
07ca107c DE |
1809 | { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS, |
1810 | "string_to_argv (String) -> Array.\n\ | |
1811 | Parse String and return an argv-like array.\n\ | |
1812 | Arguments are separate by spaces and may be quoted." | |
1813 | }, | |
99c3dc11 | 1814 | { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 1815 | "Write a string using gdb's filtered stream." }, |
99c3dc11 | 1816 | { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS, |
12453b93 | 1817 | "Flush gdb's filtered stdout stream." }, |
595939de PM |
1818 | { "selected_thread", gdbpy_selected_thread, METH_NOARGS, |
1819 | "selected_thread () -> gdb.InferiorThread.\n\ | |
1820 | Return the selected thread object." }, | |
2aa48337 KP |
1821 | { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS, |
1822 | "selected_inferior () -> gdb.Inferior.\n\ | |
1823 | Return the selected inferior object." }, | |
595939de PM |
1824 | { "inferiors", gdbpy_inferiors, METH_NOARGS, |
1825 | "inferiors () -> (gdb.Inferior, ...).\n\ | |
1826 | Return a tuple containing all inferiors." }, | |
12453b93 TJB |
1827 | {NULL, NULL, 0, NULL} |
1828 | }; | |
1829 | ||
9a27f2c6 PK |
1830 | #ifdef IS_PY3K |
1831 | static struct PyModuleDef GdbModuleDef = | |
1832 | { | |
1833 | PyModuleDef_HEAD_INIT, | |
1834 | "_gdb", | |
1835 | NULL, | |
1836 | -1, | |
1837 | GdbMethods, | |
1838 | NULL, | |
1839 | NULL, | |
1840 | NULL, | |
1841 | NULL | |
1842 | }; | |
1843 | #endif | |
12453b93 | 1844 | #endif /* HAVE_PYTHON */ |