public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* GDB 7.2 - Patch proposal for the use of GDB/Python scripts on MinGW
@ 2010-09-10 13:58 Serge CHATROUX
  2010-09-10 15:47 ` Tom Tromey
  2010-09-11 16:34 ` Daniel Jacobowitz
  0 siblings, 2 replies; 8+ messages in thread
From: Serge CHATROUX @ 2010-09-10 13:58 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2502 bytes --]

I compile gdb 7.2 using MINGW tools and I enable the Python scripting support by giving the path to a Python for Windows install directory.

    gdb/configure --with-x=no --with-python=/c/DATA/tmp/build/MINGW32_NT-5.1/install-python 

Python for windows
------------------

I get the python 2.6.x from http://www.python.org/download/

I have to make some modifications in Python distribution because header files and libraries directories are not the same between Linux and Windows Python distribution.
In order to fit gdb sources (Linux-based), I modify the Python distribution:

- Go to python windows install directory
- Create directory include\python2.6
- Move files from include to include\python2.6
- Copy file libs\libpython26.a to lib directory and rename it to libpython2.6.a

In the future, it could be useful that GDB sources (configure, Makefile.in) fit also Python  Windows-specific packaging.

GDB modifications
-----------------

I also modify the gdb/python sources to solve the following issues:

- Add some tests to disable Python scripting if Python is not available on the host at runtime. 
  I detect if the PYTHONHOME variable is set. This enhancement is maybe not useful for everybody.
- I have to modify some gdb python files because the files 'python-function.c', python-cmd.c (...) define some 'static PyTypeObject'.
  The field 'tp_new' of these static variables is statically initialized with the python 'PyType_GenericNew' function. 
  In Windows port of Python, this function is declared as a dllimport one and cannot be copied at compilation time in a static variable.
  For these files, I modified the source code
      - to initialize the 'tp_new' fields to '0' in the static variables and
      - to affect the proper 'PyType_GenericNew' value in the 'gdbpy_initialize_commands', 'gdbpy_initialize_frames' (...). 
- I also modified python-config.py to get python shared library path when Python is compiled in shared library mode. Otherwise the shared library is taken from Linux distribution, not from the Python given by --with-python flag.
- When sourcing a Python script using 'source file.py' command, we cannot use 'PyRun_SimpleFile' because Python may not be compiled using the same MSVCRT DLL than GDB, so the FILE* stream will not be known in this DLL.
  This generates an error when the MSVCRT will try to lock the file handle.

I hope that this patch will be useful and that I followed the gdb coding rules.

Regards

