15796c8dcSSimon Schubert /* General python/gdb code
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2008-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "arch-utils.h"
225796c8dcSSimon Schubert #include "command.h"
235796c8dcSSimon Schubert #include "ui-out.h"
245796c8dcSSimon Schubert #include "cli/cli-script.h"
255796c8dcSSimon Schubert #include "gdbcmd.h"
26cf7f2e2dSJohn Marino #include "progspace.h"
275796c8dcSSimon Schubert #include "objfiles.h"
285796c8dcSSimon Schubert #include "value.h"
295796c8dcSSimon Schubert #include "language.h"
30cf7f2e2dSJohn Marino #include "exceptions.h"
31c50c785cSJohn Marino #include "event-loop.h"
32c50c785cSJohn Marino #include "serial.h"
33a45ae5f8SJohn Marino #include "readline/tilde.h"
34c50c785cSJohn Marino #include "python.h"
35*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
365796c8dcSSimon Schubert
375796c8dcSSimon Schubert #include <ctype.h>
385796c8dcSSimon Schubert
39a45ae5f8SJohn Marino /* Declared constants and enum for python stack printing. */
40a45ae5f8SJohn Marino static const char python_excp_none[] = "none";
41a45ae5f8SJohn Marino static const char python_excp_full[] = "full";
42a45ae5f8SJohn Marino static const char python_excp_message[] = "message";
43a45ae5f8SJohn Marino
44a45ae5f8SJohn Marino /* "set python print-stack" choices. */
45*ef5ccd6cSJohn Marino static const char *const python_excp_enums[] =
46a45ae5f8SJohn Marino {
47a45ae5f8SJohn Marino python_excp_none,
48a45ae5f8SJohn Marino python_excp_full,
49a45ae5f8SJohn Marino python_excp_message,
50a45ae5f8SJohn Marino NULL
51a45ae5f8SJohn Marino };
52a45ae5f8SJohn Marino
53a45ae5f8SJohn Marino /* The exception printing variable. 'full' if we want to print the
54a45ae5f8SJohn Marino error message and stack, 'none' if we want to print nothing, and
55a45ae5f8SJohn Marino 'message' if we only want to print the error message. 'message' is
56a45ae5f8SJohn Marino the default. */
57a45ae5f8SJohn Marino static const char *gdbpy_should_print_stack = python_excp_message;
585796c8dcSSimon Schubert
595796c8dcSSimon Schubert #ifdef HAVE_PYTHON
605796c8dcSSimon Schubert
615796c8dcSSimon Schubert #include "libiberty.h"
625796c8dcSSimon Schubert #include "cli/cli-decode.h"
635796c8dcSSimon Schubert #include "charset.h"
645796c8dcSSimon Schubert #include "top.h"
65c50c785cSJohn Marino #include "solib.h"
665796c8dcSSimon Schubert #include "python-internal.h"
67c50c785cSJohn Marino #include "linespec.h"
68c50c785cSJohn Marino #include "source.h"
695796c8dcSSimon Schubert #include "version.h"
705796c8dcSSimon Schubert #include "target.h"
715796c8dcSSimon Schubert #include "gdbthread.h"
72a45ae5f8SJohn Marino #include "observer.h"
73a45ae5f8SJohn Marino #include "interps.h"
74*ef5ccd6cSJohn Marino #include "event-top.h"
755796c8dcSSimon Schubert
765796c8dcSSimon Schubert static PyMethodDef GdbMethods[];
775796c8dcSSimon Schubert
78*ef5ccd6cSJohn Marino #ifdef IS_PY3K
79*ef5ccd6cSJohn Marino static struct PyModuleDef GdbModuleDef;
80*ef5ccd6cSJohn Marino #endif
81*ef5ccd6cSJohn Marino
825796c8dcSSimon Schubert PyObject *gdb_module;
83*ef5ccd6cSJohn Marino PyObject *gdb_python_module;
845796c8dcSSimon Schubert
855796c8dcSSimon Schubert /* Some string constants we may wish to use. */
865796c8dcSSimon Schubert PyObject *gdbpy_to_string_cst;
875796c8dcSSimon Schubert PyObject *gdbpy_children_cst;
885796c8dcSSimon Schubert PyObject *gdbpy_display_hint_cst;
895796c8dcSSimon Schubert PyObject *gdbpy_doc_cst;
90cf7f2e2dSJohn Marino PyObject *gdbpy_enabled_cst;
91c50c785cSJohn Marino PyObject *gdbpy_value_cst;
925796c8dcSSimon Schubert
93cf7f2e2dSJohn Marino /* The GdbError exception. */
94cf7f2e2dSJohn Marino PyObject *gdbpy_gdberror_exc;
955796c8dcSSimon Schubert
96c50c785cSJohn Marino /* The `gdb.error' base class. */
97c50c785cSJohn Marino PyObject *gdbpy_gdb_error;
98c50c785cSJohn Marino
99c50c785cSJohn Marino /* The `gdb.MemoryError' exception. */
100c50c785cSJohn Marino PyObject *gdbpy_gdb_memory_error;
101c50c785cSJohn Marino
1025796c8dcSSimon Schubert /* Architecture and language to be used in callbacks from
1035796c8dcSSimon Schubert the Python interpreter. */
1045796c8dcSSimon Schubert struct gdbarch *python_gdbarch;
1055796c8dcSSimon Schubert const struct language_defn *python_language;
1065796c8dcSSimon Schubert
1075796c8dcSSimon Schubert /* Restore global language and architecture and Python GIL state
1085796c8dcSSimon Schubert when leaving the Python interpreter. */
1095796c8dcSSimon Schubert
1105796c8dcSSimon Schubert struct python_env
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert PyGILState_STATE state;
1135796c8dcSSimon Schubert struct gdbarch *gdbarch;
1145796c8dcSSimon Schubert const struct language_defn *language;
115c50c785cSJohn Marino PyObject *error_type, *error_value, *error_traceback;
1165796c8dcSSimon Schubert };
1175796c8dcSSimon Schubert
1185796c8dcSSimon Schubert static void
restore_python_env(void * p)1195796c8dcSSimon Schubert restore_python_env (void *p)
1205796c8dcSSimon Schubert {
1215796c8dcSSimon Schubert struct python_env *env = (struct python_env *)p;
122cf7f2e2dSJohn Marino
123c50c785cSJohn Marino /* Leftover Python error is forbidden by Python Exception Handling. */
124c50c785cSJohn Marino if (PyErr_Occurred ())
125c50c785cSJohn Marino {
126c50c785cSJohn Marino /* This order is similar to the one calling error afterwards. */
127c50c785cSJohn Marino gdbpy_print_stack ();
128c50c785cSJohn Marino warning (_("internal error: Unhandled Python exception"));
129c50c785cSJohn Marino }
130c50c785cSJohn Marino
131c50c785cSJohn Marino PyErr_Restore (env->error_type, env->error_value, env->error_traceback);
132c50c785cSJohn Marino
1335796c8dcSSimon Schubert PyGILState_Release (env->state);
1345796c8dcSSimon Schubert python_gdbarch = env->gdbarch;
1355796c8dcSSimon Schubert python_language = env->language;
1365796c8dcSSimon Schubert xfree (env);
1375796c8dcSSimon Schubert }
1385796c8dcSSimon Schubert
1395796c8dcSSimon Schubert /* Called before entering the Python interpreter to install the
1405796c8dcSSimon Schubert current language and architecture to be used for Python values. */
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert struct cleanup *
ensure_python_env(struct gdbarch * gdbarch,const struct language_defn * language)1435796c8dcSSimon Schubert ensure_python_env (struct gdbarch *gdbarch,
1445796c8dcSSimon Schubert const struct language_defn *language)
1455796c8dcSSimon Schubert {
1465796c8dcSSimon Schubert struct python_env *env = xmalloc (sizeof *env);
1475796c8dcSSimon Schubert
1485796c8dcSSimon Schubert env->state = PyGILState_Ensure ();
1495796c8dcSSimon Schubert env->gdbarch = python_gdbarch;
1505796c8dcSSimon Schubert env->language = python_language;
1515796c8dcSSimon Schubert
1525796c8dcSSimon Schubert python_gdbarch = gdbarch;
1535796c8dcSSimon Schubert python_language = language;
1545796c8dcSSimon Schubert
155c50c785cSJohn Marino /* Save it and ensure ! PyErr_Occurred () afterwards. */
156c50c785cSJohn Marino PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback);
157c50c785cSJohn Marino
1585796c8dcSSimon Schubert return make_cleanup (restore_python_env, env);
1595796c8dcSSimon Schubert }
1605796c8dcSSimon Schubert
161*ef5ccd6cSJohn Marino /* Clear the quit flag. */
162a45ae5f8SJohn Marino
163*ef5ccd6cSJohn Marino void
clear_quit_flag(void)164*ef5ccd6cSJohn Marino clear_quit_flag (void)
165*ef5ccd6cSJohn Marino {
166*ef5ccd6cSJohn Marino /* This clears the flag as a side effect. */
167*ef5ccd6cSJohn Marino PyOS_InterruptOccurred ();
168*ef5ccd6cSJohn Marino }
169a45ae5f8SJohn Marino
170*ef5ccd6cSJohn Marino /* Set the quit flag. */
171a45ae5f8SJohn Marino
172*ef5ccd6cSJohn Marino void
set_quit_flag(void)173*ef5ccd6cSJohn Marino set_quit_flag (void)
174*ef5ccd6cSJohn Marino {
175*ef5ccd6cSJohn Marino PyErr_SetInterrupt ();
176*ef5ccd6cSJohn Marino }
177*ef5ccd6cSJohn Marino
178*ef5ccd6cSJohn Marino /* Return true if the quit flag has been set, false otherwise. */
179*ef5ccd6cSJohn Marino
180*ef5ccd6cSJohn Marino int
check_quit_flag(void)181*ef5ccd6cSJohn Marino check_quit_flag (void)
182*ef5ccd6cSJohn Marino {
183*ef5ccd6cSJohn Marino return PyOS_InterruptOccurred ();
184*ef5ccd6cSJohn Marino }
185*ef5ccd6cSJohn Marino
186*ef5ccd6cSJohn Marino /* Evaluate a Python command like PyRun_SimpleString, but uses
187*ef5ccd6cSJohn Marino Py_single_input which prints the result of expressions, and does
188*ef5ccd6cSJohn Marino not automatically print the stack on errors. */
189*ef5ccd6cSJohn Marino
190*ef5ccd6cSJohn Marino static int
eval_python_command(const char * command)191*ef5ccd6cSJohn Marino eval_python_command (const char *command)
192*ef5ccd6cSJohn Marino {
193*ef5ccd6cSJohn Marino PyObject *m, *d, *v;
194*ef5ccd6cSJohn Marino
195*ef5ccd6cSJohn Marino m = PyImport_AddModule ("__main__");
196*ef5ccd6cSJohn Marino if (m == NULL)
197*ef5ccd6cSJohn Marino return -1;
198*ef5ccd6cSJohn Marino
199*ef5ccd6cSJohn Marino d = PyModule_GetDict (m);
200*ef5ccd6cSJohn Marino if (d == NULL)
201*ef5ccd6cSJohn Marino return -1;
202*ef5ccd6cSJohn Marino v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
203*ef5ccd6cSJohn Marino if (v == NULL)
204*ef5ccd6cSJohn Marino return -1;
205*ef5ccd6cSJohn Marino
206*ef5ccd6cSJohn Marino Py_DECREF (v);
207*ef5ccd6cSJohn Marino #ifndef IS_PY3K
208*ef5ccd6cSJohn Marino if (Py_FlushLine ())
209*ef5ccd6cSJohn Marino PyErr_Clear ();
210*ef5ccd6cSJohn Marino #endif
211*ef5ccd6cSJohn Marino
212*ef5ccd6cSJohn Marino return 0;
213*ef5ccd6cSJohn Marino }
214*ef5ccd6cSJohn Marino
215*ef5ccd6cSJohn Marino /* Implementation of the gdb "python-interactive" command. */
216a45ae5f8SJohn Marino
217a45ae5f8SJohn Marino static void
python_interactive_command(char * arg,int from_tty)218*ef5ccd6cSJohn Marino python_interactive_command (char *arg, int from_tty)
219a45ae5f8SJohn Marino {
220*ef5ccd6cSJohn Marino struct cleanup *cleanup;
221*ef5ccd6cSJohn Marino int err;
222*ef5ccd6cSJohn Marino
223*ef5ccd6cSJohn Marino cleanup = make_cleanup_restore_integer (&interpreter_async);
224*ef5ccd6cSJohn Marino interpreter_async = 0;
225*ef5ccd6cSJohn Marino
226*ef5ccd6cSJohn Marino arg = skip_spaces (arg);
227*ef5ccd6cSJohn Marino
228*ef5ccd6cSJohn Marino ensure_python_env (get_current_arch (), current_language);
229*ef5ccd6cSJohn Marino
230*ef5ccd6cSJohn Marino if (arg && *arg)
231*ef5ccd6cSJohn Marino {
232*ef5ccd6cSJohn Marino int len = strlen (arg);
233*ef5ccd6cSJohn Marino char *script = xmalloc (len + 2);
234*ef5ccd6cSJohn Marino
235*ef5ccd6cSJohn Marino strcpy (script, arg);
236*ef5ccd6cSJohn Marino script[len] = '\n';
237*ef5ccd6cSJohn Marino script[len + 1] = '\0';
238*ef5ccd6cSJohn Marino err = eval_python_command (script);
239*ef5ccd6cSJohn Marino xfree (script);
240*ef5ccd6cSJohn Marino }
241*ef5ccd6cSJohn Marino else
242*ef5ccd6cSJohn Marino {
243*ef5ccd6cSJohn Marino err = PyRun_InteractiveLoop (instream, "<stdin>");
244*ef5ccd6cSJohn Marino dont_repeat ();
245*ef5ccd6cSJohn Marino }
246*ef5ccd6cSJohn Marino
247*ef5ccd6cSJohn Marino if (err)
248*ef5ccd6cSJohn Marino {
249*ef5ccd6cSJohn Marino gdbpy_print_stack ();
250*ef5ccd6cSJohn Marino error (_("Error while executing Python code."));
251*ef5ccd6cSJohn Marino }
252*ef5ccd6cSJohn Marino
253*ef5ccd6cSJohn Marino do_cleanups (cleanup);
254*ef5ccd6cSJohn Marino }
255*ef5ccd6cSJohn Marino
256*ef5ccd6cSJohn Marino /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
257*ef5ccd6cSJohn Marino named FILENAME.
258*ef5ccd6cSJohn Marino
259*ef5ccd6cSJohn Marino On Windows hosts few users would build Python themselves (this is no
260*ef5ccd6cSJohn Marino trivial task on this platform), and thus use binaries built by
261*ef5ccd6cSJohn Marino someone else instead. There may happen situation where the Python
262*ef5ccd6cSJohn Marino library and GDB are using two different versions of the C runtime
263*ef5ccd6cSJohn Marino library. Python, being built with VC, would use one version of the
264*ef5ccd6cSJohn Marino msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
265*ef5ccd6cSJohn Marino A FILE * from one runtime does not necessarily operate correctly in
266*ef5ccd6cSJohn Marino the other runtime.
267*ef5ccd6cSJohn Marino
268*ef5ccd6cSJohn Marino To work around this potential issue, we create on Windows hosts the
269*ef5ccd6cSJohn Marino FILE object using Python routines, thus making sure that it is
270*ef5ccd6cSJohn Marino compatible with the Python library. */
271*ef5ccd6cSJohn Marino
272*ef5ccd6cSJohn Marino static void
python_run_simple_file(FILE * file,const char * filename)273*ef5ccd6cSJohn Marino python_run_simple_file (FILE *file, const char *filename)
274*ef5ccd6cSJohn Marino {
275*ef5ccd6cSJohn Marino #ifndef _WIN32
276*ef5ccd6cSJohn Marino
277*ef5ccd6cSJohn Marino PyRun_SimpleFile (file, filename);
278*ef5ccd6cSJohn Marino
279*ef5ccd6cSJohn Marino #else /* _WIN32 */
280*ef5ccd6cSJohn Marino
281a45ae5f8SJohn Marino char *full_path;
282a45ae5f8SJohn Marino PyObject *python_file;
283a45ae5f8SJohn Marino struct cleanup *cleanup;
284a45ae5f8SJohn Marino
285a45ae5f8SJohn Marino /* Because we have a string for a filename, and are using Python to
286a45ae5f8SJohn Marino open the file, we need to expand any tilde in the path first. */
287a45ae5f8SJohn Marino full_path = tilde_expand (filename);
288a45ae5f8SJohn Marino cleanup = make_cleanup (xfree, full_path);
289a45ae5f8SJohn Marino python_file = PyFile_FromString (full_path, "r");
290a45ae5f8SJohn Marino if (! python_file)
291a45ae5f8SJohn Marino {
292a45ae5f8SJohn Marino do_cleanups (cleanup);
293a45ae5f8SJohn Marino gdbpy_print_stack ();
294a45ae5f8SJohn Marino error (_("Error while opening file: %s"), full_path);
295a45ae5f8SJohn Marino }
296a45ae5f8SJohn Marino
297a45ae5f8SJohn Marino make_cleanup_py_decref (python_file);
298a45ae5f8SJohn Marino PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
299a45ae5f8SJohn Marino do_cleanups (cleanup);
300*ef5ccd6cSJohn Marino
301*ef5ccd6cSJohn Marino #endif /* _WIN32 */
302a45ae5f8SJohn Marino }
3035796c8dcSSimon Schubert
3045796c8dcSSimon Schubert /* Given a command_line, return a command string suitable for passing
3055796c8dcSSimon Schubert to Python. Lines in the string are separated by newlines. The
3065796c8dcSSimon Schubert return value is allocated using xmalloc and the caller is
3075796c8dcSSimon Schubert responsible for freeing it. */
3085796c8dcSSimon Schubert
3095796c8dcSSimon Schubert static char *
compute_python_string(struct command_line * l)3105796c8dcSSimon Schubert compute_python_string (struct command_line *l)
3115796c8dcSSimon Schubert {
3125796c8dcSSimon Schubert struct command_line *iter;
3135796c8dcSSimon Schubert char *script = NULL;
3145796c8dcSSimon Schubert int size = 0;
3155796c8dcSSimon Schubert int here;
3165796c8dcSSimon Schubert
3175796c8dcSSimon Schubert for (iter = l; iter; iter = iter->next)
3185796c8dcSSimon Schubert size += strlen (iter->line) + 1;
3195796c8dcSSimon Schubert
3205796c8dcSSimon Schubert script = xmalloc (size + 1);
3215796c8dcSSimon Schubert here = 0;
3225796c8dcSSimon Schubert for (iter = l; iter; iter = iter->next)
3235796c8dcSSimon Schubert {
3245796c8dcSSimon Schubert int len = strlen (iter->line);
325cf7f2e2dSJohn Marino
3265796c8dcSSimon Schubert strcpy (&script[here], iter->line);
3275796c8dcSSimon Schubert here += len;
3285796c8dcSSimon Schubert script[here++] = '\n';
3295796c8dcSSimon Schubert }
3305796c8dcSSimon Schubert script[here] = '\0';
3315796c8dcSSimon Schubert return script;
3325796c8dcSSimon Schubert }
3335796c8dcSSimon Schubert
3345796c8dcSSimon Schubert /* Take a command line structure representing a 'python' command, and
3355796c8dcSSimon Schubert evaluate its body using the Python interpreter. */
3365796c8dcSSimon Schubert
3375796c8dcSSimon Schubert void
eval_python_from_control_command(struct command_line * cmd)3385796c8dcSSimon Schubert eval_python_from_control_command (struct command_line *cmd)
3395796c8dcSSimon Schubert {
3405796c8dcSSimon Schubert int ret;
3415796c8dcSSimon Schubert char *script;
3425796c8dcSSimon Schubert struct cleanup *cleanup;
3435796c8dcSSimon Schubert
3445796c8dcSSimon Schubert if (cmd->body_count != 1)
3455796c8dcSSimon Schubert error (_("Invalid \"python\" block structure."));
3465796c8dcSSimon Schubert
3475796c8dcSSimon Schubert cleanup = ensure_python_env (get_current_arch (), current_language);
3485796c8dcSSimon Schubert
3495796c8dcSSimon Schubert script = compute_python_string (cmd->body_list[0]);
3505796c8dcSSimon Schubert ret = PyRun_SimpleString (script);
3515796c8dcSSimon Schubert xfree (script);
3525796c8dcSSimon Schubert if (ret)
3535796c8dcSSimon Schubert error (_("Error while executing Python code."));
3545796c8dcSSimon Schubert
3555796c8dcSSimon Schubert do_cleanups (cleanup);
3565796c8dcSSimon Schubert }
3575796c8dcSSimon Schubert
3585796c8dcSSimon Schubert /* Implementation of the gdb "python" command. */
3595796c8dcSSimon Schubert
3605796c8dcSSimon Schubert static void
python_command(char * arg,int from_tty)3615796c8dcSSimon Schubert python_command (char *arg, int from_tty)
3625796c8dcSSimon Schubert {
3635796c8dcSSimon Schubert struct cleanup *cleanup;
3645796c8dcSSimon Schubert
365cf7f2e2dSJohn Marino cleanup = ensure_python_env (get_current_arch (), current_language);
366a45ae5f8SJohn Marino
367a45ae5f8SJohn Marino make_cleanup_restore_integer (&interpreter_async);
368a45ae5f8SJohn Marino interpreter_async = 0;
369a45ae5f8SJohn Marino
370*ef5ccd6cSJohn Marino arg = skip_spaces (arg);
3715796c8dcSSimon Schubert if (arg && *arg)
3725796c8dcSSimon Schubert {
3735796c8dcSSimon Schubert if (PyRun_SimpleString (arg))
3745796c8dcSSimon Schubert error (_("Error while executing Python code."));
3755796c8dcSSimon Schubert }
3765796c8dcSSimon Schubert else
3775796c8dcSSimon Schubert {
3785796c8dcSSimon Schubert struct command_line *l = get_command_line (python_control, "");
379cf7f2e2dSJohn Marino
3805796c8dcSSimon Schubert make_cleanup_free_command_lines (&l);
3815796c8dcSSimon Schubert execute_control_command_untraced (l);
3825796c8dcSSimon Schubert }
3835796c8dcSSimon Schubert
3845796c8dcSSimon Schubert do_cleanups (cleanup);
3855796c8dcSSimon Schubert }
3865796c8dcSSimon Schubert
3875796c8dcSSimon Schubert
3885796c8dcSSimon Schubert
3895796c8dcSSimon Schubert /* Transform a gdb parameters's value into a Python value. May return
3905796c8dcSSimon Schubert NULL (and set a Python exception) on error. Helper function for
3915796c8dcSSimon Schubert get_parameter. */
392cf7f2e2dSJohn Marino PyObject *
gdbpy_parameter_value(enum var_types type,void * var)393cf7f2e2dSJohn Marino gdbpy_parameter_value (enum var_types type, void *var)
3945796c8dcSSimon Schubert {
395cf7f2e2dSJohn Marino switch (type)
3965796c8dcSSimon Schubert {
3975796c8dcSSimon Schubert case var_string:
3985796c8dcSSimon Schubert case var_string_noescape:
3995796c8dcSSimon Schubert case var_optional_filename:
4005796c8dcSSimon Schubert case var_filename:
4015796c8dcSSimon Schubert case var_enum:
4025796c8dcSSimon Schubert {
403cf7f2e2dSJohn Marino char *str = * (char **) var;
404cf7f2e2dSJohn Marino
4055796c8dcSSimon Schubert if (! str)
4065796c8dcSSimon Schubert str = "";
4075796c8dcSSimon Schubert return PyString_Decode (str, strlen (str), host_charset (), NULL);
4085796c8dcSSimon Schubert }
4095796c8dcSSimon Schubert
4105796c8dcSSimon Schubert case var_boolean:
4115796c8dcSSimon Schubert {
412cf7f2e2dSJohn Marino if (* (int *) var)
4135796c8dcSSimon Schubert Py_RETURN_TRUE;
4145796c8dcSSimon Schubert else
4155796c8dcSSimon Schubert Py_RETURN_FALSE;
4165796c8dcSSimon Schubert }
4175796c8dcSSimon Schubert
4185796c8dcSSimon Schubert case var_auto_boolean:
4195796c8dcSSimon Schubert {
420cf7f2e2dSJohn Marino enum auto_boolean ab = * (enum auto_boolean *) var;
421cf7f2e2dSJohn Marino
4225796c8dcSSimon Schubert if (ab == AUTO_BOOLEAN_TRUE)
4235796c8dcSSimon Schubert Py_RETURN_TRUE;
4245796c8dcSSimon Schubert else if (ab == AUTO_BOOLEAN_FALSE)
4255796c8dcSSimon Schubert Py_RETURN_FALSE;
4265796c8dcSSimon Schubert else
4275796c8dcSSimon Schubert Py_RETURN_NONE;
4285796c8dcSSimon Schubert }
4295796c8dcSSimon Schubert
4305796c8dcSSimon Schubert case var_integer:
431cf7f2e2dSJohn Marino if ((* (int *) var) == INT_MAX)
4325796c8dcSSimon Schubert Py_RETURN_NONE;
4335796c8dcSSimon Schubert /* Fall through. */
4345796c8dcSSimon Schubert case var_zinteger:
435cf7f2e2dSJohn Marino return PyLong_FromLong (* (int *) var);
4365796c8dcSSimon Schubert
4375796c8dcSSimon Schubert case var_uinteger:
4385796c8dcSSimon Schubert {
439cf7f2e2dSJohn Marino unsigned int val = * (unsigned int *) var;
440cf7f2e2dSJohn Marino
4415796c8dcSSimon Schubert if (val == UINT_MAX)
4425796c8dcSSimon Schubert Py_RETURN_NONE;
4435796c8dcSSimon Schubert return PyLong_FromUnsignedLong (val);
4445796c8dcSSimon Schubert }
4455796c8dcSSimon Schubert }
4465796c8dcSSimon Schubert
447cf7f2e2dSJohn Marino return PyErr_Format (PyExc_RuntimeError,
448cf7f2e2dSJohn Marino _("Programmer error: unhandled type."));
4495796c8dcSSimon Schubert }
4505796c8dcSSimon Schubert
4515796c8dcSSimon Schubert /* A Python function which returns a gdb parameter's value as a Python
4525796c8dcSSimon Schubert value. */
4535796c8dcSSimon Schubert
454cf7f2e2dSJohn Marino PyObject *
gdbpy_parameter(PyObject * self,PyObject * args)4555796c8dcSSimon Schubert gdbpy_parameter (PyObject *self, PyObject *args)
4565796c8dcSSimon Schubert {
4575796c8dcSSimon Schubert struct cmd_list_element *alias, *prefix, *cmd;
458a45ae5f8SJohn Marino const char *arg;
459a45ae5f8SJohn Marino char *newarg;
4605796c8dcSSimon Schubert int found = -1;
4615796c8dcSSimon Schubert volatile struct gdb_exception except;
4625796c8dcSSimon Schubert
4635796c8dcSSimon Schubert if (! PyArg_ParseTuple (args, "s", &arg))
4645796c8dcSSimon Schubert return NULL;
4655796c8dcSSimon Schubert
4665796c8dcSSimon Schubert newarg = concat ("show ", arg, (char *) NULL);
4675796c8dcSSimon Schubert
4685796c8dcSSimon Schubert TRY_CATCH (except, RETURN_MASK_ALL)
4695796c8dcSSimon Schubert {
4705796c8dcSSimon Schubert found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd);
4715796c8dcSSimon Schubert }
4725796c8dcSSimon Schubert xfree (newarg);
4735796c8dcSSimon Schubert GDB_PY_HANDLE_EXCEPTION (except);
4745796c8dcSSimon Schubert if (!found)
4755796c8dcSSimon Schubert return PyErr_Format (PyExc_RuntimeError,
476cf7f2e2dSJohn Marino _("Could not find parameter `%s'."), arg);
4775796c8dcSSimon Schubert
4785796c8dcSSimon Schubert if (! cmd->var)
479cf7f2e2dSJohn Marino return PyErr_Format (PyExc_RuntimeError,
480cf7f2e2dSJohn Marino _("`%s' is not a parameter."), arg);
481cf7f2e2dSJohn Marino return gdbpy_parameter_value (cmd->var_type, cmd->var);
482cf7f2e2dSJohn Marino }
483cf7f2e2dSJohn Marino
484cf7f2e2dSJohn Marino /* Wrapper for target_charset. */
485cf7f2e2dSJohn Marino
486cf7f2e2dSJohn Marino static PyObject *
gdbpy_target_charset(PyObject * self,PyObject * args)487cf7f2e2dSJohn Marino gdbpy_target_charset (PyObject *self, PyObject *args)
488cf7f2e2dSJohn Marino {
489cf7f2e2dSJohn Marino const char *cset = target_charset (python_gdbarch);
490cf7f2e2dSJohn Marino
491cf7f2e2dSJohn Marino return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
492cf7f2e2dSJohn Marino }
493cf7f2e2dSJohn Marino
494cf7f2e2dSJohn Marino /* Wrapper for target_wide_charset. */
495cf7f2e2dSJohn Marino
496cf7f2e2dSJohn Marino static PyObject *
gdbpy_target_wide_charset(PyObject * self,PyObject * args)497cf7f2e2dSJohn Marino gdbpy_target_wide_charset (PyObject *self, PyObject *args)
498cf7f2e2dSJohn Marino {
499cf7f2e2dSJohn Marino const char *cset = target_wide_charset (python_gdbarch);
500cf7f2e2dSJohn Marino
501cf7f2e2dSJohn Marino return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
5025796c8dcSSimon Schubert }
5035796c8dcSSimon Schubert
5045796c8dcSSimon Schubert /* A Python function which evaluates a string using the gdb CLI. */
5055796c8dcSSimon Schubert
5065796c8dcSSimon Schubert static PyObject *
execute_gdb_command(PyObject * self,PyObject * args,PyObject * kw)507cf7f2e2dSJohn Marino execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
5085796c8dcSSimon Schubert {
509a45ae5f8SJohn Marino const char *arg;
510cf7f2e2dSJohn Marino PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
511cf7f2e2dSJohn Marino int from_tty, to_string;
5125796c8dcSSimon Schubert volatile struct gdb_exception except;
513cf7f2e2dSJohn Marino static char *keywords[] = {"command", "from_tty", "to_string", NULL };
514cf7f2e2dSJohn Marino char *result = NULL;
5155796c8dcSSimon Schubert
516cf7f2e2dSJohn Marino if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
517cf7f2e2dSJohn Marino &PyBool_Type, &from_tty_obj,
518cf7f2e2dSJohn Marino &PyBool_Type, &to_string_obj))
5195796c8dcSSimon Schubert return NULL;
5205796c8dcSSimon Schubert
5215796c8dcSSimon Schubert from_tty = 0;
5225796c8dcSSimon Schubert if (from_tty_obj)
5235796c8dcSSimon Schubert {
524cf7f2e2dSJohn Marino int cmp = PyObject_IsTrue (from_tty_obj);
5255796c8dcSSimon Schubert if (cmp < 0)
5265796c8dcSSimon Schubert return NULL;
5275796c8dcSSimon Schubert from_tty = cmp;
5285796c8dcSSimon Schubert }
5295796c8dcSSimon Schubert
530cf7f2e2dSJohn Marino to_string = 0;
531cf7f2e2dSJohn Marino if (to_string_obj)
532cf7f2e2dSJohn Marino {
533cf7f2e2dSJohn Marino int cmp = PyObject_IsTrue (to_string_obj);
534cf7f2e2dSJohn Marino if (cmp < 0)
535cf7f2e2dSJohn Marino return NULL;
536cf7f2e2dSJohn Marino to_string = cmp;
537cf7f2e2dSJohn Marino }
538cf7f2e2dSJohn Marino
5395796c8dcSSimon Schubert TRY_CATCH (except, RETURN_MASK_ALL)
5405796c8dcSSimon Schubert {
541cf7f2e2dSJohn Marino /* Copy the argument text in case the command modifies it. */
542cf7f2e2dSJohn Marino char *copy = xstrdup (arg);
543cf7f2e2dSJohn Marino struct cleanup *cleanup = make_cleanup (xfree, copy);
544cf7f2e2dSJohn Marino
545a45ae5f8SJohn Marino make_cleanup_restore_integer (&interpreter_async);
546a45ae5f8SJohn Marino interpreter_async = 0;
547a45ae5f8SJohn Marino
548c50c785cSJohn Marino prevent_dont_repeat ();
549cf7f2e2dSJohn Marino if (to_string)
550cf7f2e2dSJohn Marino result = execute_command_to_string (copy, from_tty);
551cf7f2e2dSJohn Marino else
552cf7f2e2dSJohn Marino {
553cf7f2e2dSJohn Marino result = NULL;
554cf7f2e2dSJohn Marino execute_command (copy, from_tty);
555cf7f2e2dSJohn Marino }
556cf7f2e2dSJohn Marino
557cf7f2e2dSJohn Marino do_cleanups (cleanup);
5585796c8dcSSimon Schubert }
5595796c8dcSSimon Schubert GDB_PY_HANDLE_EXCEPTION (except);
5605796c8dcSSimon Schubert
5615796c8dcSSimon Schubert /* Do any commands attached to breakpoint we stopped at. */
5625796c8dcSSimon Schubert bpstat_do_actions ();
5635796c8dcSSimon Schubert
564cf7f2e2dSJohn Marino if (result)
565cf7f2e2dSJohn Marino {
566cf7f2e2dSJohn Marino PyObject *r = PyString_FromString (result);
567cf7f2e2dSJohn Marino xfree (result);
568cf7f2e2dSJohn Marino return r;
569cf7f2e2dSJohn Marino }
5705796c8dcSSimon Schubert Py_RETURN_NONE;
5715796c8dcSSimon Schubert }
5725796c8dcSSimon Schubert
573c50c785cSJohn Marino /* Implementation of gdb.solib_name (Long) -> String.
574c50c785cSJohn Marino Returns the name of the shared library holding a given address, or None. */
575c50c785cSJohn Marino
576c50c785cSJohn Marino static PyObject *
gdbpy_solib_name(PyObject * self,PyObject * args)577c50c785cSJohn Marino gdbpy_solib_name (PyObject *self, PyObject *args)
578c50c785cSJohn Marino {
579c50c785cSJohn Marino char *soname;
580c50c785cSJohn Marino PyObject *str_obj;
581c50c785cSJohn Marino gdb_py_longest pc;
582c50c785cSJohn Marino
583c50c785cSJohn Marino if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc))
584c50c785cSJohn Marino return NULL;
585c50c785cSJohn Marino
586c50c785cSJohn Marino soname = solib_name_from_address (current_program_space, pc);
587c50c785cSJohn Marino if (soname)
588c50c785cSJohn Marino str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL);
589c50c785cSJohn Marino else
590c50c785cSJohn Marino {
591c50c785cSJohn Marino str_obj = Py_None;
592c50c785cSJohn Marino Py_INCREF (Py_None);
593c50c785cSJohn Marino }
594c50c785cSJohn Marino
595c50c785cSJohn Marino return str_obj;
596c50c785cSJohn Marino }
597c50c785cSJohn Marino
598c50c785cSJohn Marino /* A Python function which is a wrapper for decode_line_1. */
599c50c785cSJohn Marino
600c50c785cSJohn Marino static PyObject *
gdbpy_decode_line(PyObject * self,PyObject * args)601c50c785cSJohn Marino gdbpy_decode_line (PyObject *self, PyObject *args)
602c50c785cSJohn Marino {
603c50c785cSJohn Marino struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to
604c50c785cSJohn Marino appease gcc. */
605c50c785cSJohn Marino struct symtab_and_line sal;
606a45ae5f8SJohn Marino const char *arg = NULL;
607*ef5ccd6cSJohn Marino char *copy_to_free = NULL, *copy = NULL;
608c50c785cSJohn Marino struct cleanup *cleanups;
609c50c785cSJohn Marino PyObject *result = NULL;
610c50c785cSJohn Marino PyObject *return_result = NULL;
611c50c785cSJohn Marino PyObject *unparsed = NULL;
612c50c785cSJohn Marino volatile struct gdb_exception except;
613c50c785cSJohn Marino
614c50c785cSJohn Marino if (! PyArg_ParseTuple (args, "|s", &arg))
615c50c785cSJohn Marino return NULL;
616c50c785cSJohn Marino
617a45ae5f8SJohn Marino cleanups = make_cleanup (null_cleanup, NULL);
618c50c785cSJohn Marino
619*ef5ccd6cSJohn Marino sals.sals = NULL;
620c50c785cSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL)
621c50c785cSJohn Marino {
622c50c785cSJohn Marino if (arg)
623c50c785cSJohn Marino {
624a45ae5f8SJohn Marino copy = xstrdup (arg);
625*ef5ccd6cSJohn Marino copy_to_free = copy;
626a45ae5f8SJohn Marino sals = decode_line_1 (©, 0, 0, 0);
627c50c785cSJohn Marino }
628c50c785cSJohn Marino else
629c50c785cSJohn Marino {
630c50c785cSJohn Marino set_default_source_symtab_and_line ();
631c50c785cSJohn Marino sal = get_current_source_symtab_and_line ();
632c50c785cSJohn Marino sals.sals = &sal;
633c50c785cSJohn Marino sals.nelts = 1;
634c50c785cSJohn Marino }
635c50c785cSJohn Marino }
636*ef5ccd6cSJohn Marino
637*ef5ccd6cSJohn Marino if (sals.sals != NULL && sals.sals != &sal)
638*ef5ccd6cSJohn Marino {
639*ef5ccd6cSJohn Marino make_cleanup (xfree, copy_to_free);
640*ef5ccd6cSJohn Marino make_cleanup (xfree, sals.sals);
641*ef5ccd6cSJohn Marino }
642*ef5ccd6cSJohn Marino
643c50c785cSJohn Marino if (except.reason < 0)
644c50c785cSJohn Marino {
645c50c785cSJohn Marino do_cleanups (cleanups);
646c50c785cSJohn Marino /* We know this will always throw. */
647c50c785cSJohn Marino GDB_PY_HANDLE_EXCEPTION (except);
648c50c785cSJohn Marino }
649c50c785cSJohn Marino
650c50c785cSJohn Marino if (sals.nelts)
651c50c785cSJohn Marino {
652c50c785cSJohn Marino int i;
653c50c785cSJohn Marino
654c50c785cSJohn Marino result = PyTuple_New (sals.nelts);
655c50c785cSJohn Marino if (! result)
656c50c785cSJohn Marino goto error;
657c50c785cSJohn Marino for (i = 0; i < sals.nelts; ++i)
658c50c785cSJohn Marino {
659c50c785cSJohn Marino PyObject *obj;
660c50c785cSJohn Marino
661c50c785cSJohn Marino obj = symtab_and_line_to_sal_object (sals.sals[i]);
662c50c785cSJohn Marino if (! obj)
663c50c785cSJohn Marino {
664c50c785cSJohn Marino Py_DECREF (result);
665c50c785cSJohn Marino goto error;
666c50c785cSJohn Marino }
667c50c785cSJohn Marino
668c50c785cSJohn Marino PyTuple_SetItem (result, i, obj);
669c50c785cSJohn Marino }
670c50c785cSJohn Marino }
671c50c785cSJohn Marino else
672c50c785cSJohn Marino {
673c50c785cSJohn Marino result = Py_None;
674c50c785cSJohn Marino Py_INCREF (Py_None);
675c50c785cSJohn Marino }
676c50c785cSJohn Marino
677c50c785cSJohn Marino return_result = PyTuple_New (2);
678c50c785cSJohn Marino if (! return_result)
679c50c785cSJohn Marino {
680c50c785cSJohn Marino Py_DECREF (result);
681c50c785cSJohn Marino goto error;
682c50c785cSJohn Marino }
683c50c785cSJohn Marino
684c50c785cSJohn Marino if (copy && strlen (copy) > 0)
685*ef5ccd6cSJohn Marino {
686c50c785cSJohn Marino unparsed = PyString_FromString (copy);
687*ef5ccd6cSJohn Marino if (unparsed == NULL)
688*ef5ccd6cSJohn Marino {
689*ef5ccd6cSJohn Marino Py_DECREF (result);
690*ef5ccd6cSJohn Marino Py_DECREF (return_result);
691*ef5ccd6cSJohn Marino return_result = NULL;
692*ef5ccd6cSJohn Marino goto error;
693*ef5ccd6cSJohn Marino }
694*ef5ccd6cSJohn Marino }
695c50c785cSJohn Marino else
696c50c785cSJohn Marino {
697c50c785cSJohn Marino unparsed = Py_None;
698c50c785cSJohn Marino Py_INCREF (Py_None);
699c50c785cSJohn Marino }
700c50c785cSJohn Marino
701c50c785cSJohn Marino PyTuple_SetItem (return_result, 0, unparsed);
702c50c785cSJohn Marino PyTuple_SetItem (return_result, 1, result);
703c50c785cSJohn Marino
704*ef5ccd6cSJohn Marino error:
705c50c785cSJohn Marino do_cleanups (cleanups);
706c50c785cSJohn Marino
707c50c785cSJohn Marino return return_result;
708c50c785cSJohn Marino }
709c50c785cSJohn Marino
710cf7f2e2dSJohn Marino /* Parse a string and evaluate it as an expression. */
711cf7f2e2dSJohn Marino static PyObject *
gdbpy_parse_and_eval(PyObject * self,PyObject * args)712cf7f2e2dSJohn Marino gdbpy_parse_and_eval (PyObject *self, PyObject *args)
713cf7f2e2dSJohn Marino {
714a45ae5f8SJohn Marino const char *expr_str;
715cf7f2e2dSJohn Marino struct value *result = NULL;
716cf7f2e2dSJohn Marino volatile struct gdb_exception except;
717cf7f2e2dSJohn Marino
718cf7f2e2dSJohn Marino if (!PyArg_ParseTuple (args, "s", &expr_str))
719cf7f2e2dSJohn Marino return NULL;
720cf7f2e2dSJohn Marino
721cf7f2e2dSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL)
722cf7f2e2dSJohn Marino {
723*ef5ccd6cSJohn Marino result = parse_and_eval (expr_str);
724cf7f2e2dSJohn Marino }
725cf7f2e2dSJohn Marino GDB_PY_HANDLE_EXCEPTION (except);
726cf7f2e2dSJohn Marino
727cf7f2e2dSJohn Marino return value_to_value_object (result);
728cf7f2e2dSJohn Marino }
729cf7f2e2dSJohn Marino
730*ef5ccd6cSJohn Marino /* Implementation of gdb.find_pc_line function.
731*ef5ccd6cSJohn Marino Returns the gdb.Symtab_and_line object corresponding to a PC value. */
732*ef5ccd6cSJohn Marino
733*ef5ccd6cSJohn Marino static PyObject *
gdbpy_find_pc_line(PyObject * self,PyObject * args)734*ef5ccd6cSJohn Marino gdbpy_find_pc_line (PyObject *self, PyObject *args)
735*ef5ccd6cSJohn Marino {
736*ef5ccd6cSJohn Marino gdb_py_ulongest pc_llu;
737*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
738*ef5ccd6cSJohn Marino PyObject *result = NULL; /* init for gcc -Wall */
739*ef5ccd6cSJohn Marino
740*ef5ccd6cSJohn Marino if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
741*ef5ccd6cSJohn Marino return NULL;
742*ef5ccd6cSJohn Marino
743*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL)
744*ef5ccd6cSJohn Marino {
745*ef5ccd6cSJohn Marino struct symtab_and_line sal;
746*ef5ccd6cSJohn Marino CORE_ADDR pc;
747*ef5ccd6cSJohn Marino
748*ef5ccd6cSJohn Marino pc = (CORE_ADDR) pc_llu;
749*ef5ccd6cSJohn Marino sal = find_pc_line (pc, 0);
750*ef5ccd6cSJohn Marino result = symtab_and_line_to_sal_object (sal);
751*ef5ccd6cSJohn Marino }
752*ef5ccd6cSJohn Marino GDB_PY_HANDLE_EXCEPTION (except);
753*ef5ccd6cSJohn Marino
754*ef5ccd6cSJohn Marino return result;
755*ef5ccd6cSJohn Marino }
756*ef5ccd6cSJohn Marino
757a45ae5f8SJohn Marino /* Read a file as Python code.
758*ef5ccd6cSJohn Marino FILE is the file to run. FILENAME is name of the file FILE.
759a45ae5f8SJohn Marino This does not throw any errors. If an exception occurs python will print
760a45ae5f8SJohn Marino the traceback and clear the error indicator. */
761cf7f2e2dSJohn Marino
762cf7f2e2dSJohn Marino void
source_python_script(FILE * file,const char * filename)763*ef5ccd6cSJohn Marino source_python_script (FILE *file, const char *filename)
764cf7f2e2dSJohn Marino {
765cf7f2e2dSJohn Marino struct cleanup *cleanup;
766cf7f2e2dSJohn Marino
767cf7f2e2dSJohn Marino cleanup = ensure_python_env (get_current_arch (), current_language);
768*ef5ccd6cSJohn Marino python_run_simple_file (file, filename);
769cf7f2e2dSJohn Marino do_cleanups (cleanup);
770cf7f2e2dSJohn Marino }
771cf7f2e2dSJohn Marino
7725796c8dcSSimon Schubert
7735796c8dcSSimon Schubert
774c50c785cSJohn Marino /* Posting and handling events. */
7755796c8dcSSimon Schubert
776c50c785cSJohn Marino /* A single event. */
777c50c785cSJohn Marino struct gdbpy_event
7785796c8dcSSimon Schubert {
779c50c785cSJohn Marino /* The Python event. This is just a callable object. */
780c50c785cSJohn Marino PyObject *event;
781c50c785cSJohn Marino /* The next event. */
782c50c785cSJohn Marino struct gdbpy_event *next;
783c50c785cSJohn Marino };
784cf7f2e2dSJohn Marino
785c50c785cSJohn Marino /* All pending events. */
786c50c785cSJohn Marino static struct gdbpy_event *gdbpy_event_list;
787c50c785cSJohn Marino /* The final link of the event list. */
788c50c785cSJohn Marino static struct gdbpy_event **gdbpy_event_list_end;
789c50c785cSJohn Marino
790c50c785cSJohn Marino /* We use a file handler, and not an async handler, so that we can
791c50c785cSJohn Marino wake up the main thread even when it is blocked in poll(). */
792c50c785cSJohn Marino static struct serial *gdbpy_event_fds[2];
793c50c785cSJohn Marino
794c50c785cSJohn Marino /* The file handler callback. This reads from the internal pipe, and
795c50c785cSJohn Marino then processes the Python event queue. This will always be run in
796c50c785cSJohn Marino the main gdb thread. */
797c50c785cSJohn Marino
798c50c785cSJohn Marino static void
gdbpy_run_events(struct serial * scb,void * context)799c50c785cSJohn Marino gdbpy_run_events (struct serial *scb, void *context)
800c50c785cSJohn Marino {
801c50c785cSJohn Marino struct cleanup *cleanup;
802c50c785cSJohn Marino
803c50c785cSJohn Marino cleanup = ensure_python_env (get_current_arch (), current_language);
804c50c785cSJohn Marino
805c50c785cSJohn Marino /* Flush the fd. Do this before flushing the events list, so that
806c50c785cSJohn Marino any new event post afterwards is sure to re-awake the event
807c50c785cSJohn Marino loop. */
808c50c785cSJohn Marino while (serial_readchar (gdbpy_event_fds[0], 0) >= 0)
809c50c785cSJohn Marino ;
810c50c785cSJohn Marino
811c50c785cSJohn Marino while (gdbpy_event_list)
812c50c785cSJohn Marino {
813c50c785cSJohn Marino /* Dispatching the event might push a new element onto the event
814c50c785cSJohn Marino loop, so we update here "atomically enough". */
815c50c785cSJohn Marino struct gdbpy_event *item = gdbpy_event_list;
816c50c785cSJohn Marino gdbpy_event_list = gdbpy_event_list->next;
817c50c785cSJohn Marino if (gdbpy_event_list == NULL)
818c50c785cSJohn Marino gdbpy_event_list_end = &gdbpy_event_list;
819c50c785cSJohn Marino
820c50c785cSJohn Marino /* Ignore errors. */
821c50c785cSJohn Marino if (PyObject_CallObject (item->event, NULL) == NULL)
822c50c785cSJohn Marino PyErr_Clear ();
823c50c785cSJohn Marino
824c50c785cSJohn Marino Py_DECREF (item->event);
825c50c785cSJohn Marino xfree (item);
826c50c785cSJohn Marino }
827c50c785cSJohn Marino
828c50c785cSJohn Marino do_cleanups (cleanup);
829c50c785cSJohn Marino }
830c50c785cSJohn Marino
831c50c785cSJohn Marino /* Submit an event to the gdb thread. */
832c50c785cSJohn Marino static PyObject *
gdbpy_post_event(PyObject * self,PyObject * args)833c50c785cSJohn Marino gdbpy_post_event (PyObject *self, PyObject *args)
834c50c785cSJohn Marino {
835c50c785cSJohn Marino struct gdbpy_event *event;
836c50c785cSJohn Marino PyObject *func;
837c50c785cSJohn Marino int wakeup;
838c50c785cSJohn Marino
839c50c785cSJohn Marino if (!PyArg_ParseTuple (args, "O", &func))
8405796c8dcSSimon Schubert return NULL;
841c50c785cSJohn Marino
842c50c785cSJohn Marino if (!PyCallable_Check (func))
843c50c785cSJohn Marino {
844c50c785cSJohn Marino PyErr_SetString (PyExc_RuntimeError,
845c50c785cSJohn Marino _("Posted event is not callable"));
846c50c785cSJohn Marino return NULL;
847c50c785cSJohn Marino }
848c50c785cSJohn Marino
849c50c785cSJohn Marino Py_INCREF (func);
850c50c785cSJohn Marino
851c50c785cSJohn Marino /* From here until the end of the function, we have the GIL, so we
852c50c785cSJohn Marino can operate on our global data structures without worrying. */
853c50c785cSJohn Marino wakeup = gdbpy_event_list == NULL;
854c50c785cSJohn Marino
855c50c785cSJohn Marino event = XNEW (struct gdbpy_event);
856c50c785cSJohn Marino event->event = func;
857c50c785cSJohn Marino event->next = NULL;
858c50c785cSJohn Marino *gdbpy_event_list_end = event;
859c50c785cSJohn Marino gdbpy_event_list_end = &event->next;
860c50c785cSJohn Marino
861c50c785cSJohn Marino /* Wake up gdb when needed. */
862c50c785cSJohn Marino if (wakeup)
863c50c785cSJohn Marino {
864c50c785cSJohn Marino char c = 'q'; /* Anything. */
865c50c785cSJohn Marino
866c50c785cSJohn Marino if (serial_write (gdbpy_event_fds[1], &c, 1))
867c50c785cSJohn Marino return PyErr_SetFromErrno (PyExc_IOError);
868c50c785cSJohn Marino }
869c50c785cSJohn Marino
8705796c8dcSSimon Schubert Py_RETURN_NONE;
8715796c8dcSSimon Schubert }
8725796c8dcSSimon Schubert
873c50c785cSJohn Marino /* Initialize the Python event handler. */
874c50c785cSJohn Marino static void
gdbpy_initialize_events(void)875c50c785cSJohn Marino gdbpy_initialize_events (void)
8765796c8dcSSimon Schubert {
877c50c785cSJohn Marino if (serial_pipe (gdbpy_event_fds) == 0)
878c50c785cSJohn Marino {
879c50c785cSJohn Marino gdbpy_event_list_end = &gdbpy_event_list;
880c50c785cSJohn Marino serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL);
881c50c785cSJohn Marino }
882c50c785cSJohn Marino }
883c50c785cSJohn Marino
884a45ae5f8SJohn Marino
885a45ae5f8SJohn Marino
886a45ae5f8SJohn Marino static void
before_prompt_hook(const char * current_gdb_prompt)887a45ae5f8SJohn Marino before_prompt_hook (const char *current_gdb_prompt)
888a45ae5f8SJohn Marino {
889a45ae5f8SJohn Marino struct cleanup *cleanup;
890a45ae5f8SJohn Marino char *prompt = NULL;
891a45ae5f8SJohn Marino
892a45ae5f8SJohn Marino cleanup = ensure_python_env (get_current_arch (), current_language);
893a45ae5f8SJohn Marino
894*ef5ccd6cSJohn Marino if (gdb_python_module
895*ef5ccd6cSJohn Marino && PyObject_HasAttrString (gdb_python_module, "prompt_hook"))
896a45ae5f8SJohn Marino {
897a45ae5f8SJohn Marino PyObject *hook;
898a45ae5f8SJohn Marino
899*ef5ccd6cSJohn Marino hook = PyObject_GetAttrString (gdb_python_module, "prompt_hook");
900a45ae5f8SJohn Marino if (hook == NULL)
901a45ae5f8SJohn Marino goto fail;
902a45ae5f8SJohn Marino
903a45ae5f8SJohn Marino if (PyCallable_Check (hook))
904a45ae5f8SJohn Marino {
905a45ae5f8SJohn Marino PyObject *result;
906a45ae5f8SJohn Marino PyObject *current_prompt;
907a45ae5f8SJohn Marino
908a45ae5f8SJohn Marino current_prompt = PyString_FromString (current_gdb_prompt);
909a45ae5f8SJohn Marino if (current_prompt == NULL)
910a45ae5f8SJohn Marino goto fail;
911a45ae5f8SJohn Marino
912a45ae5f8SJohn Marino result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL);
913a45ae5f8SJohn Marino
914a45ae5f8SJohn Marino Py_DECREF (current_prompt);
915a45ae5f8SJohn Marino
916a45ae5f8SJohn Marino if (result == NULL)
917a45ae5f8SJohn Marino goto fail;
918a45ae5f8SJohn Marino
919a45ae5f8SJohn Marino make_cleanup_py_decref (result);
920a45ae5f8SJohn Marino
921a45ae5f8SJohn Marino /* Return type should be None, or a String. If it is None,
922a45ae5f8SJohn Marino fall through, we will not set a prompt. If it is a
923a45ae5f8SJohn Marino string, set PROMPT. Anything else, set an exception. */
924a45ae5f8SJohn Marino if (result != Py_None && ! PyString_Check (result))
925a45ae5f8SJohn Marino {
926a45ae5f8SJohn Marino PyErr_Format (PyExc_RuntimeError,
927a45ae5f8SJohn Marino _("Return from prompt_hook must " \
928a45ae5f8SJohn Marino "be either a Python string, or None"));
929a45ae5f8SJohn Marino goto fail;
930a45ae5f8SJohn Marino }
931a45ae5f8SJohn Marino
932a45ae5f8SJohn Marino if (result != Py_None)
933a45ae5f8SJohn Marino {
934a45ae5f8SJohn Marino prompt = python_string_to_host_string (result);
935a45ae5f8SJohn Marino
936a45ae5f8SJohn Marino if (prompt == NULL)
937a45ae5f8SJohn Marino goto fail;
938a45ae5f8SJohn Marino else
939a45ae5f8SJohn Marino make_cleanup (xfree, prompt);
940a45ae5f8SJohn Marino }
941a45ae5f8SJohn Marino }
942a45ae5f8SJohn Marino }
943a45ae5f8SJohn Marino
944a45ae5f8SJohn Marino /* If a prompt has been set, PROMPT will not be NULL. If it is
945a45ae5f8SJohn Marino NULL, do not set the prompt. */
946a45ae5f8SJohn Marino if (prompt != NULL)
947a45ae5f8SJohn Marino set_prompt (prompt);
948a45ae5f8SJohn Marino
949a45ae5f8SJohn Marino do_cleanups (cleanup);
950a45ae5f8SJohn Marino return;
951a45ae5f8SJohn Marino
952a45ae5f8SJohn Marino fail:
953a45ae5f8SJohn Marino gdbpy_print_stack ();
954a45ae5f8SJohn Marino do_cleanups (cleanup);
955a45ae5f8SJohn Marino return;
956a45ae5f8SJohn Marino }
957a45ae5f8SJohn Marino
958a45ae5f8SJohn Marino
959a45ae5f8SJohn Marino
960c50c785cSJohn Marino /* Printing. */
961c50c785cSJohn Marino
962c50c785cSJohn Marino /* A python function to write a single string using gdb's filtered
963c50c785cSJohn Marino output stream . The optional keyword STREAM can be used to write
964c50c785cSJohn Marino to a particular stream. The default stream is to gdb_stdout. */
965c50c785cSJohn Marino
966c50c785cSJohn Marino static PyObject *
gdbpy_write(PyObject * self,PyObject * args,PyObject * kw)967c50c785cSJohn Marino gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
968c50c785cSJohn Marino {
969a45ae5f8SJohn Marino const char *arg;
970c50c785cSJohn Marino static char *keywords[] = {"text", "stream", NULL };
971c50c785cSJohn Marino int stream_type = 0;
972*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
973c50c785cSJohn Marino
974c50c785cSJohn Marino if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
975c50c785cSJohn Marino &stream_type))
976c50c785cSJohn Marino return NULL;
977c50c785cSJohn Marino
978*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL)
979*ef5ccd6cSJohn Marino {
980c50c785cSJohn Marino switch (stream_type)
981c50c785cSJohn Marino {
982c50c785cSJohn Marino case 1:
983c50c785cSJohn Marino {
984c50c785cSJohn Marino fprintf_filtered (gdb_stderr, "%s", arg);
985c50c785cSJohn Marino break;
986c50c785cSJohn Marino }
987c50c785cSJohn Marino case 2:
988c50c785cSJohn Marino {
989c50c785cSJohn Marino fprintf_filtered (gdb_stdlog, "%s", arg);
990c50c785cSJohn Marino break;
991c50c785cSJohn Marino }
992c50c785cSJohn Marino default:
993c50c785cSJohn Marino fprintf_filtered (gdb_stdout, "%s", arg);
994c50c785cSJohn Marino }
995*ef5ccd6cSJohn Marino }
996*ef5ccd6cSJohn Marino GDB_PY_HANDLE_EXCEPTION (except);
997c50c785cSJohn Marino
998c50c785cSJohn Marino Py_RETURN_NONE;
999c50c785cSJohn Marino }
1000c50c785cSJohn Marino
1001c50c785cSJohn Marino /* A python function to flush a gdb stream. The optional keyword
1002c50c785cSJohn Marino STREAM can be used to flush a particular stream. The default stream
1003c50c785cSJohn Marino is gdb_stdout. */
1004c50c785cSJohn Marino
1005c50c785cSJohn Marino static PyObject *
gdbpy_flush(PyObject * self,PyObject * args,PyObject * kw)1006c50c785cSJohn Marino gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
1007c50c785cSJohn Marino {
1008c50c785cSJohn Marino static char *keywords[] = {"stream", NULL };
1009c50c785cSJohn Marino int stream_type = 0;
1010c50c785cSJohn Marino
1011c50c785cSJohn Marino if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
1012c50c785cSJohn Marino &stream_type))
1013c50c785cSJohn Marino return NULL;
1014c50c785cSJohn Marino
1015c50c785cSJohn Marino switch (stream_type)
1016c50c785cSJohn Marino {
1017c50c785cSJohn Marino case 1:
1018c50c785cSJohn Marino {
1019c50c785cSJohn Marino gdb_flush (gdb_stderr);
1020c50c785cSJohn Marino break;
1021c50c785cSJohn Marino }
1022c50c785cSJohn Marino case 2:
1023c50c785cSJohn Marino {
1024c50c785cSJohn Marino gdb_flush (gdb_stdlog);
1025c50c785cSJohn Marino break;
1026c50c785cSJohn Marino }
1027c50c785cSJohn Marino default:
10285796c8dcSSimon Schubert gdb_flush (gdb_stdout);
1029c50c785cSJohn Marino }
1030c50c785cSJohn Marino
10315796c8dcSSimon Schubert Py_RETURN_NONE;
10325796c8dcSSimon Schubert }
10335796c8dcSSimon Schubert
1034a45ae5f8SJohn Marino /* Print a python exception trace, print just a message, or print
1035a45ae5f8SJohn Marino nothing and clear the python exception, depending on
1036a45ae5f8SJohn Marino gdbpy_should_print_stack. Only call this if a python exception is
1037a45ae5f8SJohn Marino set. */
10385796c8dcSSimon Schubert void
gdbpy_print_stack(void)10395796c8dcSSimon Schubert gdbpy_print_stack (void)
10405796c8dcSSimon Schubert {
1041*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
1042*ef5ccd6cSJohn Marino
1043a45ae5f8SJohn Marino /* Print "none", just clear exception. */
1044a45ae5f8SJohn Marino if (gdbpy_should_print_stack == python_excp_none)
1045a45ae5f8SJohn Marino {
1046a45ae5f8SJohn Marino PyErr_Clear ();
1047a45ae5f8SJohn Marino }
1048a45ae5f8SJohn Marino /* Print "full" message and backtrace. */
1049a45ae5f8SJohn Marino else if (gdbpy_should_print_stack == python_excp_full)
1050cf7f2e2dSJohn Marino {
10515796c8dcSSimon Schubert PyErr_Print ();
1052cf7f2e2dSJohn Marino /* PyErr_Print doesn't necessarily end output with a newline.
1053cf7f2e2dSJohn Marino This works because Python's stdout/stderr is fed through
1054cf7f2e2dSJohn Marino printf_filtered. */
1055*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL)
1056*ef5ccd6cSJohn Marino {
1057cf7f2e2dSJohn Marino begin_line ();
1058cf7f2e2dSJohn Marino }
1059*ef5ccd6cSJohn Marino }
1060a45ae5f8SJohn Marino /* Print "message", just error print message. */
10615796c8dcSSimon Schubert else
1062a45ae5f8SJohn Marino {
1063a45ae5f8SJohn Marino PyObject *ptype, *pvalue, *ptraceback;
1064a45ae5f8SJohn Marino char *msg = NULL, *type = NULL;
1065a45ae5f8SJohn Marino
1066a45ae5f8SJohn Marino PyErr_Fetch (&ptype, &pvalue, &ptraceback);
1067a45ae5f8SJohn Marino
1068a45ae5f8SJohn Marino /* Fetch the error message contained within ptype, pvalue. */
1069a45ae5f8SJohn Marino msg = gdbpy_exception_to_string (ptype, pvalue);
1070a45ae5f8SJohn Marino type = gdbpy_obj_to_string (ptype);
1071*ef5ccd6cSJohn Marino
1072*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ALL)
1073*ef5ccd6cSJohn Marino {
1074a45ae5f8SJohn Marino if (msg == NULL)
1075a45ae5f8SJohn Marino {
1076a45ae5f8SJohn Marino /* An error occurred computing the string representation of the
1077a45ae5f8SJohn Marino error message. */
1078a45ae5f8SJohn Marino fprintf_filtered (gdb_stderr,
1079a45ae5f8SJohn Marino _("Error occurred computing Python error" \
1080a45ae5f8SJohn Marino "message.\n"));
1081a45ae5f8SJohn Marino }
1082a45ae5f8SJohn Marino else
1083a45ae5f8SJohn Marino fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
1084a45ae5f8SJohn Marino type, msg);
1085*ef5ccd6cSJohn Marino }
1086a45ae5f8SJohn Marino
1087a45ae5f8SJohn Marino Py_XDECREF (ptype);
1088a45ae5f8SJohn Marino Py_XDECREF (pvalue);
1089a45ae5f8SJohn Marino Py_XDECREF (ptraceback);
1090a45ae5f8SJohn Marino xfree (msg);
1091a45ae5f8SJohn Marino }
10925796c8dcSSimon Schubert }
10935796c8dcSSimon Schubert
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert
1096cf7f2e2dSJohn Marino /* Return the current Progspace.
1097cf7f2e2dSJohn Marino There always is one. */
1098cf7f2e2dSJohn Marino
1099cf7f2e2dSJohn Marino static PyObject *
gdbpy_get_current_progspace(PyObject * unused1,PyObject * unused2)1100cf7f2e2dSJohn Marino gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
1101cf7f2e2dSJohn Marino {
1102cf7f2e2dSJohn Marino PyObject *result;
1103cf7f2e2dSJohn Marino
1104cf7f2e2dSJohn Marino result = pspace_to_pspace_object (current_program_space);
1105cf7f2e2dSJohn Marino if (result)
1106cf7f2e2dSJohn Marino Py_INCREF (result);
1107cf7f2e2dSJohn Marino return result;
1108cf7f2e2dSJohn Marino }
1109cf7f2e2dSJohn Marino
1110cf7f2e2dSJohn Marino /* Return a sequence holding all the Progspaces. */
1111cf7f2e2dSJohn Marino
1112cf7f2e2dSJohn Marino static PyObject *
gdbpy_progspaces(PyObject * unused1,PyObject * unused2)1113cf7f2e2dSJohn Marino gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
1114cf7f2e2dSJohn Marino {
1115cf7f2e2dSJohn Marino struct program_space *ps;
1116cf7f2e2dSJohn Marino PyObject *list;
1117cf7f2e2dSJohn Marino
1118cf7f2e2dSJohn Marino list = PyList_New (0);
1119cf7f2e2dSJohn Marino if (!list)
1120cf7f2e2dSJohn Marino return NULL;
1121cf7f2e2dSJohn Marino
1122cf7f2e2dSJohn Marino ALL_PSPACES (ps)
1123cf7f2e2dSJohn Marino {
1124cf7f2e2dSJohn Marino PyObject *item = pspace_to_pspace_object (ps);
1125cf7f2e2dSJohn Marino
1126cf7f2e2dSJohn Marino if (!item || PyList_Append (list, item) == -1)
1127cf7f2e2dSJohn Marino {
1128cf7f2e2dSJohn Marino Py_DECREF (list);
1129cf7f2e2dSJohn Marino return NULL;
1130cf7f2e2dSJohn Marino }
1131cf7f2e2dSJohn Marino }
1132cf7f2e2dSJohn Marino
1133cf7f2e2dSJohn Marino return list;
1134cf7f2e2dSJohn Marino }
1135cf7f2e2dSJohn Marino
1136cf7f2e2dSJohn Marino
1137cf7f2e2dSJohn Marino
11385796c8dcSSimon Schubert /* The "current" objfile. This is set when gdb detects that a new
1139cf7f2e2dSJohn Marino objfile has been loaded. It is only set for the duration of a call to
1140cf7f2e2dSJohn Marino source_python_script_for_objfile; it is NULL at other times. */
11415796c8dcSSimon Schubert static struct objfile *gdbpy_current_objfile;
11425796c8dcSSimon Schubert
1143*ef5ccd6cSJohn Marino /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1144*ef5ccd6cSJohn Marino as Python code. This does not throw any errors. If an exception
1145*ef5ccd6cSJohn Marino occurs python will print the traceback and clear the error indicator. */
11465796c8dcSSimon Schubert
1147cf7f2e2dSJohn Marino void
source_python_script_for_objfile(struct objfile * objfile,FILE * file,const char * filename)1148*ef5ccd6cSJohn Marino source_python_script_for_objfile (struct objfile *objfile, FILE *file,
1149*ef5ccd6cSJohn Marino const char *filename)
11505796c8dcSSimon Schubert {
11515796c8dcSSimon Schubert struct cleanup *cleanups;
11525796c8dcSSimon Schubert
11535796c8dcSSimon Schubert cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
11545796c8dcSSimon Schubert gdbpy_current_objfile = objfile;
11555796c8dcSSimon Schubert
1156*ef5ccd6cSJohn Marino python_run_simple_file (file, filename);
11575796c8dcSSimon Schubert
11585796c8dcSSimon Schubert do_cleanups (cleanups);
11595796c8dcSSimon Schubert gdbpy_current_objfile = NULL;
11605796c8dcSSimon Schubert }
11615796c8dcSSimon Schubert
11625796c8dcSSimon Schubert /* Return the current Objfile, or None if there isn't one. */
1163cf7f2e2dSJohn Marino
11645796c8dcSSimon Schubert static PyObject *
gdbpy_get_current_objfile(PyObject * unused1,PyObject * unused2)11655796c8dcSSimon Schubert gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
11665796c8dcSSimon Schubert {
11675796c8dcSSimon Schubert PyObject *result;
11685796c8dcSSimon Schubert
11695796c8dcSSimon Schubert if (! gdbpy_current_objfile)
11705796c8dcSSimon Schubert Py_RETURN_NONE;
11715796c8dcSSimon Schubert
11725796c8dcSSimon Schubert result = objfile_to_objfile_object (gdbpy_current_objfile);
11735796c8dcSSimon Schubert if (result)
11745796c8dcSSimon Schubert Py_INCREF (result);
11755796c8dcSSimon Schubert return result;
11765796c8dcSSimon Schubert }
11775796c8dcSSimon Schubert
11785796c8dcSSimon Schubert /* Return a sequence holding all the Objfiles. */
1179cf7f2e2dSJohn Marino
11805796c8dcSSimon Schubert static PyObject *
gdbpy_objfiles(PyObject * unused1,PyObject * unused2)11815796c8dcSSimon Schubert gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
11825796c8dcSSimon Schubert {
11835796c8dcSSimon Schubert struct objfile *objf;
11845796c8dcSSimon Schubert PyObject *list;
11855796c8dcSSimon Schubert
11865796c8dcSSimon Schubert list = PyList_New (0);
11875796c8dcSSimon Schubert if (!list)
11885796c8dcSSimon Schubert return NULL;
11895796c8dcSSimon Schubert
11905796c8dcSSimon Schubert ALL_OBJFILES (objf)
11915796c8dcSSimon Schubert {
11925796c8dcSSimon Schubert PyObject *item = objfile_to_objfile_object (objf);
1193cf7f2e2dSJohn Marino
11945796c8dcSSimon Schubert if (!item || PyList_Append (list, item) == -1)
11955796c8dcSSimon Schubert {
11965796c8dcSSimon Schubert Py_DECREF (list);
11975796c8dcSSimon Schubert return NULL;
11985796c8dcSSimon Schubert }
11995796c8dcSSimon Schubert }
12005796c8dcSSimon Schubert
12015796c8dcSSimon Schubert return list;
12025796c8dcSSimon Schubert }
12035796c8dcSSimon Schubert
1204*ef5ccd6cSJohn Marino /* Compute the list of active type printers and return it. The result
1205*ef5ccd6cSJohn Marino of this function can be passed to apply_type_printers, and should
1206*ef5ccd6cSJohn Marino be freed by free_type_printers. */
1207*ef5ccd6cSJohn Marino
1208*ef5ccd6cSJohn Marino void *
start_type_printers(void)1209*ef5ccd6cSJohn Marino start_type_printers (void)
1210*ef5ccd6cSJohn Marino {
1211*ef5ccd6cSJohn Marino struct cleanup *cleanups;
1212*ef5ccd6cSJohn Marino PyObject *type_module, *func, *result_obj = NULL;
1213*ef5ccd6cSJohn Marino
1214*ef5ccd6cSJohn Marino cleanups = ensure_python_env (get_current_arch (), current_language);
1215*ef5ccd6cSJohn Marino
1216*ef5ccd6cSJohn Marino type_module = PyImport_ImportModule ("gdb.types");
1217*ef5ccd6cSJohn Marino if (type_module == NULL)
1218*ef5ccd6cSJohn Marino {
1219*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1220*ef5ccd6cSJohn Marino goto done;
1221*ef5ccd6cSJohn Marino }
1222*ef5ccd6cSJohn Marino make_cleanup_py_decref (type_module);
1223*ef5ccd6cSJohn Marino
1224*ef5ccd6cSJohn Marino func = PyObject_GetAttrString (type_module, "get_type_recognizers");
1225*ef5ccd6cSJohn Marino if (func == NULL)
1226*ef5ccd6cSJohn Marino {
1227*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1228*ef5ccd6cSJohn Marino goto done;
1229*ef5ccd6cSJohn Marino }
1230*ef5ccd6cSJohn Marino make_cleanup_py_decref (func);
1231*ef5ccd6cSJohn Marino
1232*ef5ccd6cSJohn Marino result_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
1233*ef5ccd6cSJohn Marino if (result_obj == NULL)
1234*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1235*ef5ccd6cSJohn Marino
1236*ef5ccd6cSJohn Marino done:
1237*ef5ccd6cSJohn Marino do_cleanups (cleanups);
1238*ef5ccd6cSJohn Marino return result_obj;
1239*ef5ccd6cSJohn Marino }
1240*ef5ccd6cSJohn Marino
1241*ef5ccd6cSJohn Marino /* If TYPE is recognized by some type printer, return a newly
1242*ef5ccd6cSJohn Marino allocated string holding the type's replacement name. The caller
1243*ef5ccd6cSJohn Marino is responsible for freeing the string. Otherwise, return NULL.
1244*ef5ccd6cSJohn Marino
1245*ef5ccd6cSJohn Marino This function has a bit of a funny name, since it actually applies
1246*ef5ccd6cSJohn Marino recognizers, but this seemed clearer given the start_type_printers
1247*ef5ccd6cSJohn Marino and free_type_printers functions. */
1248*ef5ccd6cSJohn Marino
1249*ef5ccd6cSJohn Marino char *
apply_type_printers(void * printers,struct type * type)1250*ef5ccd6cSJohn Marino apply_type_printers (void *printers, struct type *type)
1251*ef5ccd6cSJohn Marino {
1252*ef5ccd6cSJohn Marino struct cleanup *cleanups;
1253*ef5ccd6cSJohn Marino PyObject *type_obj, *type_module, *func, *result_obj;
1254*ef5ccd6cSJohn Marino PyObject *printers_obj = printers;
1255*ef5ccd6cSJohn Marino char *result = NULL;
1256*ef5ccd6cSJohn Marino
1257*ef5ccd6cSJohn Marino if (printers_obj == NULL)
1258*ef5ccd6cSJohn Marino return NULL;
1259*ef5ccd6cSJohn Marino
1260*ef5ccd6cSJohn Marino cleanups = ensure_python_env (get_current_arch (), current_language);
1261*ef5ccd6cSJohn Marino
1262*ef5ccd6cSJohn Marino type_obj = type_to_type_object (type);
1263*ef5ccd6cSJohn Marino if (type_obj == NULL)
1264*ef5ccd6cSJohn Marino {
1265*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1266*ef5ccd6cSJohn Marino goto done;
1267*ef5ccd6cSJohn Marino }
1268*ef5ccd6cSJohn Marino make_cleanup_py_decref (type_obj);
1269*ef5ccd6cSJohn Marino
1270*ef5ccd6cSJohn Marino type_module = PyImport_ImportModule ("gdb.types");
1271*ef5ccd6cSJohn Marino if (type_module == NULL)
1272*ef5ccd6cSJohn Marino {
1273*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1274*ef5ccd6cSJohn Marino goto done;
1275*ef5ccd6cSJohn Marino }
1276*ef5ccd6cSJohn Marino make_cleanup_py_decref (type_module);
1277*ef5ccd6cSJohn Marino
1278*ef5ccd6cSJohn Marino func = PyObject_GetAttrString (type_module, "apply_type_recognizers");
1279*ef5ccd6cSJohn Marino if (func == NULL)
1280*ef5ccd6cSJohn Marino {
1281*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1282*ef5ccd6cSJohn Marino goto done;
1283*ef5ccd6cSJohn Marino }
1284*ef5ccd6cSJohn Marino make_cleanup_py_decref (func);
1285*ef5ccd6cSJohn Marino
1286*ef5ccd6cSJohn Marino result_obj = PyObject_CallFunctionObjArgs (func, printers_obj,
1287*ef5ccd6cSJohn Marino type_obj, (char *) NULL);
1288*ef5ccd6cSJohn Marino if (result_obj == NULL)
1289*ef5ccd6cSJohn Marino {
1290*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1291*ef5ccd6cSJohn Marino goto done;
1292*ef5ccd6cSJohn Marino }
1293*ef5ccd6cSJohn Marino make_cleanup_py_decref (result_obj);
1294*ef5ccd6cSJohn Marino
1295*ef5ccd6cSJohn Marino if (result_obj != Py_None)
1296*ef5ccd6cSJohn Marino {
1297*ef5ccd6cSJohn Marino result = python_string_to_host_string (result_obj);
1298*ef5ccd6cSJohn Marino if (result == NULL)
1299*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1300*ef5ccd6cSJohn Marino }
1301*ef5ccd6cSJohn Marino
1302*ef5ccd6cSJohn Marino done:
1303*ef5ccd6cSJohn Marino do_cleanups (cleanups);
1304*ef5ccd6cSJohn Marino return result;
1305*ef5ccd6cSJohn Marino }
1306*ef5ccd6cSJohn Marino
1307*ef5ccd6cSJohn Marino /* Free the result of start_type_printers. */
1308*ef5ccd6cSJohn Marino
1309*ef5ccd6cSJohn Marino void
free_type_printers(void * arg)1310*ef5ccd6cSJohn Marino free_type_printers (void *arg)
1311*ef5ccd6cSJohn Marino {
1312*ef5ccd6cSJohn Marino struct cleanup *cleanups;
1313*ef5ccd6cSJohn Marino PyObject *printers = arg;
1314*ef5ccd6cSJohn Marino
1315*ef5ccd6cSJohn Marino if (printers == NULL)
1316*ef5ccd6cSJohn Marino return;
1317*ef5ccd6cSJohn Marino
1318*ef5ccd6cSJohn Marino cleanups = ensure_python_env (get_current_arch (), current_language);
1319*ef5ccd6cSJohn Marino Py_DECREF (printers);
1320*ef5ccd6cSJohn Marino do_cleanups (cleanups);
1321*ef5ccd6cSJohn Marino }
1322*ef5ccd6cSJohn Marino
13235796c8dcSSimon Schubert #else /* HAVE_PYTHON */
13245796c8dcSSimon Schubert
1325*ef5ccd6cSJohn Marino /* Dummy implementation of the gdb "python-interactive" and "python"
1326*ef5ccd6cSJohn Marino command. */
13275796c8dcSSimon Schubert
13285796c8dcSSimon Schubert static void
python_interactive_command(char * arg,int from_tty)1329*ef5ccd6cSJohn Marino python_interactive_command (char *arg, int from_tty)
13305796c8dcSSimon Schubert {
1331*ef5ccd6cSJohn Marino arg = skip_spaces (arg);
13325796c8dcSSimon Schubert if (arg && *arg)
13335796c8dcSSimon Schubert error (_("Python scripting is not supported in this copy of GDB."));
13345796c8dcSSimon Schubert else
13355796c8dcSSimon Schubert {
13365796c8dcSSimon Schubert struct command_line *l = get_command_line (python_control, "");
13375796c8dcSSimon Schubert struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
1338cf7f2e2dSJohn Marino
13395796c8dcSSimon Schubert execute_control_command_untraced (l);
13405796c8dcSSimon Schubert do_cleanups (cleanups);
13415796c8dcSSimon Schubert }
13425796c8dcSSimon Schubert }
13435796c8dcSSimon Schubert
1344*ef5ccd6cSJohn Marino static void
python_command(char * arg,int from_tty)1345*ef5ccd6cSJohn Marino python_command (char *arg, int from_tty)
1346*ef5ccd6cSJohn Marino {
1347*ef5ccd6cSJohn Marino python_interactive_command (arg, from_tty);
1348*ef5ccd6cSJohn Marino }
1349*ef5ccd6cSJohn Marino
13505796c8dcSSimon Schubert void
eval_python_from_control_command(struct command_line * cmd)13515796c8dcSSimon Schubert eval_python_from_control_command (struct command_line *cmd)
13525796c8dcSSimon Schubert {
13535796c8dcSSimon Schubert error (_("Python scripting is not supported in this copy of GDB."));
13545796c8dcSSimon Schubert }
13555796c8dcSSimon Schubert
1356cf7f2e2dSJohn Marino void
source_python_script(FILE * file,const char * filename)1357*ef5ccd6cSJohn Marino source_python_script (FILE *file, const char *filename)
1358cf7f2e2dSJohn Marino {
1359cf7f2e2dSJohn Marino throw_error (UNSUPPORTED_ERROR,
1360cf7f2e2dSJohn Marino _("Python scripting is not supported in this copy of GDB."));
1361cf7f2e2dSJohn Marino }
1362cf7f2e2dSJohn Marino
1363c50c785cSJohn Marino int
gdbpy_should_stop(struct breakpoint_object * bp_obj)1364c50c785cSJohn Marino gdbpy_should_stop (struct breakpoint_object *bp_obj)
1365c50c785cSJohn Marino {
1366c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
1367c50c785cSJohn Marino _("gdbpy_should_stop called when Python scripting is " \
1368c50c785cSJohn Marino "not supported."));
1369c50c785cSJohn Marino }
1370c50c785cSJohn Marino
1371c50c785cSJohn Marino int
gdbpy_breakpoint_has_py_cond(struct breakpoint_object * bp_obj)1372c50c785cSJohn Marino gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
1373c50c785cSJohn Marino {
1374c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
1375c50c785cSJohn Marino _("gdbpy_breakpoint_has_py_cond called when Python " \
1376c50c785cSJohn Marino "scripting is not supported."));
1377c50c785cSJohn Marino }
1378c50c785cSJohn Marino
1379*ef5ccd6cSJohn Marino void *
start_type_printers(void)1380*ef5ccd6cSJohn Marino start_type_printers (void)
1381*ef5ccd6cSJohn Marino {
1382*ef5ccd6cSJohn Marino return NULL;
1383*ef5ccd6cSJohn Marino }
1384*ef5ccd6cSJohn Marino
1385*ef5ccd6cSJohn Marino char *
apply_type_printers(void * ignore,struct type * type)1386*ef5ccd6cSJohn Marino apply_type_printers (void *ignore, struct type *type)
1387*ef5ccd6cSJohn Marino {
1388*ef5ccd6cSJohn Marino return NULL;
1389*ef5ccd6cSJohn Marino }
1390*ef5ccd6cSJohn Marino
1391*ef5ccd6cSJohn Marino void
free_type_printers(void * arg)1392*ef5ccd6cSJohn Marino free_type_printers (void *arg)
1393*ef5ccd6cSJohn Marino {
1394*ef5ccd6cSJohn Marino }
1395*ef5ccd6cSJohn Marino
13965796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
13975796c8dcSSimon Schubert
13985796c8dcSSimon Schubert
1399a45ae5f8SJohn Marino
1400a45ae5f8SJohn Marino /* Lists for 'set python' commands. */
1401a45ae5f8SJohn Marino
1402a45ae5f8SJohn Marino static struct cmd_list_element *user_set_python_list;
1403a45ae5f8SJohn Marino static struct cmd_list_element *user_show_python_list;
1404a45ae5f8SJohn Marino
1405a45ae5f8SJohn Marino /* Function for use by 'set python' prefix command. */
1406a45ae5f8SJohn Marino
1407a45ae5f8SJohn Marino static void
user_set_python(char * args,int from_tty)1408a45ae5f8SJohn Marino user_set_python (char *args, int from_tty)
1409a45ae5f8SJohn Marino {
1410a45ae5f8SJohn Marino help_list (user_set_python_list, "set python ", all_commands,
1411a45ae5f8SJohn Marino gdb_stdout);
1412a45ae5f8SJohn Marino }
1413a45ae5f8SJohn Marino
1414a45ae5f8SJohn Marino /* Function for use by 'show python' prefix command. */
1415a45ae5f8SJohn Marino
1416a45ae5f8SJohn Marino static void
user_show_python(char * args,int from_tty)1417a45ae5f8SJohn Marino user_show_python (char *args, int from_tty)
1418a45ae5f8SJohn Marino {
1419a45ae5f8SJohn Marino cmd_show_list (user_show_python_list, from_tty, "");
14205796c8dcSSimon Schubert }
14215796c8dcSSimon Schubert
14225796c8dcSSimon Schubert /* Initialize the Python code. */
14235796c8dcSSimon Schubert
1424*ef5ccd6cSJohn Marino #ifdef HAVE_PYTHON
1425*ef5ccd6cSJohn Marino
1426*ef5ccd6cSJohn Marino /* This is installed as a final cleanup and cleans up the
1427*ef5ccd6cSJohn Marino interpreter. This lets Python's 'atexit' work. */
1428*ef5ccd6cSJohn Marino
1429*ef5ccd6cSJohn Marino static void
finalize_python(void * ignore)1430*ef5ccd6cSJohn Marino finalize_python (void *ignore)
1431*ef5ccd6cSJohn Marino {
1432*ef5ccd6cSJohn Marino /* We don't use ensure_python_env here because if we ever ran the
1433*ef5ccd6cSJohn Marino cleanup, gdb would crash -- because the cleanup calls into the
1434*ef5ccd6cSJohn Marino Python interpreter, which we are about to destroy. It seems
1435*ef5ccd6cSJohn Marino clearer to make the needed calls explicitly here than to create a
1436*ef5ccd6cSJohn Marino cleanup and then mysteriously discard it. */
1437*ef5ccd6cSJohn Marino (void) PyGILState_Ensure ();
1438*ef5ccd6cSJohn Marino python_gdbarch = target_gdbarch ();
1439*ef5ccd6cSJohn Marino python_language = current_language;
1440*ef5ccd6cSJohn Marino
1441*ef5ccd6cSJohn Marino Py_Finalize ();
1442*ef5ccd6cSJohn Marino }
1443*ef5ccd6cSJohn Marino #endif
1444*ef5ccd6cSJohn Marino
14455796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes. */
14465796c8dcSSimon Schubert extern initialize_file_ftype _initialize_python;
14475796c8dcSSimon Schubert
14485796c8dcSSimon Schubert void
_initialize_python(void)14495796c8dcSSimon Schubert _initialize_python (void)
14505796c8dcSSimon Schubert {
1451*ef5ccd6cSJohn Marino char *progname;
1452*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1453*ef5ccd6cSJohn Marino int i;
1454*ef5ccd6cSJohn Marino size_t progsize, count;
1455*ef5ccd6cSJohn Marino char *oldloc;
1456*ef5ccd6cSJohn Marino wchar_t *progname_copy;
1457*ef5ccd6cSJohn Marino #endif
1458*ef5ccd6cSJohn Marino
1459*ef5ccd6cSJohn Marino add_com ("python-interactive", class_obscure,
1460*ef5ccd6cSJohn Marino python_interactive_command,
1461*ef5ccd6cSJohn Marino #ifdef HAVE_PYTHON
1462*ef5ccd6cSJohn Marino _("\
1463*ef5ccd6cSJohn Marino Start an interactive Python prompt.\n\
1464*ef5ccd6cSJohn Marino \n\
1465*ef5ccd6cSJohn Marino To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
1466*ef5ccd6cSJohn Marino prompt).\n\
1467*ef5ccd6cSJohn Marino \n\
1468*ef5ccd6cSJohn Marino Alternatively, a single-line Python command can be given as an\n\
1469*ef5ccd6cSJohn Marino argument, and if the command is an expression, the result will be\n\
1470*ef5ccd6cSJohn Marino printed. For example:\n\
1471*ef5ccd6cSJohn Marino \n\
1472*ef5ccd6cSJohn Marino (gdb) python-interactive 2 + 3\n\
1473*ef5ccd6cSJohn Marino 5\n\
1474*ef5ccd6cSJohn Marino ")
1475*ef5ccd6cSJohn Marino #else /* HAVE_PYTHON */
1476*ef5ccd6cSJohn Marino _("\
1477*ef5ccd6cSJohn Marino Start a Python interactive prompt.\n\
1478*ef5ccd6cSJohn Marino \n\
1479*ef5ccd6cSJohn Marino Python scripting is not supported in this copy of GDB.\n\
1480*ef5ccd6cSJohn Marino This command is only a placeholder.")
1481*ef5ccd6cSJohn Marino #endif /* HAVE_PYTHON */
1482*ef5ccd6cSJohn Marino );
1483*ef5ccd6cSJohn Marino add_com_alias ("pi", "python-interactive", class_obscure, 1);
1484a45ae5f8SJohn Marino
14855796c8dcSSimon Schubert add_com ("python", class_obscure, python_command,
14865796c8dcSSimon Schubert #ifdef HAVE_PYTHON
14875796c8dcSSimon Schubert _("\
14885796c8dcSSimon Schubert Evaluate a Python command.\n\
14895796c8dcSSimon Schubert \n\
14905796c8dcSSimon Schubert The command can be given as an argument, for instance:\n\
14915796c8dcSSimon Schubert \n\
14925796c8dcSSimon Schubert python print 23\n\
14935796c8dcSSimon Schubert \n\
14945796c8dcSSimon Schubert If no argument is given, the following lines are read and used\n\
14955796c8dcSSimon Schubert as the Python commands. Type a line containing \"end\" to indicate\n\
14965796c8dcSSimon Schubert the end of the command.")
14975796c8dcSSimon Schubert #else /* HAVE_PYTHON */
14985796c8dcSSimon Schubert _("\
14995796c8dcSSimon Schubert Evaluate a Python command.\n\
15005796c8dcSSimon Schubert \n\
15015796c8dcSSimon Schubert Python scripting is not supported in this copy of GDB.\n\
15025796c8dcSSimon Schubert This command is only a placeholder.")
15035796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
15045796c8dcSSimon Schubert );
1505*ef5ccd6cSJohn Marino add_com_alias ("py", "python", class_obscure, 1);
1506a45ae5f8SJohn Marino
1507a45ae5f8SJohn Marino /* Add set/show python print-stack. */
1508a45ae5f8SJohn Marino add_prefix_cmd ("python", no_class, user_show_python,
1509a45ae5f8SJohn Marino _("Prefix command for python preference settings."),
1510a45ae5f8SJohn Marino &user_show_python_list, "show python ", 0,
1511a45ae5f8SJohn Marino &showlist);
1512a45ae5f8SJohn Marino
1513a45ae5f8SJohn Marino add_prefix_cmd ("python", no_class, user_set_python,
1514a45ae5f8SJohn Marino _("Prefix command for python preference settings."),
1515a45ae5f8SJohn Marino &user_set_python_list, "set python ", 0,
1516a45ae5f8SJohn Marino &setlist);
1517a45ae5f8SJohn Marino
1518a45ae5f8SJohn Marino add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
1519a45ae5f8SJohn Marino &gdbpy_should_print_stack, _("\
1520a45ae5f8SJohn Marino Set mode for Python stack dump on error."), _("\
1521a45ae5f8SJohn Marino Show the mode of Python stack printing on error."), _("\
1522a45ae5f8SJohn Marino none == no stack or message will be printed.\n\
1523a45ae5f8SJohn Marino full == a message and a stack will be printed.\n\
1524a45ae5f8SJohn Marino message == an error message without a stack will be printed."),
15255796c8dcSSimon Schubert NULL, NULL,
1526a45ae5f8SJohn Marino &user_set_python_list,
1527a45ae5f8SJohn Marino &user_show_python_list);
15285796c8dcSSimon Schubert
15295796c8dcSSimon Schubert #ifdef HAVE_PYTHON
1530cf7f2e2dSJohn Marino #ifdef WITH_PYTHON_PATH
1531cf7f2e2dSJohn Marino /* Work around problem where python gets confused about where it is,
1532cf7f2e2dSJohn Marino and then can't find its libraries, etc.
1533cf7f2e2dSJohn Marino NOTE: Python assumes the following layout:
1534cf7f2e2dSJohn Marino /foo/bin/python
1535cf7f2e2dSJohn Marino /foo/lib/pythonX.Y/...
1536cf7f2e2dSJohn Marino This must be done before calling Py_Initialize. */
1537*ef5ccd6cSJohn Marino progname = concat (ldirname (python_libdir), SLASH_STRING, "bin",
1538*ef5ccd6cSJohn Marino SLASH_STRING, "python", NULL);
1539*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1540*ef5ccd6cSJohn Marino oldloc = setlocale (LC_ALL, NULL);
1541*ef5ccd6cSJohn Marino setlocale (LC_ALL, "");
1542*ef5ccd6cSJohn Marino progsize = strlen (progname);
1543*ef5ccd6cSJohn Marino if (progsize == (size_t) -1)
1544*ef5ccd6cSJohn Marino {
1545*ef5ccd6cSJohn Marino fprintf (stderr, "Could not convert python path to string\n");
1546*ef5ccd6cSJohn Marino return;
1547*ef5ccd6cSJohn Marino }
1548*ef5ccd6cSJohn Marino progname_copy = PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
1549*ef5ccd6cSJohn Marino if (!progname_copy)
1550*ef5ccd6cSJohn Marino {
1551*ef5ccd6cSJohn Marino fprintf (stderr, "out of memory\n");
1552*ef5ccd6cSJohn Marino return;
1553*ef5ccd6cSJohn Marino }
1554*ef5ccd6cSJohn Marino count = mbstowcs (progname_copy, progname, progsize + 1);
1555*ef5ccd6cSJohn Marino if (count == (size_t) -1)
1556*ef5ccd6cSJohn Marino {
1557*ef5ccd6cSJohn Marino fprintf (stderr, "Could not convert python path to string\n");
1558*ef5ccd6cSJohn Marino return;
1559*ef5ccd6cSJohn Marino }
1560*ef5ccd6cSJohn Marino setlocale (LC_ALL, oldloc);
1561*ef5ccd6cSJohn Marino
1562*ef5ccd6cSJohn Marino /* Note that Py_SetProgramName expects the string it is passed to
1563*ef5ccd6cSJohn Marino remain alive for the duration of the program's execution, so
1564*ef5ccd6cSJohn Marino it is not freed after this call. */
1565*ef5ccd6cSJohn Marino Py_SetProgramName (progname_copy);
1566*ef5ccd6cSJohn Marino #else
1567*ef5ccd6cSJohn Marino Py_SetProgramName (progname);
1568*ef5ccd6cSJohn Marino #endif
1569cf7f2e2dSJohn Marino #endif
1570cf7f2e2dSJohn Marino
15715796c8dcSSimon Schubert Py_Initialize ();
15725796c8dcSSimon Schubert PyEval_InitThreads ();
15735796c8dcSSimon Schubert
1574*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1575*ef5ccd6cSJohn Marino gdb_module = PyModule_Create (&GdbModuleDef);
1576*ef5ccd6cSJohn Marino /* Add _gdb module to the list of known built-in modules. */
1577*ef5ccd6cSJohn Marino _PyImport_FixupBuiltin (gdb_module, "_gdb");
1578*ef5ccd6cSJohn Marino #else
1579*ef5ccd6cSJohn Marino gdb_module = Py_InitModule ("_gdb", GdbMethods);
1580*ef5ccd6cSJohn Marino #endif
15815796c8dcSSimon Schubert
15825796c8dcSSimon Schubert /* The casts to (char*) are for python 2.4. */
15835796c8dcSSimon Schubert PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
15845796c8dcSSimon Schubert PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
1585c50c785cSJohn Marino PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
1586c50c785cSJohn Marino (char*) target_name);
1587c50c785cSJohn Marino
1588c50c785cSJohn Marino /* Add stream constants. */
1589c50c785cSJohn Marino PyModule_AddIntConstant (gdb_module, "STDOUT", 0);
1590c50c785cSJohn Marino PyModule_AddIntConstant (gdb_module, "STDERR", 1);
1591c50c785cSJohn Marino PyModule_AddIntConstant (gdb_module, "STDLOG", 2);
1592c50c785cSJohn Marino
1593c50c785cSJohn Marino gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL);
1594c50c785cSJohn Marino PyModule_AddObject (gdb_module, "error", gdbpy_gdb_error);
1595c50c785cSJohn Marino
1596c50c785cSJohn Marino gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError",
1597c50c785cSJohn Marino gdbpy_gdb_error, NULL);
1598c50c785cSJohn Marino PyModule_AddObject (gdb_module, "MemoryError", gdbpy_gdb_memory_error);
1599c50c785cSJohn Marino
1600cf7f2e2dSJohn Marino gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
1601cf7f2e2dSJohn Marino PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
1602cf7f2e2dSJohn Marino
1603*ef5ccd6cSJohn Marino gdbpy_initialize_gdb_readline ();
1604cf7f2e2dSJohn Marino gdbpy_initialize_auto_load ();
16055796c8dcSSimon Schubert gdbpy_initialize_values ();
16065796c8dcSSimon Schubert gdbpy_initialize_frames ();
16075796c8dcSSimon Schubert gdbpy_initialize_commands ();
1608cf7f2e2dSJohn Marino gdbpy_initialize_symbols ();
1609cf7f2e2dSJohn Marino gdbpy_initialize_symtabs ();
1610cf7f2e2dSJohn Marino gdbpy_initialize_blocks ();
16115796c8dcSSimon Schubert gdbpy_initialize_functions ();
1612cf7f2e2dSJohn Marino gdbpy_initialize_parameters ();
16135796c8dcSSimon Schubert gdbpy_initialize_types ();
1614cf7f2e2dSJohn Marino gdbpy_initialize_pspace ();
16155796c8dcSSimon Schubert gdbpy_initialize_objfile ();
1616cf7f2e2dSJohn Marino gdbpy_initialize_breakpoints ();
1617a45ae5f8SJohn Marino gdbpy_initialize_finishbreakpoints ();
1618cf7f2e2dSJohn Marino gdbpy_initialize_lazy_string ();
1619cf7f2e2dSJohn Marino gdbpy_initialize_thread ();
1620cf7f2e2dSJohn Marino gdbpy_initialize_inferior ();
1621c50c785cSJohn Marino gdbpy_initialize_events ();
1622c50c785cSJohn Marino
1623c50c785cSJohn Marino gdbpy_initialize_eventregistry ();
1624c50c785cSJohn Marino gdbpy_initialize_py_events ();
1625c50c785cSJohn Marino gdbpy_initialize_event ();
1626c50c785cSJohn Marino gdbpy_initialize_stop_event ();
1627c50c785cSJohn Marino gdbpy_initialize_signal_event ();
1628c50c785cSJohn Marino gdbpy_initialize_breakpoint_event ();
1629c50c785cSJohn Marino gdbpy_initialize_continue_event ();
1630c50c785cSJohn Marino gdbpy_initialize_exited_event ();
1631c50c785cSJohn Marino gdbpy_initialize_thread_event ();
1632a45ae5f8SJohn Marino gdbpy_initialize_new_objfile_event () ;
1633*ef5ccd6cSJohn Marino gdbpy_initialize_arch ();
1634a45ae5f8SJohn Marino
1635a45ae5f8SJohn Marino observer_attach_before_prompt (before_prompt_hook);
16365796c8dcSSimon Schubert
16375796c8dcSSimon Schubert gdbpy_to_string_cst = PyString_FromString ("to_string");
16385796c8dcSSimon Schubert gdbpy_children_cst = PyString_FromString ("children");
16395796c8dcSSimon Schubert gdbpy_display_hint_cst = PyString_FromString ("display_hint");
16405796c8dcSSimon Schubert gdbpy_doc_cst = PyString_FromString ("__doc__");
1641cf7f2e2dSJohn Marino gdbpy_enabled_cst = PyString_FromString ("enabled");
1642c50c785cSJohn Marino gdbpy_value_cst = PyString_FromString ("value");
16435796c8dcSSimon Schubert
1644c50c785cSJohn Marino /* Release the GIL while gdb runs. */
1645c50c785cSJohn Marino PyThreadState_Swap (NULL);
1646c50c785cSJohn Marino PyEval_ReleaseLock ();
1647c50c785cSJohn Marino
1648*ef5ccd6cSJohn Marino make_final_cleanup (finalize_python, NULL);
1649c50c785cSJohn Marino #endif /* HAVE_PYTHON */
1650c50c785cSJohn Marino }
1651c50c785cSJohn Marino
1652c50c785cSJohn Marino #ifdef HAVE_PYTHON
1653c50c785cSJohn Marino
1654c50c785cSJohn Marino /* Perform the remaining python initializations.
1655c50c785cSJohn Marino These must be done after GDB is at least mostly initialized.
1656c50c785cSJohn Marino E.g., The "info pretty-printer" command needs the "info" prefix
1657c50c785cSJohn Marino command installed. */
1658c50c785cSJohn Marino
1659c50c785cSJohn Marino void
finish_python_initialization(void)1660c50c785cSJohn Marino finish_python_initialization (void)
1661c50c785cSJohn Marino {
1662*ef5ccd6cSJohn Marino PyObject *m;
1663*ef5ccd6cSJohn Marino char *gdb_pythondir;
1664*ef5ccd6cSJohn Marino PyObject *sys_path;
1665c50c785cSJohn Marino struct cleanup *cleanup;
1666c50c785cSJohn Marino
1667c50c785cSJohn Marino cleanup = ensure_python_env (get_current_arch (), current_language);
1668c50c785cSJohn Marino
1669*ef5ccd6cSJohn Marino /* Add the initial data-directory to sys.path. */
16705796c8dcSSimon Schubert
1671*ef5ccd6cSJohn Marino gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL);
1672*ef5ccd6cSJohn Marino make_cleanup (xfree, gdb_pythondir);
1673*ef5ccd6cSJohn Marino
1674*ef5ccd6cSJohn Marino sys_path = PySys_GetObject ("path");
1675*ef5ccd6cSJohn Marino
1676*ef5ccd6cSJohn Marino /* If sys.path is not defined yet, define it first. */
1677*ef5ccd6cSJohn Marino if (!(sys_path && PyList_Check (sys_path)))
1678*ef5ccd6cSJohn Marino {
1679*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1680*ef5ccd6cSJohn Marino PySys_SetPath (L"");
1681*ef5ccd6cSJohn Marino #else
1682*ef5ccd6cSJohn Marino PySys_SetPath ("");
1683*ef5ccd6cSJohn Marino #endif
1684*ef5ccd6cSJohn Marino sys_path = PySys_GetObject ("path");
1685*ef5ccd6cSJohn Marino }
1686*ef5ccd6cSJohn Marino if (sys_path && PyList_Check (sys_path))
1687*ef5ccd6cSJohn Marino {
1688*ef5ccd6cSJohn Marino PyObject *pythondir;
1689*ef5ccd6cSJohn Marino int err;
1690*ef5ccd6cSJohn Marino
1691*ef5ccd6cSJohn Marino pythondir = PyString_FromString (gdb_pythondir);
1692*ef5ccd6cSJohn Marino if (pythondir == NULL)
1693*ef5ccd6cSJohn Marino goto fail;
1694*ef5ccd6cSJohn Marino
1695*ef5ccd6cSJohn Marino err = PyList_Insert (sys_path, 0, pythondir);
1696*ef5ccd6cSJohn Marino if (err)
1697*ef5ccd6cSJohn Marino goto fail;
1698*ef5ccd6cSJohn Marino
1699*ef5ccd6cSJohn Marino Py_DECREF (pythondir);
1700*ef5ccd6cSJohn Marino }
1701*ef5ccd6cSJohn Marino else
1702*ef5ccd6cSJohn Marino goto fail;
1703*ef5ccd6cSJohn Marino
1704*ef5ccd6cSJohn Marino /* Import the gdb module to finish the initialization, and
1705*ef5ccd6cSJohn Marino add it to __main__ for convenience. */
1706*ef5ccd6cSJohn Marino m = PyImport_AddModule ("__main__");
1707*ef5ccd6cSJohn Marino if (m == NULL)
1708*ef5ccd6cSJohn Marino goto fail;
1709*ef5ccd6cSJohn Marino
1710*ef5ccd6cSJohn Marino gdb_python_module = PyImport_ImportModule ("gdb");
1711*ef5ccd6cSJohn Marino if (gdb_python_module == NULL)
1712*ef5ccd6cSJohn Marino {
1713*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1714*ef5ccd6cSJohn Marino /* This is passed in one call to warning so that blank lines aren't
1715*ef5ccd6cSJohn Marino inserted between each line of text. */
1716*ef5ccd6cSJohn Marino warning (_("\n"
1717*ef5ccd6cSJohn Marino "Could not load the Python gdb module from `%s'.\n"
1718*ef5ccd6cSJohn Marino "Limited Python support is available from the _gdb module.\n"
1719*ef5ccd6cSJohn Marino "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
1720*ef5ccd6cSJohn Marino gdb_pythondir);
1721*ef5ccd6cSJohn Marino do_cleanups (cleanup);
1722*ef5ccd6cSJohn Marino return;
1723*ef5ccd6cSJohn Marino }
1724*ef5ccd6cSJohn Marino
1725*ef5ccd6cSJohn Marino if (PyModule_AddObject (m, "gdb", gdb_python_module))
1726*ef5ccd6cSJohn Marino goto fail;
1727*ef5ccd6cSJohn Marino
1728*ef5ccd6cSJohn Marino /* Keep the reference to gdb_python_module since it is in a global
1729*ef5ccd6cSJohn Marino variable. */
1730*ef5ccd6cSJohn Marino
1731*ef5ccd6cSJohn Marino do_cleanups (cleanup);
1732*ef5ccd6cSJohn Marino return;
1733*ef5ccd6cSJohn Marino
1734*ef5ccd6cSJohn Marino fail:
1735*ef5ccd6cSJohn Marino gdbpy_print_stack ();
1736*ef5ccd6cSJohn Marino warning (_("internal error: Unhandled Python exception"));
1737c50c785cSJohn Marino do_cleanups (cleanup);
1738c50c785cSJohn Marino }
17395796c8dcSSimon Schubert
17405796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
17415796c8dcSSimon Schubert
17425796c8dcSSimon Schubert
17435796c8dcSSimon Schubert
1744c50c785cSJohn Marino #ifdef HAVE_PYTHON
17455796c8dcSSimon Schubert
17465796c8dcSSimon Schubert static PyMethodDef GdbMethods[] =
17475796c8dcSSimon Schubert {
17485796c8dcSSimon Schubert { "history", gdbpy_history, METH_VARARGS,
17495796c8dcSSimon Schubert "Get a value from history" },
1750cf7f2e2dSJohn Marino { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
17515796c8dcSSimon Schubert "Execute a gdb command" },
17525796c8dcSSimon Schubert { "parameter", gdbpy_parameter, METH_VARARGS,
17535796c8dcSSimon Schubert "Return a gdb parameter's value" },
17545796c8dcSSimon Schubert
1755cf7f2e2dSJohn Marino { "breakpoints", gdbpy_breakpoints, METH_NOARGS,
1756cf7f2e2dSJohn Marino "Return a tuple of all breakpoint objects" },
1757cf7f2e2dSJohn Marino
17585796c8dcSSimon Schubert { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
17595796c8dcSSimon Schubert "Find the default visualizer for a Value." },
17605796c8dcSSimon Schubert
1761cf7f2e2dSJohn Marino { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS,
1762cf7f2e2dSJohn Marino "Return the current Progspace." },
1763cf7f2e2dSJohn Marino { "progspaces", gdbpy_progspaces, METH_NOARGS,
1764cf7f2e2dSJohn Marino "Return a sequence of all progspaces." },
1765cf7f2e2dSJohn Marino
17665796c8dcSSimon Schubert { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
17675796c8dcSSimon Schubert "Return the current Objfile being loaded, or None." },
17685796c8dcSSimon Schubert { "objfiles", gdbpy_objfiles, METH_NOARGS,
17695796c8dcSSimon Schubert "Return a sequence of all loaded objfiles." },
17705796c8dcSSimon Schubert
1771c50c785cSJohn Marino { "newest_frame", gdbpy_newest_frame, METH_NOARGS,
1772c50c785cSJohn Marino "newest_frame () -> gdb.Frame.\n\
1773c50c785cSJohn Marino Return the newest frame object." },
17745796c8dcSSimon Schubert { "selected_frame", gdbpy_selected_frame, METH_NOARGS,
17755796c8dcSSimon Schubert "selected_frame () -> gdb.Frame.\n\
17765796c8dcSSimon Schubert Return the selected frame object." },
17775796c8dcSSimon Schubert { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
17785796c8dcSSimon Schubert "stop_reason_string (Integer) -> String.\n\
17795796c8dcSSimon Schubert Return a string explaining unwind stop reason." },
17805796c8dcSSimon Schubert
17815796c8dcSSimon Schubert { "lookup_type", (PyCFunction) gdbpy_lookup_type,
17825796c8dcSSimon Schubert METH_VARARGS | METH_KEYWORDS,
17835796c8dcSSimon Schubert "lookup_type (name [, block]) -> type\n\
17845796c8dcSSimon Schubert Return a Type corresponding to the given name." },
1785cf7f2e2dSJohn Marino { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol,
1786cf7f2e2dSJohn Marino METH_VARARGS | METH_KEYWORDS,
1787cf7f2e2dSJohn Marino "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
1788cf7f2e2dSJohn Marino Return a tuple with the symbol corresponding to the given name (or None) and\n\
1789cf7f2e2dSJohn Marino a boolean indicating if name is a field of the current implied argument\n\
1790cf7f2e2dSJohn Marino `this' (when the current language is object-oriented)." },
1791c50c785cSJohn Marino { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol,
1792c50c785cSJohn Marino METH_VARARGS | METH_KEYWORDS,
1793c50c785cSJohn Marino "lookup_global_symbol (name [, domain]) -> symbol\n\
1794c50c785cSJohn Marino Return the symbol corresponding to the given name (or None)." },
1795cf7f2e2dSJohn Marino { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS,
1796cf7f2e2dSJohn Marino "Return the block containing the given pc value, or None." },
1797c50c785cSJohn Marino { "solib_name", gdbpy_solib_name, METH_VARARGS,
1798c50c785cSJohn Marino "solib_name (Long) -> String.\n\
1799c50c785cSJohn Marino Return the name of the shared library holding a given address, or None." },
1800c50c785cSJohn Marino { "decode_line", gdbpy_decode_line, METH_VARARGS,
1801c50c785cSJohn Marino "decode_line (String) -> Tuple. Decode a string argument the way\n\
1802c50c785cSJohn Marino that 'break' or 'edit' does. Return a tuple containing two elements.\n\
1803c50c785cSJohn Marino The first element contains any unparsed portion of the String parameter\n\
1804c50c785cSJohn Marino (or None if the string was fully parsed). The second element contains\n\
1805c50c785cSJohn Marino a tuple that contains all the locations that match, represented as\n\
1806c50c785cSJohn Marino gdb.Symtab_and_line objects (or None)."},
1807cf7f2e2dSJohn Marino { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
1808cf7f2e2dSJohn Marino "parse_and_eval (String) -> Value.\n\
1809cf7f2e2dSJohn Marino Parse String as an expression, evaluate it, and return the result as a Value."
1810cf7f2e2dSJohn Marino },
1811*ef5ccd6cSJohn Marino { "find_pc_line", gdbpy_find_pc_line, METH_VARARGS,
1812*ef5ccd6cSJohn Marino "find_pc_line (pc) -> Symtab_and_line.\n\
1813*ef5ccd6cSJohn Marino Return the gdb.Symtab_and_line object corresponding to the pc value." },
1814cf7f2e2dSJohn Marino
1815c50c785cSJohn Marino { "post_event", gdbpy_post_event, METH_VARARGS,
1816c50c785cSJohn Marino "Post an event into gdb's event loop." },
1817c50c785cSJohn Marino
1818cf7f2e2dSJohn Marino { "target_charset", gdbpy_target_charset, METH_NOARGS,
1819cf7f2e2dSJohn Marino "target_charset () -> string.\n\
1820cf7f2e2dSJohn Marino Return the name of the current target charset." },
1821cf7f2e2dSJohn Marino { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
1822cf7f2e2dSJohn Marino "target_wide_charset () -> string.\n\
1823cf7f2e2dSJohn Marino Return the name of the current target wide charset." },
1824cf7f2e2dSJohn Marino
1825cf7f2e2dSJohn Marino { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS,
1826cf7f2e2dSJohn Marino "string_to_argv (String) -> Array.\n\
1827cf7f2e2dSJohn Marino Parse String and return an argv-like array.\n\
1828cf7f2e2dSJohn Marino Arguments are separate by spaces and may be quoted."
1829cf7f2e2dSJohn Marino },
1830c50c785cSJohn Marino { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS,
18315796c8dcSSimon Schubert "Write a string using gdb's filtered stream." },
1832c50c785cSJohn Marino { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS,
18335796c8dcSSimon Schubert "Flush gdb's filtered stdout stream." },
1834cf7f2e2dSJohn Marino { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
1835cf7f2e2dSJohn Marino "selected_thread () -> gdb.InferiorThread.\n\
1836cf7f2e2dSJohn Marino Return the selected thread object." },
1837a45ae5f8SJohn Marino { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
1838a45ae5f8SJohn Marino "selected_inferior () -> gdb.Inferior.\n\
1839a45ae5f8SJohn Marino Return the selected inferior object." },
1840cf7f2e2dSJohn Marino { "inferiors", gdbpy_inferiors, METH_NOARGS,
1841cf7f2e2dSJohn Marino "inferiors () -> (gdb.Inferior, ...).\n\
1842cf7f2e2dSJohn Marino Return a tuple containing all inferiors." },
18435796c8dcSSimon Schubert {NULL, NULL, 0, NULL}
18445796c8dcSSimon Schubert };
18455796c8dcSSimon Schubert
1846*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1847*ef5ccd6cSJohn Marino static struct PyModuleDef GdbModuleDef =
1848*ef5ccd6cSJohn Marino {
1849*ef5ccd6cSJohn Marino PyModuleDef_HEAD_INIT,
1850*ef5ccd6cSJohn Marino "_gdb",
1851*ef5ccd6cSJohn Marino NULL,
1852*ef5ccd6cSJohn Marino -1,
1853*ef5ccd6cSJohn Marino GdbMethods,
1854*ef5ccd6cSJohn Marino NULL,
1855*ef5ccd6cSJohn Marino NULL,
1856*ef5ccd6cSJohn Marino NULL,
1857*ef5ccd6cSJohn Marino NULL
1858*ef5ccd6cSJohn Marino };
1859*ef5ccd6cSJohn Marino #endif
18605796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
1861