1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
#include "defs.h"
#include "user-selection.h"
#include "inferior.h"
#include "gdbthread.h"
#include "observer.h"
#include "gdbcmd.h"
/* The user-visible selection. */
static user_selection main_user_selection;
/* Knob for user-selection related debug traces. */
static int debug_user_selection = 0;
/* See user-selection.h. */
user_selection *
global_user_selection ()
{
return &main_user_selection;
}
/* See user-selection.h. */
void
init_global_user_selection ()
{
/* Fetch the initial inferior, which should have been added by now. The
initial inferior is selected on startup. */
struct inferior *inf = find_inferior_id (1);
gdb_assert (inf != nullptr);
global_user_selection ()->select_inferior (inf, false);
}
/* See user-selection.h. */
bool
user_selection::select_inferior (struct inferior *inf, bool notify)
{
const char *debug_prefix = "user_selection::select_thread";
/* There is always a selected inferior. */
gdb_assert (inf != nullptr);
if (debug_user_selection)
printf_unfiltered ("%s: num=%d\n", debug_prefix, inf->num);
/* No-op if this is already the currently selected inferior. */
if (inf == m_inferior)
{
if (debug_user_selection)
printf_unfiltered ("%s: already selected inferior", debug_prefix);
return false;
}
/* When we change inferior, thread and frame will change as well. */
user_selected_what what = USER_SELECTED_INFERIOR | USER_SELECTED_THREAD | USER_SELECTED_FRAME;
/* INF becomes selected. */
m_inferior = inf;
/* Clear the thread and frame fields. */
if (m_thread != nullptr)
{
m_thread->put ();
m_thread = nullptr;
}
m_frame_id = null_frame_id;
m_frame_level = INVALID_FRAME_LEVEL;
if (m_inferior->pid != 0)
{
/* This inferior is executing, so it should have threads. Select the first
one. */
m_thread = iterate_over_threads (
[inf] (struct thread_info *thread, void *) -> int
{
return inf->pid == ptid_get_pid (thread->ptid);
}
);
/* We expect this inferior to have at least one thread. If we didn't
find it, we have a problem. */
gdb_assert (m_thread != nullptr);
/* Acquire a strong reference, so the thread_info object doesn't get freed
while it's selected. */
m_thread->get ();
}
sanity_check ();
if (notify)
observer_notify_global_user_selection_changed (this, what);
return true;
}
/* See user-selection.h. */
bool
user_selection::select_thread (struct thread_info *thread, bool notify)
{
const char *debug_prefix = "user_selection::select_thread";
/* When changing thread, the frame will necessarily change as well. */
user_selected_what what = USER_SELECTED_THREAD | USER_SELECTED_FRAME;
if (debug_user_selection)
printf_unfiltered ("%s: num=%d, ptid=%s",
debug_prefix, thread->global_num,
target_pid_to_str (thread->ptid));
/* No-op if this is already the currently selected thread. */
if (thread == m_thread)
{
if (debug_user_selection)
printf_unfiltered ("%s: already selected thread", debug_prefix);
return false;
}
/* Clear the frame fields. */
m_frame_id = null_frame_id;
m_frame_level = INVALID_FRAME_LEVEL;
/* Release the reference. */
if (m_thread != nullptr)
m_thread->put ();
m_thread = thread;
if (m_thread != nullptr)
{
/* Acquire a strong reference, so the thread_info object doesn't get freed
while it's selected. */
m_thread->get ();
/* The inferior of the thread becomes the newly selected inferior, if it's
not already. */
if (m_inferior != thread->inf)
{
m_inferior = thread->inf;
what |= USER_SELECTED_INFERIOR;
}
}
sanity_check ();
if (notify)
observer_notify_global_user_selection_changed (this, what);
return true;
}
bool
user_selection::select_frame (struct frame_info *frame, bool notify)
{
const char *debug_prefix = "user_selection::select_frame";
/* No-op if this is already the selected frame. */
if (frame_id_eq (m_frame_id, get_frame_id (frame))
&& m_frame_level == frame_relative_level (frame))
return false;
m_frame_id = get_frame_id (frame);
m_frame_level = frame_relative_level (frame);
if (debug_user_selection)
{
string_file buf;
fprint_frame_id (&buf, m_frame_id);
printf_unfiltered ("%s: Selected frame #%d %s\n", debug_prefix,
m_frame_level, buf.c_str ());
}
sanity_check ();
if (notify)
observer_notify_global_user_selection_changed (this, USER_SELECTED_FRAME);
return true;
}
/* Do some basic checks to verify that the selection is coherent. */
void
user_selection::sanity_check () const
{
/* We always have a current inferior. */
gdb_assert (m_inferior != nullptr);
/* The selected thread must match the selected inferior. */
if (m_thread != nullptr)
gdb_assert (m_thread->inf == m_inferior);
/* Can't have a current frame without a current thread. */
if (m_frame_level != INVALID_FRAME_LEVEL)
gdb_assert (m_thread != nullptr);
}
/* Apply US to the core of gdb. This makes the internally selected inferior,
thread and frame reflect the selection in US. */
static void
apply_user_selection (user_selection *us)
{
/* Select inferior. */
set_current_inferior (us->inferior ());
set_current_program_space (us->inferior ()->pspace);
/* Select thread. */
if (us->thread () != nullptr)
switch_to_thread (us->thread ()->ptid);
else
switch_to_thread (null_ptid);
/* Select frame. */
if (us->has_valid_frame ())
{
struct frame_info *fi = us->frame ();
select_frame (fi);
}
}
/* Try to make the current (as in: where the program is currently stopped) frame
the selected one. */
void
user_selection::try_select_current_frame ()
{
/* This function should only be called when we don't have a selected frame
yet. */
gdb_assert (!has_valid_frame ());
/* We need to select the relevant inferior/thread internally in order for
get_current_frame to work. */
apply_user_selection (this);
TRY
{
struct frame_info *fi = get_current_frame ();
m_frame_id = get_frame_id (fi);
m_frame_level = frame_relative_level (fi);
}
CATCH (exception, RETURN_MASK_ALL)
{
/* We can't determine the current frame, too bad. */
}
END_CATCH
}
/* See user-selection.h. */
void
apply_global_user_selection ()
{
apply_user_selection (global_user_selection ());
}
/* Callback for the new_thread observer. */
static void
global_user_selection_on_new_thread (struct thread_info *tp)
{
user_selection *us = global_user_selection ();
/* If a new thread is created while:
1. We don't have a currently selected thread,
2. The inferior of the new thread is the currently selected inferior,
then we silently make that new thread the selected one. It covers the case
of automatically selecting the initial thread when starting an
inferior. */
if (us->thread () == nullptr && tp->inf == us->inferior ())
us->select_thread (tp, false);
}
/* Callback for the on_exited observer. */
static void
global_user_selection_on_exited (struct inferior *inferior)
{
user_selection *us = global_user_selection ();
/* When an inferior exits and it's the current inferior, it means we have one
of its thread currently selected. De-select it. */
if (inferior == us->inferior ())
us->select_thread (NULL, false);
}
/* Callback for the on_target_resumed observer. */
static void
global_user_selection_on_target_resumed (ptid_t ptid)
{
user_selection *us = global_user_selection ();
/* If the selected thread has been resumed, our frame isn't valid anymore. */
if (us->thread () != nullptr && ptid_match (us->thread ()->ptid, ptid))
us->select_frame (NULL, false);
}
/* Implementation of the "maintenance print user-selection" command. */
static void
maint_print_user_selection (char *cmd, int from_tty)
{
user_selection *us = global_user_selection ();
struct inferior *inf = us->inferior ();
struct thread_info *tp = us->thread ();
struct frame_id frame_id = us->raw_frame_id ();
int frame_level = us->raw_frame_level ();
/* Print inferior. */
fprintf_filtered(gdb_stdout, "inferior %p (num=%d)\n", inf, inf->num);
/* Print thread. */
if (tp != nullptr)
fprintf_filtered (gdb_stdout,
"thread %p (gnum=%d, per-inf-num=%d, inf=%p)\n", tp,
tp->global_num, tp->per_inf_num, tp->inf);
else
fprintf_filtered(gdb_stdout, "thread null\n");
/* Print frame. */
fprint_frame_id (gdb_stdout, frame_id);
fprintf_filtered (gdb_stdout, ", level=%d\n", frame_level);
}
/* Initialize observer callbacks and commands. */
void
_initialize_user_selection ()
{
observer_attach_new_thread (global_user_selection_on_new_thread);
observer_attach_inferior_exit (global_user_selection_on_exited);
observer_attach_target_resumed (global_user_selection_on_target_resumed);
add_setshow_boolean_cmd ("user-selection", class_maintenance,
&debug_user_selection, "blah", "blah", "blah", NULL,
NULL, &setdebuglist, &showdebuglist);
add_cmd ("user-selection", class_maintenance, maint_print_user_selection,
"foo", &maintenanceprintlist);
}
|