[-- Attachment #2: gdb.python-for-mingw.diff --]
[-- Type: application/octet-stream, Size: 15076 bytes --]

diff -cp python-gdb-7.2/py-cmd.c python-gdb/py-cmd.c
*** python-gdb-7.2/py-cmd.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-cmd.c	Fri Sep 10 11:08:00 2010
*************** gdbpy_initialize_commands (void)
*** 526,531 ****
--- 526,541 ----
    if (PyType_Ready (&cmdpy_object_type) < 0)
      return;
  
+ #ifdef __MINGW32__
+   /* 
+ 	 I have to modify some gdb python files because the files 'python-function.c', python-cmd.c and python-frame.c define some 'static PyTypeObject'.
+      The field 'tp_new' of these static variables statically initialized with the python 'PyType_GenericNew' function. 
+      In Windows port of Python, this function is declared as a dllimport function and can not be copied at compilation time in a static variable.
+      For these 3 files, I modify the source code to initialize the 'tp_new' fields to '0' in the static variables and to affect the proper 'PyType_GenericNew' value in the 'gdbpy_initialize_commands', 'gdbpy_initialize_frames' and 'gdbpy_initialize_functions'.
+   */
+   cmdpy_object_type.tp_new = PyType_GenericNew;
+ #endif
+ 
    /* Note: alias and user are special; pseudo appears to be unused,
       and there is no reason to expose tui or xdb, I think.  */
    if (PyModule_AddIntConstant (gdb_module, "COMMAND_NONE", no_class) < 0
*************** static PyTypeObject cmdpy_object_type =
*** 610,616 ****
--- 620,630 ----
    0,				  /* tp_dictoffset */
    cmdpy_init,			  /* tp_init */
    0,				  /* tp_alloc */
+ #ifdef __MINGW32__
+   0				  /* tp_new */
+ #else
    PyType_GenericNew		  /* tp_new */
+ #endif
  };
  
  \f
diff -cp python-gdb-7.2/py-frame.c python-gdb/py-frame.c
*** python-gdb-7.2/py-frame.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-frame.c	Fri Sep 10 11:08:00 2010
*************** gdbpy_initialize_frames (void)
*** 568,573 ****
--- 568,583 ----
    if (PyType_Ready (&frame_object_type) < 0)
      return;
  
+ #ifdef __MINGW32__
+   /* 
+      I have to modify some gdb python files because the files 'python-function.c', python-cmd.c and python-frame.c define some 'static PyTypeObject'.
+      The field 'tp_new' of these static variables statically initialized with the python 'PyType_GenericNew' function. 
+      In Windows port of Python, this function is declared as a dllimport function and can not be copied at compilation time in a static variable.
+      For these 3 files, I modify the source code to initialize the 'tp_new' fields to '0' in the static variables and to affect the proper 'PyType_GenericNew' value in the 'gdbpy_initialize_commands', 'gdbpy_initialize_frames' and 'gdbpy_initialize_functions'.
+   */
+   frame_object_type.tp_new = PyType_GenericNew;
+ #endif
+ 
    /* Note: These would probably be best exposed as class attributes of Frame,
       but I don't know how to do it except by messing with the type's dictionary.
       That seems too messy.  */
*************** static PyTypeObject frame_object_type = 
*** 672,676 ****
--- 682,690 ----
    0,				  /* tp_dictoffset */
    0,				  /* tp_init */
    0,				  /* tp_alloc */
+ #ifdef __MINGW32__
+   0				  /* tp_new */
+ #else
    PyType_GenericNew		  /* tp_new */
+ #endif
  };
diff -cp python-gdb-7.2/py-function.c python-gdb/py-function.c
*** python-gdb-7.2/py-function.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-function.c	Fri Sep 10 11:08:00 2010
*************** gdbpy_initialize_functions (void)
*** 130,135 ****
--- 130,145 ----
    if (PyType_Ready (&fnpy_object_type) < 0)
      return;
  
+ #ifdef __MINGW32__
+   /* 
+      I have to modify some gdb python files because the files 'python-function.c', python-cmd.c and python-frame.c define some 'static PyTypeObject'.
+      The field 'tp_new' of these static variables statically initialized with the python 'PyType_GenericNew' function. 
+      In Windows port of Python, this function is declared as a dllimport function and can not be copied at compilation time in a static variable.
+      For these 3 files, I modify the source code to initialize the 'tp_new' fields to '0' in the static variables and to affect the proper 'PyType_GenericNew' value in the 'gdbpy_initialize_commands', 'gdbpy_initialize_frames' and 'gdbpy_initialize_functions'.
+   */
+   fnpy_object_type.tp_new = PyType_GenericNew;
+ #endif
+ 
    Py_INCREF (&fnpy_object_type);
    PyModule_AddObject (gdb_module, "Function", (PyObject *) &fnpy_object_type);
  }
*************** static PyTypeObject fnpy_object_type =
*** 176,180 ****
--- 186,194 ----
    0,				  /* tp_dictoffset */
    fnpy_init,			  /* tp_init */
    0,				  /* tp_alloc */
+ #ifdef __MINGW32__
+   0				  /* tp_new */
+ #else
    PyType_GenericNew		  /* tp_new */
+ #endif
  };
