testsuite/gdb.base: Make skip test use defined behaviour.
[deliverable/binutils-gdb.git] / gdb / python / python.c
index 21895a88d2bbde81725f1a5470137a4e72577668..c94198e2985f8e4c1b378714ea1ff24d187780aa 100644 (file)
@@ -1,6 +1,6 @@
 /* General python/gdb code
 
-   Copyright (C) 2008-2012 Free Software Foundation, Inc.
+   Copyright (C) 2008-2013 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -32,6 +32,7 @@
 #include "serial.h"
 #include "readline/tilde.h"
 #include "python.h"
+#include "cli/cli-utils.h"
 
 #include <ctype.h>
 
@@ -72,6 +73,11 @@ static const char *gdbpy_should_print_stack = python_excp_message;
 #include "interps.h"
 #include "event-top.h"
 
+/* True if Python has been successfully initialized, false
+   otherwise.  */
+
+int gdb_python_initialized;
+
 static PyMethodDef GdbMethods[];
 
 #ifdef IS_PY3K
@@ -144,6 +150,10 @@ ensure_python_env (struct gdbarch *gdbarch,
 {
   struct python_env *env = xmalloc (sizeof *env);
 
+  /* We should not ever enter Python unless initialized.  */
+  if (!gdb_python_initialized)
+    error (_("Python not initialized"));
+
   env->state = PyGILState_Ensure ();
   env->gdbarch = python_gdbarch;
   env->language = python_language;
@@ -222,8 +232,7 @@ python_interactive_command (char *arg, int from_tty)
   cleanup = make_cleanup_restore_integer (&interpreter_async);
   interpreter_async = 0;
 
-  while (arg && *arg && isspace (*arg))
-    ++arg;
+  arg = skip_spaces (arg);
 
   ensure_python_env (get_current_arch (), current_language);
 
@@ -367,8 +376,7 @@ python_command (char *arg, int from_tty)
   make_cleanup_restore_integer (&interpreter_async);
   interpreter_async = 0;
 
-  while (arg && *arg && isspace (*arg))
-    ++arg;
+  arg = skip_spaces (arg);
   if (arg && *arg)
     {
       if (PyRun_SimpleString (arg))
@@ -645,7 +653,8 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
     {
       do_cleanups (cleanups);
       /* We know this will always throw.  */
-      GDB_PY_HANDLE_EXCEPTION (except);
+      gdbpy_convert_exception (except);
+      return NULL;
     }
 
   if (sals.nelts)
@@ -721,11 +730,7 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args)
 
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
-      char *copy = xstrdup (expr_str);
-      struct cleanup *cleanup = make_cleanup (xfree, copy);
-
-      result = parse_and_eval (copy);
-      do_cleanups (cleanup);
+      result = parse_and_eval (expr_str);
     }
   GDB_PY_HANDLE_EXCEPTION (except);
 
@@ -738,16 +743,25 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args)
 static PyObject *
 gdbpy_find_pc_line (PyObject *self, PyObject *args)
 {
-  struct symtab_and_line sal;
-  CORE_ADDR pc;
   gdb_py_ulongest pc_llu;
+  volatile struct gdb_exception except;
+  PyObject *result = NULL; /* init for gcc -Wall */
 
   if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
     return NULL;
 
-  pc = (CORE_ADDR) pc_llu;
-  sal = find_pc_line (pc, 0);
-  return symtab_and_line_to_sal_object (sal);
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      struct symtab_and_line sal;
+      CORE_ADDR pc;
+
+      pc = (CORE_ADDR) pc_llu;
+      sal = find_pc_line (pc, 0);
+      result = symtab_and_line_to_sal_object (sal);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  return result;
 }
 
 /* Read a file as Python code.
@@ -806,6 +820,8 @@ gdbpy_run_events (struct serial *scb, void *context)
 
   while (gdbpy_event_list)
     {
+      PyObject *call_result;
+
       /* Dispatching the event might push a new element onto the event
         loop, so we update here "atomically enough".  */
       struct gdbpy_event *item = gdbpy_event_list;
