OLD | NEW |
1 /* General python/gdb code | 1 /* General python/gdb code |
2 | 2 |
3 Copyright (C) 2008-2012 Free Software Foundation, Inc. | 3 Copyright (C) 2008-2013 Free Software Foundation, Inc. |
4 | 4 |
5 This file is part of GDB. | 5 This file is part of GDB. |
6 | 6 |
7 This program is free software; you can redistribute it and/or modify | 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 | 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 | 9 the Free Software Foundation; either version 3 of the License, or |
10 (at your option) any later version. | 10 (at your option) any later version. |
11 | 11 |
12 This program is distributed in the hope that it will be useful, | 12 This program is distributed in the hope that it will be useful, |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 11 matching lines...) Expand all Loading... |
25 #include "gdbcmd.h" | 25 #include "gdbcmd.h" |
26 #include "progspace.h" | 26 #include "progspace.h" |
27 #include "objfiles.h" | 27 #include "objfiles.h" |
28 #include "value.h" | 28 #include "value.h" |
29 #include "language.h" | 29 #include "language.h" |
30 #include "exceptions.h" | 30 #include "exceptions.h" |
31 #include "event-loop.h" | 31 #include "event-loop.h" |
32 #include "serial.h" | 32 #include "serial.h" |
33 #include "readline/tilde.h" | 33 #include "readline/tilde.h" |
34 #include "python.h" | 34 #include "python.h" |
| 35 #include "cli/cli-utils.h" |
35 | 36 |
36 #include <ctype.h> | 37 #include <ctype.h> |
37 | 38 |
38 /* Declared constants and enum for python stack printing. */ | 39 /* Declared constants and enum for python stack printing. */ |
39 static const char python_excp_none[] = "none"; | 40 static const char python_excp_none[] = "none"; |
40 static const char python_excp_full[] = "full"; | 41 static const char python_excp_full[] = "full"; |
41 static const char python_excp_message[] = "message"; | 42 static const char python_excp_message[] = "message"; |
42 | 43 |
43 /* "set python print-stack" choices. */ | 44 /* "set python print-stack" choices. */ |
44 static const char *const python_excp_enums[] = | 45 static const char *const python_excp_enums[] = |
(...skipping 18 matching lines...) Expand all Loading... |
63 #include "top.h" | 64 #include "top.h" |
64 #include "solib.h" | 65 #include "solib.h" |
65 #include "python-internal.h" | 66 #include "python-internal.h" |
66 #include "linespec.h" | 67 #include "linespec.h" |
67 #include "source.h" | 68 #include "source.h" |
68 #include "version.h" | 69 #include "version.h" |
69 #include "target.h" | 70 #include "target.h" |
70 #include "gdbthread.h" | 71 #include "gdbthread.h" |
71 #include "observer.h" | 72 #include "observer.h" |
72 #include "interps.h" | 73 #include "interps.h" |
| 74 #include "event-top.h" |
| 75 |
| 76 /* True if Python has been successfully initialized, false |
| 77 otherwise. */ |
| 78 |
| 79 int gdb_python_initialized; |
73 | 80 |
74 static PyMethodDef GdbMethods[]; | 81 static PyMethodDef GdbMethods[]; |
75 | 82 |
| 83 #ifdef IS_PY3K |
| 84 static struct PyModuleDef GdbModuleDef; |
| 85 #endif |
| 86 |
76 PyObject *gdb_module; | 87 PyObject *gdb_module; |
| 88 PyObject *gdb_python_module; |
77 | 89 |
78 /* Some string constants we may wish to use. */ | 90 /* Some string constants we may wish to use. */ |
79 PyObject *gdbpy_to_string_cst; | 91 PyObject *gdbpy_to_string_cst; |
80 PyObject *gdbpy_children_cst; | 92 PyObject *gdbpy_children_cst; |
81 PyObject *gdbpy_display_hint_cst; | 93 PyObject *gdbpy_display_hint_cst; |
82 PyObject *gdbpy_doc_cst; | 94 PyObject *gdbpy_doc_cst; |
83 PyObject *gdbpy_enabled_cst; | 95 PyObject *gdbpy_enabled_cst; |
84 PyObject *gdbpy_value_cst; | 96 PyObject *gdbpy_value_cst; |
85 | 97 |
86 /* The GdbError exception. */ | 98 /* The GdbError exception. */ |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 143 |
132 /* Called before entering the Python interpreter to install the | 144 /* Called before entering the Python interpreter to install the |
133 current language and architecture to be used for Python values. */ | 145 current language and architecture to be used for Python values. */ |
134 | 146 |
135 struct cleanup * | 147 struct cleanup * |
136 ensure_python_env (struct gdbarch *gdbarch, | 148 ensure_python_env (struct gdbarch *gdbarch, |
137 const struct language_defn *language) | 149 const struct language_defn *language) |
138 { | 150 { |
139 struct python_env *env = xmalloc (sizeof *env); | 151 struct python_env *env = xmalloc (sizeof *env); |
140 | 152 |
| 153 /* We should not ever enter Python unless initialized. */ |
| 154 if (!gdb_python_initialized) |
| 155 error (_("Python not initialized")); |
| 156 |
141 env->state = PyGILState_Ensure (); | 157 env->state = PyGILState_Ensure (); |
142 env->gdbarch = python_gdbarch; | 158 env->gdbarch = python_gdbarch; |
143 env->language = python_language; | 159 env->language = python_language; |
144 | 160 |
145 python_gdbarch = gdbarch; | 161 python_gdbarch = gdbarch; |
146 python_language = language; | 162 python_language = language; |
147 | 163 |
148 /* Save it and ensure ! PyErr_Occurred () afterwards. */ | 164 /* Save it and ensure ! PyErr_Occurred () afterwards. */ |
149 PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback); | 165 PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback); |
150 | 166 |
151 return make_cleanup (restore_python_env, env); | 167 return make_cleanup (restore_python_env, env); |
152 } | 168 } |
153 | 169 |
| 170 /* Clear the quit flag. */ |
| 171 |
| 172 void |
| 173 clear_quit_flag (void) |
| 174 { |
| 175 /* This clears the flag as a side effect. */ |
| 176 PyOS_InterruptOccurred (); |
| 177 } |
| 178 |
| 179 /* Set the quit flag. */ |
| 180 |
| 181 void |
| 182 set_quit_flag (void) |
| 183 { |
| 184 PyErr_SetInterrupt (); |
| 185 } |
| 186 |
| 187 /* Return true if the quit flag has been set, false otherwise. */ |
| 188 |
| 189 int |
| 190 check_quit_flag (void) |
| 191 { |
| 192 return PyOS_InterruptOccurred (); |
| 193 } |
| 194 |
| 195 /* Evaluate a Python command like PyRun_SimpleString, but uses |
| 196 Py_single_input which prints the result of expressions, and does |
| 197 not automatically print the stack on errors. */ |
| 198 |
| 199 static int |
| 200 eval_python_command (const char *command) |
| 201 { |
| 202 PyObject *m, *d, *v; |
| 203 |
| 204 m = PyImport_AddModule ("__main__"); |
| 205 if (m == NULL) |
| 206 return -1; |
| 207 |
| 208 d = PyModule_GetDict (m); |
| 209 if (d == NULL) |
| 210 return -1; |
| 211 v = PyRun_StringFlags (command, Py_single_input, d, d, NULL); |
| 212 if (v == NULL) |
| 213 return -1; |
| 214 |
| 215 Py_DECREF (v); |
| 216 #ifndef IS_PY3K |
| 217 if (Py_FlushLine ()) |
| 218 PyErr_Clear (); |
| 219 #endif |
| 220 |
| 221 return 0; |
| 222 } |
| 223 |
| 224 /* Implementation of the gdb "python-interactive" command. */ |
| 225 |
| 226 static void |
| 227 python_interactive_command (char *arg, int from_tty) |
| 228 { |
| 229 struct cleanup *cleanup; |
| 230 int err; |
| 231 |
| 232 cleanup = make_cleanup_restore_integer (&interpreter_async); |
| 233 interpreter_async = 0; |
| 234 |
| 235 arg = skip_spaces (arg); |
| 236 |
| 237 ensure_python_env (get_current_arch (), current_language); |
| 238 |
| 239 if (arg && *arg) |
| 240 { |
| 241 int len = strlen (arg); |
| 242 char *script = xmalloc (len + 2); |
| 243 |
| 244 strcpy (script, arg); |
| 245 script[len] = '\n'; |
| 246 script[len + 1] = '\0'; |
| 247 err = eval_python_command (script); |
| 248 xfree (script); |
| 249 } |
| 250 else |
| 251 { |
| 252 err = PyRun_InteractiveLoop (instream, "<stdin>"); |
| 253 dont_repeat (); |
| 254 } |
| 255 |
| 256 if (err) |
| 257 { |
| 258 gdbpy_print_stack (); |
| 259 error (_("Error while executing Python code.")); |
| 260 } |
| 261 |
| 262 do_cleanups (cleanup); |
| 263 } |
| 264 |
154 /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run | 265 /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run |
155 named FILENAME. | 266 named FILENAME. |
156 | 267 |
157 On Windows hosts few users would build Python themselves (this is no | 268 On Windows hosts few users would build Python themselves (this is no |
158 trivial task on this platform), and thus use binaries built by | 269 trivial task on this platform), and thus use binaries built by |
159 someone else instead. There may happen situation where the Python | 270 someone else instead. There may happen situation where the Python |
160 library and GDB are using two different versions of the C runtime | 271 library and GDB are using two different versions of the C runtime |
161 library. Python, being built with VC, would use one version of the | 272 library. Python, being built with VC, would use one version of the |
162 msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll. | 273 msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll. |
163 A FILE * from one runtime does not necessarily operate correctly in | 274 A FILE * from one runtime does not necessarily operate correctly in |
(...skipping 20 matching lines...) Expand all Loading... |
184 open the file, we need to expand any tilde in the path first. */ | 295 open the file, we need to expand any tilde in the path first. */ |
185 full_path = tilde_expand (filename); | 296 full_path = tilde_expand (filename); |
186 cleanup = make_cleanup (xfree, full_path); | 297 cleanup = make_cleanup (xfree, full_path); |
187 python_file = PyFile_FromString (full_path, "r"); | 298 python_file = PyFile_FromString (full_path, "r"); |
188 if (! python_file) | 299 if (! python_file) |
189 { | 300 { |
190 do_cleanups (cleanup); | 301 do_cleanups (cleanup); |
191 gdbpy_print_stack (); | 302 gdbpy_print_stack (); |
192 error (_("Error while opening file: %s"), full_path); | 303 error (_("Error while opening file: %s"), full_path); |
193 } | 304 } |
194 | 305 |
195 make_cleanup_py_decref (python_file); | 306 make_cleanup_py_decref (python_file); |
196 PyRun_SimpleFile (PyFile_AsFile (python_file), filename); | 307 PyRun_SimpleFile (PyFile_AsFile (python_file), filename); |
197 do_cleanups (cleanup); | 308 do_cleanups (cleanup); |
198 | 309 |
199 #endif /* _WIN32 */ | 310 #endif /* _WIN32 */ |
200 } | 311 } |
201 | 312 |
202 /* Given a command_line, return a command string suitable for passing | 313 /* Given a command_line, return a command string suitable for passing |
203 to Python. Lines in the string are separated by newlines. The | 314 to Python. Lines in the string are separated by newlines. The |
204 return value is allocated using xmalloc and the caller is | 315 return value is allocated using xmalloc and the caller is |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 static void | 369 static void |
259 python_command (char *arg, int from_tty) | 370 python_command (char *arg, int from_tty) |
260 { | 371 { |
261 struct cleanup *cleanup; | 372 struct cleanup *cleanup; |
262 | 373 |
263 cleanup = ensure_python_env (get_current_arch (), current_language); | 374 cleanup = ensure_python_env (get_current_arch (), current_language); |
264 | 375 |
265 make_cleanup_restore_integer (&interpreter_async); | 376 make_cleanup_restore_integer (&interpreter_async); |
266 interpreter_async = 0; | 377 interpreter_async = 0; |
267 | 378 |
268 while (arg && *arg && isspace (*arg)) | 379 arg = skip_spaces (arg); |
269 ++arg; | |
270 if (arg && *arg) | 380 if (arg && *arg) |
271 { | 381 { |
272 if (PyRun_SimpleString (arg)) | 382 if (PyRun_SimpleString (arg)) |
273 error (_("Error while executing Python code.")); | 383 error (_("Error while executing Python code.")); |
274 } | 384 } |
275 else | 385 else |
276 { | 386 { |
277 struct command_line *l = get_command_line (python_control, ""); | 387 struct command_line *l = get_command_line (python_control, ""); |
278 | 388 |
279 make_cleanup_free_command_lines (&l); | 389 make_cleanup_free_command_lines (&l); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 /* Fall through. */ | 443 /* Fall through. */ |
334 case var_zinteger: | 444 case var_zinteger: |
335 return PyLong_FromLong (* (int *) var); | 445 return PyLong_FromLong (* (int *) var); |
336 | 446 |
337 case var_uinteger: | 447 case var_uinteger: |
338 { | 448 { |
339 unsigned int val = * (unsigned int *) var; | 449 unsigned int val = * (unsigned int *) var; |
340 | 450 |
341 if (val == UINT_MAX) | 451 if (val == UINT_MAX) |
342 Py_RETURN_NONE; | 452 Py_RETURN_NONE; |
error: old chunk mismatch |
None
OLD | NEW |