diff -cp python-gdb-7.2/py-inferior.c python-gdb/py-inferior.c
*** python-gdb-7.2/py-inferior.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-inferior.c	Fri Sep 10 11:08:00 2010
*************** gdbpy_initialize_inferior (void)
*** 593,598 ****
--- 593,608 ----
    if (PyType_Ready (&membuf_object_type) < 0)
      return;
  
+ #ifdef __MINGW32__
+   /* 
+      I have to modify some gdb python files because the files 'python-function.c', python-cmd.c and python-frame.c define some 'static PyTypeObject'.
+      The field 'tp_new' of these static variables statically initialized with the python 'PyType_GenericNew' function. 
+      In Windows port of Python, this function is declared as a dllimport function and can not be copied at compilation time in a static variable.
+      For these 3 files, I modify the source code to initialize the 'tp_new' fields to '0' in the static variables and to affect the proper 'PyType_GenericNew' value in the 'gdbpy_initialize_commands', 'gdbpy_initialize_frames' and 'gdbpy_initialize_functions'.
+   */
+   membuf_object_type.tp_new = PyType_GenericNew;
+ #endif
+ 
    Py_INCREF (&membuf_object_type);
    PyModule_AddObject (gdb_module, "Membuf", (PyObject *)
  		      &membuf_object_type);
*************** static PyTypeObject membuf_object_type =
*** 724,728 ****
--- 734,742 ----
    0,				  /* tp_dictoffset */
    0,				  /* tp_init */
    0,				  /* tp_alloc */
+ #ifdef __MINGW32__
+   0				  /* tp_new */
+ #else
    PyType_GenericNew		  /* tp_new */
+ #endif
  };
diff -cp python-gdb-7.2/py-param.c python-gdb/py-param.c
*** python-gdb-7.2/py-param.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-param.c	Fri Sep 10 11:08:00 2010
*************** gdbpy_initialize_parameters (void)
*** 555,560 ****
--- 555,570 ----
    if (PyType_Ready (&parmpy_object_type) < 0)
      return;
  
+ #ifdef __MINGW32__
+   /* 
+      I have to modify some gdb python files because the files 'python-function.c', python-cmd.c and python-frame.c define some 'static PyTypeObject'.
+      The field 'tp_new' of these static variables statically initialized with the python 'PyType_GenericNew' function. 
+      In Windows port of Python, this function is declared as a dllimport function and can not be copied at compilation time in a static variable.
+      For these 3 files, I modify the source code to initialize the 'tp_new' fields to '0' in the static variables and to affect the proper 'PyType_GenericNew' value in the 'gdbpy_initialize_commands', 'gdbpy_initialize_frames' and 'gdbpy_initialize_functions'.
+   */
+   parmpy_object_type.tp_new = PyType_GenericNew;
+ #endif
+ 
    set_doc_cst = PyString_FromString ("set_doc");
    if (! set_doc_cst)
      return;
*************** static PyTypeObject parmpy_object_type =
*** 617,621 ****
--- 627,635 ----
    0,				  /* tp_dictoffset */
    parmpy_init,			  /* tp_init */
    0,				  /* tp_alloc */
+ #ifdef __MINGW32__
+   0				  /* tp_new */
+ #else
    PyType_GenericNew		  /* tp_new */
+ #endif
  };