@@ -814,9 +830,11 @@ gdbpy_run_events (struct serial *scb, void *context)
        gdbpy_event_list_end = &gdbpy_event_list;
 
       /* Ignore errors.  */
-      if (PyObject_CallObject (item->event, NULL) == NULL)
+      call_result = PyObject_CallObject (item->event, NULL);
+      if (call_result == NULL)
        PyErr_Clear ();
 
+      Py_XDECREF (call_result);
       Py_DECREF (item->event);
       xfree (item);
     }
@@ -867,7 +885,7 @@ gdbpy_post_event (PyObject *self, PyObject *args)
 }
 
 /* Initialize the Python event handler.  */
-static void
+static int
 gdbpy_initialize_events (void)
 {
   if (serial_pipe (gdbpy_event_fds) == 0)
@@ -875,6 +893,8 @@ gdbpy_initialize_events (void)
       gdbpy_event_list_end = &gdbpy_event_list;
       serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL);
     }
+
+  return 0;
 }
 
 \f
@@ -885,6 +905,9 @@ before_prompt_hook (const char *current_gdb_prompt)
   struct cleanup *cleanup;
   char *prompt = NULL;
 
+  if (!gdb_python_initialized)
+    return;
+
   cleanup = ensure_python_env (get_current_arch (), current_language);
 
   if (gdb_python_module
@@ -896,6 +919,8 @@ before_prompt_hook (const char *current_gdb_prompt)
       if (hook == NULL)
        goto fail;
 
+      make_cleanup_py_decref (hook);
+
       if (PyCallable_Check (hook))
        {
          PyObject *result;
@@ -1034,6 +1059,8 @@ gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
 void
 gdbpy_print_stack (void)
 {
+  volatile struct gdb_exception except;
+
   /* Print "none", just clear exception.  */
   if (gdbpy_should_print_stack == python_excp_none)
     {
@@ -1046,7 +1073,10 @@ gdbpy_print_stack (void)
       /* PyErr_Print doesn't necessarily end output with a newline.
         This works because Python's stdout/stderr is fed through
         printf_filtered.  */
-      begin_line ();
+      TRY_CATCH (except, RETURN_MASK_ALL)
+       {
+         begin_line ();
+       }
     }
   /* Print "message", just error print message.  */
   else
@@ -1059,17 +1089,21 @@ gdbpy_print_stack (void)
       /* Fetch the error message contained within ptype, pvalue.  */
       msg = gdbpy_exception_to_string (ptype, pvalue);
       type = gdbpy_obj_to_string (ptype);
-      if (msg == NULL)
+
+      TRY_CATCH (except, RETURN_MASK_ALL)
        {
-         /* An error occurred computing the string representation of the
-            error message.  */
-         fprintf_filtered (gdb_stderr,
-                           _("Error occurred computing Python error"   \
-                             "message.\n"));
+         if (msg == NULL)
+           {
+             /* An error occurred computing the string representation of the
+                error message.  */
+             fprintf_filtered (gdb_stderr,
+                               _("Error occurred computing Python error" \
+                                 "message.\n"));
+           }
+         else
+           fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
+                             type, msg);
        }
-      else
-       fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
-                         type, msg);
 
       Py_XDECREF (ptype);
       Py_XDECREF (pvalue);
@@ -1137,6 +1171,9 @@ source_python_script_for_objfile (struct objfile *objfile, FILE *file,
 {
   struct cleanup *cleanups;
 
+  if (!gdb_python_initialized)
+    return;
+
   cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
   gdbpy_current_objfile = objfile;
 
@@ -1196,7 +1233,10 @@ void *
 start_type_printers (void)
 {
   struct cleanup *cleanups;
-  PyObject *type_module, *func, *result_obj = NULL;
+  PyObject *type_module, *func = NULL, *result_obj = NULL;
+
+  if (!gdb_python_initialized)
+    return NULL;
 
   cleanups = ensure_python_env (get_current_arch (), current_language);
 
@@ -1206,7 +1246,6 @@ start_type_printers (void)
       gdbpy_print_stack ();
       goto done;
     }
-  make_cleanup_py_decref (type_module);
 
   func = PyObject_GetAttrString (type_module, "get_type_recognizers");
   if (func == NULL)
@@ -1214,13 +1253,14 @@ start_type_printers (void)
       gdbpy_print_stack ();
       goto done;
     }
-  make_cleanup_py_decref (func);
 
   result_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
   if (result_obj == NULL)
     gdbpy_print_stack ();
 
  done:
+  Py_XDECREF (type_module);
+  Py_XDECREF (func);
   do_cleanups (cleanups);
   return result_obj;
 }
@@ -1237,13 +1277,17 @@ char *
 apply_type_printers (void *printers, struct type *type)
 {
   struct cleanup *cleanups;
-  PyObject *type_obj, *type_module, *func, *result_obj;
+  PyObject *type_obj, *type_module = NULL, *func = NULL;
+  PyObject *result_obj = NULL;
   PyObject *printers_obj = printers;
   char *result = NULL;
 
   if (printers_obj == NULL)
     return NULL;
 
+  if (!gdb_python_initialized)
+    return NULL;
+
   cleanups = ensure_python_env (get_current_arch (), current_language);
 
   type_obj = type_to_type_object (type);
@@ -1252,7 +1296,6 @@ apply_type_printers (void *printers, struct type *type)
       gdbpy_print_stack ();
       goto done;
     }
-  make_cleanup_py_decref (type_obj);
 
   type_module = PyImport_ImportModule ("gdb.types");
   if (type_module == NULL)
@@ -1260,7 +1303,6 @@ apply_type_printers (void *printers, struct type *type)
       gdbpy_print_stack ();
       goto done;
     }
