Skip to content

Commit 1da5d0e

Browse files
committed
Change how Python architecture and language are handled
Currently, gdb's Python layer captures the current architecture and language when "entering" Python code. This has some undesirable effects, and so this series changes how this is handled. First, there is code like this: gdbpy_enter enter_py (python_gdbarch, python_language); This is incorrect, because both of these are NULL when not otherwise assigned. This can cause crashes in some cases -- I've added one to the test suite. (Note that this crasher is just an example, other ones along the same lines are possible.) Second, when the language is captured in this way, it means that Python code cannot affect the current language for its own purposes. It's reasonable to want to write code like this: gdb.execute('set language mumble') ... stuff using the current language gdb.execute('set language previous-value') However, this won't actually work, because the language is captured on entry. I've added a test to show this as well. This patch changes gdb to try to avoid capturing the current values. The Python concept of the current gdbarch is only set in those few cases where a non-default value is computed or needed; and the language is not captured at all -- instead, in the cases where it's required, the current language is temporarily changed.
1 parent 8a782bb commit 1da5d0e

20 files changed

+146
-105
lines changed

gdb/python/py-breakpoint.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
858858
const struct breakpoint_ops *ops =
859859
breakpoint_ops_for_event_location (location.get (), false);
860860

861-
create_breakpoint (python_gdbarch,
861+
create_breakpoint (gdbpy_enter::get_gdbarch (),
862862
location.get (), NULL, -1, NULL, false,
863863
0,
864864
temporary_bp, type,
@@ -954,15 +954,13 @@ gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
954954
int stop;
955955
struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
956956
PyObject *py_bp = (PyObject *) bp_obj;
957-
struct gdbarch *garch;
958957

959958
if (bp_obj == NULL)
960959
return EXT_LANG_BP_STOP_UNSET;
961960

962961
stop = -1;
963-
garch = b->gdbarch ? b->gdbarch : get_current_arch ();
964962

965-
gdbpy_enter enter_py (garch, current_language);
963+
gdbpy_enter enter_py (b->gdbarch);
966964

967965
if (bp_obj->is_finish_bp)
968966
bpfinishpy_pre_stop_hook (bp_obj);
@@ -1005,15 +1003,13 @@ gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
10051003
struct breakpoint *b)
10061004
{
10071005
PyObject *py_bp;
1008-
struct gdbarch *garch;
10091006

10101007
if (b->py_bp_object == NULL)
10111008
return 0;
10121009

10131010
py_bp = (PyObject *) b->py_bp_object;
1014-
garch = b->gdbarch ? b->gdbarch : get_current_arch ();
10151011

1016-
gdbpy_enter enter_py (garch, current_language);
1012+
gdbpy_enter enter_py (b->gdbarch);
10171013
return PyObject_HasAttrString (py_bp, stop_func);
10181014
}
10191015

@@ -1048,8 +1044,7 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
10481044
return;
10491045
}
10501046

1051-
struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1052-
gdbpy_enter enter_py (garch, current_language);
1047+
gdbpy_enter enter_py (bp->gdbarch);
10531048

