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