-  make_cleanup_py_decref (type_module);
 
   func = PyObject_GetAttrString (type_module, "apply_type_recognizers");
   if (func == NULL)
@@ -1268,7 +1310,6 @@ apply_type_printers (void *printers, struct type *type)
       gdbpy_print_stack ();
       goto done;
     }
-  make_cleanup_py_decref (func);
 
   result_obj = PyObject_CallFunctionObjArgs (func, printers_obj,
                                             type_obj, (char *) NULL);
@@ -1277,7 +1318,6 @@ apply_type_printers (void *printers, struct type *type)
       gdbpy_print_stack ();
       goto done;
     }
-  make_cleanup_py_decref (result_obj);
 
   if (result_obj != Py_None)
     {
@@ -1287,6 +1327,10 @@ apply_type_printers (void *printers, struct type *type)
     }
 
  done:
+  Py_XDECREF (type_obj);
+  Py_XDECREF (type_module);
+  Py_XDECREF (func);
+  Py_XDECREF (result_obj);
   do_cleanups (cleanups);
   return result;
 }
@@ -1302,6 +1346,9 @@ free_type_printers (void *arg)
   if (printers == NULL)
     return;
 
+  if (!gdb_python_initialized)
+    return;
+
   cleanups = ensure_python_env (get_current_arch (), current_language);
   Py_DECREF (printers);
   do_cleanups (cleanups);
