python.c revision 1.8 1 1.1 christos /* General python/gdb code
2 1.1 christos
3 1.8 christos Copyright (C) 2008-2019 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "arch-utils.h"
22 1.1 christos #include "command.h"
23 1.1 christos #include "ui-out.h"
24 1.1 christos #include "cli/cli-script.h"
25 1.1 christos #include "gdbcmd.h"
26 1.1 christos #include "progspace.h"
27 1.1 christos #include "objfiles.h"
28 1.1 christos #include "value.h"
29 1.1 christos #include "language.h"
30 1.1 christos #include "event-loop.h"
31 1.1 christos #include "serial.h"
32 1.1 christos #include "readline/tilde.h"
33 1.1 christos #include "python.h"
34 1.3 christos #include "extension-priv.h"
35 1.1 christos #include "cli/cli-utils.h"
36 1.1 christos #include <ctype.h>
37 1.6 christos #include "location.h"
38 1.6 christos #include "ser-event.h"
39 1.1 christos
40 1.1 christos /* Declared constants and enum for python stack printing. */
41 1.1 christos static const char python_excp_none[] = "none";
42 1.1 christos static const char python_excp_full[] = "full";
43 1.1 christos static const char python_excp_message[] = "message";
44 1.1 christos
45 1.1 christos /* "set python print-stack" choices. */
46 1.1 christos static const char *const python_excp_enums[] =
47 1.1 christos {
48 1.1 christos python_excp_none,
49 1.1 christos python_excp_full,
50 1.1 christos python_excp_message,
51 1.1 christos NULL
52 1.1 christos };
53 1.1 christos
54 1.1 christos /* The exception printing variable. 'full' if we want to print the
55 1.1 christos error message and stack, 'none' if we want to print nothing, and
56 1.1 christos 'message' if we only want to print the error message. 'message' is
57 1.1 christos the default. */
58 1.1 christos static const char *gdbpy_should_print_stack = python_excp_message;
59 1.1 christos
60 1.1 christos #ifdef HAVE_PYTHON
61 1.3 christos /* Forward decls, these are defined later. */
62 1.5 christos extern const struct extension_language_script_ops python_extension_script_ops;
63 1.5 christos extern const struct extension_language_ops python_extension_ops;
64 1.3 christos #endif
65 1.3 christos
66 1.3 christos /* The main struct describing GDB's interface to the Python
67 1.3 christos extension language. */
68 1.3 christos const struct extension_language_defn extension_language_python =
69 1.3 christos {
70 1.3 christos EXT_LANG_PYTHON,
71 1.3 christos "python",
72 1.3 christos "Python",
73 1.3 christos
74 1.3 christos ".py",
75 1.3 christos "-gdb.py",
76 1.3 christos
77 1.3 christos python_control,
78 1.3 christos
79 1.3 christos #ifdef HAVE_PYTHON
80 1.3 christos &python_extension_script_ops,
81 1.3 christos &python_extension_ops
82 1.3 christos #else
83 1.3 christos NULL,
84 1.3 christos NULL
85 1.3 christos #endif
86 1.3 christos };
87 1.3 christos
88 1.3 christos #ifdef HAVE_PYTHON
90 1.1 christos
91 1.1 christos #include "cli/cli-decode.h"
92 1.1 christos #include "charset.h"
93 1.1 christos #include "top.h"
94 1.1 christos #include "python-internal.h"
95 1.1 christos #include "linespec.h"
96 1.8 christos #include "source.h"
97 1.1 christos #include "common/version.h"
98 1.1 christos #include "target.h"
99 1.1 christos #include "gdbthread.h"
100 1.1 christos #include "interps.h"
101 1.7 christos #include "event-top.h"
102 1.1 christos #include "py-event.h"
103 1.1 christos
104 1.1 christos /* True if Python has been successfully initialized, false
105 1.1 christos otherwise. */
106 1.1 christos
107 1.1 christos int gdb_python_initialized;
108 1.5 christos
109 1.1 christos extern PyMethodDef python_GdbMethods[];
110 1.1 christos
111 1.5 christos #ifdef IS_PY3K
112 1.1 christos extern struct PyModuleDef python_GdbModuleDef;
113 1.1 christos #endif
114 1.1 christos
115 1.1 christos PyObject *gdb_module;
116 1.1 christos PyObject *gdb_python_module;
117 1.1 christos
118 1.1 christos /* Some string constants we may wish to use. */
119 1.1 christos PyObject *gdbpy_to_string_cst;
120 1.1 christos PyObject *gdbpy_children_cst;
121 1.1 christos PyObject *gdbpy_display_hint_cst;
122 1.1 christos PyObject *gdbpy_doc_cst;
123 1.1 christos PyObject *gdbpy_enabled_cst;
124 1.1 christos PyObject *gdbpy_value_cst;
125 1.1 christos
126 1.1 christos /* The GdbError exception. */
127 1.1 christos PyObject *gdbpy_gdberror_exc;
128 1.1 christos
129 1.1 christos /* The `gdb.error' base class. */
130 1.1 christos PyObject *gdbpy_gdb_error;
131 1.1 christos
132 1.1 christos /* The `gdb.MemoryError' exception. */
133 1.1 christos PyObject *gdbpy_gdb_memory_error;
134 1.3 christos
135 1.3 christos static script_sourcer_func gdbpy_source_script;
136 1.5 christos static objfile_script_sourcer_func gdbpy_source_objfile_script;
137 1.3 christos static objfile_script_executor_func gdbpy_execute_objfile_script;
138 1.3 christos static void gdbpy_finish_initialization
139 1.3 christos (const struct extension_language_defn *);
140 1.3 christos static int gdbpy_initialized (const struct extension_language_defn *);
141 1.3 christos static void gdbpy_eval_from_control_command
142 1.3 christos (const struct extension_language_defn *, struct command_line *cmd);
143 1.3 christos static void gdbpy_start_type_printers (const struct extension_language_defn *,
144 1.3 christos struct ext_lang_type_printers *);
145 1.3 christos static enum ext_lang_rc gdbpy_apply_type_printers
146 1.3 christos (const struct extension_language_defn *,
147 1.3 christos const struct ext_lang_type_printers *, struct type *, char **);
148 1.3 christos static void gdbpy_free_type_printers (const struct extension_language_defn *,
149 1.3 christos struct ext_lang_type_printers *);
150 1.3 christos static void gdbpy_set_quit_flag (const struct extension_language_defn *);
151 1.3 christos static int gdbpy_check_quit_flag (const struct extension_language_defn *);
152 1.3 christos static enum ext_lang_rc gdbpy_before_prompt_hook
153 1.3 christos (const struct extension_language_defn *, const char *current_gdb_prompt);
154 1.3 christos
155 1.3 christos /* The interface between gdb proper and loading of python scripts. */
156 1.5 christos
157 1.3 christos const struct extension_language_script_ops python_extension_script_ops =
158 1.3 christos {
159 1.3 christos gdbpy_source_script,
160 1.5 christos gdbpy_source_objfile_script,
161 1.3 christos gdbpy_execute_objfile_script,
162 1.3 christos gdbpy_auto_load_enabled
163 1.3 christos };
164 1.3 christos
165 1.3 christos /* The interface between gdb proper and python extensions. */
166 1.5 christos
167 1.3 christos const struct extension_language_ops python_extension_ops =
168 1.3 christos {
169 1.3 christos gdbpy_finish_initialization,
170 1.3 christos gdbpy_initialized,
171 1.3 christos
172 1.3 christos gdbpy_eval_from_control_command,
173 1.3 christos
174 1.3 christos gdbpy_start_type_printers,
175 1.3 christos gdbpy_apply_type_printers,
176 1.3 christos gdbpy_free_type_printers,
177 1.3 christos
178 1.3 christos gdbpy_apply_val_pretty_printer,
179 1.3 christos
180 1.3 christos gdbpy_apply_frame_filter,
181 1.3 christos
182 1.3 christos gdbpy_preserve_values,
183 1.3 christos
184 1.3 christos gdbpy_breakpoint_has_cond,
185 1.3 christos gdbpy_breakpoint_cond_says_stop,
186 1.3 christos
187 1.3 christos gdbpy_set_quit_flag,
188 1.3 christos gdbpy_check_quit_flag,
189 1.3 christos
190 1.3 christos gdbpy_before_prompt_hook,
191 1.3 christos
192 1.3 christos gdbpy_get_matching_xmethod_workers,
193 1.3 christos };
194 1.1 christos
195 1.1 christos /* Architecture and language to be used in callbacks from
196 1.1 christos the Python interpreter. */
197 1.1 christos struct gdbarch *python_gdbarch;
198 1.1 christos const struct language_defn *python_language;
199 1.7 christos
200 1.7 christos gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch,
201 1.7 christos const struct language_defn *language)
202 1.7 christos : m_gdbarch (python_gdbarch),
203 1.7 christos m_language (python_language)
204 1.7 christos {
205 1.7 christos /* We should not ever enter Python unless initialized. */
206 1.7 christos if (!gdb_python_initialized)
207 1.7 christos error (_("Python not initialized"));
208 1.7 christos
209 1.7 christos m_previous_active = set_active_ext_lang (&extension_language_python);
210 1.7 christos
211 1.7 christos m_state = PyGILState_Ensure ();
212 1.7 christos
213 1.7 christos python_gdbarch = gdbarch;
214 1.1 christos python_language = language;
215 1.7 christos
216 1.8 christos /* Save it and ensure ! PyErr_Occurred () afterwards. */
217 1.7 christos m_error.emplace ();
218 1.1 christos }
219 1.7 christos
220 1.1 christos gdbpy_enter::~gdbpy_enter ()
221 1.1 christos {
222 1.1 christos /* Leftover Python error is forbidden by Python Exception Handling. */
223 1.1 christos if (PyErr_Occurred ())
224 1.1 christos {
225 1.1 christos /* This order is similar to the one calling error afterwards. */
226 1.1 christos gdbpy_print_stack ();
227 1.1 christos warning (_("internal error: Unhandled Python exception"));
228 1.1 christos }
229 1.8 christos
230 1.3 christos m_error->restore ();
231 1.7 christos
232 1.7 christos PyGILState_Release (m_state);
233 1.7 christos python_gdbarch = m_gdbarch;
234 1.3 christos python_language = m_language;
235 1.7 christos
236 1.1 christos restore_active_ext_lang (m_previous_active);
237 1.1 christos }
238 1.1 christos
239 1.1 christos /* Set the quit flag. */
240 1.3 christos
241 1.3 christos static void
242 1.1 christos gdbpy_set_quit_flag (const struct extension_language_defn *extlang)
243 1.1 christos {
244 1.1 christos PyErr_SetInterrupt ();
245 1.1 christos }
246 1.1 christos
247 1.1 christos /* Return true if the quit flag has been set, false otherwise. */
248 1.3 christos
249 1.3 christos static int
250 1.1 christos gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
251 1.1 christos {
252 1.1 christos return PyOS_InterruptOccurred ();
253 1.1 christos }
254 1.1 christos
255 1.1 christos /* Evaluate a Python command like PyRun_SimpleString, but uses
256 1.1 christos Py_single_input which prints the result of expressions, and does
257 1.1 christos not automatically print the stack on errors. */
258 1.1 christos
259 1.1 christos static int
260 1.1 christos eval_python_command (const char *command)
261 1.7 christos {
262 1.1 christos PyObject *m, *d;
263 1.1 christos
264 1.1 christos m = PyImport_AddModule ("__main__");
265 1.1 christos if (m == NULL)
266 1.1 christos return -1;
267 1.1 christos
268 1.1 christos d = PyModule_GetDict (m);
269 1.1 christos if (d == NULL)
270 1.7 christos return -1;
271 1.1 christos gdbpy_ref<> v (PyRun_StringFlags (command, Py_single_input, d, d, NULL));
272 1.1 christos if (v == NULL)
273 1.1 christos return -1;
274 1.1 christos
275 1.1 christos #ifndef IS_PY3K
276 1.1 christos if (Py_FlushLine ())
277 1.1 christos PyErr_Clear ();
278 1.1 christos #endif
279 1.1 christos
280 1.1 christos return 0;
281 1.1 christos }
282 1.1 christos
283 1.1 christos /* Implementation of the gdb "python-interactive" command. */
284 1.1 christos
285 1.8 christos static void
286 1.1 christos python_interactive_command (const char *arg, int from_tty)
287 1.6 christos {
288 1.1 christos struct ui *ui = current_ui;
289 1.1 christos int err;
290 1.7 christos
291 1.1 christos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
292 1.1 christos
293 1.1 christos arg = skip_spaces (arg);
294 1.7 christos
295 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
296 1.1 christos
297 1.1 christos if (arg && *arg)
298 1.8 christos {
299 1.8 christos std::string script = std::string (arg) + "\n";
300 1.1 christos err = eval_python_command (script.c_str ());
301 1.1 christos }
302 1.1 christos else
303 1.6 christos {
304 1.1 christos err = PyRun_InteractiveLoop (ui->instream, "<stdin>");
305 1.1 christos dont_repeat ();
306 1.1 christos }
307 1.1 christos
308 1.1 christos if (err)
309 1.1 christos {
310 1.1 christos gdbpy_print_stack ();
311 1.1 christos error (_("Error while executing Python code."));
312 1.1 christos }
313 1.1 christos }
314 1.1 christos
315 1.1 christos /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
316 1.1 christos named FILENAME.
317 1.1 christos
318 1.1 christos On Windows hosts few users would build Python themselves (this is no
319 1.1 christos trivial task on this platform), and thus use binaries built by
320 1.1 christos someone else instead. There may happen situation where the Python
321 1.1 christos library and GDB are using two different versions of the C runtime
322 1.1 christos library. Python, being built with VC, would use one version of the
323 1.1 christos msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
324 1.1 christos A FILE * from one runtime does not necessarily operate correctly in
325 1.1 christos the other runtime.
326 1.1 christos
327 1.1 christos To work around this potential issue, we create on Windows hosts the
328 1.1 christos FILE object using Python routines, thus making sure that it is
329 1.1 christos compatible with the Python library. */
330 1.1 christos
331 1.1 christos static void
332 1.1 christos python_run_simple_file (FILE *file, const char *filename)
333 1.1 christos {
334 1.1 christos #ifndef _WIN32
335 1.1 christos
336 1.1 christos PyRun_SimpleFile (file, filename);
337 1.1 christos
338 1.1 christos #else /* _WIN32 */
339 1.1 christos
340 1.1 christos /* Because we have a string for a filename, and are using Python to
341 1.7 christos open the file, we need to expand any tilde in the path first. */
342 1.7 christos gdb::unique_xmalloc_ptr<char> full_path (tilde_expand (filename));
343 1.7 christos gdbpy_ref<> python_file (PyFile_FromString (full_path.get (), (char *) "r"));
344 1.1 christos if (python_file == NULL)
345 1.1 christos {
346 1.7 christos gdbpy_print_stack ();
347 1.1 christos error (_("Error while opening file: %s"), full_path.get ());
348 1.1 christos }
349 1.7 christos
350 1.1 christos PyRun_SimpleFile (PyFile_AsFile (python_file.get ()), filename);
351 1.1 christos
352 1.1 christos #endif /* _WIN32 */
353 1.1 christos }
354 1.1 christos
355 1.8 christos /* Given a command_line, return a command string suitable for passing
356 1.1 christos to Python. Lines in the string are separated by newlines. */
357 1.8 christos
358 1.1 christos static std::string
359 1.1 christos compute_python_string (struct command_line *l)
360 1.1 christos {
361 1.8 christos struct command_line *iter;
362 1.1 christos std::string script;
363 1.1 christos
364 1.1 christos for (iter = l; iter; iter = iter->next)
365 1.8 christos {
366 1.8 christos script += iter->line;
367 1.1 christos script += '\n';
368 1.1 christos }
369 1.1 christos return script;
370 1.1 christos }
371 1.1 christos
372 1.1 christos /* Take a command line structure representing a 'python' command, and
373 1.1 christos evaluate its body using the Python interpreter. */
374 1.3 christos
375 1.3 christos static void
376 1.3 christos gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
377 1.1 christos struct command_line *cmd)
378 1.1 christos {
379 1.1 christos int ret;
380 1.8 christos
381 1.1 christos if (cmd->body_list_1 != nullptr)
382 1.1 christos error (_("Invalid \"python\" block structure."));
383 1.7 christos
384 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
385 1.8 christos
386 1.8 christos std::string script = compute_python_string (cmd->body_list_0.get ());
387 1.1 christos ret = PyRun_SimpleString (script.c_str ());
388 1.1 christos if (ret)
389 1.1 christos error (_("Error while executing Python code."));
390 1.1 christos }
391 1.1 christos
392 1.1 christos /* Implementation of the gdb "python" command. */
393 1.1 christos
394 1.8 christos static void
395 1.1 christos python_command (const char *arg, int from_tty)
396 1.7 christos {
397 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
398 1.7 christos
399 1.1 christos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
400 1.1 christos
401 1.1 christos arg = skip_spaces (arg);
402 1.1 christos if (arg && *arg)
403 1.1 christos {
404 1.1 christos if (PyRun_SimpleString (arg))
405 1.1 christos error (_("Error while executing Python code."));
406 1.1 christos }
407 1.1 christos else
408 1.8 christos {
409 1.1 christos counted_command_line l = get_command_line (python_control, "");
410 1.7 christos
411 1.1 christos execute_control_command_untraced (l.get ());
412 1.1 christos }
413 1.1 christos }
414 1.1 christos
415 1.1 christos
416 1.1 christos
418 1.1 christos /* Transform a gdb parameters's value into a Python value. May return
419 1.1 christos NULL (and set a Python exception) on error. Helper function for
420 1.1 christos get_parameter. */
421 1.1 christos PyObject *
422 1.1 christos gdbpy_parameter_value (enum var_types type, void *var)
423 1.1 christos {
424 1.1 christos switch (type)
425 1.1 christos {
426 1.1 christos case var_string:
427 1.1 christos case var_string_noescape:
428 1.1 christos case var_optional_filename:
429 1.1 christos case var_filename:
430 1.7 christos case var_enum:
431 1.1 christos {
432 1.1 christos const char *str = *(char **) var;
433 1.1 christos
434 1.8 christos if (! str)
435 1.1 christos str = "";
436 1.1 christos return host_string_to_python_string (str).release ();
437 1.1 christos }
438 1.1 christos
439 1.1 christos case var_boolean:
440 1.1 christos {
441 1.1 christos if (* (int *) var)
442 1.1 christos Py_RETURN_TRUE;
443 1.1 christos else
444 1.1 christos Py_RETURN_FALSE;
445 1.1 christos }
446 1.1 christos
447 1.1 christos case var_auto_boolean:
448 1.1 christos {
449 1.1 christos enum auto_boolean ab = * (enum auto_boolean *) var;
450 1.1 christos
451 1.1 christos if (ab == AUTO_BOOLEAN_TRUE)
452 1.1 christos Py_RETURN_TRUE;
453 1.1 christos else if (ab == AUTO_BOOLEAN_FALSE)
454 1.1 christos Py_RETURN_FALSE;
455 1.1 christos else
456 1.1 christos Py_RETURN_NONE;
457 1.1 christos }
458 1.1 christos
459 1.1 christos case var_integer:
460 1.1 christos if ((* (int *) var) == INT_MAX)
461 1.1 christos Py_RETURN_NONE;
462 1.8 christos /* Fall through. */
463 1.1 christos case var_zinteger:
464 1.1 christos case var_zuinteger_unlimited:
465 1.1 christos return PyLong_FromLong (* (int *) var);
466 1.1 christos
467 1.1 christos case var_uinteger:
468 1.1 christos {
469 1.1 christos unsigned int val = * (unsigned int *) var;
470 1.1 christos
471 1.1 christos if (val == UINT_MAX)
472 1.1 christos Py_RETURN_NONE;
473 1.8 christos return PyLong_FromUnsignedLong (val);
474 1.8 christos }
475 1.8 christos
476 1.8 christos case var_zuinteger:
477 1.8 christos {
478 1.8 christos unsigned int val = * (unsigned int *) var;
479 1.1 christos return PyLong_FromUnsignedLong (val);
480 1.1 christos }
481 1.1 christos }
482 1.1 christos
483 1.1 christos return PyErr_Format (PyExc_RuntimeError,
484 1.1 christos _("Programmer error: unhandled type."));
485 1.1 christos }
486 1.1 christos
487 1.1 christos /* A Python function which returns a gdb parameter's value as a Python
488 1.6 christos value. */
489 1.1 christos
490 1.1 christos static PyObject *
491 1.1 christos gdbpy_parameter (PyObject *self, PyObject *args)
492 1.1 christos {
493 1.1 christos struct cmd_list_element *alias, *prefix, *cmd;
494 1.1 christos const char *arg;
495 1.1 christos int found = -1;
496 1.1 christos
497 1.1 christos if (! PyArg_ParseTuple (args, "s", &arg))
498 1.8 christos return NULL;
499 1.1 christos
500 1.5 christos std::string newarg = std::string ("show ") + arg;
501 1.1 christos
502 1.8 christos TRY
503 1.1 christos {
504 1.5 christos found = lookup_cmd_composition (newarg.c_str (), &alias, &prefix, &cmd);
505 1.5 christos }
506 1.8 christos CATCH (ex, RETURN_MASK_ALL)
507 1.5 christos {
508 1.5 christos GDB_PY_HANDLE_EXCEPTION (ex);
509 1.5 christos }
510 1.1 christos END_CATCH
511 1.1 christos
512 1.1 christos if (!found)
513 1.1 christos return PyErr_Format (PyExc_RuntimeError,
514 1.1 christos _("Could not find parameter `%s'."), arg);
515 1.1 christos
516 1.1 christos if (! cmd->var)
517 1.1 christos return PyErr_Format (PyExc_RuntimeError,
518 1.1 christos _("`%s' is not a parameter."), arg);
519 1.1 christos return gdbpy_parameter_value (cmd->var_type, cmd->var);
520 1.1 christos }
521 1.1 christos
522 1.1 christos /* Wrapper for target_charset. */
523 1.1 christos
524 1.1 christos static PyObject *
525 1.1 christos gdbpy_target_charset (PyObject *self, PyObject *args)
526 1.1 christos {
527 1.1 christos const char *cset = target_charset (python_gdbarch);
528 1.1 christos
529 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
530 1.1 christos }
531 1.1 christos
532 1.1 christos /* Wrapper for target_wide_charset. */
533 1.1 christos
534 1.1 christos static PyObject *
535 1.1 christos gdbpy_target_wide_charset (PyObject *self, PyObject *args)
536 1.1 christos {
537 1.1 christos const char *cset = target_wide_charset (python_gdbarch);
538 1.1 christos
539 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
540 1.1 christos }
541 1.1 christos
542 1.1 christos /* A Python function which evaluates a string using the gdb CLI. */
543 1.1 christos
544 1.1 christos static PyObject *
545 1.1 christos execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
546 1.1 christos {
547 1.1 christos const char *arg;
548 1.7 christos PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
549 1.1 christos int from_tty, to_string;
550 1.7 christos static const char *keywords[] = { "command", "from_tty", "to_string", NULL };
551 1.7 christos
552 1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
553 1.1 christos &PyBool_Type, &from_tty_obj,
554 1.1 christos &PyBool_Type, &to_string_obj))
555 1.1 christos return NULL;
556 1.1 christos
557 1.1 christos from_tty = 0;
558 1.1 christos if (from_tty_obj)
559 1.1 christos {
560 1.1 christos int cmp = PyObject_IsTrue (from_tty_obj);
561 1.1 christos if (cmp < 0)
562 1.1 christos return NULL;
563 1.1 christos from_tty = cmp;
564 1.1 christos }
565 1.1 christos
566 1.1 christos to_string = 0;
567 1.1 christos if (to_string_obj)
568 1.1 christos {
569 1.1 christos int cmp = PyObject_IsTrue (to_string_obj);
570 1.1 christos if (cmp < 0)
571 1.1 christos return NULL;
572 1.1 christos to_string = cmp;
573 1.7 christos }
574 1.7 christos
575 1.8 christos std::string to_string_res;
576 1.8 christos
577 1.5 christos scoped_restore preventer = prevent_dont_repeat ();
578 1.1 christos
579 1.8 christos TRY
580 1.8 christos {
581 1.6 christos gdbpy_allow_threads allow_threads;
582 1.6 christos
583 1.8 christos struct interp *interp;
584 1.8 christos
585 1.8 christos std::string arg_copy = arg;
586 1.8 christos bool first = true;
587 1.8 christos char *save_ptr = nullptr;
588 1.8 christos auto reader
589 1.8 christos = [&] ()
590 1.8 christos {
591 1.8 christos const char *result = strtok_r (first ? &arg_copy[0] : nullptr,
592 1.8 christos "\n", &save_ptr);
593 1.8 christos first = false;
594 1.8 christos return result;
595 1.8 christos };
596 1.8 christos
597 1.8 christos counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
598 1.8 christos
599 1.8 christos {
600 1.8 christos scoped_restore save_async = make_scoped_restore (¤t_ui->async,
601 1.8 christos 0);
602 1.6 christos
603 1.8 christos scoped_restore save_uiout = make_scoped_restore (¤t_uiout);
604 1.8 christos
605 1.8 christos /* Use the console interpreter uiout to have the same print format
606 1.8 christos for console or MI. */
607 1.8 christos interp = interp_lookup (current_ui, "console");
608 1.8 christos current_uiout = interp->interp_ui_out ();
609 1.8 christos
610 1.8 christos if (to_string)
611 1.8 christos to_string_res = execute_control_commands_to_string (lines.get (),
612 1.8 christos from_tty);
613 1.8 christos else
614 1.1 christos execute_control_commands (lines.get (), from_tty);
615 1.8 christos }
616 1.8 christos
617 1.1 christos /* Do any commands attached to breakpoint we stopped at. */
618 1.5 christos bpstat_do_actions ();
619 1.5 christos }
620 1.5 christos CATCH (except, RETURN_MASK_ALL)
621 1.5 christos {
622 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
623 1.1 christos }
624 1.7 christos END_CATCH
625 1.7 christos
626 1.1 christos if (to_string)
627 1.1 christos return PyString_FromString (to_string_res.c_str ());
628 1.1 christos Py_RETURN_NONE;
629 1.8 christos }
630 1.8 christos
631 1.8 christos /* Implementation of Python rbreak command. Take a REGEX and
632 1.8 christos optionally a MINSYMS, THROTTLE and SYMTABS keyword and return a
633 1.8 christos Python list that contains newly set breakpoints that match that
634 1.8 christos criteria. REGEX refers to a GDB format standard regex pattern of
635 1.8 christos symbols names to search; MINSYMS is an optional boolean (default
636 1.8 christos False) that indicates if the function should search GDB's minimal
637 1.8 christos symbols; THROTTLE is an optional integer (default unlimited) that
638 1.8 christos indicates the maximum amount of breakpoints allowable before the
639 1.8 christos function exits (note, if the throttle bound is passed, no
640 1.8 christos breakpoints will be set and a runtime error returned); SYMTABS is
641 1.1 christos an optional Python iterable that contains a set of gdb.Symtabs to
642 1.1 christos constrain the search within. */
643 1.8 christos
644 1.1 christos static PyObject *
645 1.8 christos gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
646 1.8 christos {
647 1.8 christos /* A simple type to ensure clean up of a vector of allocated strings
648 1.8 christos when a C interface demands a const char *array[] type
649 1.8 christos interface. */
650 1.8 christos struct symtab_list_type
651 1.8 christos {
652 1.8 christos ~symtab_list_type ()
653 1.8 christos {
654 1.8 christos for (const char *elem: vec)
655 1.8 christos xfree ((void *) elem);
656 1.8 christos }
657 1.1 christos std::vector<const char *> vec;
658 1.8 christos };
659 1.8 christos
660 1.8 christos char *regex = NULL;
661 1.8 christos std::vector<symbol_search> symbols;
662 1.8 christos unsigned long count = 0;
663 1.8 christos PyObject *symtab_list = NULL;
664 1.8 christos PyObject *minsyms_p_obj = NULL;
665 1.8 christos int minsyms_p = 0;
666 1.8 christos unsigned int throttle = 0;
667 1.8 christos static const char *keywords[] = {"regex","minsyms", "throttle",
668 1.8 christos "symtabs", NULL};
669 1.8 christos symtab_list_type symtab_paths;
670 1.8 christos
671 1.8 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!IO", keywords,
672 1.8 christos ®ex, &PyBool_Type,
673 1.1 christos &minsyms_p_obj, &throttle,
674 1.1 christos &symtab_list))
675 1.8 christos return NULL;
676 1.8 christos
677 1.8 christos /* Parse minsyms keyword. */
678 1.8 christos if (minsyms_p_obj != NULL)
679 1.8 christos {
680 1.8 christos int cmp = PyObject_IsTrue (minsyms_p_obj);
681 1.8 christos if (cmp < 0)
682 1.8 christos return NULL;
683 1.8 christos minsyms_p = cmp;
684 1.8 christos }
685 1.8 christos
686 1.8 christos /* The "symtabs" keyword is any Python iterable object that returns
687 1.8 christos a gdb.Symtab on each iteration. If specified, iterate through
688 1.8 christos the provided gdb.Symtabs and extract their full path. As
689 1.8 christos python_string_to_target_string returns a
690 1.8 christos gdb::unique_xmalloc_ptr<char> and a vector containing these types
691 1.8 christos cannot be coerced to a const char **p[] via the vector.data call,
692 1.8 christos release the value from the unique_xmalloc_ptr and place it in a
693 1.8 christos simple type symtab_list_type (which holds the vector and a
694 1.8 christos destructor that frees the contents of the allocated strings. */
695 1.8 christos if (symtab_list != NULL)
696 1.8 christos {
697 1.8 christos gdbpy_ref<> iter (PyObject_GetIter (symtab_list));
698 1.8 christos
699 1.8 christos if (iter == NULL)
700 1.8 christos return NULL;
701 1.8 christos
702 1.8 christos while (true)
703 1.8 christos {
704 1.8 christos gdbpy_ref<> next (PyIter_Next (iter.get ()));
705 1.8 christos
706 1.8 christos if (next == NULL)
707 1.8 christos {
708 1.8 christos if (PyErr_Occurred ())
709 1.8 christos return NULL;
710 1.8 christos break;
711 1.8 christos }
712 1.8 christos
713 1.8 christos gdbpy_ref<> obj_name (PyObject_GetAttrString (next.get (),
714 1.8 christos "filename"));
715 1.8 christos
716 1.8 christos if (obj_name == NULL)
717 1.8 christos return NULL;
718 1.8 christos
719 1.8 christos /* Is the object file still valid? */
720 1.8 christos if (obj_name == Py_None)
721 1.8 christos continue;
722 1.8 christos
723 1.8 christos gdb::unique_xmalloc_ptr<char> filename =
724 1.8 christos python_string_to_target_string (obj_name.get ());
725 1.8 christos
726 1.8 christos if (filename == NULL)
727 1.8 christos return NULL;
728 1.8 christos
729 1.8 christos /* Make sure there is a definite place to store the value of
730 1.8 christos filename before it is released. */
731 1.8 christos symtab_paths.vec.push_back (nullptr);
732 1.8 christos symtab_paths.vec.back () = filename.release ();
733 1.8 christos }
734 1.8 christos }
735 1.8 christos
736 1.8 christos if (symtab_list)
737 1.8 christos {
738 1.8 christos const char **files = symtab_paths.vec.data ();
739 1.8 christos
740 1.8 christos symbols = search_symbols (regex, FUNCTIONS_DOMAIN, NULL,
741 1.1 christos symtab_paths.vec.size (), files);
742 1.8 christos }
743 1.8 christos else
744 1.8 christos symbols = search_symbols (regex, FUNCTIONS_DOMAIN, NULL, 0, NULL);
745 1.8 christos
746 1.8 christos /* Count the number of symbols (both symbols and optionally minimal
747 1.8 christos symbols) so we can correctly check the throttle limit. */
748 1.8 christos for (const symbol_search &p : symbols)
749 1.8 christos {
750 1.8 christos /* Minimal symbols included? */
751 1.8 christos if (minsyms_p)
752 1.8 christos {
753 1.8 christos if (p.msymbol.minsym != NULL)
754 1.8 christos count++;
755 1.8 christos }
756 1.8 christos
757 1.8 christos if (p.symbol != NULL)
758 1.8 christos count++;
759 1.8 christos }
760 1.8 christos
761 1.1 christos /* Check throttle bounds and exit if in excess. */
762 1.8 christos if (throttle != 0 && count > throttle)
763 1.8 christos {
764 1.8 christos PyErr_SetString (PyExc_RuntimeError,
765 1.1 christos _("Number of breakpoints exceeds throttled maximum."));
766 1.1 christos return NULL;
767 1.8 christos }
768 1.8 christos
769 1.8 christos gdbpy_ref<> return_list (PyList_New (0));
770 1.8 christos
771 1.8 christos if (return_list == NULL)
772 1.8 christos return NULL;
773 1.8 christos
774 1.8 christos /* Construct full path names for symbols and call the Python
775 1.8 christos breakpoint constructor on the resulting names. Be tolerant of
776 1.8 christos individual breakpoint failures. */
777 1.8 christos for (const symbol_search &p : symbols)
778 1.8 christos {
779 1.8 christos std::string symbol_name;
780 1.8 christos
781 1.8 christos /* Skipping minimal symbols? */
782 1.8 christos if (minsyms_p == 0)
783 1.8 christos if (p.msymbol.minsym != NULL)
784 1.8 christos continue;
785 1.8 christos
786 1.8 christos if (p.msymbol.minsym == NULL)
787 1.8 christos {
788 1.8 christos struct symtab *symtab = symbol_symtab (p.symbol);
789 1.8 christos const char *fullname = symtab_to_fullname (symtab);
790 1.8 christos
791 1.8 christos symbol_name = fullname;
792 1.8 christos symbol_name += ":";
793 1.8 christos symbol_name += SYMBOL_LINKAGE_NAME (p.symbol);
794 1.8 christos }
795 1.8 christos else
796 1.8 christos symbol_name = MSYMBOL_LINKAGE_NAME (p.msymbol.minsym);
797 1.8 christos
798 1.8 christos gdbpy_ref<> argList (Py_BuildValue("(s)", symbol_name.c_str ()));
799 1.8 christos gdbpy_ref<> obj (PyObject_CallObject ((PyObject *)
800 1.8 christos &breakpoint_object_type,
801 1.8 christos argList.get ()));
802 1.8 christos
803 1.8 christos /* Tolerate individual breakpoint failures. */
804 1.8 christos if (obj == NULL)
805 1.8 christos gdbpy_print_stack ();
806 1.8 christos else
807 1.8 christos {
808 1.8 christos if (PyList_Append (return_list.get (), obj.get ()) == -1)
809 1.8 christos return NULL;
810 1.8 christos }
811 1.1 christos }
812 1.1 christos return return_list.release ();
813 1.1 christos }
814 1.1 christos
815 1.1 christos /* A Python function which is a wrapper for decode_line_1. */
816 1.1 christos
817 1.1 christos static PyObject *
818 1.8 christos gdbpy_decode_line (PyObject *self, PyObject *args)
819 1.7 christos {
820 1.7 christos const char *arg = NULL;
821 1.7 christos gdbpy_ref<> result;
822 1.1 christos gdbpy_ref<> unparsed;
823 1.1 christos event_location_up location;
824 1.1 christos
825 1.1 christos if (! PyArg_ParseTuple (args, "|s", &arg))
826 1.6 christos return NULL;
827 1.8 christos
828 1.8 christos if (arg != NULL)
829 1.6 christos location = string_to_event_location_basic (&arg, python_language,
830 1.8 christos symbol_name_match_type::WILD);
831 1.8 christos
832 1.8 christos std::vector<symtab_and_line> decoded_sals;
833 1.5 christos symtab_and_line def_sal;
834 1.1 christos gdb::array_view<symtab_and_line> sals;
835 1.6 christos TRY
836 1.8 christos {
837 1.8 christos if (location != NULL)
838 1.8 christos {
839 1.8 christos decoded_sals = decode_line_1 (location.get (), 0, NULL, NULL, 0);
840 1.1 christos sals = decoded_sals;
841 1.1 christos }
842 1.1 christos else
843 1.8 christos {
844 1.8 christos set_default_source_symtab_and_line ();
845 1.1 christos def_sal = get_current_source_symtab_and_line ();
846 1.1 christos sals = def_sal;
847 1.5 christos }
848 1.5 christos }
849 1.1 christos CATCH (ex, RETURN_MASK_ALL)
850 1.8 christos {
851 1.1 christos /* We know this will always throw. */
852 1.1 christos gdbpy_convert_exception (ex);
853 1.8 christos return NULL;
854 1.1 christos }
855 1.8 christos END_CATCH
856 1.1 christos
857 1.8 christos if (!sals.empty ())
858 1.7 christos {
859 1.8 christos result.reset (PyTuple_New (sals.size ()));
860 1.8 christos if (result == NULL)
861 1.7 christos return NULL;
862 1.8 christos for (size_t i = 0; i < sals.size (); ++i)
863 1.8 christos {
864 1.8 christos PyObject *obj = symtab_and_line_to_sal_object (sals[i]);
865 1.1 christos if (obj == NULL)
866 1.7 christos return NULL;
867 1.1 christos
868 1.1 christos PyTuple_SetItem (result.get (), i, obj);
869 1.1 christos }
870 1.8 christos }
871 1.1 christos else
872 1.7 christos result = gdbpy_ref<>::new_reference (Py_None);
873 1.7 christos
874 1.8 christos gdbpy_ref<> return_result (PyTuple_New (2));
875 1.1 christos if (return_result == NULL)
876 1.6 christos return NULL;
877 1.1 christos
878 1.7 christos if (arg != NULL && strlen (arg) > 0)
879 1.1 christos {
880 1.8 christos unparsed.reset (PyString_FromString (arg));
881 1.1 christos if (unparsed == NULL)
882 1.1 christos return NULL;
883 1.8 christos }
884 1.1 christos else
885 1.7 christos unparsed = gdbpy_ref<>::new_reference (Py_None);
886 1.7 christos
887 1.1 christos PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
888 1.7 christos PyTuple_SetItem (return_result.get (), 1, result.release ());
889 1.1 christos
890 1.1 christos return return_result.release ();
891 1.1 christos }
892 1.1 christos
893 1.1 christos /* Parse a string and evaluate it as an expression. */
894 1.1 christos static PyObject *
895 1.1 christos gdbpy_parse_and_eval (PyObject *self, PyObject *args)
896 1.1 christos {
897 1.1 christos const char *expr_str;
898 1.1 christos struct value *result = NULL;
899 1.1 christos
900 1.1 christos if (!PyArg_ParseTuple (args, "s", &expr_str))
901 1.5 christos return NULL;
902 1.1 christos
903 1.8 christos TRY
904 1.1 christos {
905 1.1 christos gdbpy_allow_threads allow_threads;
906 1.5 christos result = parse_and_eval (expr_str);
907 1.5 christos }
908 1.5 christos CATCH (except, RETURN_MASK_ALL)
909 1.5 christos {
910 1.5 christos GDB_PY_HANDLE_EXCEPTION (except);
911 1.1 christos }
912 1.1 christos END_CATCH
913 1.1 christos
914 1.1 christos return value_to_value_object (result);
915 1.6 christos }
916 1.6 christos
917 1.6 christos /* Implementation of gdb.invalidate_cached_frames. */
918 1.6 christos
919 1.6 christos static PyObject *
920 1.6 christos gdbpy_invalidate_cached_frames (PyObject *self, PyObject *args)
921 1.6 christos {
922 1.6 christos reinit_frame_cache ();
923 1.6 christos Py_RETURN_NONE;
924 1.1 christos }
925 1.3 christos
926 1.3 christos /* Read a file as Python code.
927 1.1 christos This is the extension_language_script_ops.script_sourcer "method".
928 1.1 christos FILE is the file to load. FILENAME is name of the file FILE.
929 1.1 christos This does not throw any errors. If an exception occurs python will print
930 1.3 christos the traceback and clear the error indicator. */
931 1.3 christos
932 1.3 christos static void
933 1.1 christos gdbpy_source_script (const struct extension_language_defn *extlang,
934 1.7 christos FILE *file, const char *filename)
935 1.1 christos {
936 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
937 1.1 christos python_run_simple_file (file, filename);
938 1.1 christos }
939 1.1 christos
940 1.1 christos
941 1.1 christos
943 1.1 christos /* Posting and handling events. */
944 1.1 christos
945 1.1 christos /* A single event. */
946 1.1 christos struct gdbpy_event
947 1.1 christos {
948 1.1 christos /* The Python event. This is just a callable object. */
949 1.1 christos PyObject *event;
950 1.1 christos /* The next event. */
951 1.1 christos struct gdbpy_event *next;
952 1.1 christos };
953 1.1 christos
954 1.1 christos /* All pending events. */
955 1.1 christos static struct gdbpy_event *gdbpy_event_list;
956 1.6 christos /* The final link of the event list. */
957 1.6 christos static struct gdbpy_event **gdbpy_event_list_end;
958 1.6 christos
959 1.1 christos /* So that we can wake up the main thread even when it is blocked in
960 1.1 christos poll(). */
961 1.1 christos static struct serial_event *gdbpy_serial_event;
962 1.1 christos
963 1.1 christos /* The file handler callback. This reads from the internal pipe, and
964 1.1 christos then processes the Python event queue. This will always be run in
965 1.6 christos the main gdb thread. */
966 1.1 christos
967 1.7 christos static void
968 1.1 christos gdbpy_run_events (int error, gdb_client_data client_data)
969 1.6 christos {
970 1.6 christos gdbpy_enter enter_py (get_current_arch (), current_language);
971 1.1 christos
972 1.6 christos /* Clear the event fd. Do this before flushing the events list, so
973 1.1 christos that any new event post afterwards is sure to re-awake the event
974 1.1 christos loop. */
975 1.1 christos serial_event_clear (gdbpy_serial_event);
976 1.1 christos
977 1.1 christos while (gdbpy_event_list)
978 1.1 christos {
979 1.1 christos /* Dispatching the event might push a new element onto the event
980 1.1 christos loop, so we update here "atomically enough". */
981 1.1 christos struct gdbpy_event *item = gdbpy_event_list;
982 1.1 christos gdbpy_event_list = gdbpy_event_list->next;
983 1.7 christos if (gdbpy_event_list == NULL)
984 1.1 christos gdbpy_event_list_end = &gdbpy_event_list;
985 1.8 christos
986 1.1 christos gdbpy_ref<> call_result (PyObject_CallObject (item->event, NULL));
987 1.1 christos if (call_result == NULL)
988 1.1 christos gdbpy_print_stack ();
989 1.1 christos
990 1.1 christos Py_DECREF (item->event);
991 1.1 christos xfree (item);
992 1.1 christos }
993 1.1 christos }
994 1.1 christos
995 1.1 christos /* Submit an event to the gdb thread. */
996 1.1 christos static PyObject *
997 1.1 christos gdbpy_post_event (PyObject *self, PyObject *args)
998 1.1 christos {
999 1.1 christos struct gdbpy_event *event;
1000 1.1 christos PyObject *func;
1001 1.1 christos int wakeup;
1002 1.1 christos
1003 1.1 christos if (!PyArg_ParseTuple (args, "O", &func))
1004 1.1 christos return NULL;
1005 1.1 christos
1006 1.1 christos if (!PyCallable_Check (func))
1007 1.1 christos {
1008 1.1 christos PyErr_SetString (PyExc_RuntimeError,
1009 1.1 christos _("Posted event is not callable"));
1010 1.1 christos return NULL;
1011 1.1 christos }
1012 1.1 christos
1013 1.1 christos Py_INCREF (func);
1014 1.1 christos
1015 1.1 christos /* From here until the end of the function, we have the GIL, so we
1016 1.1 christos can operate on our global data structures without worrying. */
1017 1.1 christos wakeup = gdbpy_event_list == NULL;
1018 1.1 christos
1019 1.1 christos event = XNEW (struct gdbpy_event);
1020 1.1 christos event->event = func;
1021 1.1 christos event->next = NULL;
1022 1.1 christos *gdbpy_event_list_end = event;
1023 1.1 christos gdbpy_event_list_end = &event->next;
1024 1.6 christos
1025 1.1 christos /* Wake up gdb when needed. */
1026 1.1 christos if (wakeup)
1027 1.1 christos serial_event_set (gdbpy_serial_event);
1028 1.1 christos
1029 1.1 christos Py_RETURN_NONE;
1030 1.1 christos }
1031 1.1 christos
1032 1.1 christos /* Initialize the Python event handler. */
1033 1.6 christos static int
1034 1.6 christos gdbpy_initialize_events (void)
1035 1.6 christos {
1036 1.6 christos gdbpy_event_list_end = &gdbpy_event_list;
1037 1.6 christos
1038 1.1 christos gdbpy_serial_event = make_serial_event ();
1039 1.1 christos add_file_handler (serial_event_fd (gdbpy_serial_event),
1040 1.1 christos gdbpy_run_events, NULL);
1041 1.1 christos
1042 1.1 christos return 0;
1043 1.1 christos }
1044 1.3 christos
1045 1.3 christos
1046 1.3 christos
1048 1.3 christos /* This is the extension_language_ops.before_prompt "method". */
1049 1.1 christos
1050 1.1 christos static enum ext_lang_rc
1051 1.3 christos gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
1052 1.1 christos const char *current_gdb_prompt)
1053 1.7 christos {
1054 1.7 christos if (!gdb_python_initialized)
1055 1.7 christos return EXT_LANG_RC_NOP;
1056 1.7 christos
1057 1.7 christos gdbpy_enter enter_py (get_current_arch (), current_language);
1058 1.1 christos
1059 1.1 christos if (!evregpy_no_listeners_p (gdb_py_events.before_prompt)
1060 1.1 christos && evpy_emit_event (NULL, gdb_py_events.before_prompt) < 0)
1061 1.1 christos return EXT_LANG_RC_ERROR;
1062 1.7 christos
1063 1.7 christos if (gdb_python_module
1064 1.1 christos && PyObject_HasAttrString (gdb_python_module, "prompt_hook"))
1065 1.7 christos {
1066 1.7 christos gdbpy_ref<> hook (PyObject_GetAttrString (gdb_python_module,
1067 1.7 christos "prompt_hook"));
1068 1.7 christos if (hook == NULL)
1069 1.1 christos {
1070 1.7 christos gdbpy_print_stack ();
1071 1.1 christos return EXT_LANG_RC_ERROR;
1072 1.7 christos }
1073 1.1 christos
1074 1.7 christos if (PyCallable_Check (hook.get ()))
1075 1.7 christos {
1076 1.7 christos gdbpy_ref<> current_prompt (PyString_FromString (current_gdb_prompt));
1077 1.7 christos if (current_prompt == NULL)
1078 1.1 christos {
1079 1.7 christos gdbpy_print_stack ();
1080 1.7 christos return EXT_LANG_RC_ERROR;
1081 1.7 christos }
1082 1.1 christos
1083 1.7 christos gdbpy_ref<> result
1084 1.7 christos (PyObject_CallFunctionObjArgs (hook.get (), current_prompt.get (),
1085 1.7 christos NULL));
1086 1.7 christos if (result == NULL)
1087 1.1 christos {
1088 1.1 christos gdbpy_print_stack ();
1089 1.1 christos return EXT_LANG_RC_ERROR;
1090 1.1 christos }
1091 1.7 christos
1092 1.1 christos /* Return type should be None, or a String. If it is None,
1093 1.1 christos fall through, we will not set a prompt. If it is a
1094 1.1 christos string, set PROMPT. Anything else, set an exception. */
1095 1.1 christos if (result != Py_None && ! PyString_Check (result.get ()))
1096 1.7 christos {
1097 1.7 christos PyErr_Format (PyExc_RuntimeError,
1098 1.1 christos _("Return from prompt_hook must " \
1099 1.1 christos "be either a Python string, or None"));
1100 1.1 christos gdbpy_print_stack ();
1101 1.1 christos return EXT_LANG_RC_ERROR;
1102 1.7 christos }
1103 1.7 christos
1104 1.1 christos if (result != Py_None)
1105 1.1 christos {
1106 1.7 christos gdb::unique_xmalloc_ptr<char>
1107 1.7 christos prompt (python_string_to_host_string (result.get ()));
1108 1.7 christos
1109 1.7 christos if (prompt == NULL)
1110 1.7 christos {
1111 1.7 christos gdbpy_print_stack ();
1112 1.7 christos return EXT_LANG_RC_ERROR;
1113 1.1 christos }
1114 1.1 christos
1115 1.1 christos set_prompt (prompt.get ());
1116 1.1 christos return EXT_LANG_RC_OK;
1117 1.7 christos }
1118 1.1 christos }
1119 1.1 christos }
1120 1.1 christos
1121 1.1 christos return EXT_LANG_RC_NOP;
1122 1.1 christos }
1123 1.1 christos
1124 1.1 christos
1125 1.1 christos
1127 1.1 christos /* Printing. */
1128 1.1 christos
1129 1.1 christos /* A python function to write a single string using gdb's filtered
1130 1.1 christos output stream . The optional keyword STREAM can be used to write
1131 1.1 christos to a particular stream. The default stream is to gdb_stdout. */
1132 1.7 christos
1133 1.1 christos static PyObject *
1134 1.1 christos gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
1135 1.7 christos {
1136 1.7 christos const char *arg;
1137 1.1 christos static const char *keywords[] = { "text", "stream", NULL };
1138 1.1 christos int stream_type = 0;
1139 1.5 christos
1140 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
1141 1.1 christos &stream_type))
1142 1.1 christos return NULL;
1143 1.1 christos
1144 1.1 christos TRY
1145 1.1 christos {
1146 1.1 christos switch (stream_type)
1147 1.1 christos {
1148 1.1 christos case 1:
1149 1.1 christos {
1150 1.1 christos fprintf_filtered (gdb_stderr, "%s", arg);
1151 1.1 christos break;
1152 1.1 christos }
1153 1.1 christos case 2:
1154 1.1 christos {
1155 1.1 christos fprintf_filtered (gdb_stdlog, "%s", arg);
1156 1.1 christos break;
1157 1.5 christos }
1158 1.5 christos default:
1159 1.5 christos fprintf_filtered (gdb_stdout, "%s", arg);
1160 1.5 christos }
1161 1.5 christos }
1162 1.1 christos CATCH (except, RETURN_MASK_ALL)
1163 1.1 christos {
1164 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
1165 1.1 christos }
1166 1.1 christos END_CATCH
1167 1.1 christos
1168 1.1 christos Py_RETURN_NONE;
1169 1.1 christos }
1170 1.1 christos
1171 1.1 christos /* A python function to flush a gdb stream. The optional keyword
1172 1.1 christos STREAM can be used to flush a particular stream. The default stream
1173 1.7 christos is gdb_stdout. */
1174 1.1 christos
1175 1.1 christos static PyObject *
1176 1.7 christos gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
1177 1.7 christos {
1178 1.1 christos static const char *keywords[] = { "stream", NULL };
1179 1.1 christos int stream_type = 0;
1180 1.1 christos
1181 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
1182 1.1 christos &stream_type))
1183 1.1 christos return NULL;
1184 1.1 christos
1185 1.1 christos switch (stream_type)
1186 1.1 christos {
1187 1.1 christos case 1:
1188 1.1 christos {
1189 1.1 christos gdb_flush (gdb_stderr);
1190 1.1 christos break;
1191 1.1 christos }
1192 1.1 christos case 2:
1193 1.1 christos {
1194 1.1 christos gdb_flush (gdb_stdlog);
1195 1.1 christos break;
1196 1.1 christos }
1197 1.1 christos default:
1198 1.1 christos gdb_flush (gdb_stdout);
1199 1.5 christos }
1200 1.5 christos
1201 1.5 christos Py_RETURN_NONE;
1202 1.5 christos }
1203 1.5 christos
1204 1.5 christos /* Return non-zero if print-stack is not "none". */
1205 1.5 christos
1206 1.5 christos int
1207 1.1 christos gdbpy_print_python_errors_p (void)
1208 1.1 christos {
1209 1.1 christos return gdbpy_should_print_stack != python_excp_none;
1210 1.1 christos }
1211 1.1 christos
1212 1.1 christos /* Print a python exception trace, print just a message, or print
1213 1.1 christos nothing and clear the python exception, depending on
1214 1.1 christos gdbpy_should_print_stack. Only call this if a python exception is
1215 1.1 christos set. */
1216 1.1 christos void
1217 1.1 christos gdbpy_print_stack (void)
1218 1.1 christos {
1219 1.1 christos
1220 1.1 christos /* Print "none", just clear exception. */
1221 1.1 christos if (gdbpy_should_print_stack == python_excp_none)
1222 1.1 christos {
1223 1.1 christos PyErr_Clear ();
1224 1.1 christos }
1225 1.1 christos /* Print "full" message and backtrace. */
1226 1.1 christos else if (gdbpy_should_print_stack == python_excp_full)
1227 1.5 christos {
1228 1.1 christos PyErr_Print ();
1229 1.1 christos /* PyErr_Print doesn't necessarily end output with a newline.
1230 1.1 christos This works because Python's stdout/stderr is fed through
1231 1.5 christos printf_filtered. */
1232 1.5 christos TRY
1233 1.5 christos {
1234 1.5 christos begin_line ();
1235 1.1 christos }
1236 1.1 christos CATCH (except, RETURN_MASK_ALL)
1237 1.1 christos {
1238 1.1 christos }
1239 1.8 christos END_CATCH
1240 1.1 christos }
1241 1.8 christos /* Print "message", just error print message. */
1242 1.8 christos else
1243 1.8 christos {
1244 1.8 christos gdbpy_err_fetch fetched_error;
1245 1.8 christos
1246 1.8 christos gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string ();
1247 1.1 christos gdb::unique_xmalloc_ptr<char> type;
1248 1.5 christos /* Don't compute TYPE if MSG already indicates that there is an
1249 1.1 christos error. */
1250 1.8 christos if (msg != NULL)
1251 1.1 christos type = fetched_error.type_to_string ();
1252 1.1 christos
1253 1.1 christos TRY
1254 1.1 christos {
1255 1.1 christos if (msg == NULL || type == NULL)
1256 1.1 christos {
1257 1.8 christos /* An error occurred computing the string representation of the
1258 1.1 christos error message. */
1259 1.1 christos fprintf_filtered (gdb_stderr,
1260 1.1 christos _("Error occurred computing Python error" \
1261 1.7 christos "message.\n"));
1262 1.1 christos PyErr_Clear ();
1263 1.5 christos }
1264 1.5 christos else
1265 1.5 christos fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
1266 1.5 christos type.get (), msg.get ());
1267 1.1 christos }
1268 1.1 christos CATCH (except, RETURN_MASK_ALL)
1269 1.1 christos {
1270 1.8 christos }
1271 1.8 christos END_CATCH
1272 1.1 christos }
1273 1.8 christos }
1274 1.8 christos
1275 1.1 christos /* Like gdbpy_print_stack, but if the exception is a
1276 1.8 christos KeyboardException, throw a gdb "quit" instead. */
1277 1.8 christos
1278 1.8 christos void
1279 1.8 christos gdbpy_print_stack_or_quit ()
1280 1.8 christos {
1281 1.8 christos if (PyErr_ExceptionMatches (PyExc_KeyboardInterrupt))
1282 1.8 christos {
1283 1.1 christos PyErr_Clear ();
1284 1.8 christos throw_quit ("Quit");
1285 1.1 christos }
1286 1.1 christos gdbpy_print_stack ();
1287 1.1 christos }
1288 1.1 christos
1289 1.1 christos
1290 1.1 christos
1292 1.1 christos /* Return a sequence holding all the Progspaces. */
1293 1.7 christos
1294 1.7 christos static PyObject *
1295 1.1 christos gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
1296 1.1 christos {
1297 1.1 christos struct program_space *ps;
1298 1.1 christos
1299 1.8 christos gdbpy_ref<> list (PyList_New (0));
1300 1.1 christos if (list == NULL)
1301 1.8 christos return NULL;
1302 1.7 christos
1303 1.1 christos ALL_PSPACES (ps)
1304 1.1 christos {
1305 1.7 christos gdbpy_ref<> item = pspace_to_pspace_object (ps);
1306 1.1 christos
1307 1.1 christos if (item == NULL || PyList_Append (list.get (), item.get ()) == -1)
1308 1.1 christos return NULL;
1309 1.1 christos }
1310 1.1 christos
1311 1.1 christos return list.release ();
1312 1.5 christos }
1313 1.5 christos
1314 1.1 christos
1315 1.1 christos
1317 1.1 christos /* The "current" objfile. This is set when gdb detects that a new
1318 1.3 christos objfile has been loaded. It is only set for the duration of a call to
1319 1.3 christos gdbpy_source_objfile_script and gdbpy_execute_objfile_script; it is NULL
1320 1.3 christos at other times. */
1321 1.1 christos static struct objfile *gdbpy_current_objfile;
1322 1.3 christos
1323 1.3 christos /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1324 1.3 christos as Python code. This does not throw any errors. If an exception
1325 1.3 christos occurs python will print the traceback and clear the error indicator.
1326 1.1 christos This is the extension_language_script_ops.objfile_script_sourcer
1327 1.1 christos "method". */
1328 1.1 christos
1329 1.1 christos static void
1330 1.7 christos gdbpy_source_objfile_script (const struct extension_language_defn *extlang,
1331 1.1 christos struct objfile *objfile, FILE *file,
1332 1.1 christos const char *filename)
1333 1.1 christos {
1334 1.1 christos if (!gdb_python_initialized)
1335 1.1 christos return;
1336 1.1 christos
1337 1.1 christos gdbpy_enter enter_py (get_objfile_arch (objfile), current_language);
1338 1.5 christos gdbpy_current_objfile = objfile;
1339 1.5 christos
1340 1.5 christos python_run_simple_file (file, filename);
1341 1.5 christos
1342 1.5 christos gdbpy_current_objfile = NULL;
1343 1.5 christos }
1344 1.5 christos
1345 1.5 christos /* Set the current objfile to OBJFILE and then execute SCRIPT
1346 1.5 christos as Python code. This does not throw any errors. If an exception
1347 1.5 christos occurs python will print the traceback and clear the error indicator.
1348 1.5 christos This is the extension_language_script_ops.objfile_script_executor
1349 1.5 christos "method". */
1350 1.5 christos
1351 1.5 christos static void
1352 1.7 christos gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
1353 1.5 christos struct objfile *objfile, const char *name,
1354 1.5 christos const char *script)
1355 1.5 christos {
1356 1.5 christos if (!gdb_python_initialized)
1357 1.5 christos return;
1358 1.5 christos
1359 1.5 christos gdbpy_enter enter_py (get_objfile_arch (objfile), current_language);
1360 1.1 christos gdbpy_current_objfile = objfile;
1361 1.1 christos
1362 1.1 christos PyRun_SimpleString (script);
1363 1.1 christos
1364 1.1 christos gdbpy_current_objfile = NULL;
1365 1.1 christos }
1366 1.1 christos
1367 1.1 christos /* Return the current Objfile, or None if there isn't one. */
1368 1.8 christos
1369 1.1 christos static PyObject *
1370 1.1 christos gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
1371 1.3 christos {
1372 1.3 christos if (! gdbpy_current_objfile)
1373 1.3 christos Py_RETURN_NONE;
1374 1.3 christos
1375 1.1 christos return objfile_to_objfile_object (gdbpy_current_objfile).release ();
1376 1.3 christos }
1377 1.3 christos
1378 1.3 christos /* Compute the list of active python type printers and store them in
1379 1.1 christos EXT_PRINTERS->py_type_printers. The product of this function is used by
1380 1.7 christos gdbpy_apply_type_printers, and freed by gdbpy_free_type_printers.
1381 1.1 christos This is the extension_language_ops.start_type_printers "method". */
1382 1.1 christos
1383 1.3 christos static void
1384 1.1 christos gdbpy_start_type_printers (const struct extension_language_defn *extlang,
1385 1.7 christos struct ext_lang_type_printers *ext_printers)
1386 1.1 christos {
1387 1.7 christos PyObject *printers_obj = NULL;
1388 1.1 christos
1389 1.1 christos if (!gdb_python_initialized)
1390 1.1 christos return;
1391 1.7 christos
1392 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
1393 1.1 christos
1394 1.7 christos gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
1395 1.7 christos if (type_module == NULL)
1396 1.1 christos {
1397 1.1 christos gdbpy_print_stack ();
1398 1.1 christos return;
1399 1.7 christos }
1400 1.1 christos
1401 1.1 christos gdbpy_ref<> func (PyObject_GetAttrString (type_module.get (),
1402 1.7 christos "get_type_recognizers"));
1403 1.3 christos if (func == NULL)
1404 1.1 christos {
1405 1.3 christos gdbpy_print_stack ();
1406 1.3 christos return;
1407 1.1 christos }
1408 1.1 christos
1409 1.3 christos printers_obj = PyObject_CallFunctionObjArgs (func.get (), (char *) NULL);
1410 1.3 christos if (printers_obj == NULL)
1411 1.3 christos gdbpy_print_stack ();
1412 1.3 christos else
1413 1.3 christos ext_printers->py_type_printers = printers_obj;
1414 1.3 christos }
1415 1.3 christos
1416 1.3 christos /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE
1417 1.3 christos a newly allocated string holding the type's replacement name, and return
1418 1.3 christos EXT_LANG_RC_OK. The caller is responsible for freeing the string.
1419 1.3 christos If there's a Python error return EXT_LANG_RC_ERROR.
1420 1.1 christos Otherwise, return EXT_LANG_RC_NOP.
1421 1.6 christos This is the extension_language_ops.apply_type_printers "method". */
1422 1.7 christos
1423 1.1 christos static enum ext_lang_rc
1424 1.1 christos gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
1425 1.3 christos const struct ext_lang_type_printers *ext_printers,
1426 1.1 christos struct type *type, char **prettied_type)
1427 1.1 christos {
1428 1.3 christos PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
1429 1.1 christos gdb::unique_xmalloc_ptr<char> result;
1430 1.7 christos
1431 1.1 christos if (printers_obj == NULL)
1432 1.7 christos return EXT_LANG_RC_NOP;
1433 1.1 christos
1434 1.1 christos if (!gdb_python_initialized)
1435 1.1 christos return EXT_LANG_RC_NOP;
1436 1.7 christos
1437 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
1438 1.1 christos
1439 1.7 christos gdbpy_ref<> type_obj (type_to_type_object (type));
1440 1.1 christos if (type_obj == NULL)
1441 1.1 christos {
1442 1.1 christos gdbpy_print_stack ();
1443 1.7 christos return EXT_LANG_RC_ERROR;
1444 1.1 christos }
1445 1.1 christos
1446 1.7 christos gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
1447 1.7 christos if (type_module == NULL)
1448 1.1 christos {
1449 1.1 christos gdbpy_print_stack ();
1450 1.1 christos return EXT_LANG_RC_ERROR;
1451 1.7 christos }
1452 1.1 christos
1453 1.1 christos gdbpy_ref<> func (PyObject_GetAttrString (type_module.get (),
1454 1.7 christos "apply_type_recognizers"));
1455 1.7 christos if (func == NULL)
1456 1.7 christos {
1457 1.7 christos gdbpy_print_stack ();
1458 1.1 christos return EXT_LANG_RC_ERROR;
1459 1.1 christos }
1460 1.1 christos
1461 1.7 christos gdbpy_ref<> result_obj (PyObject_CallFunctionObjArgs (func.get (),
1462 1.1 christos printers_obj,
1463 1.1 christos type_obj.get (),
1464 1.7 christos (char *) NULL));
1465 1.7 christos if (result_obj == NULL)
1466 1.7 christos {
1467 1.7 christos gdbpy_print_stack ();
1468 1.7 christos return EXT_LANG_RC_ERROR;
1469 1.1 christos }
1470 1.7 christos
1471 1.7 christos if (result_obj == Py_None)
1472 1.1 christos return EXT_LANG_RC_NOP;
1473 1.1 christos
1474 1.7 christos result = python_string_to_host_string (result_obj.get ());
1475 1.7 christos if (result == NULL)
1476 1.1 christos {
1477 1.1 christos gdbpy_print_stack ();
1478 1.3 christos return EXT_LANG_RC_ERROR;
1479 1.3 christos }
1480 1.1 christos
1481 1.3 christos *prettied_type = result.release ();
1482 1.3 christos return EXT_LANG_RC_OK;
1483 1.3 christos }
1484 1.1 christos
1485 1.6 christos /* Free the result of start_type_printers.
1486 1.1 christos This is the extension_language_ops.free_type_printers "method". */
1487 1.1 christos
1488 1.1 christos static void
1489 1.1 christos gdbpy_free_type_printers (const struct extension_language_defn *extlang,
1490 1.1 christos struct ext_lang_type_printers *ext_printers)
1491 1.1 christos {
1492 1.1 christos PyObject *printers = (PyObject *) ext_printers->py_type_printers;
1493 1.7 christos
1494 1.1 christos if (printers == NULL)
1495 1.1 christos return;
1496 1.1 christos
1497 1.1 christos if (!gdb_python_initialized)
1498 1.1 christos return;
1499 1.1 christos
1500 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
1501 1.1 christos Py_DECREF (printers);
1502 1.1 christos }
1503 1.8 christos
1504 1.1 christos #else /* HAVE_PYTHON */
1505 1.1 christos
1506 1.1 christos /* Dummy implementation of the gdb "python-interactive" and "python"
1507 1.1 christos command. */
1508 1.1 christos
1509 1.1 christos static void
1510 1.8 christos python_interactive_command (const char *arg, int from_tty)
1511 1.1 christos {
1512 1.7 christos arg = skip_spaces (arg);
1513 1.1 christos if (arg && *arg)
1514 1.1 christos error (_("Python scripting is not supported in this copy of GDB."));
1515 1.1 christos else
1516 1.1 christos {
1517 1.8 christos counted_command_line l = get_command_line (python_control, "");
1518 1.1 christos
1519 1.1 christos execute_control_command_untraced (l.get ());
1520 1.1 christos }
1521 1.1 christos }
1522 1.1 christos
1523 1.1 christos static void
1524 1.1 christos python_command (const char *arg, int from_tty)
1525 1.1 christos {
1526 1.1 christos python_interactive_command (arg, from_tty);
1527 1.1 christos }
1528 1.1 christos
1529 1.1 christos #endif /* HAVE_PYTHON */
1530 1.1 christos
1531 1.1 christos
1532 1.1 christos
1534 1.8 christos /* Lists for 'set python' commands. */
1535 1.1 christos
1536 1.1 christos static struct cmd_list_element *user_set_python_list;
1537 1.1 christos static struct cmd_list_element *user_show_python_list;
1538 1.1 christos
1539 1.1 christos /* Function for use by 'set python' prefix command. */
1540 1.1 christos
1541 1.1 christos static void
1542 1.1 christos user_set_python (const char *args, int from_tty)
1543 1.8 christos {
1544 1.1 christos help_list (user_set_python_list, "set python ", all_commands,
1545 1.1 christos gdb_stdout);
1546 1.1 christos }
1547 1.1 christos
1548 1.1 christos /* Function for use by 'show python' prefix command. */
1549 1.1 christos
1550 1.1 christos static void
1551 1.1 christos user_show_python (const char *args, int from_tty)
1552 1.1 christos {
1553 1.1 christos cmd_show_list (user_show_python_list, from_tty, "");
1554 1.1 christos }
1555 1.1 christos
1556 1.1 christos /* Initialize the Python code. */
1557 1.1 christos
1558 1.3 christos #ifdef HAVE_PYTHON
1559 1.3 christos
1560 1.1 christos /* This is installed as a final cleanup and cleans up the
1561 1.1 christos interpreter. This lets Python's 'atexit' work. */
1562 1.1 christos
1563 1.1 christos static void
1564 1.1 christos finalize_python (void *ignore)
1565 1.3 christos {
1566 1.3 christos struct active_ext_lang_state *previous_active;
1567 1.3 christos
1568 1.3 christos /* We don't use ensure_python_env here because if we ever ran the
1569 1.3 christos cleanup, gdb would crash -- because the cleanup calls into the
1570 1.1 christos Python interpreter, which we are about to destroy. It seems
1571 1.1 christos clearer to make the needed calls explicitly here than to create a
1572 1.1 christos cleanup and then mysteriously discard it. */
1573 1.1 christos
1574 1.1 christos /* This is only called as a final cleanup so we can assume the active
1575 1.3 christos SIGINT handler is gdb's. We still need to tell it to notify Python. */
1576 1.3 christos previous_active = set_active_ext_lang (&extension_language_python);
1577 1.1 christos
1578 1.8 christos (void) PyGILState_Ensure ();
1579 1.8 christos python_gdbarch = target_gdbarch ();
1580 1.8 christos python_language = current_language;
1581 1.8 christos
1582 1.8 christos Py_Finalize ();
1583 1.8 christos
1584 1.8 christos restore_active_ext_lang (previous_active);
1585 1.8 christos }
1586 1.8 christos
1587 1.8 christos #ifdef IS_PY3K
1588 1.1 christos /* This is called via the PyImport_AppendInittab mechanism called
1589 1.1 christos during initialization, to make the built-in _gdb module known to
1590 1.7 christos Python. */
1591 1.7 christos PyMODINIT_FUNC
1592 1.1 christos init__gdb_module (void)
1593 1.1 christos {
1594 1.1 christos return PyModule_Create (&python_GdbModuleDef);
1595 1.1 christos }
1596 1.1 christos #endif
1597 1.1 christos
1598 1.1 christos static bool
1599 1.1 christos do_start_initialization ()
1600 1.1 christos {
1601 1.1 christos #ifdef IS_PY3K
1602 1.1 christos size_t progsize, count;
1603 1.1 christos wchar_t *progname_copy;
1604 1.1 christos #endif
1605 1.8 christos
1606 1.8 christos #ifdef WITH_PYTHON_PATH
1607 1.8 christos /* Work around problem where python gets confused about where it is,
1608 1.1 christos and then can't find its libraries, etc.
1609 1.8 christos NOTE: Python assumes the following layout:
1610 1.1 christos /foo/bin/python
1611 1.8 christos /foo/lib/pythonX.Y/...
1612 1.6 christos This must be done before calling Py_Initialize. */
1613 1.1 christos gdb::unique_xmalloc_ptr<char> progname
1614 1.1 christos (concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin",
1615 1.1 christos SLASH_STRING, "python", (char *) NULL));
1616 1.7 christos #ifdef IS_PY3K
1617 1.1 christos std::string oldloc = setlocale (LC_ALL, NULL);
1618 1.8 christos setlocale (LC_ALL, "");
1619 1.1 christos progsize = strlen (progname.get ());
1620 1.1 christos progname_copy = (wchar_t *) PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
1621 1.1 christos if (!progname_copy)
1622 1.7 christos {
1623 1.1 christos fprintf (stderr, "out of memory\n");
1624 1.8 christos return false;
1625 1.1 christos }
1626 1.1 christos count = mbstowcs (progname_copy, progname.get (), progsize + 1);
1627 1.1 christos if (count == (size_t) -1)
1628 1.1 christos {
1629 1.1 christos fprintf (stderr, "Could not convert python path to string\n");
1630 1.8 christos return false;
1631 1.8 christos }
1632 1.8 christos setlocale (LC_ALL, oldloc.c_str ());
1633 1.1 christos
1634 1.8 christos /* Note that Py_SetProgramName expects the string it is passed to
1635 1.1 christos remain alive for the duration of the program's execution, so
1636 1.1 christos it is not freed after this call. */
1637 1.1 christos Py_SetProgramName (progname_copy);
1638 1.1 christos
1639 1.1 christos /* Define _gdb as a built-in module. */
1640 1.1 christos PyImport_AppendInittab ("_gdb", init__gdb_module);
1641 1.1 christos #else
1642 1.8 christos Py_SetProgramName (progname.release ());
1643 1.1 christos #endif
1644 1.5 christos #endif
1645 1.1 christos
1646 1.1 christos Py_Initialize ();
1647 1.7 christos PyEval_InitThreads ();
1648 1.1 christos
1649 1.1 christos #ifdef IS_PY3K
1650 1.1 christos gdb_module = PyImport_ImportModule ("_gdb");
1651 1.1 christos #else
1652 1.1 christos gdb_module = Py_InitModule ("_gdb", python_GdbMethods);
1653 1.1 christos #endif
1654 1.1 christos if (gdb_module == NULL)
1655 1.7 christos return false;
1656 1.1 christos
1657 1.1 christos /* The casts to (char*) are for python 2.4. */
1658 1.1 christos if (PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version) < 0
1659 1.1 christos || PyModule_AddStringConstant (gdb_module, "HOST_CONFIG",
1660 1.1 christos (char*) host_name) < 0
1661 1.7 christos || PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
1662 1.1 christos (char*) target_name) < 0)
1663 1.1 christos return false;
1664 1.1 christos
1665 1.1 christos /* Add stream constants. */
1666 1.7 christos if (PyModule_AddIntConstant (gdb_module, "STDOUT", 0) < 0
1667 1.1 christos || PyModule_AddIntConstant (gdb_module, "STDERR", 1) < 0
1668 1.1 christos || PyModule_AddIntConstant (gdb_module, "STDLOG", 2) < 0)
1669 1.1 christos return false;
1670 1.1 christos
1671 1.1 christos gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL);
1672 1.1 christos if (gdbpy_gdb_error == NULL
1673 1.7 christos || gdb_pymodule_addobject (gdb_module, "error", gdbpy_gdb_error) < 0)
1674 1.1 christos return false;
1675 1.1 christos
1676 1.1 christos gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError",
1677 1.1 christos gdbpy_gdb_error, NULL);
1678 1.1 christos if (gdbpy_gdb_memory_error == NULL
1679 1.7 christos || gdb_pymodule_addobject (gdb_module, "MemoryError",
1680 1.1 christos gdbpy_gdb_memory_error) < 0)
1681 1.1 christos return false;
1682 1.1 christos
1683 1.1 christos gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
1684 1.1 christos if (gdbpy_gdberror_exc == NULL
1685 1.1 christos || gdb_pymodule_addobject (gdb_module, "GdbError",
1686 1.1 christos gdbpy_gdberror_exc) < 0)
1687 1.7 christos return false;
1688 1.7 christos
1689 1.7 christos gdbpy_initialize_gdb_readline ();
1690 1.1 christos
1691 1.1 christos if (gdbpy_initialize_auto_load () < 0
1692 1.1 christos || gdbpy_initialize_values () < 0
1693 1.1 christos || gdbpy_initialize_frames () < 0
1694 1.1 christos || gdbpy_initialize_commands () < 0
1695 1.1 christos || gdbpy_initialize_instruction () < 0
1696 1.1 christos || gdbpy_initialize_record () < 0
1697 1.1 christos || gdbpy_initialize_btrace () < 0
1698 1.1 christos || gdbpy_initialize_symbols () < 0
1699 1.1 christos || gdbpy_initialize_symtabs () < 0
1700 1.1 christos || gdbpy_initialize_blocks () < 0
1701 1.1 christos || gdbpy_initialize_functions () < 0
1702 1.1 christos || gdbpy_initialize_parameters () < 0
1703 1.1 christos || gdbpy_initialize_types () < 0
1704 1.1 christos || gdbpy_initialize_pspace () < 0
1705 1.1 christos || gdbpy_initialize_objfile () < 0
1706 1.1 christos || gdbpy_initialize_breakpoints () < 0
1707 1.1 christos || gdbpy_initialize_finishbreakpoints () < 0
1708 1.3 christos || gdbpy_initialize_lazy_string () < 0
1709 1.5 christos || gdbpy_initialize_linetable () < 0
1710 1.5 christos || gdbpy_initialize_thread () < 0
1711 1.7 christos || gdbpy_initialize_inferior () < 0
1712 1.1 christos || gdbpy_initialize_events () < 0
1713 1.8 christos || gdbpy_initialize_eventregistry () < 0
1714 1.8 christos || gdbpy_initialize_py_events () < 0
1715 1.8 christos || gdbpy_initialize_event () < 0
1716 1.8 christos || gdbpy_initialize_arch () < 0
1717 1.8 christos || gdbpy_initialize_xmethods () < 0
1718 1.8 christos || gdbpy_initialize_unwind () < 0)
1719 1.1 christos return false;
1720 1.1 christos
1721 1.7 christos #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base) \
1722 1.1 christos if (gdbpy_initialize_event_generic (&name##_event_object_type, py_name) < 0) \
1723 1.1 christos return false;
1724 1.7 christos #include "py-event-types.def"
1725 1.1 christos #undef GDB_PY_DEFINE_EVENT_TYPE
1726 1.1 christos
1727 1.7 christos gdbpy_to_string_cst = PyString_FromString ("to_string");
1728 1.1 christos if (gdbpy_to_string_cst == NULL)
1729 1.1 christos return false;
1730 1.7 christos gdbpy_children_cst = PyString_FromString ("children");
1731 1.1 christos if (gdbpy_children_cst == NULL)
1732 1.1 christos return false;
1733 1.7 christos gdbpy_display_hint_cst = PyString_FromString ("display_hint");
1734 1.1 christos if (gdbpy_display_hint_cst == NULL)
1735 1.1 christos return false;
1736 1.7 christos gdbpy_doc_cst = PyString_FromString ("__doc__");
1737 1.1 christos if (gdbpy_doc_cst == NULL)
1738 1.1 christos return false;
1739 1.1 christos gdbpy_enabled_cst = PyString_FromString ("enabled");
1740 1.1 christos if (gdbpy_enabled_cst == NULL)
1741 1.1 christos return false;
1742 1.1 christos gdbpy_value_cst = PyString_FromString ("value");
1743 1.1 christos if (gdbpy_value_cst == NULL)
1744 1.7 christos return false;
1745 1.1 christos
1746 1.7 christos /* Release the GIL while gdb runs. */
1747 1.7 christos PyThreadState_Swap (NULL);
1748 1.7 christos PyEval_ReleaseLock ();
1749 1.7 christos
1750 1.7 christos make_final_cleanup (finalize_python, NULL);
1751 1.8 christos
1752 1.8 christos /* Only set this when initialization has succeeded. */
1753 1.8 christos gdb_python_initialized = 1;
1754 1.7 christos return true;
1755 1.7 christos }
1756 1.7 christos
1757 1.7 christos #endif /* HAVE_PYTHON */
1758 1.7 christos
1759 1.7 christos /* See python.h. */
1760 1.7 christos cmd_list_element *python_cmd_element = nullptr;
1761 1.7 christos
1762 1.7 christos void
1763 1.7 christos _initialize_python (void)
1764 1.7 christos {
1765 1.7 christos add_com ("python-interactive", class_obscure,
1766 1.7 christos python_interactive_command,
1767 1.7 christos #ifdef HAVE_PYTHON
1768 1.7 christos _("\
1769 1.7 christos Start an interactive Python prompt.\n\
1770 1.7 christos \n\
1771 1.7 christos To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
1772 1.7 christos prompt).\n\
1773 1.7 christos \n\
1774 1.7 christos Alternatively, a single-line Python command can be given as an\n\
1775 1.7 christos argument, and if the command is an expression, the result will be\n\
1776 1.7 christos printed. For example:\n\
1777 1.7 christos \n\
1778 1.7 christos (gdb) python-interactive 2 + 3\n\
1779 1.7 christos 5\n\
1780 1.7 christos ")
1781 1.7 christos #else /* HAVE_PYTHON */
1782 1.7 christos _("\
1783 1.8 christos Start a Python interactive prompt.\n\
1784 1.7 christos \n\
1785 1.7 christos Python scripting is not supported in this copy of GDB.\n\
1786 1.7 christos This command is only a placeholder.")
1787 1.7 christos #endif /* HAVE_PYTHON */
1788 1.7 christos );
1789 1.7 christos add_com_alias ("pi", "python-interactive", class_obscure, 1);
1790 1.8 christos
1791 1.7 christos python_cmd_element = add_com ("python", class_obscure, python_command,
1792 1.7 christos #ifdef HAVE_PYTHON
1793 1.7 christos _("\
1794 1.7 christos Evaluate a Python command.\n\
1795 1.7 christos \n\
1796 1.7 christos The command can be given as an argument, for instance:\n\
1797 1.7 christos \n\
1798 1.7 christos python print (23)\n\
1799 1.7 christos \n\
1800 1.7 christos If no argument is given, the following lines are read and used\n\
1801 1.7 christos as the Python commands. Type a line containing \"end\" to indicate\n\
1802 1.7 christos the end of the command.")
1803 1.7 christos #else /* HAVE_PYTHON */
1804 1.1 christos _("\
1805 1.7 christos Evaluate a Python command.\n\
1806 1.7 christos \n\
1807 1.7 christos Python scripting is not supported in this copy of GDB.\n\
1808 1.7 christos This command is only a placeholder.")
1809 1.7 christos #endif /* HAVE_PYTHON */
1810 1.7 christos );
1811 1.7 christos add_com_alias ("py", "python", class_obscure, 1);
1812 1.7 christos
1813 1.7 christos /* Add set/show python print-stack. */
1814 1.7 christos add_prefix_cmd ("python", no_class, user_show_python,
1815 1.7 christos _("Prefix command for python preference settings."),
1816 1.7 christos &user_show_python_list, "show python ", 0,
1817 1.7 christos &showlist);
1818 1.7 christos
1819 1.7 christos add_prefix_cmd ("python", no_class, user_set_python,
1820 1.7 christos _("Prefix command for python preference settings."),
1821 1.7 christos &user_set_python_list, "set python ", 0,
1822 1.7 christos &setlist);
1823 1.7 christos
1824 1.7 christos add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
1825 1.7 christos &gdbpy_should_print_stack, _("\
1826 1.1 christos Set mode for Python stack dump on error."), _("\
1827 1.7 christos Show the mode of Python stack printing on error."), _("\
1828 1.7 christos none == no stack or message will be printed.\n\
1829 1.7 christos full == a message and a stack will be printed.\n\
1830 1.1 christos message == an error message without a stack will be printed."),
1831 1.1 christos NULL, NULL,
1832 1.1 christos &user_set_python_list,
1833 1.1 christos &user_show_python_list);
1834 1.1 christos
1835 1.7 christos #ifdef HAVE_PYTHON
1836 1.7 christos if (!do_start_initialization () && PyErr_Occurred ())
1837 1.7 christos gdbpy_print_stack ();
1838 1.1 christos #endif /* HAVE_PYTHON */
1839 1.7 christos }
1840 1.7 christos
1841 1.1 christos #ifdef HAVE_PYTHON
1842 1.1 christos
1843 1.1 christos /* Helper function for gdbpy_finish_initialization. This does the
1844 1.1 christos work and then returns false if an error has occurred and must be
1845 1.1 christos displayed, or true on success. */
1846 1.1 christos
1847 1.7 christos static bool
1848 1.7 christos do_finish_initialization (const struct extension_language_defn *extlang)
1849 1.1 christos {
1850 1.1 christos PyObject *m;
1851 1.1 christos PyObject *sys_path;
1852 1.1 christos
1853 1.1 christos /* Add the initial data-directory to sys.path. */
1854 1.1 christos
1855 1.1 christos std::string gdb_pythondir = (std::string (gdb_datadir) + SLASH_STRING
1856 1.1 christos + "python");
1857 1.1 christos
1858 1.1 christos sys_path = PySys_GetObject ("path");
1859 1.1 christos
1860 1.1 christos /* If sys.path is not defined yet, define it first. */
1861 1.1 christos if (!(sys_path && PyList_Check (sys_path)))
1862 1.1 christos {
1863 1.1 christos #ifdef IS_PY3K
1864 1.7 christos PySys_SetPath (L"");
1865 1.7 christos #else
1866 1.7 christos PySys_SetPath ("");
1867 1.1 christos #endif
1868 1.1 christos sys_path = PySys_GetObject ("path");
1869 1.7 christos }
1870 1.1 christos if (sys_path && PyList_Check (sys_path))
1871 1.1 christos {
1872 1.1 christos gdbpy_ref<> pythondir (PyString_FromString (gdb_pythondir.c_str ()));
1873 1.1 christos if (pythondir == NULL || PyList_Insert (sys_path, 0, pythondir.get ()))
1874 1.1 christos return false;
1875 1.7 christos }
1876 1.1 christos else
1877 1.7 christos return false;
1878 1.7 christos
1879 1.1 christos /* Import the gdb module to finish the initialization, and
1880 1.1 christos add it to __main__ for convenience. */
1881 1.1 christos m = PyImport_AddModule ("__main__");
1882 1.1 christos if (m == NULL)
1883 1.1 christos return false;
1884 1.1 christos
1885 1.1 christos /* Keep the reference to gdb_python_module since it is in a global
1886 1.1 christos variable. */
1887 1.1 christos gdb_python_module = PyImport_ImportModule ("gdb");
1888 1.1 christos if (gdb_python_module == NULL)
1889 1.7 christos {
1890 1.7 christos gdbpy_print_stack ();
1891 1.7 christos /* This is passed in one call to warning so that blank lines aren't
1892 1.7 christos inserted between each line of text. */
1893 1.1 christos warning (_("\n"
1894 1.1 christos "Could not load the Python gdb module from `%s'.\n"
1895 1.7 christos "Limited Python support is available from the _gdb module.\n"
1896 1.7 christos "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
1897 1.1 christos gdb_pythondir.c_str ());
1898 1.7 christos /* We return "success" here as we've already emitted the
1899 1.7 christos warning. */
1900 1.7 christos return true;
1901 1.7 christos }
1902 1.7 christos
1903 1.1 christos return gdb_pymodule_addobject (m, "gdb", gdb_python_module) >= 0;
1904 1.7 christos }
1905 1.7 christos
1906 1.7 christos /* Perform the remaining python initializations.
1907 1.7 christos These must be done after GDB is at least mostly initialized.
1908 1.1 christos E.g., The "info pretty-printer" command needs the "info" prefix
1909 1.7 christos command installed.
1910 1.7 christos This is the extension_language_ops.finish_initialization "method". */
1911 1.7 christos
1912 1.7 christos static void
1913 1.7 christos gdbpy_finish_initialization (const struct extension_language_defn *extlang)
1914 1.1 christos {
1915 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language);
1916 1.3 christos
1917 1.3 christos if (!do_finish_initialization (extlang))
1918 1.3 christos {
1919 1.3 christos gdbpy_print_stack ();
1920 1.3 christos warning (_("internal error: Unhandled Python exception"));
1921 1.3 christos }
1922 1.3 christos }
1923 1.3 christos
1924 1.3 christos /* Return non-zero if Python has successfully initialized.
1925 1.1 christos This is the extension_languages_ops.initialized "method". */
1926 1.1 christos
1927 1.1 christos static int
1928 1.1 christos gdbpy_initialized (const struct extension_language_defn *extlang)
1929 1.1 christos {
1930 1.1 christos return gdb_python_initialized;
1931 1.5 christos }
1932 1.1 christos
1933 1.1 christos #endif /* HAVE_PYTHON */
1934 1.1 christos
1935 1.1 christos
1936 1.1 christos
1938 1.1 christos #ifdef HAVE_PYTHON
1939 1.1 christos
1940 1.1 christos PyMethodDef python_GdbMethods[] =
1941 1.1 christos {
1942 1.1 christos { "history", gdbpy_history, METH_VARARGS,
1943 1.1 christos "Get a value from history" },
1944 1.1 christos { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
1945 1.1 christos "execute (command [, from_tty] [, to_string]) -> [String]\n\
1946 1.1 christos Evaluate command, a string, as a gdb CLI command. Optionally returns\n\
1947 1.1 christos a Python String containing the output of the command if to_string is\n\
1948 1.1 christos set to True." },
1949 1.1 christos { "parameter", gdbpy_parameter, METH_VARARGS,
1950 1.1 christos "Return a gdb parameter's value" },
1951 1.1 christos
1952 1.1 christos { "breakpoints", gdbpy_breakpoints, METH_NOARGS,
1953 1.1 christos "Return a tuple of all breakpoint objects" },
1954 1.1 christos
1955 1.1 christos { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
1956 1.1 christos "Find the default visualizer for a Value." },
1957 1.1 christos
1958 1.1 christos { "progspaces", gdbpy_progspaces, METH_NOARGS,
1959 1.1 christos "Return a sequence of all progspaces." },
1960 1.1 christos
1961 1.1 christos { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
1962 1.1 christos "Return the current Objfile being loaded, or None." },
1963 1.1 christos
1964 1.1 christos { "newest_frame", gdbpy_newest_frame, METH_NOARGS,
1965 1.7 christos "newest_frame () -> gdb.Frame.\n\
1966 1.7 christos Return the newest frame object." },
1967 1.7 christos { "selected_frame", gdbpy_selected_frame, METH_NOARGS,
1968 1.7 christos "selected_frame () -> gdb.Frame.\n\
1969 1.7 christos Return the selected frame object." },
1970 1.7 christos { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
1971 1.7 christos "stop_reason_string (Integer) -> String.\n\
1972 1.7 christos Return a string explaining unwind stop reason." },
1973 1.7 christos
1974 1.7 christos { "start_recording", gdbpy_start_recording, METH_VARARGS,
1975 1.7 christos "start_recording ([method] [, format]) -> gdb.Record.\n\
1976 1.7 christos Start recording with the given method. If no method is given, will fall back\n\
1977 1.1 christos to the system default method. If no format is given, will fall back to the\n\
1978 1.1 christos default format for the given method."},
1979 1.1 christos { "current_recording", gdbpy_current_recording, METH_NOARGS,
1980 1.1 christos "current_recording () -> gdb.Record.\n\
1981 1.1 christos Return current recording object." },
1982 1.1 christos { "stop_recording", gdbpy_stop_recording, METH_NOARGS,
1983 1.1 christos "stop_recording () -> None.\n\
1984 1.1 christos Stop current recording." },
1985 1.1 christos
1986 1.1 christos { "lookup_type", (PyCFunction) gdbpy_lookup_type,
1987 1.1 christos METH_VARARGS | METH_KEYWORDS,
1988 1.1 christos "lookup_type (name [, block]) -> type\n\
1989 1.1 christos Return a Type corresponding to the given name." },
1990 1.1 christos { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol,
1991 1.3 christos METH_VARARGS | METH_KEYWORDS,
1992 1.3 christos "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
1993 1.3 christos Return a tuple with the symbol corresponding to the given name (or None) and\n\
1994 1.3 christos a boolean indicating if name is a field of the current implied argument\n\
1995 1.3 christos `this' (when the current language is object-oriented)." },
1996 1.3 christos { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol,
1997 1.3 christos METH_VARARGS | METH_KEYWORDS,
1998 1.3 christos "lookup_global_symbol (name [, domain]) -> symbol\n\
1999 1.1 christos Return the symbol corresponding to the given name (or None)." },
2000 1.1 christos
2001 1.1 christos { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
2002 1.1 christos METH_VARARGS | METH_KEYWORDS,
2003 1.1 christos "lookup_objfile (name, [by_build_id]) -> objfile\n\
2004 1.1 christos Look up the specified objfile.\n\
2005 1.1 christos If by_build_id is True, the objfile is looked up by using name\n\
2006 1.1 christos as its build id." },
2007 1.1 christos
2008 1.1 christos { "decode_line", gdbpy_decode_line, METH_VARARGS,
2009 1.1 christos "decode_line (String) -> Tuple. Decode a string argument the way\n\
2010 1.1 christos that 'break' or 'edit' does. Return a tuple containing two elements.\n\
2011 1.1 christos The first element contains any unparsed portion of the String parameter\n\
2012 1.1 christos (or None if the string was fully parsed). The second element contains\n\
2013 1.1 christos a tuple that contains all the locations that match, represented as\n\
2014 1.1 christos gdb.Symtab_and_line objects (or None)."},
2015 1.1 christos { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
2016 1.1 christos "parse_and_eval (String) -> Value.\n\
2017 1.1 christos Parse String as an expression, evaluate it, and return the result as a Value."
2018 1.1 christos },
2019 1.1 christos
2020 1.8 christos { "post_event", gdbpy_post_event, METH_VARARGS,
2021 1.8 christos "Post an event into gdb's event loop." },
2022 1.8 christos
2023 1.1 christos { "target_charset", gdbpy_target_charset, METH_NOARGS,
2024 1.1 christos "target_charset () -> string.\n\
2025 1.1 christos Return the name of the current target charset." },
2026 1.1 christos { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
2027 1.1 christos "target_wide_charset () -> string.\n\
2028 1.1 christos Return the name of the current target wide charset." },
2029 1.1 christos { "rbreak", (PyCFunction) gdbpy_rbreak, METH_VARARGS | METH_KEYWORDS,
2030 1.1 christos "rbreak (Regex) -> List.\n\
2031 1.1 christos Return a Tuple containing gdb.Breakpoint objects that match the given Regex." },
2032 1.1 christos { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS,
2033 1.1 christos "string_to_argv (String) -> Array.\n\
2034 1.1 christos Parse String and return an argv-like array.\n\
2035 1.1 christos Arguments are separate by spaces and may be quoted."
2036 1.1 christos },
2037 1.1 christos { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS,
2038 1.1 christos "Write a string using gdb's filtered stream." },
2039 1.1 christos { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS,
2040 1.1 christos "Flush gdb's filtered stdout stream." },
2041 1.6 christos { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
2042 1.6 christos "selected_thread () -> gdb.InferiorThread.\n\
2043 1.6 christos Return the selected thread object." },
2044 1.6 christos { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
2045 1.6 christos "selected_inferior () -> gdb.Inferior.\n\
2046 1.6 christos Return the selected inferior object." },
2047 1.8 christos { "inferiors", gdbpy_inferiors, METH_NOARGS,
2048 1.8 christos "inferiors () -> (gdb.Inferior, ...).\n\
2049 1.8 christos Return a tuple containing all inferiors." },
2050 1.8 christos
2051 1.8 christos { "invalidate_cached_frames", gdbpy_invalidate_cached_frames, METH_NOARGS,
2052 1.8 christos "invalidate_cached_frames () -> None.\n\
2053 1.8 christos Invalidate any cached frame objects in gdb.\n\
2054 1.8 christos Intended for internal use only." },
2055 1.1 christos
2056 1.1 christos { "convenience_variable", gdbpy_convenience_variable, METH_VARARGS,
2057 1.1 christos "convenience_variable (NAME) -> value.\n\
2058 1.1 christos Return the value of the convenience variable $NAME,\n\
2059 1.5 christos or None if not set." },
2060 1.1 christos { "set_convenience_variable", gdbpy_set_convenience_variable, METH_VARARGS,
2061 1.1 christos "convenience_variable (NAME, VALUE) -> None.\n\
2062 1.1 christos Set the value of the convenience variable $NAME." },
2063 1.1 christos
2064 1.1 christos {NULL, NULL, 0, NULL}
2065 1.5 christos };
2066 1.1 christos
2067 1.1 christos #ifdef IS_PY3K
2068 1.1 christos struct PyModuleDef python_GdbModuleDef =
2069 1.1 christos {
2070 1.1 christos PyModuleDef_HEAD_INIT,
2071 1.1 christos "_gdb",
2072 1.8 christos NULL,
2073 1.8 christos -1,
2074 1.8 christos python_GdbMethods,
2075 1.8 christos NULL,
2076 1.8 christos NULL,
2077 1.8 christos NULL,
2078 1.8 christos NULL
2079 1.8 christos };
2080 1.8 christos #endif
2081 1.8 christos
2082 1.8 christos /* Define all the event objects. */
2083 1.8 christos #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base) \
2084 1.8 christos PyTypeObject name##_event_object_type \
2085 1.8 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object") \
2086 1.8 christos = { \
2087 1.8 christos PyVarObject_HEAD_INIT (NULL, 0) \
2088 1.8 christos "gdb." py_name, /* tp_name */ \
2089 1.8 christos sizeof (event_object), /* tp_basicsize */ \
2090 1.8 christos 0, /* tp_itemsize */ \
2091 1.8 christos evpy_dealloc, /* tp_dealloc */ \
2092 1.8 christos 0, /* tp_print */ \
2093 1.8 christos 0, /* tp_getattr */ \
2094 1.8 christos 0, /* tp_setattr */ \
2095 1.8 christos 0, /* tp_compare */ \
2096 1.8 christos 0, /* tp_repr */ \
2097 1.8 christos 0, /* tp_as_number */ \
2098 1.8 christos 0, /* tp_as_sequence */ \
2099 1.8 christos 0, /* tp_as_mapping */ \
2100 1.8 christos 0, /* tp_hash */ \
2101 1.8 christos 0, /* tp_call */ \
2102 1.8 christos 0, /* tp_str */ \
2103 1.8 christos 0, /* tp_getattro */ \
2104 1.8 christos 0, /* tp_setattro */ \
2105 1.8 christos 0, /* tp_as_buffer */ \
2106 1.8 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ \
2107 1.8 christos doc, /* tp_doc */ \
2108 1.8 christos 0, /* tp_traverse */ \
2109 1.8 christos 0, /* tp_clear */ \
2110 1.8 christos 0, /* tp_richcompare */ \
2111 1.8 christos 0, /* tp_weaklistoffset */ \
2112 1.8 christos 0, /* tp_iter */ \
2113 1.8 christos 0, /* tp_iternext */ \
2114 1.8 christos 0, /* tp_methods */ \
2115 1.8 christos 0, /* tp_members */ \
2116 1.8 christos 0, /* tp_getset */ \
2117 1.8 christos &base, /* tp_base */ \
2118 1.8 christos 0, /* tp_dict */ \
2119 1.1 christos 0, /* tp_descr_get */ \
2120 0, /* tp_descr_set */ \
2121 0, /* tp_dictoffset */ \
2122 0, /* tp_init */ \
2123 0 /* tp_alloc */ \
2124 };
2125 #include "py-event-types.def"
2126 #undef GDB_PY_DEFINE_EVENT_TYPE
2127
2128 #endif /* HAVE_PYTHON */
2129