10541049
if (bppy_pending_object)
10551050
{
@@ -1099,8 +1094,7 @@ gdbpy_breakpoint_deleted (struct breakpoint *b)
10991094
bp = get_breakpoint (num);
11001095
if (bp)
11011096
{
1102-
struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1103-
gdbpy_enter enter_py (garch, current_language);
1097+
gdbpy_enter enter_py (b->gdbarch);
11041098

11051099
gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
11061100
if (bp_obj != NULL)
@@ -1131,8 +1125,7 @@ gdbpy_breakpoint_modified (struct breakpoint *b)
11311125
bp = get_breakpoint (num);
11321126
if (bp)
11331127
{
1134-
struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1135-
gdbpy_enter enter_py (garch, current_language);
1128+
gdbpy_enter enter_py (b->gdbarch);
11361129

11371130
PyObject *bp_obj = (PyObject *) bp->py_bp_object;
11381131
if (bp_obj)

gdb/python/py-cmd.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ cmdpy_dont_repeat (PyObject *self, PyObject *args)
9090
static void
9191
cmdpy_destroyer (struct cmd_list_element *self, void *context)
9292
{
93-
gdbpy_enter enter_py (get_current_arch (), current_language);
93+
gdbpy_enter enter_py;
9494

9595
/* Release our hold on the command object. */
9696
gdbpy_ref<cmdpy_object> cmd ((cmdpy_object *) context);
@@ -104,7 +104,7 @@ cmdpy_function (const char *args, int from_tty, cmd_list_element *command)
104104
{
105105
cmdpy_object *obj = (cmdpy_object *) command->context ();
106106

107-
gdbpy_enter enter_py (get_current_arch (), current_language);
107+
gdbpy_enter enter_py;
108108

109109
if (! obj)
110110
error (_("Invalid invocation of Python command object."));
@@ -223,7 +223,7 @@ cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
223223
completion_tracker &tracker,
224224
const char *text, const char *word)
225225
{
226-
gdbpy_enter enter_py (get_current_arch (), current_language);
226+
gdbpy_enter enter_py;
227227

228228
/* Calling our helper to obtain a reference to the PyObject of the Python
229229
function. */
@@ -266,7 +266,7 @@ cmdpy_completer (struct cmd_list_element *command,
266266
completion_tracker &tracker,
267267
const char *text, const char *word)
268268
{
269-
gdbpy_enter enter_py (get_current_arch (), current_language);
269+
gdbpy_enter enter_py;
270270

271271
/* Calling our helper to obtain a reference to the PyObject of the Python
272272
function. */

gdb/python/py-connection.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ connpy_connection_removed (process_stratum_target *target)
161161
if (!gdb_python_initialized)
162162
return;
163163

164-
gdbpy_enter enter_py (get_current_arch (), current_language);
164+
gdbpy_enter enter_py;
165165

166166
if (!evregpy_no_listeners_p (gdb_py_events.connection_removed))
167167
if (emit_connection_event (target, gdb_py_events.connection_removed) < 0)

gdb/python/py-finishbreakpoint.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
293293
/* Set a breakpoint on the return address. */
294294
event_location_up location
295295
= new_address_location (get_frame_pc (prev_frame), NULL, 0);
296-
create_breakpoint (python_gdbarch,
296+
create_breakpoint (gdbpy_enter::get_gdbarch (),
297297
location.get (), NULL, thread, NULL, false,
298298
0,
299299
1 /*temp_flag*/,
@@ -380,7 +380,7 @@ bpfinishpy_detect_out_scope_cb (struct breakpoint *b,
380380
static void
381381
bpfinishpy_handle_stop (struct bpstat *bs, int print_frame)
382382
{
383-
gdbpy_enter enter_py (get_current_arch (), current_language);
383+
gdbpy_enter enter_py;
384384

385385
for (breakpoint *bp : all_breakpoints_safe ())
386386
bpfinishpy_detect_out_scope_cb (bp, bs == NULL ? NULL : bs->breakpoint_at);
@@ -392,7 +392,7 @@ bpfinishpy_handle_stop (struct bpstat *bs, int print_frame)
392392
static void
393393
bpfinishpy_handle_exit (struct inferior *inf)
394394
{
395-
gdbpy_enter enter_py (target_gdbarch (), current_language);
395+
gdbpy_enter enter_py (target_gdbarch ());
396396

397397
for (breakpoint *bp : all_breakpoints_safe ())
398398
bpfinishpy_detect_out_scope_cb (bp, nullptr);

gdb/python/py-framefilter.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr<char> *name,
7474
if (*name == NULL)
7575
return EXT_LANG_BT_ERROR;
7676
/* If the API returns a string (and not a symbol), then there is
77-
no symbol derived language available and the frame filter has
78-
either overridden the symbol with a string, or supplied a
79-
entirely synthetic symbol/value pairing. In that case, use
80-
python_language. */
81-
*language = python_language;
77+
no symbol derived language available and the frame filter has
78+
either overridden the symbol with a string, or supplied a
79+
entirely synthetic symbol/value pairing. In that case, use
80+
the current language. */
81+
*language = current_language;
8282
*sym = NULL;
8383
*sym_block = NULL;
8484
}
@@ -1157,7 +1157,7 @@ gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
11571157
return EXT_LANG_BT_NO_FILTERS;
11581158
}
11591159

1160-
gdbpy_enter enter_py (gdbarch, current_language);
1160+
gdbpy_enter enter_py (gdbarch);
11611161

11621162
/* When we're limiting the number of frames, be careful to request
11631163
one extra frame, so that we can print a message if there are more

gdb/python/py-inferior.c

+13-14
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ python_on_normal_stop (struct bpstat *bs, int print_frame)
8686

8787
stop_signal = inferior_thread ()->stop_signal ();
8888

89-
gdbpy_enter enter_py (get_current_arch (), current_language);
89+
gdbpy_enter enter_py;
9090

9191
if (emit_stop_event (bs, stop_signal) < 0)
9292
gdbpy_print_stack ();
@@ -98,7 +98,7 @@ python_on_resume (ptid_t ptid)
9898
if (!gdb_python_initialized)
9999
return;
100100

101-
gdbpy_enter enter_py (target_gdbarch (), current_language);
101+
gdbpy_enter enter_py (target_gdbarch ());
102102

103103
if (emit_continue_event (ptid) < 0)
104104
gdbpy_print_stack ();
@@ -110,7 +110,7 @@ python_on_resume (ptid_t ptid)
110110
static void
111111
python_on_inferior_call_pre (ptid_t thread, CORE_ADDR address)
112112
{
113-
gdbpy_enter enter_py (target_gdbarch (), current_language);
113+
gdbpy_enter enter_py (target_gdbarch ());
114114

115115
if (emit_inferior_call_event (INFERIOR_CALL_PRE, thread, address) < 0)
116116
gdbpy_print_stack ();
@@ -122,7 +122,7 @@ python_on_inferior_call_pre (ptid_t thread, CORE_ADDR address)
122122
static void
123123
python_on_inferior_call_post (ptid_t thread, CORE_ADDR address)
124124
{
125-
gdbpy_enter enter_py (target_gdbarch (), current_language);
125+
gdbpy_enter enter_py (target_gdbarch ());
126126

127127
if (emit_inferior_call_event (INFERIOR_CALL_POST, thread, address) < 0)
128128
gdbpy_print_stack ();
@@ -135,7 +135,7 @@ python_on_inferior_call_post (ptid_t thread, CORE_ADDR address)
135135
static void
136136
python_on_memory_change (struct inferior *inferior, CORE_ADDR addr, ssize_t len, const bfd_byte *data)
137137
{
138-
gdbpy_enter enter_py (target_gdbarch (), current_language);
138+
gdbpy_enter enter_py (target_gdbarch ());
139139

140140
if (emit_memory_changed_event (addr, len) < 0)
141141
gdbpy_print_stack ();
@@ -148,7 +148,7 @@ python_on_memory_change (struct inferior *inferior, CORE_ADDR addr, ssize_t len,
148148
static void
149149
python_on_register_change (struct frame_info *frame, int regnum)
150150
{
151-
gdbpy_enter enter_py (target_gdbarch (), current_language);
151+
gdbpy_enter enter_py (target_gdbarch ());
152152

153153
if (emit_register_changed_event (frame, regnum) < 0)
154154
gdbpy_print_stack ();
@@ -162,7 +162,7 @@ python_inferior_exit (struct inferior *inf)
162162
if (!gdb_python_initialized)
163163
return;
164164

165-
gdbpy_enter enter_py (target_gdbarch (), current_language);
165+
gdbpy_enter enter_py (target_gdbarch ());
166166

167167
if (inf->has_exit_code)
168168
exit_code = &inf->exit_code;
@@ -183,8 +183,7 @@ python_new_objfile (struct objfile *objfile)
183183

184184
gdbpy_enter enter_py (objfile != NULL
185185
? objfile->arch ()
186-
: target_gdbarch (),
187-
current_language);
186+
: target_gdbarch ());
188187

189188
if (objfile == NULL)
190189
{
@@ -237,7 +236,7 @@ python_new_inferior (struct inferior *inf)
237236
if (!gdb_python_initialized)
238237
return;
239238

240-
gdbpy_enter enter_py (python_gdbarch, python_language);
239+
gdbpy_enter enter_py;
241240

242241
if (evregpy_no_listeners_p (gdb_py_events.new_inferior))
243242
return;
@@ -265,7 +264,7 @@ python_inferior_deleted (struct inferior *inf)
265264
if (!gdb_python_initialized)
266265
return;
267266

268-
gdbpy_enter enter_py (python_gdbarch, python_language);
267+
gdbpy_enter enter_py;
269268

270269
if (evregpy_no_listeners_p (gdb_py_events.inferior_deleted))
271270
return;
@@ -312,7 +311,7 @@ add_thread_object (struct thread_info *tp)
312311
if (!gdb_python_initialized)
313312
return;
314313

315-
gdbpy_enter enter_py (python_gdbarch, python_language);
314+
gdbpy_enter enter_py;
316315

317316
gdbpy_ref<thread_object> thread_obj = create_thread_object (tp);
318317
if (thread_obj == NULL)
@@ -348,7 +347,7 @@ delete_thread_object (struct thread_info *tp, int ignore)
348347
if (!gdb_python_initialized)
349348
return;
350349

351-
gdbpy_enter enter_py (python_gdbarch, python_language);
350+
gdbpy_enter enter_py;
352351

353352
gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
354353
if (inf_obj == NULL)
@@ -792,7 +791,7 @@ py_free_inferior (struct inferior *inf, void *datum)
792791
if (!gdb_python_initialized)
793792
return;
794793

795-
gdbpy_enter enter_py (python_gdbarch, python_language);
794+
gdbpy_enter enter_py;
796795
gdbpy_ref<inferior_object> inf_obj ((inferior_object *) datum);
797796

798797
inf_obj->inferior = NULL;

gdb/python/py-membuf.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ mbpy_str (PyObject *self)
8383

8484
return PyString_FromFormat (_("Memory buffer for address %s, \
8585
which is %s bytes long."),
86-
paddress (python_gdbarch, membuf_obj->addr),
86+
paddress (gdbpy_enter::get_gdbarch (),
87+
membuf_obj->addr),
8788
pulongest (membuf_obj->length));
8889
}
8990

gdb/python/py-objfile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
661661
static void
662662
py_free_objfile (struct objfile *objfile, void *datum)
663663
{
664-
gdbpy_enter enter_py (objfile->arch (), current_language);
664+
gdbpy_enter enter_py (objfile->arch ());
665665
gdbpy_ref<objfile_object> object ((objfile_object *) datum);
666666
object->objfile = NULL;
667667
}

gdb/python/py-param.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ get_set_value (const char *args, int from_tty,
396396
PyObject *obj = (PyObject *) c->context ();
397397
gdb::unique_xmalloc_ptr<char> set_doc_string;
398398

399-
gdbpy_enter enter_py (get_current_arch (), current_language);
399+
gdbpy_enter enter_py;
400400
gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
401401

402402
if (set_doc_func == NULL)
@@ -431,7 +431,7 @@ get_show_value (struct ui_file *file, int from_tty,
431431
PyObject *obj = (PyObject *) c->context ();
432432
gdb::unique_xmalloc_ptr<char> show_doc_string;
433433

434-
gdbpy_enter enter_py (get_current_arch (), current_language);
434+
gdbpy_enter enter_py;
435435
gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
436436

437437
if (show_doc_func == NULL)

gdb/python/py-progspace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ py_free_pspace (struct program_space *pspace, void *datum)
472472
being deleted. */
473473
struct gdbarch *arch = target_gdbarch ();
474474

475-
gdbpy_enter enter_py (arch, current_language);
475+
gdbpy_enter enter_py (arch);
476476
gdbpy_ref<pspace_object> object ((pspace_object *) datum);
477477
object->pspace = NULL;
478478
}

0 commit comments

Comments
 (0)