python.c revision 1.10 1 1.1 christos /* General python/gdb code
2 1.1 christos
3 1.10 christos Copyright (C) 2008-2023 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.9 christos #include "gdbsupport/event-loop.h"
31 1.1 christos #include "readline/tilde.h"
32 1.1 christos #include "python.h"
33 1.3 christos #include "extension-priv.h"
34 1.1 christos #include "cli/cli-utils.h"
35 1.1 christos #include <ctype.h>
36 1.6 christos #include "location.h"
37 1.9 christos #include "run-on-main-thread.h"
38 1.10 christos #include "gdbsupport/selftest.h"
39 1.10 christos #include "observable.h"
40 1.1 christos
41 1.1 christos /* Declared constants and enum for python stack printing. */
42 1.1 christos static const char python_excp_none[] = "none";
43 1.1 christos static const char python_excp_full[] = "full";
44 1.1 christos static const char python_excp_message[] = "message";
45 1.1 christos
46 1.1 christos /* "set python print-stack" choices. */
47 1.1 christos static const char *const python_excp_enums[] =
48 1.1 christos {
49 1.1 christos python_excp_none,
50 1.1 christos python_excp_full,
51 1.1 christos python_excp_message,
52 1.1 christos NULL
53 1.1 christos };
54 1.1 christos
55 1.1 christos /* The exception printing variable. 'full' if we want to print the
56 1.1 christos error message and stack, 'none' if we want to print nothing, and
57 1.1 christos 'message' if we only want to print the error message. 'message' is
58 1.1 christos the default. */
59 1.1 christos static const char *gdbpy_should_print_stack = python_excp_message;
60 1.1 christos
61 1.3 christos
62 1.3 christos #ifdef HAVE_PYTHON
64 1.1 christos
65 1.1 christos #include "cli/cli-decode.h"
66 1.1 christos #include "charset.h"
67 1.1 christos #include "top.h"
68 1.1 christos #include "python-internal.h"
69 1.1 christos #include "linespec.h"
70 1.9 christos #include "source.h"
71 1.1 christos #include "gdbsupport/version.h"
72 1.1 christos #include "target.h"
73 1.1 christos #include "gdbthread.h"
74 1.1 christos #include "interps.h"
75 1.7 christos #include "event-top.h"
76 1.1 christos #include "py-event.h"
77 1.1 christos
78 1.1 christos /* True if Python has been successfully initialized, false
79 1.1 christos otherwise. */
80 1.1 christos
81 1.1 christos int gdb_python_initialized;
82 1.5 christos
83 1.1 christos extern PyMethodDef python_GdbMethods[];
84 1.1 christos
85 1.1 christos PyObject *gdb_module;
86 1.1 christos PyObject *gdb_python_module;
87 1.1 christos
88 1.1 christos /* Some string constants we may wish to use. */
89 1.1 christos PyObject *gdbpy_to_string_cst;
90 1.1 christos PyObject *gdbpy_children_cst;
91 1.1 christos PyObject *gdbpy_display_hint_cst;
92 1.1 christos PyObject *gdbpy_doc_cst;
93 1.1 christos PyObject *gdbpy_enabled_cst;
94 1.1 christos PyObject *gdbpy_value_cst;
95 1.1 christos
96 1.1 christos /* The GdbError exception. */
97 1.1 christos PyObject *gdbpy_gdberror_exc;
98 1.1 christos
99 1.1 christos /* The `gdb.error' base class. */
100 1.1 christos PyObject *gdbpy_gdb_error;
101 1.1 christos
102 1.1 christos /* The `gdb.MemoryError' exception. */
103 1.1 christos PyObject *gdbpy_gdb_memory_error;
104 1.3 christos
105 1.3 christos static script_sourcer_func gdbpy_source_script;
106 1.5 christos static objfile_script_sourcer_func gdbpy_source_objfile_script;
107 1.10 christos static objfile_script_executor_func gdbpy_execute_objfile_script;
108 1.3 christos static void gdbpy_initialize (const struct extension_language_defn *);
109 1.3 christos static int gdbpy_initialized (const struct extension_language_defn *);
110 1.3 christos static void gdbpy_eval_from_control_command
111 1.3 christos (const struct extension_language_defn *, struct command_line *cmd);
112 1.3 christos static void gdbpy_start_type_printers (const struct extension_language_defn *,
113 1.3 christos struct ext_lang_type_printers *);
114 1.3 christos static enum ext_lang_rc gdbpy_apply_type_printers
115 1.3 christos (const struct extension_language_defn *,
116 1.3 christos const struct ext_lang_type_printers *, struct type *, char **);
117 1.3 christos static void gdbpy_free_type_printers (const struct extension_language_defn *,
118 1.3 christos struct ext_lang_type_printers *);
119 1.3 christos static void gdbpy_set_quit_flag (const struct extension_language_defn *);
120 1.3 christos static int gdbpy_check_quit_flag (const struct extension_language_defn *);
121 1.3 christos static enum ext_lang_rc gdbpy_before_prompt_hook
122 1.9 christos (const struct extension_language_defn *, const char *current_gdb_prompt);
123 1.9 christos static gdb::optional<std::string> gdbpy_colorize
124 1.10 christos (const std::string &filename, const std::string &contents);
125 1.10 christos static gdb::optional<std::string> gdbpy_colorize_disasm
126 1.3 christos (const std::string &content, gdbarch *gdbarch);
127 1.3 christos
128 1.3 christos /* The interface between gdb proper and loading of python scripts. */
129 1.10 christos
130 1.3 christos static const struct extension_language_script_ops python_extension_script_ops =
131 1.3 christos {
132 1.3 christos gdbpy_source_script,
133 1.5 christos gdbpy_source_objfile_script,
134 1.3 christos gdbpy_execute_objfile_script,
135 1.3 christos gdbpy_auto_load_enabled
136 1.3 christos };
137 1.3 christos
138 1.3 christos /* The interface between gdb proper and python extensions. */
139 1.10 christos
140 1.3 christos static const struct extension_language_ops python_extension_ops =
141 1.10 christos {
142 1.3 christos gdbpy_initialize,
143 1.3 christos gdbpy_initialized,
144 1.3 christos
145 1.3 christos gdbpy_eval_from_control_command,
146 1.3 christos
147 1.3 christos gdbpy_start_type_printers,
148 1.3 christos gdbpy_apply_type_printers,
149 1.3 christos gdbpy_free_type_printers,
150 1.3 christos
151 1.3 christos gdbpy_apply_val_pretty_printer,
152 1.3 christos
153 1.3 christos gdbpy_apply_frame_filter,
154 1.3 christos
155 1.3 christos gdbpy_preserve_values,
156 1.3 christos
157 1.3 christos gdbpy_breakpoint_has_cond,
158 1.3 christos gdbpy_breakpoint_cond_says_stop,
159 1.3 christos
160 1.3 christos gdbpy_set_quit_flag,
161 1.3 christos gdbpy_check_quit_flag,
162 1.3 christos
163 1.3 christos gdbpy_before_prompt_hook,
164 1.3 christos
165 1.9 christos gdbpy_get_matching_xmethod_workers,
166 1.9 christos
167 1.10 christos gdbpy_colorize,
168 1.10 christos
169 1.10 christos gdbpy_colorize_disasm,
170 1.10 christos
171 1.3 christos gdbpy_print_insn,
172 1.3 christos };
173 1.10 christos
174 1.10 christos #endif /* HAVE_PYTHON */
175 1.10 christos
176 1.10 christos /* The main struct describing GDB's interface to the Python
177 1.10 christos extension language. */
178 1.10 christos const struct extension_language_defn extension_language_python =
179 1.10 christos {
180 1.10 christos EXT_LANG_PYTHON,
181 1.10 christos "python",
182 1.10 christos "Python",
183 1.10 christos
184 1.10 christos ".py",
185 1.10 christos "-gdb.py",
186 1.10 christos
187 1.10 christos python_control,
188 1.10 christos
189 1.10 christos #ifdef HAVE_PYTHON
190 1.10 christos &python_extension_script_ops,
191 1.10 christos &python_extension_ops
192 1.10 christos #else
193 1.10 christos NULL,
194 1.10 christos NULL
195 1.10 christos #endif
196 1.10 christos };
197 1.10 christos
198 1.10 christos #ifdef HAVE_PYTHON
199 1.1 christos
200 1.1 christos /* Architecture and language to be used in callbacks from
201 1.10 christos the Python interpreter. */
202 1.1 christos struct gdbarch *gdbpy_enter::python_gdbarch;
203 1.7 christos
204 1.7 christos gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch,
205 1.7 christos const struct language_defn *language)
206 1.10 christos : m_gdbarch (python_gdbarch),
207 1.7 christos m_language (language == nullptr ? nullptr : current_language)
208 1.7 christos {
209 1.7 christos /* We should not ever enter Python unless initialized. */
210 1.7 christos if (!gdb_python_initialized)
211 1.7 christos error (_("Python not initialized"));
212 1.7 christos
213 1.7 christos m_previous_active = set_active_ext_lang (&extension_language_python);
214 1.7 christos
215 1.7 christos m_state = PyGILState_Ensure ();
216 1.7 christos
217 1.10 christos python_gdbarch = gdbarch;
218 1.10 christos if (language != nullptr)
219 1.1 christos set_language (language->la_language);
220 1.7 christos
221 1.8 christos /* Save it and ensure ! PyErr_Occurred () afterwards. */
222 1.7 christos m_error.emplace ();
223 1.1 christos }
224 1.7 christos
225 1.1 christos gdbpy_enter::~gdbpy_enter ()
226 1.1 christos {
227 1.1 christos /* Leftover Python error is forbidden by Python Exception Handling. */
228 1.1 christos if (PyErr_Occurred ())
229 1.1 christos {
230 1.1 christos /* This order is similar to the one calling error afterwards. */
231 1.1 christos gdbpy_print_stack ();
232 1.1 christos warning (_("internal error: Unhandled Python exception"));
233 1.1 christos }
234 1.8 christos
235 1.3 christos m_error->restore ();
236 1.7 christos
237 1.10 christos python_gdbarch = m_gdbarch;
238 1.10 christos if (m_language != nullptr)
239 1.3 christos set_language (m_language->la_language);
240 1.7 christos
241 1.9 christos restore_active_ext_lang (m_previous_active);
242 1.1 christos PyGILState_Release (m_state);
243 1.1 christos }
244 1.10 christos
245 1.10 christos struct gdbarch *
246 1.10 christos gdbpy_enter::get_gdbarch ()
247 1.10 christos {
248 1.10 christos if (python_gdbarch != nullptr)
249 1.10 christos return python_gdbarch;
250 1.10 christos return get_current_arch ();
251 1.10 christos }
252 1.10 christos
253 1.10 christos void
254 1.10 christos gdbpy_enter::finalize ()
255 1.10 christos {
256 1.10 christos python_gdbarch = target_gdbarch ();
257 1.10 christos }
258 1.9 christos
259 1.9 christos /* A helper class to save and restore the GIL, but without touching
260 1.9 christos the other globals that are handled by gdbpy_enter. */
261 1.9 christos
262 1.9 christos class gdbpy_gil
263 1.9 christos {
264 1.9 christos public:
265 1.9 christos
266 1.9 christos gdbpy_gil ()
267 1.9 christos : m_state (PyGILState_Ensure ())
268 1.9 christos {
269 1.9 christos }
270 1.9 christos
271 1.9 christos ~gdbpy_gil ()
272 1.9 christos {
273 1.9 christos PyGILState_Release (m_state);
274 1.9 christos }
275 1.9 christos
276 1.9 christos DISABLE_COPY_AND_ASSIGN (gdbpy_gil);
277 1.9 christos
278 1.9 christos private:
279 1.9 christos
280 1.9 christos PyGILState_STATE m_state;
281 1.9 christos };
282 1.1 christos
283 1.1 christos /* Set the quit flag. */
284 1.3 christos
285 1.3 christos static void
286 1.1 christos gdbpy_set_quit_flag (const struct extension_language_defn *extlang)
287 1.1 christos {
288 1.1 christos PyErr_SetInterrupt ();
289 1.1 christos }
290 1.1 christos
291 1.1 christos /* Return true if the quit flag has been set, false otherwise. */
292 1.3 christos
293 1.3 christos static int
294 1.1 christos gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
295 1.9 christos {
296 1.9 christos if (!gdb_python_initialized)
297 1.9 christos return 0;
298 1.9 christos
299 1.1 christos gdbpy_gil gil;
300 1.1 christos return PyOS_InterruptOccurred ();
301 1.1 christos }
302 1.1 christos
303 1.1 christos /* Evaluate a Python command like PyRun_SimpleString, but uses
304 1.1 christos Py_single_input which prints the result of expressions, and does
305 1.1 christos not automatically print the stack on errors. */
306 1.1 christos
307 1.1 christos static int
308 1.1 christos eval_python_command (const char *command)
309 1.7 christos {
310 1.1 christos PyObject *m, *d;
311 1.1 christos
312 1.1 christos m = PyImport_AddModule ("__main__");
313 1.1 christos if (m == NULL)
314 1.1 christos return -1;
315 1.1 christos
316 1.1 christos d = PyModule_GetDict (m);
317 1.1 christos if (d == NULL)
318 1.7 christos return -1;
319 1.1 christos gdbpy_ref<> v (PyRun_StringFlags (command, Py_single_input, d, d, NULL));
320 1.1 christos if (v == NULL)
321 1.1 christos return -1;
322 1.1 christos
323 1.1 christos return 0;
324 1.1 christos }
325 1.1 christos
326 1.1 christos /* Implementation of the gdb "python-interactive" command. */
327 1.1 christos
328 1.8 christos static void
329 1.1 christos python_interactive_command (const char *arg, int from_tty)
330 1.6 christos {
331 1.1 christos struct ui *ui = current_ui;
332 1.1 christos int err;
333 1.7 christos
334 1.1 christos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
335 1.1 christos
336 1.1 christos arg = skip_spaces (arg);
337 1.10 christos
338 1.1 christos gdbpy_enter enter_py;
339 1.1 christos
340 1.1 christos if (arg && *arg)
341 1.8 christos {
342 1.8 christos std::string script = std::string (arg) + "\n";
343 1.1 christos err = eval_python_command (script.c_str ());
344 1.1 christos }
345 1.1 christos else
346 1.6 christos {
347 1.1 christos err = PyRun_InteractiveLoop (ui->instream, "<stdin>");
348 1.1 christos dont_repeat ();
349 1.1 christos }
350 1.1 christos
351 1.1 christos if (err)
352 1.1 christos {
353 1.1 christos gdbpy_print_stack ();
354 1.1 christos error (_("Error while executing Python code."));
355 1.1 christos }
356 1.1 christos }
357 1.1 christos
358 1.1 christos /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
359 1.1 christos named FILENAME.
360 1.1 christos
361 1.1 christos On Windows hosts few users would build Python themselves (this is no
362 1.1 christos trivial task on this platform), and thus use binaries built by
363 1.1 christos someone else instead. There may happen situation where the Python
364 1.1 christos library and GDB are using two different versions of the C runtime
365 1.1 christos library. Python, being built with VC, would use one version of the
366 1.1 christos msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
367 1.1 christos A FILE * from one runtime does not necessarily operate correctly in
368 1.1 christos the other runtime.
369 1.9 christos
370 1.9 christos To work around this potential issue, we run code in Python to load
371 1.1 christos the script. */
372 1.1 christos
373 1.1 christos static void
374 1.1 christos python_run_simple_file (FILE *file, const char *filename)
375 1.1 christos {
376 1.1 christos #ifndef _WIN32
377 1.1 christos
378 1.1 christos PyRun_SimpleFile (file, filename);
379 1.1 christos
380 1.1 christos #else /* _WIN32 */
381 1.1 christos
382 1.1 christos /* Because we have a string for a filename, and are using Python to
383 1.7 christos open the file, we need to expand any tilde in the path first. */
384 1.9 christos gdb::unique_xmalloc_ptr<char> full_path (tilde_expand (filename));
385 1.9 christos
386 1.9 christos if (gdb_python_module == nullptr
387 1.9 christos || ! PyObject_HasAttrString (gdb_python_module, "_execute_file"))
388 1.9 christos error (_("Installation error: gdb._execute_file function is missing"));
389 1.9 christos
390 1.9 christos gdbpy_ref<> return_value
391 1.9 christos (PyObject_CallMethod (gdb_python_module, "_execute_file", "s",
392 1.9 christos full_path.get ()));
393 1.9 christos if (return_value == nullptr)
394 1.9 christos {
395 1.10 christos /* Use PyErr_PrintEx instead of gdbpy_print_stack to better match the
396 1.9 christos behavior of the non-Windows codepath. */
397 1.1 christos PyErr_PrintEx(0);
398 1.1 christos }
399 1.1 christos
400 1.1 christos #endif /* _WIN32 */
401 1.1 christos }
402 1.1 christos
403 1.8 christos /* Given a command_line, return a command string suitable for passing
404 1.1 christos to Python. Lines in the string are separated by newlines. */
405 1.8 christos
406 1.1 christos static std::string
407 1.1 christos compute_python_string (struct command_line *l)
408 1.1 christos {
409 1.8 christos struct command_line *iter;
410 1.1 christos std::string script;
411 1.1 christos
412 1.1 christos for (iter = l; iter; iter = iter->next)
413 1.8 christos {
414 1.8 christos script += iter->line;
415 1.1 christos script += '\n';
416 1.1 christos }
417 1.1 christos return script;
418 1.1 christos }
419 1.1 christos
420 1.1 christos /* Take a command line structure representing a 'python' command, and
421 1.1 christos evaluate its body using the Python interpreter. */
422 1.3 christos
423 1.3 christos static void
424 1.3 christos gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
425 1.1 christos struct command_line *cmd)
426 1.1 christos {
427 1.1 christos int ret;
428 1.8 christos
429 1.1 christos if (cmd->body_list_1 != nullptr)
430 1.1 christos error (_("Invalid \"python\" block structure."));
431 1.10 christos
432 1.1 christos gdbpy_enter enter_py;
433 1.8 christos
434 1.8 christos std::string script = compute_python_string (cmd->body_list_0.get ());
435 1.1 christos ret = PyRun_SimpleString (script.c_str ());
436 1.1 christos if (ret)
437 1.1 christos error (_("Error while executing Python code."));
438 1.1 christos }
439 1.1 christos
440 1.1 christos /* Implementation of the gdb "python" command. */
441 1.1 christos
442 1.8 christos static void
443 1.1 christos python_command (const char *arg, int from_tty)
444 1.10 christos {
445 1.1 christos gdbpy_enter enter_py;
446 1.7 christos
447 1.1 christos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
448 1.1 christos
449 1.1 christos arg = skip_spaces (arg);
450 1.1 christos if (arg && *arg)
451 1.1 christos {
452 1.1 christos if (PyRun_SimpleString (arg))
453 1.1 christos error (_("Error while executing Python code."));
454 1.1 christos }
455 1.1 christos else
456 1.8 christos {
457 1.1 christos counted_command_line l = get_command_line (python_control, "");
458 1.7 christos
459 1.1 christos execute_control_command_untraced (l.get ());
460 1.1 christos }
461 1.1 christos }
462 1.1 christos
463 1.1 christos
464 1.1 christos
466 1.1 christos /* Transform a gdb parameters's value into a Python value. May return
467 1.1 christos NULL (and set a Python exception) on error. Helper function for
468 1.10 christos get_parameter. */
469 1.1 christos PyObject *
470 1.10 christos gdbpy_parameter_value (const setting &var)
471 1.1 christos {
472 1.1 christos switch (var.type ())
473 1.1 christos {
474 1.1 christos case var_string:
475 1.1 christos case var_string_noescape:
476 1.1 christos case var_optional_filename:
477 1.1 christos case var_filename:
478 1.10 christos case var_enum:
479 1.10 christos {
480 1.10 christos const char *str;
481 1.10 christos if (var.type () == var_enum)
482 1.10 christos str = var.get<const char *> ();
483 1.1 christos else
484 1.8 christos str = var.get<std::string> ().c_str ();
485 1.1 christos
486 1.1 christos return host_string_to_python_string (str).release ();
487 1.1 christos }
488 1.1 christos
489 1.10 christos case var_boolean:
490 1.1 christos {
491 1.1 christos if (var.get<bool> ())
492 1.1 christos Py_RETURN_TRUE;
493 1.1 christos else
494 1.1 christos Py_RETURN_FALSE;
495 1.1 christos }
496 1.1 christos
497 1.10 christos case var_auto_boolean:
498 1.1 christos {
499 1.1 christos enum auto_boolean ab = var.get<enum auto_boolean> ();
500 1.1 christos
501 1.1 christos if (ab == AUTO_BOOLEAN_TRUE)
502 1.1 christos Py_RETURN_TRUE;
503 1.1 christos else if (ab == AUTO_BOOLEAN_FALSE)
504 1.1 christos Py_RETURN_FALSE;
505 1.1 christos else
506 1.1 christos Py_RETURN_NONE;
507 1.1 christos }
508 1.10 christos
509 1.1 christos case var_integer:
510 1.1 christos if (var.get<int> () == INT_MAX)
511 1.1 christos Py_RETURN_NONE;
512 1.8 christos /* Fall through. */
513 1.10 christos case var_zinteger:
514 1.1 christos case var_zuinteger_unlimited:
515 1.1 christos return gdb_py_object_from_longest (var.get<int> ()).release ();
516 1.1 christos
517 1.10 christos case var_uinteger:
518 1.1 christos {
519 1.1 christos unsigned int val = var.get<unsigned int> ();
520 1.1 christos
521 1.10 christos if (val == UINT_MAX)
522 1.1 christos Py_RETURN_NONE;
523 1.8 christos return gdb_py_object_from_ulongest (val).release ();
524 1.8 christos }
525 1.8 christos
526 1.10 christos case var_zuinteger:
527 1.10 christos {
528 1.8 christos unsigned int val = var.get<unsigned int> ();
529 1.1 christos return gdb_py_object_from_ulongest (val).release ();
530 1.1 christos }
531 1.1 christos }
532 1.1 christos
533 1.1 christos return PyErr_Format (PyExc_RuntimeError,
534 1.1 christos _("Programmer error: unhandled type."));
535 1.1 christos }
536 1.1 christos
537 1.1 christos /* A Python function which returns a gdb parameter's value as a Python
538 1.6 christos value. */
539 1.1 christos
540 1.1 christos static PyObject *
541 1.1 christos gdbpy_parameter (PyObject *self, PyObject *args)
542 1.1 christos {
543 1.1 christos struct cmd_list_element *alias, *prefix, *cmd;
544 1.1 christos const char *arg;
545 1.1 christos int found = -1;
546 1.1 christos
547 1.1 christos if (! PyArg_ParseTuple (args, "s", &arg))
548 1.8 christos return NULL;
549 1.1 christos
550 1.9 christos std::string newarg = std::string ("show ") + arg;
551 1.1 christos
552 1.8 christos try
553 1.1 christos {
554 1.9 christos found = lookup_cmd_composition (newarg.c_str (), &alias, &prefix, &cmd);
555 1.5 christos }
556 1.8 christos catch (const gdb_exception &ex)
557 1.5 christos {
558 1.5 christos GDB_PY_HANDLE_EXCEPTION (ex);
559 1.1 christos }
560 1.1 christos
561 1.1 christos if (!found)
562 1.1 christos return PyErr_Format (PyExc_RuntimeError,
563 1.10 christos _("Could not find parameter `%s'."), arg);
564 1.1 christos
565 1.1 christos if (!cmd->var.has_value ())
566 1.10 christos return PyErr_Format (PyExc_RuntimeError,
567 1.10 christos _("`%s' is not a parameter."), arg);
568 1.1 christos
569 1.1 christos return gdbpy_parameter_value (*cmd->var);
570 1.1 christos }
571 1.1 christos
572 1.1 christos /* Wrapper for target_charset. */
573 1.1 christos
574 1.1 christos static PyObject *
575 1.10 christos gdbpy_target_charset (PyObject *self, PyObject *args)
576 1.1 christos {
577 1.1 christos const char *cset = target_charset (gdbpy_enter::get_gdbarch ());
578 1.1 christos
579 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
580 1.1 christos }
581 1.1 christos
582 1.1 christos /* Wrapper for target_wide_charset. */
583 1.1 christos
584 1.1 christos static PyObject *
585 1.10 christos gdbpy_target_wide_charset (PyObject *self, PyObject *args)
586 1.10 christos {
587 1.10 christos const char *cset = target_wide_charset (gdbpy_enter::get_gdbarch ());
588 1.10 christos
589 1.10 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
590 1.10 christos }
591 1.10 christos
592 1.10 christos /* Implement gdb.host_charset(). */
593 1.10 christos
594 1.10 christos static PyObject *
595 1.10 christos gdbpy_host_charset (PyObject *self, PyObject *args)
596 1.1 christos {
597 1.1 christos const char *cset = host_charset ();
598 1.1 christos
599 1.1 christos return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
600 1.1 christos }
601 1.1 christos
602 1.1 christos /* A Python function which evaluates a string using the gdb CLI. */
603 1.1 christos
604 1.1 christos static PyObject *
605 1.1 christos execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
606 1.1 christos {
607 1.1 christos const char *arg;
608 1.7 christos PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
609 1.1 christos int from_tty, to_string;
610 1.7 christos static const char *keywords[] = { "command", "from_tty", "to_string", NULL };
611 1.7 christos
612 1.7 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
613 1.1 christos &PyBool_Type, &from_tty_obj,
614 1.1 christos &PyBool_Type, &to_string_obj))
615 1.1 christos return NULL;
616 1.1 christos
617 1.1 christos from_tty = 0;
618 1.1 christos if (from_tty_obj)
619 1.1 christos {
620 1.1 christos int cmp = PyObject_IsTrue (from_tty_obj);
621 1.1 christos if (cmp < 0)
622 1.1 christos return NULL;
623 1.1 christos from_tty = cmp;
624 1.1 christos }
625 1.1 christos
626 1.1 christos to_string = 0;
627 1.1 christos if (to_string_obj)
628 1.1 christos {
629 1.1 christos int cmp = PyObject_IsTrue (to_string_obj);
630 1.1 christos if (cmp < 0)
631 1.1 christos return NULL;
632 1.1 christos to_string = cmp;
633 1.7 christos }
634 1.7 christos
635 1.8 christos std::string to_string_res;
636 1.8 christos
637 1.9 christos scoped_restore preventer = prevent_dont_repeat ();
638 1.1 christos
639 1.8 christos try
640 1.8 christos {
641 1.6 christos gdbpy_allow_threads allow_threads;
642 1.6 christos
643 1.8 christos struct interp *interp;
644 1.8 christos
645 1.8 christos std::string arg_copy = arg;
646 1.8 christos bool first = true;
647 1.10 christos char *save_ptr = nullptr;
648 1.8 christos auto reader
649 1.8 christos = [&] (std::string &buffer)
650 1.8 christos {
651 1.8 christos const char *result = strtok_r (first ? &arg_copy[0] : nullptr,
652 1.8 christos "\n", &save_ptr);
653 1.8 christos first = false;
654 1.8 christos return result;
655 1.8 christos };
656 1.8 christos
657 1.8 christos counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
658 1.8 christos
659 1.8 christos {
660 1.8 christos scoped_restore save_async = make_scoped_restore (¤t_ui->async,
661 1.8 christos 0);
662 1.6 christos
663 1.8 christos scoped_restore save_uiout = make_scoped_restore (¤t_uiout);
664 1.8 christos
665 1.8 christos /* Use the console interpreter uiout to have the same print format
666 1.8 christos for console or MI. */
667 1.8 christos interp = interp_lookup (current_ui, "console");
668 1.8 christos current_uiout = interp->interp_ui_out ();
669 1.8 christos
670 1.8 christos if (to_string)
671 1.8 christos to_string_res = execute_control_commands_to_string (lines.get (),
672 1.8 christos from_tty);
673 1.8 christos else
674 1.1 christos execute_control_commands (lines.get (), from_tty);
675 1.8 christos }
676 1.8 christos
677 1.1 christos /* Do any commands attached to breakpoint we stopped at. */
678 1.9 christos bpstat_do_actions ();
679 1.5 christos }
680 1.9 christos catch (const gdb_exception &except)
681 1.9 christos {
682 1.9 christos /* If an exception occurred then we won't hit normal_stop (), or have
683 1.9 christos an exception reach the top level of the event loop, which are the
684 1.9 christos two usual places in which stdin would be re-enabled. So, before we
685 1.9 christos convert the exception and continue back in Python, we should
686 1.5 christos re-enable stdin here. */
687 1.5 christos async_enable_stdin ();
688 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
689 1.7 christos }
690 1.10 christos
691 1.1 christos if (to_string)
692 1.1 christos return PyUnicode_FromString (to_string_res.c_str ());
693 1.1 christos Py_RETURN_NONE;
694 1.8 christos }
695 1.8 christos
696 1.8 christos /* Implementation of Python rbreak command. Take a REGEX and
697 1.8 christos optionally a MINSYMS, THROTTLE and SYMTABS keyword and return a
698 1.8 christos Python list that contains newly set breakpoints that match that
699 1.8 christos criteria. REGEX refers to a GDB format standard regex pattern of
700 1.8 christos symbols names to search; MINSYMS is an optional boolean (default
701 1.8 christos False) that indicates if the function should search GDB's minimal
702 1.8 christos symbols; THROTTLE is an optional integer (default unlimited) that
703 1.8 christos indicates the maximum amount of breakpoints allowable before the
704 1.8 christos function exits (note, if the throttle bound is passed, no
705 1.8 christos breakpoints will be set and a runtime error returned); SYMTABS is
706 1.1 christos an optional Python iterable that contains a set of gdb.Symtabs to
707 1.1 christos constrain the search within. */
708 1.8 christos
709 1.1 christos static PyObject *
710 1.8 christos gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
711 1.8 christos {
712 1.8 christos char *regex = NULL;
713 1.8 christos std::vector<symbol_search> symbols;
714 1.8 christos unsigned long count = 0;
715 1.8 christos PyObject *symtab_list = NULL;
716 1.8 christos PyObject *minsyms_p_obj = NULL;
717 1.8 christos int minsyms_p = 0;
718 1.8 christos unsigned int throttle = 0;
719 1.8 christos static const char *keywords[] = {"regex","minsyms", "throttle",
720 1.8 christos "symtabs", NULL};
721 1.8 christos
722 1.8 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!IO", keywords,
723 1.8 christos ®ex, &PyBool_Type,
724 1.1 christos &minsyms_p_obj, &throttle,
725 1.1 christos &symtab_list))
726 1.8 christos return NULL;
727 1.8 christos
728 1.8 christos /* Parse minsyms keyword. */
729 1.8 christos if (minsyms_p_obj != NULL)
730 1.8 christos {
731 1.8 christos int cmp = PyObject_IsTrue (minsyms_p_obj);
732 1.8 christos if (cmp < 0)
733 1.8 christos return NULL;
734 1.8 christos minsyms_p = cmp;
735 1.9 christos }
736 1.9 christos
737 1.9 christos global_symbol_searcher spec (FUNCTIONS_DOMAIN, regex);
738 1.9 christos SCOPE_EXIT {
739 1.9 christos for (const char *elem : spec.filenames)
740 1.9 christos xfree ((void *) elem);
741 1.8 christos };
742 1.8 christos
743 1.8 christos /* The "symtabs" keyword is any Python iterable object that returns
744 1.8 christos a gdb.Symtab on each iteration. If specified, iterate through
745 1.8 christos the provided gdb.Symtabs and extract their full path. As
746 1.8 christos python_string_to_target_string returns a
747 1.8 christos gdb::unique_xmalloc_ptr<char> and a vector containing these types
748 1.8 christos cannot be coerced to a const char **p[] via the vector.data call,
749 1.8 christos release the value from the unique_xmalloc_ptr and place it in a
750 1.8 christos simple type symtab_list_type (which holds the vector and a
751 1.8 christos destructor that frees the contents of the allocated strings. */
752 1.8 christos if (symtab_list != NULL)
753 1.8 christos {
754 1.8 christos gdbpy_ref<> iter (PyObject_GetIter (symtab_list));
755 1.8 christos
756 1.8 christos if (iter == NULL)
757 1.8 christos return NULL;
758 1.8 christos
759 1.8 christos while (true)
760 1.8 christos {
761 1.8 christos gdbpy_ref<> next (PyIter_Next (iter.get ()));
762 1.8 christos
763 1.8 christos if (next == NULL)
764 1.8 christos {
765 1.8 christos if (PyErr_Occurred ())
766 1.8 christos return NULL;
767 1.8 christos break;
768 1.8 christos }
769 1.8 christos
770 1.8 christos gdbpy_ref<> obj_name (PyObject_GetAttrString (next.get (),
771 1.8 christos "filename"));
772 1.8 christos
773 1.8 christos if (obj_name == NULL)
774 1.8 christos return NULL;
775 1.8 christos
776 1.8 christos /* Is the object file still valid? */
777 1.8 christos if (obj_name == Py_None)
778 1.8 christos continue;
779 1.8 christos
780 1.8 christos gdb::unique_xmalloc_ptr<char> filename =
781 1.8 christos python_string_to_target_string (obj_name.get ());
782 1.8 christos
783 1.8 christos if (filename == NULL)
784 1.8 christos return NULL;
785 1.8 christos
786 1.9 christos /* Make sure there is a definite place to store the value of
787 1.9 christos filename before it is released. */
788 1.8 christos spec.filenames.push_back (nullptr);
789 1.8 christos spec.filenames.back () = filename.release ();
790 1.8 christos }
791 1.9 christos }
792 1.9 christos
793 1.8 christos /* The search spec. */
794 1.8 christos symbols = spec.search ();
795 1.8 christos
796 1.8 christos /* Count the number of symbols (both symbols and optionally minimal
797 1.8 christos symbols) so we can correctly check the throttle limit. */
798 1.8 christos for (const symbol_search &p : symbols)
799 1.8 christos {
800 1.8 christos /* Minimal symbols included? */
801 1.8 christos if (minsyms_p)
802 1.8 christos {
803 1.8 christos if (p.msymbol.minsym != NULL)
804 1.8 christos count++;
805 1.8 christos }
806 1.8 christos
807 1.8 christos if (p.symbol != NULL)
808 1.8 christos count++;
809 1.8 christos }
810 1.8 christos
811 1.1 christos /* Check throttle bounds and exit if in excess. */
812 1.8 christos if (throttle != 0 && count > throttle)
813 1.8 christos {
814 1.8 christos PyErr_SetString (PyExc_RuntimeError,
815 1.1 christos _("Number of breakpoints exceeds throttled maximum."));
816 1.1 christos return NULL;
817 1.8 christos }
818 1.8 christos
819 1.8 christos gdbpy_ref<> return_list (PyList_New (0));
820 1.8 christos
821 1.8 christos if (return_list == NULL)
822 1.8 christos return NULL;
823 1.8 christos
824 1.8 christos /* Construct full path names for symbols and call the Python
825 1.8 christos breakpoint constructor on the resulting names. Be tolerant of
826 1.8 christos individual breakpoint failures. */
827 1.8 christos for (const symbol_search &p : symbols)
828 1.8 christos {
829 1.8 christos std::string symbol_name;
830 1.8 christos
831 1.8 christos /* Skipping minimal symbols? */
832 1.8 christos if (minsyms_p == 0)
833 1.8 christos if (p.msymbol.minsym != NULL)
834 1.8 christos continue;
835 1.8 christos
836 1.10 christos if (p.msymbol.minsym == NULL)
837 1.8 christos {
838 1.8 christos struct symtab *symtab = p.symbol->symtab ();
839 1.8 christos const char *fullname = symtab_to_fullname (symtab);
840 1.8 christos
841 1.9 christos symbol_name = fullname;
842 1.8 christos symbol_name += ":";
843 1.8 christos symbol_name += p.symbol->linkage_name ();
844 1.9 christos }
845 1.8 christos else
846 1.8 christos symbol_name = p.msymbol.minsym->linkage_name ();
847 1.8 christos
848 1.8 christos gdbpy_ref<> argList (Py_BuildValue("(s)", symbol_name.c_str ()));
849 1.8 christos gdbpy_ref<> obj (PyObject_CallObject ((PyObject *)
850 1.8 christos &breakpoint_object_type,
851 1.8 christos argList.get ()));
852 1.8 christos
853 1.8 christos /* Tolerate individual breakpoint failures. */
854 1.8 christos if (obj == NULL)
855 1.8 christos gdbpy_print_stack ();
856 1.8 christos else
857 1.8 christos {
858 1.8 christos if (PyList_Append (return_list.get (), obj.get ()) == -1)
859 1.8 christos return NULL;
860 1.8 christos }
861 1.1 christos }
862 1.1 christos return return_list.release ();
863 1.1 christos }
864 1.1 christos
865 1.1 christos /* A Python function which is a wrapper for decode_line_1. */
866 1.1 christos
867 1.1 christos static PyObject *
868 1.8 christos gdbpy_decode_line (PyObject *self, PyObject *args)
869 1.7 christos {
870 1.7 christos const char *arg = NULL;
871 1.10 christos gdbpy_ref<> result;
872 1.1 christos gdbpy_ref<> unparsed;
873 1.1 christos location_spec_up locspec;
874 1.1 christos
875 1.1 christos if (! PyArg_ParseTuple (args, "|s", &arg))
876 1.9 christos return NULL;
877 1.9 christos
878 1.9 christos /* Treat a string consisting of just whitespace the same as
879 1.9 christos NULL. */
880 1.9 christos if (arg != NULL)
881 1.9 christos {
882 1.9 christos arg = skip_spaces (arg);
883 1.9 christos if (*arg == '\0')
884 1.9 christos arg = NULL;
885 1.6 christos }
886 1.10 christos
887 1.10 christos if (arg != NULL)
888 1.6 christos locspec = string_to_location_spec_basic (&arg, current_language,
889 1.8 christos symbol_name_match_type::WILD);
890 1.8 christos
891 1.8 christos std::vector<symtab_and_line> decoded_sals;
892 1.9 christos symtab_and_line def_sal;
893 1.1 christos gdb::array_view<symtab_and_line> sals;
894 1.10 christos try
895 1.8 christos {
896 1.10 christos if (locspec != NULL)
897 1.8 christos {
898 1.8 christos decoded_sals = decode_line_1 (locspec.get (), 0, NULL, NULL, 0);
899 1.1 christos sals = decoded_sals;
900 1.1 christos }
901 1.1 christos else
902 1.8 christos {
903 1.8 christos set_default_source_symtab_and_line ();
904 1.1 christos def_sal = get_current_source_symtab_and_line ();
905 1.1 christos sals = def_sal;
906 1.9 christos }
907 1.5 christos }
908 1.1 christos catch (const gdb_exception &ex)
909 1.8 christos {
910 1.1 christos /* We know this will always throw. */
911 1.1 christos gdbpy_convert_exception (ex);
912 1.1 christos return NULL;
913 1.8 christos }
914 1.1 christos
915 1.8 christos if (!sals.empty ())
916 1.7 christos {
917 1.8 christos result.reset (PyTuple_New (sals.size ()));
918 1.8 christos if (result == NULL)
919 1.7 christos return NULL;
920 1.8 christos for (size_t i = 0; i < sals.size (); ++i)
921 1.8 christos {
922 1.8 christos PyObject *obj = symtab_and_line_to_sal_object (sals[i]);
923 1.1 christos if (obj == NULL)
924 1.7 christos return NULL;
925 1.1 christos
926 1.1 christos PyTuple_SetItem (result.get (), i, obj);
927 1.1 christos }
928 1.8 christos }
929 1.1 christos else
930 1.7 christos result = gdbpy_ref<>::new_reference (Py_None);
931 1.7 christos
932 1.8 christos gdbpy_ref<> return_result (PyTuple_New (2));
933 1.1 christos if (return_result == NULL)
934 1.6 christos return NULL;
935 1.1 christos
936 1.10 christos if (arg != NULL && strlen (arg) > 0)
937 1.1 christos {
938 1.8 christos unparsed.reset (PyUnicode_FromString (arg));
939 1.1 christos if (unparsed == NULL)
940 1.1 christos return NULL;
941 1.8 christos }
942 1.1 christos else
943 1.7 christos unparsed = gdbpy_ref<>::new_reference (Py_None);
944 1.7 christos
945 1.1 christos PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
946 1.7 christos PyTuple_SetItem (return_result.get (), 1, result.release ());
947 1.1 christos
948 1.1 christos return return_result.release ();
949 1.1 christos }
950 1.1 christos
951 1.1 christos /* Parse a string and evaluate it as an expression. */
952 1.1 christos static PyObject *
953 1.1 christos gdbpy_parse_and_eval (PyObject *self, PyObject *args)
954 1.1 christos {
955 1.1 christos const char *expr_str;
956 1.1 christos struct value *result = NULL;
957 1.1 christos
958 1.1 christos if (!PyArg_ParseTuple (args, "s", &expr_str))
959 1.9 christos return NULL;
960 1.1 christos
961 1.8 christos try
962 1.1 christos {
963 1.1 christos gdbpy_allow_threads allow_threads;
964 1.9 christos result = parse_and_eval (expr_str);
965 1.5 christos }
966 1.5 christos catch (const gdb_exception &except)
967 1.5 christos {
968 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
969 1.1 christos }
970 1.1 christos
971 1.1 christos return value_to_value_object (result);
972 1.6 christos }
973 1.6 christos
974 1.6 christos /* Implementation of gdb.invalidate_cached_frames. */
975 1.6 christos
976 1.6 christos static PyObject *
977 1.6 christos gdbpy_invalidate_cached_frames (PyObject *self, PyObject *args)
978 1.6 christos {
979 1.6 christos reinit_frame_cache ();
980 1.6 christos Py_RETURN_NONE;
981 1.1 christos }
982 1.3 christos
983 1.3 christos /* Read a file as Python code.
984 1.1 christos This is the extension_language_script_ops.script_sourcer "method".
985 1.1 christos FILE is the file to load. FILENAME is name of the file FILE.
986 1.1 christos This does not throw any errors. If an exception occurs python will print
987 1.3 christos the traceback and clear the error indicator. */
988 1.3 christos
989 1.3 christos static void
990 1.1 christos gdbpy_source_script (const struct extension_language_defn *extlang,
991 1.10 christos FILE *file, const char *filename)
992 1.1 christos {
993 1.1 christos gdbpy_enter enter_py;
994 1.1 christos python_run_simple_file (file, filename);
995 1.1 christos }
996 1.1 christos
997 1.1 christos
998 1.1 christos
1000 1.1 christos /* Posting and handling events. */
1001 1.1 christos
1002 1.9 christos /* A single event. */
1003 1.9 christos struct gdbpy_event
1004 1.9 christos {
1005 1.9 christos gdbpy_event (gdbpy_ref<> &&func)
1006 1.9 christos : m_func (func.release ())
1007 1.9 christos {
1008 1.9 christos }
1009 1.9 christos
1010 1.9 christos gdbpy_event (gdbpy_event &&other) noexcept
1011 1.9 christos : m_func (other.m_func)
1012 1.9 christos {
1013 1.9 christos other.m_func = nullptr;
1014 1.9 christos }
1015 1.9 christos
1016 1.9 christos gdbpy_event (const gdbpy_event &other)
1017 1.9 christos : m_func (other.m_func)
1018 1.9 christos {
1019 1.9 christos gdbpy_gil gil;
1020 1.9 christos Py_XINCREF (m_func);
1021 1.9 christos }
1022 1.9 christos
1023 1.9 christos ~gdbpy_event ()
1024 1.9 christos {
1025 1.1 christos gdbpy_gil gil;
1026 1.9 christos Py_XDECREF (m_func);
1027 1.1 christos }
1028 1.9 christos
1029 1.9 christos gdbpy_event &operator= (const gdbpy_event &other) = delete;
1030 1.10 christos
1031 1.1 christos void operator() ()
1032 1.9 christos {
1033 1.9 christos gdbpy_enter enter_py;
1034 1.9 christos
1035 1.9 christos gdbpy_ref<> call_result (PyObject_CallObject (m_func, NULL));
1036 1.1 christos if (call_result == NULL)
1037 1.9 christos gdbpy_print_stack ();
1038 1.1 christos }
1039 1.9 christos
1040 1.9 christos private:
1041 1.9 christos
1042 1.9 christos /* The Python event. This is just a callable object. Note that
1043 1.9 christos this is not a gdbpy_ref<>, because we have to take particular
1044 1.1 christos care to only destroy the reference when holding the GIL. */
1045 1.1 christos PyObject *m_func;
1046 1.1 christos };
1047 1.1 christos
1048 1.1 christos /* Submit an event to the gdb thread. */
1049 1.1 christos static PyObject *
1050 1.1 christos gdbpy_post_event (PyObject *self, PyObject *args)
1051 1.1 christos {
1052 1.1 christos PyObject *func;
1053 1.1 christos
1054 1.1 christos if (!PyArg_ParseTuple (args, "O", &func))
1055 1.1 christos return NULL;
1056 1.1 christos
1057 1.1 christos if (!PyCallable_Check (func))
1058 1.1 christos {
1059 1.1 christos PyErr_SetString (PyExc_RuntimeError,
1060 1.1 christos _("Posted event is not callable"));
1061 1.9 christos return NULL;
1062 1.9 christos }
1063 1.9 christos
1064 1.1 christos gdbpy_ref<> func_ref = gdbpy_ref<>::new_reference (func);
1065 1.1 christos gdbpy_event event (std::move (func_ref));
1066 1.1 christos run_on_main_thread (event);
1067 1.1 christos
1068 1.1 christos Py_RETURN_NONE;
1069 1.1 christos }
1070 1.3 christos
1071 1.3 christos
1072 1.3 christos
1074 1.3 christos /* This is the extension_language_ops.before_prompt "method". */
1075 1.1 christos
1076 1.1 christos static enum ext_lang_rc
1077 1.3 christos gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
1078 1.1 christos const char *current_gdb_prompt)
1079 1.10 christos {
1080 1.7 christos if (!gdb_python_initialized)
1081 1.7 christos return EXT_LANG_RC_NOP;
1082 1.7 christos
1083 1.7 christos gdbpy_enter enter_py;
1084 1.1 christos
1085 1.1 christos if (!evregpy_no_listeners_p (gdb_py_events.before_prompt)
1086 1.1 christos && evpy_emit_event (NULL, gdb_py_events.before_prompt) < 0)
1087 1.1 christos return EXT_LANG_RC_ERROR;
1088 1.7 christos
1089 1.7 christos if (gdb_python_module
1090 1.1 christos && PyObject_HasAttrString (gdb_python_module, "prompt_hook"))
1091 1.7 christos {
1092 1.7 christos gdbpy_ref<> hook (PyObject_GetAttrString (gdb_python_module,
1093 1.7 christos "prompt_hook"));
1094 1.7 christos if (hook == NULL)
1095 1.1 christos {
1096 1.7 christos gdbpy_print_stack ();
1097 1.1 christos return EXT_LANG_RC_ERROR;
1098 1.10 christos }
1099 1.1 christos
1100 1.7 christos if (PyCallable_Check (hook.get ()))
1101 1.7 christos {
1102 1.7 christos gdbpy_ref<> current_prompt (PyUnicode_FromString (current_gdb_prompt));
1103 1.7 christos if (current_prompt == NULL)
1104 1.1 christos {
1105 1.7 christos gdbpy_print_stack ();
1106 1.7 christos return EXT_LANG_RC_ERROR;
1107 1.7 christos }
1108 1.1 christos
1109 1.7 christos gdbpy_ref<> result
1110 1.7 christos (PyObject_CallFunctionObjArgs (hook.get (), current_prompt.get (),
1111 1.7 christos NULL));
1112 1.7 christos if (result == NULL)
1113 1.1 christos {
1114 1.1 christos gdbpy_print_stack ();
1115 1.1 christos return EXT_LANG_RC_ERROR;
1116 1.1 christos }
1117 1.10 christos
1118 1.1 christos /* Return type should be None, or a String. If it is None,
1119 1.1 christos fall through, we will not set a prompt. If it is a
1120 1.1 christos string, set PROMPT. Anything else, set an exception. */
1121 1.1 christos if (result != Py_None && !PyUnicode_Check (result.get ()))
1122 1.7 christos {
1123 1.7 christos PyErr_Format (PyExc_RuntimeError,
1124 1.1 christos _("Return from prompt_hook must " \
1125 1.1 christos "be either a Python string, or None"));
1126 1.1 christos gdbpy_print_stack ();
1127 1.1 christos return EXT_LANG_RC_ERROR;
1128 1.7 christos }
1129 1.7 christos
1130 1.1 christos if (result != Py_None)
1131 1.1 christos {
1132 1.7 christos gdb::unique_xmalloc_ptr<char>
1133 1.7 christos prompt (python_string_to_host_string (result.get ()));
1134 1.7 christos
1135 1.7 christos if (prompt == NULL)
1136 1.7 christos {
1137 1.7 christos gdbpy_print_stack ();
1138 1.7 christos return EXT_LANG_RC_ERROR;
1139 1.1 christos }
1140 1.1 christos
1141 1.1 christos set_prompt (prompt.get ());
1142 1.1 christos return EXT_LANG_RC_OK;
1143 1.7 christos }
1144 1.1 christos }
1145 1.1 christos }
1146 1.9 christos
1147 1.9 christos return EXT_LANG_RC_NOP;
1148 1.9 christos }
1149 1.9 christos
1150 1.9 christos /* This is the extension_language_ops.colorize "method". */
1151 1.9 christos
1152 1.9 christos static gdb::optional<std::string>
1153 1.9 christos gdbpy_colorize (const std::string &filename, const std::string &contents)
1154 1.10 christos {
1155 1.9 christos if (!gdb_python_initialized)
1156 1.10 christos return {};
1157 1.10 christos
1158 1.10 christos gdbpy_enter enter_py;
1159 1.10 christos
1160 1.10 christos gdbpy_ref<> module (PyImport_ImportModule ("gdb.styling"));
1161 1.10 christos if (module == nullptr)
1162 1.10 christos {
1163 1.10 christos gdbpy_print_stack ();
1164 1.9 christos return {};
1165 1.9 christos }
1166 1.10 christos
1167 1.9 christos if (!PyObject_HasAttrString (module.get (), "colorize"))
1168 1.9 christos return {};
1169 1.9 christos
1170 1.9 christos gdbpy_ref<> hook (PyObject_GetAttrString (module.get (), "colorize"));
1171 1.9 christos if (hook == nullptr)
1172 1.9 christos {
1173 1.9 christos gdbpy_print_stack ();
1174 1.9 christos return {};
1175 1.9 christos }
1176 1.10 christos
1177 1.9 christos if (!PyCallable_Check (hook.get ()))
1178 1.9 christos return {};
1179 1.9 christos
1180 1.9 christos gdbpy_ref<> fname_arg (PyUnicode_FromString (filename.c_str ()));
1181 1.9 christos if (fname_arg == nullptr)
1182 1.10 christos {
1183 1.10 christos gdbpy_print_stack ();
1184 1.10 christos return {};
1185 1.10 christos }
1186 1.10 christos
1187 1.10 christos /* The pygments library, which is what we currently use for applying
1188 1.10 christos styling, is happy to take input as a bytes object, and to figure out
1189 1.10 christos the encoding for itself. This removes the need for us to figure out
1190 1.9 christos (guess?) at how the content is encoded, which is probably a good
1191 1.9 christos thing. */
1192 1.9 christos gdbpy_ref<> contents_arg (PyBytes_FromStringAndSize (contents.c_str (),
1193 1.9 christos contents.size ()));
1194 1.9 christos if (contents_arg == nullptr)
1195 1.9 christos {
1196 1.10 christos gdbpy_print_stack ();
1197 1.10 christos return {};
1198 1.10 christos }
1199 1.10 christos
1200 1.9 christos /* Calling gdb.colorize passing in the filename (a string), and the file
1201 1.9 christos contents (a bytes object). This function should return either a bytes
1202 1.9 christos object, the same contents with styling applied, or None to indicate
1203 1.9 christos that no styling should be performed. */
1204 1.9 christos gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (),
1205 1.9 christos fname_arg.get (),
1206 1.9 christos contents_arg.get (),
1207 1.9 christos nullptr));
1208 1.9 christos if (result == nullptr)
1209 1.9 christos {
1210 1.10 christos gdbpy_print_stack ();
1211 1.9 christos return {};
1212 1.10 christos }
1213 1.10 christos
1214 1.10 christos if (result == Py_None)
1215 1.10 christos return {};
1216 1.10 christos else if (!PyBytes_Check (result.get ()))
1217 1.10 christos {
1218 1.10 christos PyErr_SetString (PyExc_TypeError,
1219 1.10 christos _("Return value from gdb.colorize should be a bytes object or None."));
1220 1.10 christos gdbpy_print_stack ();
1221 1.10 christos return {};
1222 1.9 christos }
1223 1.10 christos
1224 1.10 christos return std::string (PyBytes_AsString (result.get ()));
1225 1.10 christos }
1226 1.10 christos
1227 1.10 christos /* This is the extension_language_ops.colorize_disasm "method". */
1228 1.10 christos
1229 1.10 christos static gdb::optional<std::string>
1230 1.10 christos gdbpy_colorize_disasm (const std::string &content, gdbarch *gdbarch)
1231 1.10 christos {
1232 1.10 christos if (!gdb_python_initialized)
1233 1.10 christos return {};
1234 1.10 christos
1235 1.9 christos gdbpy_enter enter_py;
1236 1.9 christos
1237 1.9 christos gdbpy_ref<> module (PyImport_ImportModule ("gdb.styling"));
1238 1.9 christos if (module == nullptr)
1239 1.10 christos {
1240 1.10 christos gdbpy_print_stack ();
1241 1.10 christos return {};
1242 1.10 christos }
1243 1.10 christos
1244 1.10 christos if (!PyObject_HasAttrString (module.get (), "colorize_disasm"))
1245 1.10 christos return {};
1246 1.10 christos
1247 1.10 christos gdbpy_ref<> hook (PyObject_GetAttrString (module.get (),
1248 1.10 christos "colorize_disasm"));
1249 1.10 christos if (hook == nullptr)
1250 1.10 christos {
1251 1.10 christos gdbpy_print_stack ();
1252 1.10 christos return {};
1253 1.10 christos }
1254 1.10 christos
1255 1.10 christos if (!PyCallable_Check (hook.get ()))
1256 1.10 christos return {};
1257 1.10 christos
1258 1.10 christos gdbpy_ref<> content_arg (PyBytes_FromString (content.c_str ()));
1259 1.10 christos if (content_arg == nullptr)
1260 1.10 christos {
1261 1.10 christos gdbpy_print_stack ();
1262 1.10 christos return {};
1263 1.10 christos }
1264 1.10 christos
1265 1.10 christos gdbpy_ref<> gdbarch_arg (gdbarch_to_arch_object (gdbarch));
1266 1.10 christos if (gdbarch_arg == nullptr)
1267 1.10 christos {
1268 1.10 christos gdbpy_print_stack ();
1269 1.10 christos return {};
1270 1.10 christos }
1271 1.10 christos
1272 1.10 christos gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (),
1273 1.10 christos content_arg.get (),
1274 1.10 christos gdbarch_arg.get (),
1275 1.10 christos nullptr));
1276 1.10 christos if (result == nullptr)
1277 1.10 christos {
1278 1.10 christos gdbpy_print_stack ();
1279 1.10 christos return {};
1280 1.10 christos }
1281 1.10 christos
1282 1.9 christos if (result == Py_None)
1283 1.10 christos return {};
1284 1.10 christos
1285 1.9 christos if (!PyBytes_Check (result.get ()))
1286 1.9 christos {
1287 1.9 christos PyErr_SetString (PyExc_TypeError,
1288 1.9 christos _("Return value from gdb.colorize_disasm should be a bytes object or None."));
1289 1.10 christos gdbpy_print_stack ();
1290 1.10 christos return {};
1291 1.10 christos }
1292 1.10 christos
1293 1.10 christos return std::string (PyBytes_AsString (result.get ()));
1294 1.10 christos }
1295 1.10 christos
1296 1.10 christos
1297 1.10 christos
1299 1.10 christos /* Implement gdb.format_address(ADDR,P_SPACE,ARCH). Provide access to
1300 1.10 christos GDB's print_address function from Python. The returned address will
1301 1.10 christos have the format '0x..... <symbol+offset>'. */
1302 1.10 christos
1303 1.10 christos static PyObject *
1304 1.10 christos gdbpy_format_address (PyObject *self, PyObject *args, PyObject *kw)
1305 1.10 christos {
1306 1.10 christos static const char *keywords[] =
1307 1.10 christos {
1308 1.10 christos "address", "progspace", "architecture", nullptr
1309 1.10 christos };
1310 1.10 christos PyObject *addr_obj = nullptr, *pspace_obj = nullptr, *arch_obj = nullptr;
1311 1.10 christos CORE_ADDR addr;
1312 1.10 christos struct gdbarch *gdbarch = nullptr;
1313 1.10 christos struct program_space *pspace = nullptr;
1314 1.10 christos
1315 1.10 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O|OO", keywords,
1316 1.10 christos &addr_obj, &pspace_obj, &arch_obj))
1317 1.10 christos return nullptr;
1318 1.10 christos
1319 1.10 christos if (get_addr_from_python (addr_obj, &addr) < 0)
1320 1.10 christos return nullptr;
1321 1.10 christos
1322 1.10 christos /* If the user passed None for progspace or architecture, then we
1323 1.10 christos consider this to mean "the default". Here we replace references to
1324 1.10 christos None with nullptr, this means that in the following code we only have
1325 1.10 christos to handle the nullptr case. These are only borrowed references, so
1326 1.10 christos no decref is required here. */
1327 1.10 christos if (pspace_obj == Py_None)
1328 1.10 christos pspace_obj = nullptr;
1329 1.10 christos if (arch_obj == Py_None)
1330 1.10 christos arch_obj = nullptr;
1331 1.10 christos
1332 1.10 christos if (pspace_obj == nullptr && arch_obj == nullptr)
1333 1.10 christos {
1334 1.10 christos /* Grab both of these from the current inferior, and its associated
1335 1.10 christos default architecture. */
1336 1.10 christos pspace = current_inferior ()->pspace;
1337 1.10 christos gdbarch = current_inferior ()->gdbarch;
1338 1.10 christos }
1339 1.10 christos else if (arch_obj == nullptr || pspace_obj == nullptr)
1340 1.10 christos {
1341 1.10 christos /* If the user has only given one of program space or architecture,
1342 1.10 christos then don't use the default for the other. Sure we could use the
1343 1.10 christos default, but it feels like there's too much scope of mistakes in
1344 1.10 christos this case, so better to require the user to provide both
1345 1.10 christos arguments. */
1346 1.10 christos PyErr_SetString (PyExc_ValueError,
1347 1.10 christos _("The architecture and progspace arguments must both be supplied"));
1348 1.10 christos return nullptr;
1349 1.10 christos }
1350 1.10 christos else
1351 1.10 christos {
1352 1.10 christos /* The user provided an address, program space, and architecture.
1353 1.10 christos Just check that these objects are valid. */
1354 1.10 christos if (!gdbpy_is_progspace (pspace_obj))
1355 1.10 christos {
1356 1.10 christos PyErr_SetString (PyExc_TypeError,
1357 1.10 christos _("The progspace argument is not a gdb.Progspace object"));
1358 1.10 christos return nullptr;
1359 1.10 christos }
1360 1.10 christos
1361 1.10 christos pspace = progspace_object_to_program_space (pspace_obj);
1362 1.10 christos if (pspace == nullptr)
1363 1.10 christos {
1364 1.10 christos PyErr_SetString (PyExc_ValueError,
1365 1.10 christos _("The progspace argument is not valid"));
1366 1.10 christos return nullptr;
1367 1.10 christos }
1368 1.10 christos
1369 1.10 christos if (!gdbpy_is_architecture (arch_obj))
1370 1.10 christos {
1371 1.10 christos PyErr_SetString (PyExc_TypeError,
1372 1.10 christos _("The architecture argument is not a gdb.Architecture object"));
1373 1.10 christos return nullptr;
1374 1.10 christos }
1375 1.10 christos
1376 1.10 christos /* Architectures are never deleted once created, so gdbarch should
1377 1.10 christos never come back as nullptr. */
1378 1.10 christos gdbarch = arch_object_to_gdbarch (arch_obj);
1379 1.10 christos gdb_assert (gdbarch != nullptr);
1380 1.10 christos }
1381 1.10 christos
1382 1.10 christos /* By this point we should know the program space and architecture we are
1383 1.10 christos going to use. */
1384 1.10 christos gdb_assert (pspace != nullptr);
1385 1.10 christos gdb_assert (gdbarch != nullptr);
1386 1.10 christos
1387 1.10 christos /* Unfortunately print_address relies on the current program space for
1388 1.10 christos its symbol lookup. Temporarily switch now. */
1389 1.10 christos scoped_restore_current_program_space restore_progspace;
1390 1.10 christos set_current_program_space (pspace);
1391 1.9 christos
1392 1.9 christos /* Format the address, and return it as a string. */
1393 1.1 christos string_file buf;
1394 1.1 christos print_address (gdbarch, addr, &buf);
1395 1.1 christos return PyUnicode_FromString (buf.c_str ());
1396 1.1 christos }
1397 1.1 christos
1398 1.1 christos
1399 1.1 christos
1401 1.1 christos /* Printing. */
1402 1.1 christos
1403 1.1 christos /* A python function to write a single string using gdb's filtered
1404 1.1 christos output stream . The optional keyword STREAM can be used to write
1405 1.7 christos to a particular stream. The default stream is to gdb_stdout. */
1406 1.1 christos
1407 1.1 christos static PyObject *
1408 1.7 christos gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
1409 1.7 christos {
1410 1.1 christos const char *arg;
1411 1.1 christos static const char *keywords[] = { "text", "stream", NULL };
1412 1.9 christos int stream_type = 0;
1413 1.1 christos
1414 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
1415 1.10 christos &stream_type))
1416 1.10 christos return NULL;
1417 1.10 christos
1418 1.10 christos try
1419 1.1 christos {
1420 1.10 christos switch (stream_type)
1421 1.10 christos {
1422 1.10 christos case 1:
1423 1.10 christos {
1424 1.1 christos gdb_printf (gdb_stderr, "%s", arg);
1425 1.10 christos break;
1426 1.10 christos }
1427 1.10 christos case 2:
1428 1.10 christos {
1429 1.1 christos gdb_printf (gdb_stdlog, "%s", arg);
1430 1.9 christos break;
1431 1.5 christos }
1432 1.5 christos default:
1433 1.5 christos gdb_printf (gdb_stdout, "%s", arg);
1434 1.1 christos }
1435 1.1 christos }
1436 1.1 christos catch (const gdb_exception &except)
1437 1.1 christos {
1438 1.1 christos GDB_PY_HANDLE_EXCEPTION (except);
1439 1.1 christos }
1440 1.1 christos
1441 1.1 christos Py_RETURN_NONE;
1442 1.1 christos }
1443 1.1 christos
1444 1.1 christos /* A python function to flush a gdb stream. The optional keyword
1445 1.7 christos STREAM can be used to flush a particular stream. The default stream
1446 1.1 christos is gdb_stdout. */
1447 1.1 christos
1448 1.7 christos static PyObject *
1449 1.7 christos gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
1450 1.1 christos {
1451 1.1 christos static const char *keywords[] = { "stream", NULL };
1452 1.1 christos int stream_type = 0;
1453 1.1 christos
1454 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
1455 1.1 christos &stream_type))
1456 1.1 christos return NULL;
1457 1.1 christos
1458 1.1 christos switch (stream_type)
1459 1.1 christos {
1460 1.1 christos case 1:
1461 1.1 christos {
1462 1.1 christos gdb_flush (gdb_stderr);
1463 1.1 christos break;
1464 1.1 christos }
1465 1.1 christos case 2:
1466 1.1 christos {
1467 1.1 christos gdb_flush (gdb_stdlog);
1468 1.1 christos break;
1469 1.1 christos }
1470 1.1 christos default:
1471 1.5 christos gdb_flush (gdb_stdout);
1472 1.5 christos }
1473 1.5 christos
1474 1.5 christos Py_RETURN_NONE;
1475 1.5 christos }
1476 1.5 christos
1477 1.5 christos /* Return non-zero if print-stack is not "none". */
1478 1.5 christos
1479 1.1 christos int
1480 1.1 christos gdbpy_print_python_errors_p (void)
1481 1.1 christos {
1482 1.1 christos return gdbpy_should_print_stack != python_excp_none;
1483 1.1 christos }
1484 1.1 christos
1485 1.1 christos /* Print a python exception trace, print just a message, or print
1486 1.1 christos nothing and clear the python exception, depending on
1487 1.1 christos gdbpy_should_print_stack. Only call this if a python exception is
1488 1.1 christos set. */
1489 1.1 christos void
1490 1.1 christos gdbpy_print_stack (void)
1491 1.1 christos {
1492 1.1 christos
1493 1.1 christos /* Print "none", just clear exception. */
1494 1.1 christos if (gdbpy_should_print_stack == python_excp_none)
1495 1.1 christos {
1496 1.1 christos PyErr_Clear ();
1497 1.1 christos }
1498 1.10 christos /* Print "full" message and backtrace. */
1499 1.9 christos else if (gdbpy_should_print_stack == python_excp_full)
1500 1.1 christos {
1501 1.1 christos PyErr_Print ();
1502 1.1 christos /* PyErr_Print doesn't necessarily end output with a newline.
1503 1.9 christos This works because Python's stdout/stderr is fed through
1504 1.5 christos gdb_printf. */
1505 1.5 christos try
1506 1.1 christos {
1507 1.1 christos begin_line ();
1508 1.1 christos }
1509 1.1 christos catch (const gdb_exception &except)
1510 1.8 christos {
1511 1.1 christos }
1512 1.8 christos }
1513 1.8 christos /* Print "message", just error print message. */
1514 1.8 christos else
1515 1.8 christos {
1516 1.8 christos gdbpy_err_fetch fetched_error;
1517 1.8 christos
1518 1.1 christos gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string ();
1519 1.9 christos gdb::unique_xmalloc_ptr<char> type;
1520 1.1 christos /* Don't compute TYPE if MSG already indicates that there is an
1521 1.8 christos error. */
1522 1.1 christos if (msg != NULL)
1523 1.1 christos type = fetched_error.type_to_string ();
1524 1.1 christos
1525 1.10 christos try
1526 1.10 christos {
1527 1.10 christos if (msg == NULL || type == NULL)
1528 1.8 christos {
1529 1.1 christos /* An error occurred computing the string representation of the
1530 1.1 christos error message. */
1531 1.10 christos gdb_printf (gdb_stderr,
1532 1.10 christos _("Error occurred computing Python error" \
1533 1.1 christos "message.\n"));
1534 1.9 christos PyErr_Clear ();
1535 1.5 christos }
1536 1.5 christos else
1537 1.1 christos gdb_printf (gdb_stderr, "Python Exception %s: %s\n",
1538 1.1 christos type.get (), msg.get ());
1539 1.1 christos }
1540 1.8 christos catch (const gdb_exception &except)
1541 1.8 christos {
1542 1.1 christos }
1543 1.8 christos }
1544 1.8 christos }
1545 1.1 christos
1546 1.8 christos /* Like gdbpy_print_stack, but if the exception is a
1547 1.8 christos KeyboardException, throw a gdb "quit" instead. */
1548 1.8 christos
1549 1.8 christos void
1550 1.8 christos gdbpy_print_stack_or_quit ()
1551 1.8 christos {
1552 1.8 christos if (PyErr_ExceptionMatches (PyExc_KeyboardInterrupt))
1553 1.1 christos {
1554 1.8 christos PyErr_Clear ();
1555 1.1 christos throw_quit ("Quit");
1556 1.1 christos }
1557 1.1 christos gdbpy_print_stack ();
1558 1.1 christos }
1559 1.1 christos
1560 1.1 christos
1561 1.7 christos
1563 1.1 christos /* Return a sequence holding all the Progspaces. */
1564 1.1 christos
1565 1.9 christos static PyObject *
1566 1.9 christos gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
1567 1.9 christos {
1568 1.1 christos gdbpy_ref<> list (PyList_New (0));
1569 1.9 christos if (list == NULL)
1570 1.9 christos return NULL;
1571 1.9 christos
1572 1.1 christos for (struct program_space *ps : program_spaces)
1573 1.7 christos {
1574 1.1 christos gdbpy_ref<> item = pspace_to_pspace_object (ps);
1575 1.1 christos
1576 1.10 christos if (item == NULL || PyList_Append (list.get (), item.get ()) == -1)
1577 1.10 christos return NULL;
1578 1.10 christos }
1579 1.10 christos
1580 1.10 christos return list.release ();
1581 1.10 christos }
1582 1.10 christos
1583 1.10 christos /* Return the name of the current language. */
1584 1.1 christos
1585 1.1 christos static PyObject *
1586 1.10 christos gdbpy_current_language (PyObject *unused1, PyObject *unused2)
1587 1.10 christos {
1588 1.1 christos return host_string_to_python_string (current_language->name ()).release ();
1589 1.1 christos }
1590 1.1 christos
1591 1.3 christos
1592 1.3 christos
1594 1.1 christos /* See python.h. */
1595 1.3 christos struct objfile *gdbpy_current_objfile;
1596 1.3 christos
1597 1.3 christos /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1598 1.3 christos as Python code. This does not throw any errors. If an exception
1599 1.1 christos occurs python will print the traceback and clear the error indicator.
1600 1.1 christos This is the extension_language_script_ops.objfile_script_sourcer
1601 1.1 christos "method". */
1602 1.1 christos
1603 1.10 christos static void
1604 1.10 christos gdbpy_source_objfile_script (const struct extension_language_defn *extlang,
1605 1.10 christos struct objfile *objfile, FILE *file,
1606 1.1 christos const char *filename)
1607 1.1 christos {
1608 1.1 christos if (!gdb_python_initialized)
1609 1.1 christos return;
1610 1.5 christos
1611 1.5 christos gdbpy_enter enter_py (objfile->arch ());
1612 1.5 christos scoped_restore restire_current_objfile
1613 1.5 christos = make_scoped_restore (&gdbpy_current_objfile, objfile);
1614 1.5 christos
1615 1.5 christos python_run_simple_file (file, filename);
1616 1.5 christos }
1617 1.5 christos
1618 1.5 christos /* Set the current objfile to OBJFILE and then execute SCRIPT
1619 1.5 christos as Python code. This does not throw any errors. If an exception
1620 1.5 christos occurs python will print the traceback and clear the error indicator.
1621 1.5 christos This is the extension_language_script_ops.objfile_script_executor
1622 1.5 christos "method". */
1623 1.5 christos
1624 1.10 christos static void
1625 1.10 christos gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
1626 1.10 christos struct objfile *objfile, const char *name,
1627 1.5 christos const char *script)
1628 1.5 christos {
1629 1.5 christos if (!gdb_python_initialized)
1630 1.5 christos return;
1631 1.1 christos
1632 1.1 christos gdbpy_enter enter_py (objfile->arch ());
1633 1.1 christos scoped_restore restire_current_objfile
1634 1.1 christos = make_scoped_restore (&gdbpy_current_objfile, objfile);
1635 1.1 christos
1636 1.1 christos PyRun_SimpleString (script);
1637 1.1 christos }
1638 1.1 christos
1639 1.8 christos /* Return the current Objfile, or None if there isn't one. */
1640 1.1 christos
1641 1.1 christos static PyObject *
1642 1.3 christos gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
1643 1.3 christos {
1644 1.3 christos if (! gdbpy_current_objfile)
1645 1.3 christos Py_RETURN_NONE;
1646 1.1 christos
1647 1.3 christos return objfile_to_objfile_object (gdbpy_current_objfile).release ();
1648 1.3 christos }
1649 1.3 christos
1650 1.1 christos /* Compute the list of active python type printers and store them in
1651 1.7 christos EXT_PRINTERS->py_type_printers. The product of this function is used by
1652 1.1 christos gdbpy_apply_type_printers, and freed by gdbpy_free_type_printers.
1653 1.1 christos This is the extension_language_ops.start_type_printers "method". */
1654 1.3 christos
1655 1.1 christos static void
1656 1.10 christos gdbpy_start_type_printers (const struct extension_language_defn *extlang,
1657 1.1 christos struct ext_lang_type_printers *ext_printers)
1658 1.7 christos {
1659 1.1 christos PyObject *printers_obj = NULL;
1660 1.1 christos
1661 1.1 christos if (!gdb_python_initialized)
1662 1.7 christos return;
1663 1.1 christos
1664 1.1 christos gdbpy_enter enter_py;
1665 1.7 christos
1666 1.7 christos gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
1667 1.1 christos if (type_module == NULL)
1668 1.1 christos {
1669 1.1 christos gdbpy_print_stack ();
1670 1.7 christos return;
1671 1.1 christos }
1672 1.1 christos
1673 1.7 christos gdbpy_ref<> func (PyObject_GetAttrString (type_module.get (),
1674 1.3 christos "get_type_recognizers"));
1675 1.1 christos if (func == NULL)
1676 1.3 christos {
1677 1.3 christos gdbpy_print_stack ();
1678 1.1 christos return;
1679 1.1 christos }
1680 1.3 christos
1681 1.3 christos printers_obj = PyObject_CallFunctionObjArgs (func.get (), (char *) NULL);
1682 1.3 christos if (printers_obj == NULL)
1683 1.3 christos gdbpy_print_stack ();
1684 1.3 christos else
1685 1.3 christos ext_printers->py_type_printers = printers_obj;
1686 1.3 christos }
1687 1.3 christos
1688 1.3 christos /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE
1689 1.3 christos a newly allocated string holding the type's replacement name, and return
1690 1.3 christos EXT_LANG_RC_OK. The caller is responsible for freeing the string.
1691 1.1 christos If there's a Python error return EXT_LANG_RC_ERROR.
1692 1.6 christos Otherwise, return EXT_LANG_RC_NOP.
1693 1.7 christos This is the extension_language_ops.apply_type_printers "method". */
1694 1.1 christos
1695 1.1 christos static enum ext_lang_rc
1696 1.3 christos gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
1697 1.1 christos const struct ext_lang_type_printers *ext_printers,
1698 1.1 christos struct type *type, char **prettied_type)
1699 1.3 christos {
1700 1.1 christos PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
1701 1.10 christos gdb::unique_xmalloc_ptr<char> result;
1702 1.1 christos
1703 1.7 christos if (printers_obj == NULL)
1704 1.1 christos return EXT_LANG_RC_NOP;
1705 1.1 christos
1706 1.1 christos if (!gdb_python_initialized)
1707 1.7 christos return EXT_LANG_RC_NOP;
1708 1.1 christos
1709 1.1 christos gdbpy_enter enter_py;
1710 1.7 christos
1711 1.1 christos gdbpy_ref<> type_obj (type_to_type_object (type));
1712 1.1 christos if (type_obj == NULL)
1713 1.1 christos {
1714 1.7 christos gdbpy_print_stack ();
1715 1.1 christos return EXT_LANG_RC_ERROR;
1716 1.1 christos }
1717 1.7 christos
1718 1.7 christos gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
1719 1.1 christos if (type_module == NULL)
1720 1.1 christos {
1721 1.1 christos gdbpy_print_stack ();
1722 1.7 christos return EXT_LANG_RC_ERROR;
1723 1.1 christos }
1724 1.1 christos
1725 1.7 christos gdbpy_ref<> func (PyObject_GetAttrString (type_module.get (),
1726 1.7 christos "apply_type_recognizers"));
1727 1.7 christos if (func == NULL)
1728 1.7 christos {
1729 1.1 christos gdbpy_print_stack ();
1730 1.1 christos return EXT_LANG_RC_ERROR;
1731 1.1 christos }
1732 1.7 christos
1733 1.1 christos gdbpy_ref<> result_obj (PyObject_CallFunctionObjArgs (func.get (),
1734 1.1 christos printers_obj,
1735 1.7 christos type_obj.get (),
1736 1.7 christos (char *) NULL));
1737 1.7 christos if (result_obj == NULL)
1738 1.7 christos {
1739 1.7 christos gdbpy_print_stack ();
1740 1.1 christos return EXT_LANG_RC_ERROR;
1741 1.7 christos }
1742 1.7 christos
1743 1.1 christos if (result_obj == Py_None)
1744 1.1 christos return EXT_LANG_RC_NOP;
1745 1.7 christos
1746 1.7 christos result = python_string_to_host_string (result_obj.get ());
1747 1.1 christos if (result == NULL)
1748 1.1 christos {
1749 1.3 christos gdbpy_print_stack ();
1750 1.3 christos return EXT_LANG_RC_ERROR;
1751 1.1 christos }
1752 1.3 christos
1753 1.3 christos *prettied_type = result.release ();
1754 1.3 christos return EXT_LANG_RC_OK;
1755 1.1 christos }
1756 1.6 christos
1757 1.1 christos /* Free the result of start_type_printers.
1758 1.1 christos This is the extension_language_ops.free_type_printers "method". */
1759 1.1 christos
1760 1.1 christos static void
1761 1.1 christos gdbpy_free_type_printers (const struct extension_language_defn *extlang,
1762 1.1 christos struct ext_lang_type_printers *ext_printers)
1763 1.1 christos {
1764 1.10 christos PyObject *printers = (PyObject *) ext_printers->py_type_printers;
1765 1.1 christos
1766 1.1 christos if (printers == NULL)
1767 1.1 christos return;
1768 1.1 christos
1769 1.1 christos if (!gdb_python_initialized)
1770 1.1 christos return;
1771 1.1 christos
1772 1.1 christos gdbpy_enter enter_py;
1773 1.1 christos Py_DECREF (printers);
1774 1.8 christos }
1775 1.1 christos
1776 1.1 christos #else /* HAVE_PYTHON */
1777 1.1 christos
1778 1.1 christos /* Dummy implementation of the gdb "python-interactive" and "python"
1779 1.1 christos command. */
1780 1.1 christos
1781 1.8 christos static void
1782 1.1 christos python_interactive_command (const char *arg, int from_tty)
1783 1.7 christos {
1784 1.1 christos arg = skip_spaces (arg);
1785 1.1 christos if (arg && *arg)
1786 1.1 christos error (_("Python scripting is not supported in this copy of GDB."));
1787 1.1 christos else
1788 1.8 christos {
1789 1.1 christos counted_command_line l = get_command_line (python_control, "");
1790 1.1 christos
1791 1.1 christos execute_control_command_untraced (l.get ());
1792 1.1 christos }
1793 1.1 christos }
1794 1.1 christos
1795 1.10 christos static void
1796 1.10 christos python_command (const char *arg, int from_tty)
1797 1.10 christos {
1798 1.10 christos python_interactive_command (arg, from_tty);
1799 1.10 christos }
1800 1.10 christos
1801 1.10 christos #endif /* HAVE_PYTHON */
1802 1.10 christos
1803 1.10 christos /* When this is turned on before Python is initialised then Python will
1804 1.10 christos ignore any environment variables related to Python. This is equivalent
1805 1.10 christos to passing `-E' to the python program. */
1806 1.10 christos static bool python_ignore_environment = false;
1807 1.10 christos
1808 1.10 christos /* Implement 'show python ignore-environment'. */
1809 1.10 christos
1810 1.10 christos static void
1811 1.10 christos show_python_ignore_environment (struct ui_file *file, int from_tty,
1812 1.10 christos struct cmd_list_element *c, const char *value)
1813 1.10 christos {
1814 1.10 christos gdb_printf (file, _("Python's ignore-environment setting is %s.\n"),
1815 1.10 christos value);
1816 1.10 christos }
1817 1.10 christos
1818 1.10 christos /* Implement 'set python ignore-environment'. This sets Python's internal
1819 1.10 christos flag no matter when the command is issued, however, if this is used
1820 1.10 christos after Py_Initialize has been called then most of the environment will
1821 1.10 christos already have been read. */
1822 1.10 christos
1823 1.10 christos static void
1824 1.10 christos set_python_ignore_environment (const char *args, int from_tty,
1825 1.10 christos struct cmd_list_element *c)
1826 1.10 christos {
1827 1.10 christos #ifdef HAVE_PYTHON
1828 1.10 christos /* Py_IgnoreEnvironmentFlag is deprecated in Python 3.12. Disable
1829 1.10 christos its usage in Python 3.10 and above since the PyConfig mechanism
1830 1.10 christos is now (also) used in 3.10 and higher. See do_start_initialization()
1831 1.10 christos in this file. */
1832 1.10 christos #if PY_VERSION_HEX < 0x030a0000
1833 1.10 christos Py_IgnoreEnvironmentFlag = python_ignore_environment ? 1 : 0;
1834 1.10 christos #endif
1835 1.10 christos #endif
1836 1.10 christos }
1837 1.10 christos
1838 1.10 christos /* When this is turned on before Python is initialised then Python will
1839 1.10 christos not write `.pyc' files on import of a module. */
1840 1.10 christos static enum auto_boolean python_dont_write_bytecode = AUTO_BOOLEAN_AUTO;
1841 1.10 christos
1842 1.10 christos /* Implement 'show python dont-write-bytecode'. */
1843 1.10 christos
1844 1.10 christos static void
1845 1.10 christos show_python_dont_write_bytecode (struct ui_file *file, int from_tty,
1846 1.10 christos struct cmd_list_element *c, const char *value)
1847 1.10 christos {
1848 1.10 christos if (python_dont_write_bytecode == AUTO_BOOLEAN_AUTO)
1849 1.10 christos {
1850 1.10 christos const char *auto_string
1851 1.10 christos = (python_ignore_environment
1852 1.10 christos || getenv ("PYTHONDONTWRITEBYTECODE") == nullptr) ? "off" : "on";
1853 1.10 christos
1854 1.10 christos gdb_printf (file,
1855 1.10 christos _("Python's dont-write-bytecode setting is %s (currently %s).\n"),
1856 1.10 christos value, auto_string);
1857 1.10 christos }
1858 1.10 christos else
1859 1.10 christos gdb_printf (file, _("Python's dont-write-bytecode setting is %s.\n"),
1860 1.10 christos value);
1861 1.10 christos }
1862 1.10 christos
1863 1.10 christos #ifdef HAVE_PYTHON
1864 1.10 christos /* Return value to assign to PyConfig.write_bytecode or, when
1865 1.10 christos negated (via !), Py_DontWriteBytecodeFlag. Py_DontWriteBytecodeFlag
1866 1.10 christos is deprecated in Python 3.12. */
1867 1.10 christos
1868 1.10 christos static int
1869 1.10 christos python_write_bytecode ()
1870 1.10 christos {
1871 1.10 christos int wbc = 0;
1872 1.10 christos
1873 1.10 christos if (python_dont_write_bytecode == AUTO_BOOLEAN_AUTO)
1874 1.10 christos {
1875 1.10 christos if (python_ignore_environment)
1876 1.10 christos wbc = 1;
1877 1.10 christos else
1878 1.10 christos {
1879 1.10 christos const char *pdwbc = getenv ("PYTHONDONTWRITEBYTECODE");
1880 1.10 christos wbc = (pdwbc == nullptr || pdwbc[0] == '\0') ? 1 : 0;
1881 1.10 christos }
1882 1.10 christos }
1883 1.10 christos else
1884 1.10 christos wbc = python_dont_write_bytecode == AUTO_BOOLEAN_TRUE ? 0 : 1;
1885 1.10 christos
1886 1.10 christos return wbc;
1887 1.10 christos }
1888 1.10 christos #endif /* HAVE_PYTHON */
1889 1.10 christos
1890 1.10 christos /* Implement 'set python dont-write-bytecode'. This sets Python's internal
1891 1.10 christos flag no matter when the command is issued, however, if this is used
1892 1.10 christos after Py_Initialize has been called then many modules could already
1893 1.10 christos have been imported and their byte code written out. */
1894 1.10 christos
1895 1.10 christos static void
1896 1.10 christos set_python_dont_write_bytecode (const char *args, int from_tty,
1897 1.10 christos struct cmd_list_element *c)
1898 1.10 christos {
1899 1.10 christos #ifdef HAVE_PYTHON
1900 1.10 christos /* Py_DontWriteBytecodeFlag is deprecated in Python 3.12. Disable
1901 1.10 christos its usage in Python 3.10 and above since the PyConfig mechanism
1902 1.1 christos is now (also) used in 3.10 and higher. See do_start_initialization()
1903 1.1 christos in this file. */
1904 1.1 christos #if PY_VERSION_HEX < 0x030a0000
1905 1.1 christos Py_DontWriteBytecodeFlag = !python_write_bytecode ();
1906 1.1 christos #endif
1907 1.1 christos #endif /* HAVE_PYTHON */
1908 1.1 christos }
1909 1.1 christos
1910 1.1 christos
1911 1.1 christos
1913 1.1 christos /* Lists for 'set python' commands. */
1914 1.1 christos
1915 1.1 christos static struct cmd_list_element *user_set_python_list;
1916 1.1 christos static struct cmd_list_element *user_show_python_list;
1917 1.1 christos
1918 1.1 christos /* Initialize the Python code. */
1919 1.3 christos
1920 1.3 christos #ifdef HAVE_PYTHON
1921 1.1 christos
1922 1.1 christos /* This is installed as a final cleanup and cleans up the
1923 1.1 christos interpreter. This lets Python's 'atexit' work. */
1924 1.1 christos
1925 1.1 christos static void
1926 1.3 christos finalize_python (void *ignore)
1927 1.3 christos {
1928 1.3 christos struct active_ext_lang_state *previous_active;
1929 1.3 christos
1930 1.3 christos /* We don't use ensure_python_env here because if we ever ran the
1931 1.1 christos cleanup, gdb would crash -- because the cleanup calls into the
1932 1.10 christos Python interpreter, which we are about to destroy. It seems
1933 1.10 christos clearer to make the needed calls explicitly here than to create a
1934 1.10 christos cleanup and then mysteriously discard it. */
1935 1.1 christos
1936 1.1 christos /* This is only called as a final cleanup so we can assume the active
1937 1.3 christos SIGINT handler is gdb's. We still need to tell it to notify Python. */
1938 1.9 christos previous_active = set_active_ext_lang (&extension_language_python);
1939 1.3 christos
1940 1.1 christos (void) PyGILState_Ensure ();
1941 1.8 christos gdbpy_enter::finalize ();
1942 1.10 christos
1943 1.10 christos gdbpy_finalize_micommands ();
1944 1.10 christos
1945 1.10 christos Py_Finalize ();
1946 1.10 christos
1947 1.10 christos gdb_python_initialized = false;
1948 1.10 christos restore_active_ext_lang (previous_active);
1949 1.10 christos }
1950 1.10 christos
1951 1.10 christos static struct PyModuleDef python_GdbModuleDef =
1952 1.10 christos {
1953 1.10 christos PyModuleDef_HEAD_INIT,
1954 1.10 christos "_gdb",
1955 1.8 christos NULL,
1956 1.8 christos -1,
1957 1.8 christos python_GdbMethods,
1958 1.9 christos NULL,
1959 1.8 christos NULL,
1960 1.8 christos NULL,
1961 1.8 christos NULL
1962 1.8 christos };
1963 1.8 christos
1964 1.10 christos /* This is called via the PyImport_AppendInittab mechanism called
1965 1.10 christos during initialization, to make the built-in _gdb module known to
1966 1.10 christos Python. */
1967 1.10 christos PyMODINIT_FUNC init__gdb_module (void);
1968 1.10 christos PyMODINIT_FUNC
1969 1.10 christos init__gdb_module (void)
1970 1.10 christos {
1971 1.10 christos return PyModule_Create (&python_GdbModuleDef);
1972 1.10 christos }
1973 1.10 christos
1974 1.10 christos /* Emit a gdb.GdbExitingEvent, return a negative value if there are any
1975 1.10 christos errors, otherwise, return 0. */
1976 1.10 christos
1977 1.10 christos static int
1978 1.10 christos emit_exiting_event (int exit_code)
1979 1.10 christos {
1980 1.10 christos if (evregpy_no_listeners_p (gdb_py_events.gdb_exiting))
1981 1.10 christos return 0;
1982 1.10 christos
1983 1.10 christos gdbpy_ref<> event_obj = create_event_object (&gdb_exiting_event_object_type);
1984 1.10 christos if (event_obj == nullptr)
1985 1.10 christos return -1;
1986 1.10 christos
1987 1.10 christos gdbpy_ref<> code = gdb_py_object_from_longest (exit_code);
1988 1.10 christos if (evpy_add_attribute (event_obj.get (), "exit_code", code.get ()) < 0)
1989 1.10 christos return -1;
1990 1.10 christos
1991 1.10 christos return evpy_emit_event (event_obj.get (), gdb_py_events.gdb_exiting);
1992 1.10 christos }
1993 1.10 christos
1994 1.10 christos /* Callback for the gdb_exiting observable. EXIT_CODE is the value GDB
1995 1.10 christos will exit with. */
1996 1.10 christos
1997 1.10 christos static void
1998 1.10 christos gdbpy_gdb_exiting (int exit_code)
1999 1.1 christos {
2000 1.7 christos if (!gdb_python_initialized)
2001 1.7 christos return;
2002 1.1 christos
2003 1.10 christos gdbpy_enter enter_py;
2004 1.10 christos
2005 1.10 christos if (emit_exiting_event (exit_code) < 0)
2006 1.10 christos gdbpy_print_stack ();
2007 1.10 christos }
2008 1.10 christos
2009 1.10 christos static bool
2010 1.10 christos do_start_initialization ()
2011 1.10 christos {
2012 1.10 christos /* Define all internal modules. These are all imported (and thus
2013 1.10 christos created) during initialization. */
2014 1.1 christos struct _inittab mods[] =
2015 1.1 christos {
2016 1.1 christos { "_gdb", init__gdb_module },
2017 1.1 christos { "_gdbevents", gdbpy_events_mod_func },
2018 1.1 christos { nullptr, nullptr }
2019 1.1 christos };
2020 1.1 christos
2021 1.1 christos if (PyImport_ExtendInittab (mods) < 0)
2022 1.8 christos return false;
2023 1.9 christos
2024 1.8 christos #ifdef WITH_PYTHON_PATH
2025 1.10 christos /* Work around problem where python gets confused about where it is,
2026 1.10 christos and then can't find its libraries, etc.
2027 1.10 christos NOTE: Python assumes the following layout:
2028 1.10 christos /foo/bin/python
2029 1.10 christos /foo/lib/pythonX.Y/...
2030 1.10 christos This must be done before calling Py_Initialize. */
2031 1.10 christos gdb::unique_xmalloc_ptr<char> progname
2032 1.10 christos (concat (ldirname (python_libdir.c_str ()).c_str (), SLASH_STRING, "bin",
2033 1.10 christos SLASH_STRING, "python", (char *) NULL));
2034 1.8 christos /* Python documentation indicates that the memory given
2035 1.1 christos to Py_SetProgramName cannot be freed. However, it seems that
2036 1.10 christos at least Python 3.7.4 Py_SetProgramName takes a copy of the
2037 1.9 christos given program_name. Making progname_copy static and not release
2038 1.10 christos the memory avoids a leak report for Python versions that duplicate
2039 1.1 christos program_name, and respect the requirement of Py_SetProgramName
2040 1.1 christos for Python versions that do not duplicate program_name. */
2041 1.1 christos static wchar_t *progname_copy;
2042 1.7 christos
2043 1.1 christos std::string oldloc = setlocale (LC_ALL, NULL);
2044 1.8 christos setlocale (LC_ALL, "");
2045 1.1 christos size_t progsize = strlen (progname.get ());
2046 1.10 christos progname_copy = XNEWVEC (wchar_t, progsize + 1);
2047 1.10 christos size_t count = mbstowcs (progname_copy, progname.get (), progsize + 1);
2048 1.10 christos if (count == (size_t) -1)
2049 1.1 christos {
2050 1.1 christos fprintf (stderr, "Could not convert python path to string\n");
2051 1.1 christos return false;
2052 1.1 christos }
2053 1.10 christos setlocale (LC_ALL, oldloc.c_str ());
2054 1.10 christos
2055 1.10 christos /* Py_SetProgramName was deprecated in Python 3.11. Use PyConfig
2056 1.8 christos mechanisms for Python 3.10 and newer. */
2057 1.10 christos #if PY_VERSION_HEX < 0x030a0000
2058 1.10 christos /* Note that Py_SetProgramName expects the string it is passed to
2059 1.10 christos remain alive for the duration of the program's execution, so
2060 1.10 christos it is not freed after this call. */
2061 1.10 christos Py_SetProgramName (progname_copy);
2062 1.10 christos Py_Initialize ();
2063 1.10 christos #else
2064 1.10 christos PyConfig config;
2065 1.10 christos
2066 1.10 christos PyConfig_InitPythonConfig (&config);
2067 1.10 christos PyStatus status = PyConfig_SetString (&config, &config.program_name,
2068 1.10 christos progname_copy);
2069 1.10 christos if (PyStatus_Exception (status))
2070 1.10 christos goto init_done;
2071 1.10 christos
2072 1.10 christos config.write_bytecode = python_write_bytecode ();
2073 1.10 christos config.use_environment = !python_ignore_environment;
2074 1.10 christos
2075 1.10 christos status = PyConfig_Read (&config);
2076 1.10 christos if (PyStatus_Exception (status))
2077 1.1 christos goto init_done;
2078 1.10 christos
2079 1.1 christos status = Py_InitializeFromConfig (&config);
2080 1.1 christos
2081 1.9 christos init_done:
2082 1.9 christos PyConfig_Clear (&config);
2083 1.9 christos if (PyStatus_Exception (status))
2084 1.9 christos return false;
2085 1.1 christos #endif
2086 1.9 christos #else
2087 1.1 christos Py_Initialize ();
2088 1.8 christos #endif
2089 1.1 christos
2090 1.7 christos #if PY_VERSION_HEX < 0x03090000
2091 1.1 christos /* PyEval_InitThreads became deprecated in Python 3.9 and will
2092 1.9 christos be removed in Python 3.11. Prior to Python 3.7, this call was
2093 1.9 christos required to initialize the GIL. */
2094 1.1 christos PyEval_InitThreads ();
2095 1.9 christos #endif
2096 1.7 christos
2097 1.1 christos gdb_module = PyImport_ImportModule ("_gdb");
2098 1.1 christos if (gdb_module == NULL)
2099 1.1 christos return false;
2100 1.1 christos
2101 1.1 christos if (PyModule_AddStringConstant (gdb_module, "VERSION", version) < 0
2102 1.7 christos || PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", host_name) < 0
2103 1.1 christos || PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
2104 1.1 christos target_name) < 0)
2105 1.1 christos return false;
2106 1.1 christos
2107 1.7 christos /* Add stream constants. */
2108 1.1 christos if (PyModule_AddIntConstant (gdb_module, "STDOUT", 0) < 0
2109 1.1 christos || PyModule_AddIntConstant (gdb_module, "STDERR", 1) < 0
2110 1.1 christos || PyModule_AddIntConstant (gdb_module, "STDLOG", 2) < 0)
2111 1.1 christos return false;
2112 1.1 christos
2113 1.1 christos gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL);
2114 1.7 christos if (gdbpy_gdb_error == NULL
2115 1.1 christos || gdb_pymodule_addobject (gdb_module, "error", gdbpy_gdb_error) < 0)
2116 1.1 christos return false;
2117 1.1 christos
2118 1.1 christos gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError",
2119 1.1 christos gdbpy_gdb_error, NULL);
2120 1.7 christos if (gdbpy_gdb_memory_error == NULL
2121 1.1 christos || gdb_pymodule_addobject (gdb_module, "MemoryError",
2122 1.1 christos gdbpy_gdb_memory_error) < 0)
2123 1.1 christos return false;
2124 1.1 christos
2125 1.1 christos gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
2126 1.10 christos if (gdbpy_gdberror_exc == NULL
2127 1.1 christos || gdb_pymodule_addobject (gdb_module, "GdbError",
2128 1.1 christos gdbpy_gdberror_exc) < 0)
2129 1.7 christos return false;
2130 1.7 christos
2131 1.7 christos gdbpy_initialize_gdb_readline ();
2132 1.1 christos
2133 1.1 christos if (gdbpy_initialize_auto_load () < 0
2134 1.1 christos || gdbpy_initialize_values () < 0
2135 1.1 christos || gdbpy_initialize_disasm () < 0
2136 1.1 christos || gdbpy_initialize_frames () < 0
2137 1.1 christos || gdbpy_initialize_commands () < 0
2138 1.1 christos || gdbpy_initialize_instruction () < 0
2139 1.1 christos || gdbpy_initialize_record () < 0
2140 1.1 christos || gdbpy_initialize_btrace () < 0
2141 1.10 christos || gdbpy_initialize_symbols () < 0
2142 1.1 christos || gdbpy_initialize_symtabs () < 0
2143 1.1 christos || gdbpy_initialize_blocks () < 0
2144 1.1 christos || gdbpy_initialize_functions () < 0
2145 1.1 christos || gdbpy_initialize_parameters () < 0
2146 1.1 christos || gdbpy_initialize_types () < 0
2147 1.1 christos || gdbpy_initialize_pspace () < 0
2148 1.1 christos || gdbpy_initialize_objfile () < 0
2149 1.3 christos || gdbpy_initialize_breakpoints () < 0
2150 1.9 christos || gdbpy_initialize_breakpoint_locations () < 0
2151 1.5 christos || gdbpy_initialize_finishbreakpoints () < 0
2152 1.9 christos || gdbpy_initialize_lazy_string () < 0
2153 1.10 christos || gdbpy_initialize_linetable () < 0
2154 1.10 christos || gdbpy_initialize_thread () < 0
2155 1.10 christos || gdbpy_initialize_inferior () < 0
2156 1.10 christos || gdbpy_initialize_eventregistry () < 0
2157 1.7 christos || gdbpy_initialize_event () < 0
2158 1.1 christos || gdbpy_initialize_arch () < 0
2159 1.8 christos || gdbpy_initialize_registers () < 0
2160 1.8 christos || gdbpy_initialize_xmethods () < 0
2161 1.8 christos || gdbpy_initialize_unwind () < 0
2162 1.8 christos || gdbpy_initialize_membuf () < 0
2163 1.8 christos || gdbpy_initialize_connection () < 0
2164 1.8 christos || gdbpy_initialize_tui () < 0
2165 1.10 christos || gdbpy_initialize_micommands () < 0)
2166 1.1 christos return false;
2167 1.7 christos
2168 1.10 christos #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base) \
2169 1.1 christos if (gdbpy_initialize_event_generic (&name##_event_object_type, py_name) < 0) \
2170 1.7 christos return false;
2171 1.10 christos #include "py-event-types.def"
2172 1.1 christos #undef GDB_PY_DEFINE_EVENT_TYPE
2173 1.7 christos
2174 1.10 christos gdbpy_to_string_cst = PyUnicode_FromString ("to_string");
2175 1.1 christos if (gdbpy_to_string_cst == NULL)
2176 1.7 christos return false;
2177 1.10 christos gdbpy_children_cst = PyUnicode_FromString ("children");
2178 1.1 christos if (gdbpy_children_cst == NULL)
2179 1.7 christos return false;
2180 1.10 christos gdbpy_display_hint_cst = PyUnicode_FromString ("display_hint");
2181 1.1 christos if (gdbpy_display_hint_cst == NULL)
2182 1.7 christos return false;
2183 1.1 christos gdbpy_doc_cst = PyUnicode_FromString ("__doc__");
2184 1.10 christos if (gdbpy_doc_cst == NULL)
2185 1.10 christos return false;
2186 1.1 christos gdbpy_enabled_cst = PyUnicode_FromString ("enabled");
2187 1.9 christos if (gdbpy_enabled_cst == NULL)
2188 1.1 christos return false;
2189 1.1 christos gdbpy_value_cst = PyUnicode_FromString ("value");
2190 1.1 christos if (gdbpy_value_cst == NULL)
2191 1.7 christos return false;
2192 1.1 christos
2193 1.7 christos gdb::observers::gdb_exiting.attach (gdbpy_gdb_exiting, "python");
2194 1.7 christos
2195 1.7 christos /* Release the GIL while gdb runs. */
2196 1.10 christos PyEval_SaveThread ();
2197 1.10 christos
2198 1.10 christos make_final_cleanup (finalize_python, NULL);
2199 1.10 christos
2200 1.10 christos /* Only set this when initialization has succeeded. */
2201 1.10 christos gdb_python_initialized = 1;
2202 1.10 christos return true;
2203 1.10 christos }
2204 1.10 christos
2205 1.10 christos #if GDB_SELF_TEST
2206 1.10 christos namespace selftests {
2207 1.10 christos
2208 1.10 christos /* Entry point for python unit tests. */
2209 1.10 christos
2210 1.10 christos static void
2211 1.10 christos test_python ()
2212 1.10 christos {
2213 1.10 christos #define CMD(S) execute_command_to_string (S, "python print(5)", 0, true)
2214 1.10 christos
2215 1.10 christos std::string output;
2216 1.10 christos
2217 1.10 christos CMD (output);
2218 1.10 christos SELF_CHECK (output == "5\n");
2219 1.10 christos output.clear ();
2220 1.10 christos
2221 1.10 christos bool saw_exception = false;
2222 1.10 christos {
2223 1.10 christos scoped_restore reset_gdb_python_initialized
2224 1.10 christos = make_scoped_restore (&gdb_python_initialized, 0);
2225 1.10 christos try
2226 1.10 christos {
2227 1.10 christos CMD (output);
2228 1.10 christos }
2229 1.10 christos catch (const gdb_exception &e)
2230 1.10 christos {
2231 1.10 christos saw_exception = true;
2232 1.10 christos SELF_CHECK (e.reason == RETURN_ERROR);
2233 1.10 christos SELF_CHECK (e.error == GENERIC_ERROR);
2234 1.10 christos SELF_CHECK (*e.message == "Python not initialized");
2235 1.10 christos }
2236 1.10 christos SELF_CHECK (saw_exception);
2237 1.10 christos SELF_CHECK (output.empty ());
2238 1.10 christos }
2239 1.10 christos
2240 1.10 christos saw_exception = false;
2241 1.10 christos {
2242 1.10 christos scoped_restore save_hook
2243 1.10 christos = make_scoped_restore (&hook_set_active_ext_lang,
2244 1.10 christos []() { raise (SIGINT); });
2245 1.10 christos try
2246 1.10 christos {
2247 1.10 christos CMD (output);
2248 1.10 christos }
2249 1.10 christos catch (const gdb_exception &e)
2250 1.10 christos {
2251 1.10 christos saw_exception = true;
2252 1.10 christos SELF_CHECK (e.reason == RETURN_ERROR);
2253 1.10 christos SELF_CHECK (e.error == GENERIC_ERROR);
2254 1.10 christos SELF_CHECK (*e.message == "Error while executing Python code.");
2255 1.10 christos }
2256 1.10 christos SELF_CHECK (saw_exception);
2257 1.10 christos std::string ref_output_0 ("Traceback (most recent call last):\n"
2258 1.10 christos " File \"<string>\", line 0, in <module>\n"
2259 1.10 christos "KeyboardInterrupt\n");
2260 1.10 christos std::string ref_output_1 ("Traceback (most recent call last):\n"
2261 1.10 christos " File \"<string>\", line 1, in <module>\n"
2262 1.10 christos "KeyboardInterrupt\n");
2263 1.10 christos SELF_CHECK (output == ref_output_0 || output == ref_output_1);
2264 1.10 christos }
2265 1.7 christos
2266 1.7 christos #undef CMD
2267 1.8 christos }
2268 1.8 christos
2269 1.8 christos #undef CHECK_OUTPUT
2270 1.9 christos
2271 1.7 christos } // namespace selftests
2272 1.9 christos #endif /* GDB_SELF_TEST */
2273 1.7 christos
2274 1.10 christos #endif /* HAVE_PYTHON */
2275 1.10 christos
2276 1.10 christos /* See python.h. */
2277 1.7 christos cmd_list_element *python_cmd_element = nullptr;
2278 1.7 christos
2279 1.7 christos void _initialize_python ();
2280 1.7 christos void
2281 1.7 christos _initialize_python ()
2282 1.7 christos {
2283 1.7 christos cmd_list_element *python_interactive_cmd
2284 1.7 christos = add_com ("python-interactive", class_obscure,
2285 1.7 christos python_interactive_command,
2286 1.7 christos #ifdef HAVE_PYTHON
2287 1.7 christos _("\
2288 1.7 christos Start an interactive Python prompt.\n\
2289 1.9 christos \n\
2290 1.7 christos To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
2291 1.7 christos prompt).\n\
2292 1.7 christos \n\
2293 1.7 christos Alternatively, a single-line Python command can be given as an\n\
2294 1.7 christos argument, and if the command is an expression, the result will be\n\
2295 1.7 christos printed. For example:\n\
2296 1.7 christos \n\
2297 1.7 christos (gdb) python-interactive 2 + 3\n\
2298 1.10 christos 5")
2299 1.7 christos #else /* HAVE_PYTHON */
2300 1.8 christos _("\
2301 1.7 christos Start a Python interactive prompt.\n\
2302 1.7 christos \n\
2303 1.7 christos Python scripting is not supported in this copy of GDB.\n\
2304 1.7 christos This command is only a placeholder.")
2305 1.7 christos #endif /* HAVE_PYTHON */
2306 1.7 christos );
2307 1.8 christos add_com_alias ("pi", python_interactive_cmd, class_obscure, 1);
2308 1.7 christos
2309 1.7 christos python_cmd_element = add_com ("python", class_obscure, python_command,
2310 1.7 christos #ifdef HAVE_PYTHON
2311 1.7 christos _("\
2312 1.7 christos Evaluate a Python command.\n\
2313 1.7 christos \n\
2314 1.7 christos The command can be given as an argument, for instance:\n\
2315 1.7 christos \n\
2316 1.7 christos python print (23)\n\
2317 1.7 christos \n\
2318 1.7 christos If no argument is given, the following lines are read and used\n\
2319 1.7 christos as the Python commands. Type a line containing \"end\" to indicate\n\
2320 1.10 christos the end of the command.")
2321 1.1 christos #else /* HAVE_PYTHON */
2322 1.7 christos _("\
2323 1.10 christos Evaluate a Python command.\n\
2324 1.10 christos \n\
2325 1.10 christos Python scripting is not supported in this copy of GDB.\n\
2326 1.10 christos This command is only a placeholder.")
2327 1.10 christos #endif /* HAVE_PYTHON */
2328 1.7 christos );
2329 1.7 christos add_com_alias ("py", python_cmd_element, class_obscure, 1);
2330 1.7 christos
2331 1.7 christos /* Add set/show python print-stack. */
2332 1.7 christos add_setshow_prefix_cmd ("python", no_class,
2333 1.7 christos _("Prefix command for python preference settings."),
2334 1.7 christos _("Prefix command for python preference settings."),
2335 1.7 christos &user_set_python_list, &user_show_python_list,
2336 1.7 christos &setlist, &showlist);
2337 1.7 christos
2338 1.7 christos add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
2339 1.1 christos &gdbpy_should_print_stack, _("\
2340 1.10 christos Set mode for Python stack dump on error."), _("\
2341 1.10 christos Show the mode of Python stack printing on error."), _("\
2342 1.10 christos none == no stack or message will be printed.\n\
2343 1.10 christos full == a message and a stack will be printed.\n\
2344 1.10 christos message == an error message without a stack will be printed."),
2345 1.10 christos NULL, NULL,
2346 1.10 christos &user_set_python_list,
2347 1.10 christos &user_show_python_list);
2348 1.10 christos
2349 1.10 christos add_setshow_boolean_cmd ("ignore-environment", no_class,
2350 1.10 christos &python_ignore_environment, _("\
2351 1.10 christos Set whether the Python interpreter should ignore environment variables."), _(" \
2352 1.10 christos Show whether the Python interpreter showlist ignore environment variables."), _(" \
2353 1.10 christos When enabled GDB's Python interpreter will ignore any Python related\n \
2354 1.10 christos flags in the environment. This is equivalent to passing `-E' to a\n \
2355 1.10 christos python executable."),
2356 1.10 christos set_python_ignore_environment,
2357 1.10 christos show_python_ignore_environment,
2358 1.10 christos &user_set_python_list,
2359 1.10 christos &user_show_python_list);
2360 1.10 christos
2361 1.10 christos add_setshow_auto_boolean_cmd ("dont-write-bytecode", no_class,
2362 1.10 christos &python_dont_write_bytecode, _("\
2363 1.10 christos Set whether the Python interpreter should avoid byte-compiling python modules."), _("\
2364 1.10 christos Show whether the Python interpreter should avoid byte-compiling python modules."), _("\
2365 1.10 christos When enabled, GDB's embedded Python interpreter won't byte-compile python\n\
2366 1.10 christos modules. In order to take effect, this setting must be enabled in an early\n\
2367 1.10 christos initialization file, i.e. those run via the --early-init-eval-command or\n\
2368 1.10 christos -eix command line options. A 'set python dont-write-bytecode on' command\n\
2369 1.10 christos can also be issued directly from the GDB command line via the\n\
2370 1.10 christos --early-init-eval-command or -eiex command line options.\n\
2371 1.10 christos \n\
2372 1.10 christos This setting defaults to 'auto'. In this mode, provided the 'python\n\
2373 1.10 christos ignore-environment' setting is 'off', the environment variable\n\
2374 1.10 christos PYTHONDONTWRITEBYTECODE is examined to determine whether or not to\n\
2375 1.7 christos byte-compile python modules. PYTHONDONTWRITEBYTECODE is considered to be\n\
2376 1.10 christos off/disabled either when set to the empty string or when the\n\
2377 1.10 christos environment variable doesn't exist. All other settings, including those\n\
2378 1.10 christos which don't seem to make sense, indicate that it's on/enabled."),
2379 1.1 christos set_python_dont_write_bytecode,
2380 1.1 christos show_python_dont_write_bytecode,
2381 1.1 christos &user_set_python_list,
2382 1.1 christos &user_show_python_list);
2383 1.1 christos
2384 1.10 christos #ifdef HAVE_PYTHON
2385 1.10 christos #if GDB_SELF_TEST
2386 1.10 christos selftests::register_test ("python", selftests::test_python);
2387 1.1 christos #endif /* GDB_SELF_TEST */
2388 1.7 christos #endif /* HAVE_PYTHON */
2389 1.10 christos }
2390 1.1 christos
2391 1.1 christos #ifdef HAVE_PYTHON
2392 1.1 christos
2393 1.1 christos /* Helper function for gdbpy_initialize. This does the work and then
2394 1.1 christos returns false if an error has occurred and must be displayed, or true on
2395 1.1 christos success. */
2396 1.7 christos
2397 1.7 christos static bool
2398 1.1 christos do_initialize (const struct extension_language_defn *extlang)
2399 1.1 christos {
2400 1.1 christos PyObject *m;
2401 1.10 christos PyObject *sys_path;
2402 1.10 christos
2403 1.10 christos /* Add the initial data-directory to sys.path. */
2404 1.10 christos
2405 1.10 christos std::string gdb_pythondir = (std::string (gdb_datadir) + SLASH_STRING
2406 1.10 christos + "python");
2407 1.10 christos
2408 1.10 christos sys_path = PySys_GetObject ("path");
2409 1.10 christos
2410 1.10 christos /* PySys_SetPath was deprecated in Python 3.11. Disable this
2411 1.1 christos deprecated code for Python 3.10 and newer. Also note that this
2412 1.1 christos ifdef eliminates potential initialization of sys.path via
2413 1.1 christos PySys_SetPath. My (kevinb's) understanding of PEP 587 suggests
2414 1.1 christos that it's not necessary due to module_search_paths being
2415 1.1 christos initialized to an empty list following any of the PyConfig
2416 1.1 christos initialization functions. If it does turn out that some kind of
2417 1.10 christos initialization is still needed, it should be added to the
2418 1.1 christos PyConfig-based initialization in do_start_initialize(). */
2419 1.1 christos #if PY_VERSION_HEX < 0x030a0000
2420 1.10 christos /* If sys.path is not defined yet, define it first. */
2421 1.7 christos if (!(sys_path && PyList_Check (sys_path)))
2422 1.7 christos {
2423 1.1 christos PySys_SetPath (L"");
2424 1.1 christos sys_path = PySys_GetObject ("path");
2425 1.7 christos }
2426 1.1 christos #endif
2427 1.1 christos if (sys_path && PyList_Check (sys_path))
2428 1.1 christos {
2429 1.1 christos gdbpy_ref<> pythondir (PyUnicode_FromString (gdb_pythondir.c_str ()));
2430 1.1 christos if (pythondir == NULL || PyList_Insert (sys_path, 0, pythondir.get ()))
2431 1.7 christos return false;
2432 1.1 christos }
2433 1.7 christos else
2434 1.7 christos return false;
2435 1.1 christos
2436 1.1 christos /* Import the gdb module to finish the initialization, and
2437 1.1 christos add it to __main__ for convenience. */
2438 1.1 christos m = PyImport_AddModule ("__main__");
2439 1.1 christos if (m == NULL)
2440 1.1 christos return false;
2441 1.1 christos
2442 1.1 christos /* Keep the reference to gdb_python_module since it is in a global
2443 1.1 christos variable. */
2444 1.9 christos gdb_python_module = PyImport_ImportModule ("gdb");
2445 1.7 christos if (gdb_python_module == NULL)
2446 1.7 christos {
2447 1.7 christos gdbpy_print_stack ();
2448 1.7 christos /* This is passed in one call to warning so that blank lines aren't
2449 1.1 christos inserted between each line of text. */
2450 1.1 christos warning (_("\n"
2451 1.7 christos "Could not load the Python gdb module from `%s'.\n"
2452 1.7 christos "Limited Python support is available from the _gdb module.\n"
2453 1.1 christos "Suggest passing --data-directory=/path/to/gdb/data-directory."),
2454 1.10 christos gdb_pythondir.c_str ());
2455 1.10 christos /* We return "success" here as we've already emitted the
2456 1.10 christos warning. */
2457 1.1 christos return true;
2458 1.7 christos }
2459 1.10 christos
2460 1.7 christos return gdb_pymodule_addobject (m, "gdb", gdb_python_module) >= 0;
2461 1.10 christos }
2462 1.10 christos
2463 1.10 christos /* Perform Python initialization. This will be called after GDB has
2464 1.10 christos performed all of its own initialization. This is the
2465 1.1 christos extension_language_ops.initialize "method". */
2466 1.10 christos
2467 1.7 christos static void
2468 1.7 christos gdbpy_initialize (const struct extension_language_defn *extlang)
2469 1.7 christos {
2470 1.7 christos if (!do_start_initialization () && PyErr_Occurred ())
2471 1.1 christos gdbpy_print_stack ();
2472 1.1 christos
2473 1.3 christos gdbpy_enter enter_py;
2474 1.3 christos
2475 1.3 christos if (!do_initialize (extlang))
2476 1.3 christos {
2477 1.3 christos gdbpy_print_stack ();
2478 1.3 christos warning (_("internal error: Unhandled Python exception"));
2479 1.3 christos }
2480 1.3 christos }
2481 1.3 christos
2482 1.5 christos /* Return non-zero if Python has successfully initialized.
2483 1.1 christos This is the extension_languages_ops.initialized "method". */
2484 1.1 christos
2485 1.1 christos static int
2486 1.10 christos gdbpy_initialized (const struct extension_language_defn *extlang)
2487 1.10 christos {
2488 1.10 christos return gdb_python_initialized;
2489 1.10 christos }
2490 1.1 christos
2491 1.1 christos PyMethodDef python_GdbMethods[] =
2492 1.1 christos {
2493 1.1 christos { "history", gdbpy_history, METH_VARARGS,
2494 1.1 christos "Get a value from history" },
2495 1.1 christos { "add_history", gdbpy_add_history, METH_VARARGS,
2496 1.1 christos "Add a value to the value history list" },
2497 1.1 christos { "history_count", gdbpy_history_count, METH_NOARGS,
2498 1.1 christos "Return an integer, the number of values in GDB's value history" },
2499 1.1 christos { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
2500 1.1 christos "execute (command [, from_tty] [, to_string]) -> [String]\n\
2501 1.1 christos Evaluate command, a string, as a gdb CLI command. Optionally returns\n\
2502 1.1 christos a Python String containing the output of the command if to_string is\n\
2503 1.1 christos set to True." },
2504 1.1 christos { "parameter", gdbpy_parameter, METH_VARARGS,
2505 1.1 christos "Return a gdb parameter's value" },
2506 1.1 christos
2507 1.1 christos { "breakpoints", gdbpy_breakpoints, METH_NOARGS,
2508 1.1 christos "Return a tuple of all breakpoint objects" },
2509 1.1 christos
2510 1.1 christos { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
2511 1.1 christos "Find the default visualizer for a Value." },
2512 1.1 christos
2513 1.1 christos { "progspaces", gdbpy_progspaces, METH_NOARGS,
2514 1.1 christos "Return a sequence of all progspaces." },
2515 1.1 christos
2516 1.1 christos { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
2517 1.1 christos "Return the current Objfile being loaded, or None." },
2518 1.1 christos
2519 1.1 christos { "newest_frame", gdbpy_newest_frame, METH_NOARGS,
2520 1.7 christos "newest_frame () -> gdb.Frame.\n\
2521 1.7 christos Return the newest frame object." },
2522 1.7 christos { "selected_frame", gdbpy_selected_frame, METH_NOARGS,
2523 1.7 christos "selected_frame () -> gdb.Frame.\n\
2524 1.7 christos Return the selected frame object." },
2525 1.7 christos { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
2526 1.7 christos "stop_reason_string (Integer) -> String.\n\
2527 1.7 christos Return a string explaining unwind stop reason." },
2528 1.7 christos
2529 1.7 christos { "start_recording", gdbpy_start_recording, METH_VARARGS,
2530 1.7 christos "start_recording ([method] [, format]) -> gdb.Record.\n\
2531 1.7 christos Start recording with the given method. If no method is given, will fall back\n\
2532 1.1 christos to the system default method. If no format is given, will fall back to the\n\
2533 1.1 christos default format for the given method."},
2534 1.1 christos { "current_recording", gdbpy_current_recording, METH_NOARGS,
2535 1.1 christos "current_recording () -> gdb.Record.\n\
2536 1.1 christos Return current recording object." },
2537 1.1 christos { "stop_recording", gdbpy_stop_recording, METH_NOARGS,
2538 1.1 christos "stop_recording () -> None.\n\
2539 1.1 christos Stop current recording." },
2540 1.1 christos
2541 1.1 christos { "lookup_type", (PyCFunction) gdbpy_lookup_type,
2542 1.1 christos METH_VARARGS | METH_KEYWORDS,
2543 1.1 christos "lookup_type (name [, block]) -> type\n\
2544 1.1 christos Return a Type corresponding to the given name." },
2545 1.1 christos { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol,
2546 1.9 christos METH_VARARGS | METH_KEYWORDS,
2547 1.9 christos "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
2548 1.9 christos Return a tuple with the symbol corresponding to the given name (or None) and\n\
2549 1.9 christos a boolean indicating if name is a field of the current implied argument\n\
2550 1.9 christos `this' (when the current language is object-oriented)." },
2551 1.9 christos { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol,
2552 1.9 christos METH_VARARGS | METH_KEYWORDS,
2553 1.9 christos "lookup_global_symbol (name [, domain]) -> symbol\n\
2554 1.3 christos Return the symbol corresponding to the given name (or None)." },
2555 1.3 christos { "lookup_static_symbol", (PyCFunction) gdbpy_lookup_static_symbol,
2556 1.3 christos METH_VARARGS | METH_KEYWORDS,
2557 1.3 christos "lookup_static_symbol (name [, domain]) -> symbol\n\
2558 1.3 christos Return the static-linkage symbol corresponding to the given name (or None)." },
2559 1.3 christos { "lookup_static_symbols", (PyCFunction) gdbpy_lookup_static_symbols,
2560 1.3 christos METH_VARARGS | METH_KEYWORDS,
2561 1.3 christos "lookup_static_symbols (name [, domain]) -> symbol\n\
2562 1.1 christos Return a list of all static-linkage symbols corresponding to the given name." },
2563 1.1 christos
2564 1.1 christos { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile,
2565 1.1 christos METH_VARARGS | METH_KEYWORDS,
2566 1.1 christos "lookup_objfile (name, [by_build_id]) -> objfile\n\
2567 1.1 christos Look up the specified objfile.\n\
2568 1.1 christos If by_build_id is True, the objfile is looked up by using name\n\
2569 1.1 christos as its build id." },
2570 1.1 christos
2571 1.1 christos { "decode_line", gdbpy_decode_line, METH_VARARGS,
2572 1.1 christos "decode_line (String) -> Tuple. Decode a string argument the way\n\
2573 1.1 christos that 'break' or 'edit' does. Return a tuple containing two elements.\n\
2574 1.1 christos The first element contains any unparsed portion of the String parameter\n\
2575 1.1 christos (or None if the string was fully parsed). The second element contains\n\
2576 1.1 christos a tuple that contains all the locations that match, represented as\n\
2577 1.1 christos gdb.Symtab_and_line objects (or None)."},
2578 1.1 christos { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
2579 1.1 christos "parse_and_eval (String) -> Value.\n\
2580 1.1 christos Parse String as an expression, evaluate it, and return the result as a Value."
2581 1.1 christos },
2582 1.1 christos
2583 1.10 christos { "post_event", gdbpy_post_event, METH_VARARGS,
2584 1.10 christos "Post an event into gdb's event loop." },
2585 1.10 christos
2586 1.8 christos { "target_charset", gdbpy_target_charset, METH_NOARGS,
2587 1.8 christos "target_charset () -> string.\n\
2588 1.8 christos Return the name of the current target charset." },
2589 1.1 christos { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
2590 1.1 christos "target_wide_charset () -> string.\n\
2591 1.1 christos Return the name of the current target wide charset." },
2592 1.1 christos { "host_charset", gdbpy_host_charset, METH_NOARGS,
2593 1.1 christos "host_charset () -> string.\n\
2594 1.1 christos Return the name of the current host charset." },
2595 1.1 christos { "rbreak", (PyCFunction) gdbpy_rbreak, METH_VARARGS | METH_KEYWORDS,
2596 1.1 christos "rbreak (Regex) -> List.\n\
2597 1.1 christos Return a Tuple containing gdb.Breakpoint objects that match the given Regex." },
2598 1.1 christos { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS,
2599 1.1 christos "string_to_argv (String) -> Array.\n\
2600 1.1 christos Parse String and return an argv-like array.\n\
2601 1.1 christos Arguments are separate by spaces and may be quoted."
2602 1.1 christos },
2603 1.1 christos { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS,
2604 1.1 christos "Write a string using gdb's filtered stream." },
2605 1.1 christos { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS,
2606 1.1 christos "Flush gdb's filtered stdout stream." },
2607 1.6 christos { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
2608 1.6 christos "selected_thread () -> gdb.InferiorThread.\n\
2609 1.6 christos Return the selected thread object." },
2610 1.6 christos { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
2611 1.6 christos "selected_inferior () -> gdb.Inferior.\n\
2612 1.6 christos Return the selected inferior object." },
2613 1.8 christos { "inferiors", gdbpy_inferiors, METH_NOARGS,
2614 1.8 christos "inferiors () -> (gdb.Inferior, ...).\n\
2615 1.8 christos Return a tuple containing all inferiors." },
2616 1.8 christos
2617 1.8 christos { "invalidate_cached_frames", gdbpy_invalidate_cached_frames, METH_NOARGS,
2618 1.8 christos "invalidate_cached_frames () -> None.\n\
2619 1.8 christos Invalidate any cached frame objects in gdb.\n\
2620 1.8 christos Intended for internal use only." },
2621 1.9 christos
2622 1.9 christos { "convenience_variable", gdbpy_convenience_variable, METH_VARARGS,
2623 1.9 christos "convenience_variable (NAME) -> value.\n\
2624 1.9 christos Return the value of the convenience variable $NAME,\n\
2625 1.9 christos or None if not set." },
2626 1.9 christos { "set_convenience_variable", gdbpy_set_convenience_variable, METH_VARARGS,
2627 1.9 christos "convenience_variable (NAME, VALUE) -> None.\n\
2628 1.10 christos Set the value of the convenience variable $NAME." },
2629 1.10 christos
2630 1.10 christos #ifdef TUI
2631 1.10 christos { "register_window_type", (PyCFunction) gdbpy_register_tui_window,
2632 1.10 christos METH_VARARGS | METH_KEYWORDS,
2633 1.10 christos "register_window_type (NAME, CONSTRUCSTOR) -> None\n\
2634 1.10 christos Register a TUI window constructor." },
2635 1.10 christos #endif /* TUI */
2636 1.10 christos
2637 1.10 christos { "architecture_names", gdbpy_all_architecture_names, METH_NOARGS,
2638 1.10 christos "architecture_names () -> List.\n\
2639 1.10 christos Return a list of all the architecture names GDB understands." },
2640 1.10 christos
2641 1.10 christos { "connections", gdbpy_connections, METH_NOARGS,
2642 1.10 christos "connections () -> List.\n\
2643 1.10 christos Return a list of gdb.TargetConnection objects." },
2644 1.10 christos
2645 1.10 christos { "format_address", (PyCFunction) gdbpy_format_address,
2646 1.10 christos METH_VARARGS | METH_KEYWORDS,
2647 1.10 christos "format_address (ADDRESS, PROG_SPACE, ARCH) -> String.\n\
2648 1.10 christos Format ADDRESS, an address within PROG_SPACE, a gdb.Progspace, using\n\
2649 1.10 christos ARCH, a gdb.Architecture to determine the address size. The format of\n\
2650 1.10 christos the returned string is 'ADDRESS <SYMBOL+OFFSET>' without the quotes." },
2651 1.1 christos
2652 1.1 christos { "current_language", gdbpy_current_language, METH_NOARGS,
2653 1.1 christos "current_language () -> string\n\
2654 1.8 christos Return the name of the currently selected language." },
2655 1.8 christos
2656 1.8 christos { "print_options", gdbpy_print_options, METH_NOARGS,
2657 1.10 christos "print_options () -> dict\n\
2658 1.8 christos Return the current print options." },
2659 1.8 christos
2660 1.8 christos {NULL, NULL, 0, NULL}
2661 1.8 christos };
2662 1.8 christos
2663 1.8 christos /* Define all the event objects. */
2664 1.8 christos #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base) \
2665 1.8 christos PyTypeObject name##_event_object_type \
2666 1.8 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object") \
2667 1.8 christos = { \
2668 1.8 christos PyVarObject_HEAD_INIT (NULL, 0) \
2669 1.8 christos "gdb." py_name, /* tp_name */ \
2670 1.8 christos sizeof (event_object), /* tp_basicsize */ \
2671 1.8 christos 0, /* tp_itemsize */ \
2672 1.8 christos evpy_dealloc, /* tp_dealloc */ \
2673 1.8 christos 0, /* tp_print */ \
2674 1.8 christos 0, /* tp_getattr */ \
2675 1.8 christos 0, /* tp_setattr */ \
2676 1.8 christos 0, /* tp_compare */ \
2677 1.8 christos 0, /* tp_repr */ \
2678 1.8 christos 0, /* tp_as_number */ \
2679 1.8 christos 0, /* tp_as_sequence */ \
2680 1.8 christos 0, /* tp_as_mapping */ \
2681 1.8 christos 0, /* tp_hash */ \
2682 1.8 christos 0, /* tp_call */ \
2683 1.8 christos 0, /* tp_str */ \
2684 1.8 christos 0, /* tp_getattro */ \
2685 1.8 christos 0, /* tp_setattro */ \
2686 1.8 christos 0, /* tp_as_buffer */ \
2687 1.8 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ \
2688 1.8 christos doc, /* tp_doc */ \
2689 1.8 christos 0, /* tp_traverse */ \
2690 1.8 christos 0, /* tp_clear */ \
2691 1.8 christos 0, /* tp_richcompare */ \
2692 1.8 christos 0, /* tp_weaklistoffset */ \
2693 1.8 christos 0, /* tp_iter */ \
2694 1.8 christos 0, /* tp_iternext */ \
2695 1.8 christos 0, /* tp_methods */ \
2696 1.8 christos 0, /* tp_members */ \
2697 1.8 christos 0, /* tp_getset */ \
2698 1.8 christos &base, /* tp_base */ \
2699 1.8 christos 0, /* tp_dict */ \
2700 1.1 christos 0, /* tp_descr_get */ \
2701 0, /* tp_descr_set */ \
2702 0, /* tp_dictoffset */ \
2703 0, /* tp_init */ \
2704 0 /* tp_alloc */ \
2705 };
2706 #include "py-event-types.def"
2707 #undef GDB_PY_DEFINE_EVENT_TYPE
2708
2709 #endif /* HAVE_PYTHON */
2710