@@ -1315,8 +1362,7 @@ free_type_printers (void *arg)
 static void
 python_interactive_command (char *arg, int from_tty)
 {
-  while (arg && *arg && isspace (*arg))
-    ++arg;
+  arg = skip_spaces (arg);
   if (arg && *arg)
     error (_("Python scripting is not supported in this copy of GDB."));
   else
@@ -1381,6 +1427,15 @@ free_type_printers (void *arg)
 {
 }
 
+enum py_bt_status
+apply_frame_filter (struct frame_info *frame, int flags,
+                   enum py_frame_args args_type,
+                   struct ui_out *out, int frame_low,
+                   int frame_high)
+{
+  return PY_BT_NO_FILTERS;
+}
+
 #endif /* HAVE_PYTHON */
 
 \f
@@ -1436,8 +1491,6 @@ extern initialize_file_ftype _initialize_python;
 void
 _initialize_python (void)
 {
-  char *cmd_name;
-  struct cmd_list_element *cmd;
   char *progname;
 #ifdef IS_PY3K
   int i;
@@ -1568,73 +1621,109 @@ message == an error message without a stack will be printed."),
 #else
   gdb_module = Py_InitModule ("_gdb", GdbMethods);
 #endif
+  if (gdb_module == NULL)
+    goto fail;
 
   /* The casts to (char*) are for python 2.4.  */
-  PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
-  PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
-  PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
-                             (char*) target_name);
+  if (PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version) < 0
+      || PyModule_AddStringConstant (gdb_module, "HOST_CONFIG",
+                                    (char*) host_name) < 0
+      || PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
+                                    (char*) target_name) < 0)
+    goto fail;
 
   /* Add stream constants.  */
-  PyModule_AddIntConstant (gdb_module, "STDOUT", 0);
-  PyModule_AddIntConstant (gdb_module, "STDERR", 1);
-  PyModule_AddIntConstant (gdb_module, "STDLOG", 2);
+  if (PyModule_AddIntConstant (gdb_module, "STDOUT", 0) < 0
+      || PyModule_AddIntConstant (gdb_module, "STDERR", 1) < 0
+      || PyModule_AddIntConstant (gdb_module, "STDLOG", 2) < 0)
+    goto fail;
 
   gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL);
-  PyModule_AddObject (gdb_module, "error", gdbpy_gdb_error);
+  if (gdbpy_gdb_error == NULL
+      || gdb_pymodule_addobject (gdb_module, "error", gdbpy_gdb_error) < 0)
+    goto fail;
 
   gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError",
                                               gdbpy_gdb_error, NULL);
-  PyModule_AddObject (gdb_module, "MemoryError", gdbpy_gdb_memory_error);
+  if (gdbpy_gdb_memory_error == NULL
+      || gdb_pymodule_addobject (gdb_module, "MemoryError",
+                                gdbpy_gdb_memory_error) < 0)
+    goto fail;
 
   gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
-  PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
+  if (gdbpy_gdberror_exc == NULL
+      || gdb_pymodule_addobject (gdb_module, "GdbError",
+                                gdbpy_gdberror_exc) < 0)
+    goto fail;
 
   gdbpy_initialize_gdb_readline ();
