* cli/cli-cmds.c (source_script_with_search): Pass full path to
[deliverable/binutils-gdb.git] / gdb / python / python.c
index 360bed4ae946212180c233f0f594951e6de11fff..3a5a6b5b2dabe93182896ca71b93d253246730fa 100644 (file)
@@ -134,6 +134,45 @@ ensure_python_env (struct gdbarch *gdbarch,
   return make_cleanup (restore_python_env, env);
 }
 
+/* A wrapper around PyRun_SimpleFile.  FILENAME is the name of
+   the Python script to run.
+
+   One of the parameters of PyRun_SimpleFile is a FILE *.
+   The problem is that type FILE is extremely system and compiler
+   dependent.  So, unless the Python library has been compiled using
+   the same build environment as GDB, we run the risk of getting
+   a crash due to inconsistencies between the definition used by GDB,
+   and the definition used by Python.  A mismatch can very likely
+   lead to a crash.
+
+   There is also the situation where the Python library and GDB
+   are using two different versions of the C runtime library.
+   This is particularly visible on Windows, where few users would
+   build Python themselves (this is no trivial task on this platform),
+   and thus use binaries built by someone else instead. Python,
+   being built with VC, would use one version of the msvcr DLL
+   (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.  A FILE *
+   from one runtime does not necessarily operate correctly in
+   the other runtime.
+
+   To work around this potential issue, we create the FILE object
+   using Python routines, thus making sure that it is compatible
+   with the Python library.  */
+
+static void
+python_run_simple_file (const char *filename)
+{
+  char *filename_copy;
+  PyObject *python_file;
+  struct cleanup *cleanup;
+
+  filename_copy = xstrdup (filename);
+  cleanup = make_cleanup (xfree, filename_copy);
+  python_file = PyFile_FromString (filename_copy, "r");
+  make_cleanup_py_decref (python_file);
+  PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
+  do_cleanups (cleanup);
+}
 
 /* Given a command_line, return a command string suitable for passing
    to Python.  Lines in the string are separated by newlines.  The
@@ -560,21 +599,18 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args)
   return value_to_value_object (result);
 }
 
-/* Read a file as Python code.  STREAM is the input file; FILE is the
-   name of the file.
-   STREAM is not closed, that is the caller's responsibility.  */
+/* Read a file as Python code.
+   FILE is the name of the file.
+   This does not throw any errors.  If an exception occurs python will print
+   the traceback and clear the error indicator.  */
 
 void
-source_python_script (FILE *stream, const char *file)
+source_python_script (const char *file)
 {
   struct cleanup *cleanup;
 
   cleanup = ensure_python_env (get_current_arch (), current_language);
-
-  /* Note: If an exception occurs python will print the traceback and
-     clear the error indicator.  */
-  PyRun_SimpleFile (stream, file);
-
+  python_run_simple_file (file);
   do_cleanups (cleanup);
 }
 
@@ -753,7 +789,7 @@ before_prompt_hook (const char *current_gdb_prompt)
   /* If a prompt has been set, PROMPT will not be NULL.  If it is
      NULL, do not set the prompt.  */
   if (prompt != NULL)
-    set_prompt (prompt, 0);
+    set_prompt (prompt);
 
   do_cleanups (cleanup);
   return;
@@ -902,21 +938,19 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
    source_python_script_for_objfile; it is NULL at other times.  */
 static struct objfile *gdbpy_current_objfile;
 
-/* Set the current objfile to OBJFILE and then read STREAM,FILE as
-   Python code.  */
+/* Set the current objfile to OBJFILE and then read FILE as Python code.
+   This does not throw any errors.  If an exception occurs python will print
+   the traceback and clear the error indicator.  */
 
 void
-source_python_script_for_objfile (struct objfile *objfile,
-                                 FILE *stream, const char *file)
+source_python_script_for_objfile (struct objfile *objfile, const char *file)
 {
   struct cleanup *cleanups;
 
   cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
   gdbpy_current_objfile = objfile;
 
-  /* Note: If an exception occurs python will print the traceback and
-     clear the error indicator.  */
-  PyRun_SimpleFile (stream, file);
+  python_run_simple_file (file);
 
   do_cleanups (cleanups);
   gdbpy_current_objfile = NULL;
@@ -992,7 +1026,7 @@ eval_python_from_control_command (struct command_line *cmd)
 }
 
 void
-source_python_script (FILE *stream, const char *file)
+source_python_script (const char *file)
 {
   throw_error (UNSUPPORTED_ERROR,
               _("Python scripting is not supported in this copy of GDB."));
@@ -1217,6 +1251,7 @@ Enables or disables printing of Python stack traces."),
   gdbpy_initialize_continue_event ();
   gdbpy_initialize_exited_event ();
   gdbpy_initialize_thread_event ();
+  gdbpy_initialize_new_objfile_event () ;
 
   observer_attach_before_prompt (before_prompt_hook);
 
@@ -1423,6 +1458,9 @@ Arguments are separate by spaces and may be quoted."
   { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
     "selected_thread () -> gdb.InferiorThread.\n\
 Return the selected thread object." },
+  { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
+    "selected_inferior () -> gdb.Inferior.\n\
+Return the selected inferior object." },
   { "inferiors", gdbpy_inferiors, METH_NOARGS,
     "inferiors () -> (gdb.Inferior, ...).\n\
 Return a tuple containing all inferiors." },
This page took 0.025596 seconds and 4 git commands to generate.