diff -cp python-gdb-7.2/py-prettyprint.c python-gdb/py-prettyprint.c
*** python-gdb-7.2/py-prettyprint.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-prettyprint.c	Fri Sep 10 11:08:00 2010
*************** apply_val_pretty_printer (struct type *t
*** 617,622 ****
--- 617,627 ----
    struct cleanup *cleanups;
    int result = 0;
    int is_py_none = 0;
+ 
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+   if (have_python() == 0)
+     return 0;
+ 
    cleanups = ensure_python_env (gdbarch, language);
  
    /* Instantiate the printer.  */
diff -cp python-gdb-7.2/py-value.c python-gdb/py-value.c
*** python-gdb-7.2/py-value.c	Fri Sep 10 09:27:40 2010
--- python-gdb/py-value.c	Fri Sep 10 11:08:00 2010
*************** preserve_python_values (struct objfile *
*** 158,163 ****
--- 158,167 ----
  {
    value_object *iter;
  
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+   if (have_python() == 0)
+     return;
+ 
    for (iter = values_in_python; iter; iter = iter->next)
      preserve_one_value (iter->value, objfile, copied_types);
  }
diff -cp python-gdb-7.2/python-config.py python-gdb/python-config.py
*** python-gdb-7.2/python-config.py	Fri Sep 10 09:27:40 2010
--- python-gdb/python-config.py	Fri Sep 10 11:08:00 2010
*************** elif opt in ('--libs', '--ldflags'):
*** 50,54 ****
--- 50,59 ----
      # shared library in prefix/lib/.
      if opt == '--ldflags' and not getvar('Py_ENABLE_SHARED'):
          libs.insert(0, '-L' + getvar('LIBPL'))
+ 
+     # Get python shared library path
+     if opt == '--ldflags' and getvar('Py_ENABLE_SHARED'):
+         libs.insert(0, '-L' + getvar('LIBDIR'))        
+         
      print ' '.join(libs)
  
diff -cp python-gdb-7.2/python-internal.h python-gdb/python-internal.h
*** python-gdb-7.2/python-internal.h	Fri Sep 10 09:27:40 2010
--- python-gdb/python-internal.h	Fri Sep 10 11:08:00 2010
***************
*** 26,31 ****
--- 26,34 ----
     needed by pyport.h.  */
  #include <stdint.h>
  
+ /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+ int have_python();
+ 
  /* /usr/include/features.h on linux systems will define _POSIX_C_SOURCE
     if it sees _GNU_SOURCE (which config.h will define).
     pyconfig.h defines _POSIX_C_SOURCE to a different value than
diff -cp python-gdb-7.2/python.c python-gdb/python.c
*** python-gdb-7.2/python.c	Fri Sep 10 09:27:40 2010
--- python-gdb/python.c	Fri Sep 10 11:08:00 2010
*************** PyObject *gdbpy_enabled_cst;
*** 61,66 ****
--- 61,73 ----
  /* The GdbError exception.  */
  PyObject *gdbpy_gdberror_exc;
  
+ /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+ int havePython = 1;
+ int have_python()
+ {
+   return havePython;
+ }
+ 
  /* Architecture and language to be used in callbacks from
     the Python interpreter.  */
  struct gdbarch *python_gdbarch;
*************** eval_python_from_control_command (struct
*** 147,152 ****
--- 154,165 ----
    char *script;
    struct cleanup *cleanup;
  
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+   if (havePython == 0)
+     {
+       return;
+     }
+ 
    if (cmd->body_count != 1)
      error (_("Invalid \"python\" block structure."));
  
*************** python_command (char *arg, int from_tty)
*** 171,176 ****
--- 184,209 ----
  {
    struct cleanup *cleanup;
  
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+ #ifdef HAVE_PYTHON
+   if (havePython == 0)
+     {
+       while (arg && *arg && isspace (*arg))
+ 	++arg;
+       if (arg && *arg)
+ 	error (_("Python scripting is not supported when the PYTHONHOME variable is undefined."));
+       else
+ 	{
+ 	  struct command_line *l = get_command_line (python_control, "");
+ 	  struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
+ 	  execute_control_command_untraced (l);
+ 	  do_cleanups (cleanups);
+ 	}
+       return;
+     }
+ #endif /* HAVE_PYTHON */
+ 
+ 
    cleanup = ensure_python_env (get_current_arch (), current_language);
    while (arg && *arg && isspace (*arg))
      ++arg;
*************** source_python_script (FILE *stream, cons
*** 403,413 ****
--- 436,467 ----
  {
    struct cleanup *cleanup;
  
+ #ifdef HAVE_PYTHON
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+   if (havePython == 0)
+     {
+       error (_("Python scripting is not supported when the PYTHONHOME variable is undefined."));
+       return;
+     }
+ #endif /* HAVE_PYTHON */
+ 
    cleanup = ensure_python_env (get_current_arch (), current_language);
  
+ #ifdef __MINGW32__
+   {
+     /* We can not use 'PyRun_SimpleFile' because Python may not be compiled using the same MSVCRT DLL than GDB
+        so the FILE* stream will not be known in this DLL. This generate an error when the MSVCRT will try to lock the file handle. */
+     PyObject* PyFileObject = PyFile_FromString( (char*) file, "r"); 
+     if (PyFileObject == NULL)
+       error (_("Could not source file %s."), file);
+     PyRun_SimpleFile(PyFile_AsFile(PyFileObject), file); 
+     Py_DECREF(PyFileObject);
+   }
+ #else
    /* Note: If an exception occurs python will print the traceback and
       clear the error indicator.  */
    PyRun_SimpleFile (stream, file);
+ #endif
  
    do_cleanups (cleanup);
  }
*************** source_python_script_for_objfile (struct
*** 516,524 ****
--- 570,590 ----
    cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
    gdbpy_current_objfile = objfile;
  
+ #ifdef __MINGW32__
+   {
+     /* We can not use 'PyRun_SimpleFile' because Python may not be compiled using the same MSVCRT DLL than GDB
+        so the FILE* stream will not be known in this DLL. This generate an error when the MSVCRT will try to lock the file handle. */
+     PyObject* PyFileObject = PyFile_FromString( (char*) file, "r"); 
+     if (PyFileObject == NULL)
+       error (_("Could not source file %s."), file);
+     PyRun_SimpleFile(PyFile_AsFile(PyFileObject), file); 
+     Py_DECREF(PyFileObject);
+   }
+ #else
    /* Note: If an exception occurs python will print the traceback and
       clear the error indicator.  */
    PyRun_SimpleFile (stream, file);
+ #endif
  
    do_cleanups (cleanups);
    gdbpy_current_objfile = NULL;
*************** extern initialize_file_ftype _initialize
*** 633,638 ****
--- 699,719 ----
  void
  _initialize_python (void)
  {
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+ #ifdef HAVE_PYTHON
+   if (getenv( "PYTHONHOME") == NULL)
+     {
+        havePython = 0;
+        add_com ("python", class_obscure, python_command,
+ 		_("\
+ Evaluate a Python command.\n\
+ \n\
+ Python scripting is not supported when the PYTHONHOME variable is undefined.")
+ 		);
+     }
+   else
+ #endif /* HAVE_PYTHON */
+ 
    add_com ("python", class_obscure, python_command,
  #ifdef HAVE_PYTHON
  	   _("\
*************** Enables or disables printing of Python s
*** 683,688 ****
--- 764,778 ----
    Py_SetProgramName (concat (ldirname (python_libdir), SLASH_STRING, "bin",
  			     SLASH_STRING, "python", NULL));
  #endif
+ 
+   /* Do not enable python scripting if the PYTHONHOME variable is undefined */
+ #ifdef HAVE_PYTHON
+   if (havePython == 0)
+     {
+       return;
+     }
+ #endif /* HAVE_PYTHON */
+ 
  
    Py_Initialize ();
    PyEval_InitThreads ();

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-09-21 21:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-10 13:58 GDB 7.2 - Patch proposal for the use of GDB/Python scripts on MinGW Serge CHATROUX
2010-09-10 15:47 ` Tom Tromey
2010-09-13 17:09   ` Christophe Lyon
2010-09-13 17:41     ` Serge CHATROUX
2010-09-13 19:15       ` Tom Tromey
2010-09-17 14:42         ` Serge CHATROUX
2010-09-21 22:50           ` Tom Tromey
2010-09-11 16:34 ` Daniel Jacobowitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).