-  gdbpy_initialize_auto_load ();
-  gdbpy_initialize_values ();
-  gdbpy_initialize_frames ();
-  gdbpy_initialize_commands ();
-  gdbpy_initialize_symbols ();
-  gdbpy_initialize_symtabs ();
-  gdbpy_initialize_blocks ();
-  gdbpy_initialize_functions ();
-  gdbpy_initialize_parameters ();
-  gdbpy_initialize_types ();
-  gdbpy_initialize_pspace ();
-  gdbpy_initialize_objfile ();
-  gdbpy_initialize_breakpoints ();
-  gdbpy_initialize_finishbreakpoints ();
-  gdbpy_initialize_lazy_string ();
-  gdbpy_initialize_thread ();
-  gdbpy_initialize_inferior ();
-  gdbpy_initialize_events ();
-
-  gdbpy_initialize_eventregistry ();
-  gdbpy_initialize_py_events ();
-  gdbpy_initialize_event ();
-  gdbpy_initialize_stop_event ();
-  gdbpy_initialize_signal_event ();
-  gdbpy_initialize_breakpoint_event ();
-  gdbpy_initialize_continue_event ();
-  gdbpy_initialize_exited_event ();
-  gdbpy_initialize_thread_event ();
-  gdbpy_initialize_new_objfile_event () ;
+
+  if (gdbpy_initialize_auto_load () < 0
+      || gdbpy_initialize_values () < 0
+      || gdbpy_initialize_frames () < 0
+      || gdbpy_initialize_commands () < 0
+      || gdbpy_initialize_symbols () < 0
+      || gdbpy_initialize_symtabs () < 0
+      || gdbpy_initialize_blocks () < 0
+      || gdbpy_initialize_functions () < 0
+      || gdbpy_initialize_parameters () < 0
+      || gdbpy_initialize_types () < 0
+      || gdbpy_initialize_pspace () < 0
+      || gdbpy_initialize_objfile () < 0
+      || gdbpy_initialize_breakpoints () < 0
+      || gdbpy_initialize_finishbreakpoints () < 0
+      || gdbpy_initialize_lazy_string () < 0
+      || gdbpy_initialize_thread () < 0
+      || gdbpy_initialize_inferior () < 0
+      || gdbpy_initialize_events () < 0
+      || gdbpy_initialize_eventregistry () < 0
+      || gdbpy_initialize_py_events () < 0
+      || gdbpy_initialize_event () < 0
+      || gdbpy_initialize_stop_event () < 0
+      || gdbpy_initialize_signal_event () < 0
+      || gdbpy_initialize_breakpoint_event () < 0
+      || gdbpy_initialize_continue_event () < 0
+      || gdbpy_initialize_exited_event () < 0
+      || gdbpy_initialize_thread_event () < 0
+      || gdbpy_initialize_new_objfile_event ()  < 0
+      || gdbpy_initialize_arch () < 0)
+    goto fail;
 
   observer_attach_before_prompt (before_prompt_hook);
 
   gdbpy_to_string_cst = PyString_FromString ("to_string");
+  if (gdbpy_to_string_cst == NULL)
+    goto fail;
   gdbpy_children_cst = PyString_FromString ("children");
+  if (gdbpy_children_cst == NULL)
+    goto fail;
   gdbpy_display_hint_cst = PyString_FromString ("display_hint");
+  if (gdbpy_display_hint_cst == NULL)
+    goto fail;
   gdbpy_doc_cst = PyString_FromString ("__doc__");
+  if (gdbpy_doc_cst == NULL)
+    goto fail;
   gdbpy_enabled_cst = PyString_FromString ("enabled");
+  if (gdbpy_enabled_cst == NULL)
+    goto fail;
   gdbpy_value_cst = PyString_FromString ("value");
+  if (gdbpy_value_cst == NULL)
+    goto fail;
 
   /* Release the GIL while gdb runs.  */
   PyThreadState_Swap (NULL);
   PyEval_ReleaseLock ();
 
   make_final_cleanup (finalize_python, NULL);
+
+  gdb_python_initialized = 1;
+  return;
+
+ fail:
+  gdbpy_print_stack ();
+  /* Do not set 'gdb_python_initialized'.  */
+  return;
+
 #endif /* HAVE_PYTHON */
 }
 
@@ -1700,14 +1789,18 @@ finish_python_initialization (void)
   if (gdb_python_module == NULL)
     {
       gdbpy_print_stack ();
-      warning (_("Could not load the Python gdb module from `%s'."),
+      /* This is passed in one call to warning so that blank lines aren't
+        inserted between each line of text.  */
+      warning (_("\n"
+                "Could not load the Python gdb module from `%s'.\n"
+                "Limited Python support is available from the _gdb module.\n"
+                "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
                 gdb_pythondir);
-      warning (_("Limited Python support is available from the _gdb module."));
       do_cleanups (cleanup);
       return;
     }
 
-  if (PyModule_AddObject (m, "gdb", gdb_python_module))
+  if (gdb_pymodule_addobject (m, "gdb", gdb_python_module) < 0)
     goto fail;
 
   /* Keep the reference to gdb_python_module since it is in a global
This page took 0.050427 seconds and 4 git commands to generate.