gdb: introduce target_waitkind_str, use it in target_waitstatus::to_string
[binutils-gdb.git] / gdb / target / waitstatus.h
1 /* Target waitstatus definitions and prototypes.
2
3 Copyright (C) 1990-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef TARGET_WAITSTATUS_H
21 #define TARGET_WAITSTATUS_H
22
23 #include "gdbsupport/gdb_signals.h"
24
25 /* Stuff for target_wait. */
26
27 /* Generally, what has the program done? */
28 enum target_waitkind
29 {
30 /* The program has exited. The exit status is in value.integer. */
31 TARGET_WAITKIND_EXITED,
32
33 /* The program has stopped with a signal. Which signal is in
34 value.sig. */
35 TARGET_WAITKIND_STOPPED,
36
37 /* The program has terminated with a signal. Which signal is in
38 value.sig. */
39 TARGET_WAITKIND_SIGNALLED,
40
41 /* The program is letting us know that it dynamically loaded
42 something (e.g. it called load(2) on AIX). */
43 TARGET_WAITKIND_LOADED,
44
45 /* The program has forked. A "related" process' PTID is in
46 value.related_pid. I.e., if the child forks, value.related_pid
47 is the parent's ID. */
48 TARGET_WAITKIND_FORKED,
49
50 /* The program has vforked. A "related" process's PTID is in
51 value.related_pid. */
52 TARGET_WAITKIND_VFORKED,
53
54 /* The program has exec'ed a new executable file. The new file's
55 pathname is pointed to by value.execd_pathname. */
56 TARGET_WAITKIND_EXECD,
57
58 /* The program had previously vforked, and now the child is done
59 with the shared memory region, because it exec'ed or exited.
60 Note that the event is reported to the vfork parent. This is
61 only used if GDB did not stay attached to the vfork child,
62 otherwise, a TARGET_WAITKIND_EXECD or
63 TARGET_WAITKIND_EXIT|SIGNALLED event associated with the child
64 has the same effect. */
65 TARGET_WAITKIND_VFORK_DONE,
66
67 /* The program has entered or returned from a system call. On
68 HP-UX, this is used in the hardware watchpoint implementation.
69 The syscall's unique integer ID number is in
70 value.syscall_id. */
71 TARGET_WAITKIND_SYSCALL_ENTRY,
72 TARGET_WAITKIND_SYSCALL_RETURN,
73
74 /* Nothing happened, but we stopped anyway. This perhaps should
75 be handled within target_wait, but I'm not sure target_wait
76 should be resuming the inferior. */
77 TARGET_WAITKIND_SPURIOUS,
78
79 /* An event has occured, but we should wait again.
80 Remote_async_wait() returns this when there is an event
81 on the inferior, but the rest of the world is not interested in
82 it. The inferior has not stopped, but has just sent some output
83 to the console, for instance. In this case, we want to go back
84 to the event loop and wait there for another event from the
85 inferior, rather than being stuck in the remote_async_wait()
86 function. This way the event loop is responsive to other events,
87 like for instance the user typing. */
88 TARGET_WAITKIND_IGNORE,
89
90 /* The target has run out of history information,
91 and cannot run backward any further. */
92 TARGET_WAITKIND_NO_HISTORY,
93
94 /* There are no resumed children left in the program. */
95 TARGET_WAITKIND_NO_RESUMED,
96
97 /* The thread was created. */
98 TARGET_WAITKIND_THREAD_CREATED,
99
100 /* The thread has exited. The exit status is in value.integer. */
101 TARGET_WAITKIND_THREAD_EXITED,
102 };
103
104 /* Return KIND as a string. */
105
106 static inline const char *
107 target_waitkind_str (target_waitkind kind)
108 {
109 /* Make sure the compiler warns if a new TARGET_WAITKIND enumerator is added
110 but not handled here. */
111 #pragma GCC diagnostic push
112 #pragma GCC diagnostic error "-Wswitch"
113 switch (kind)
114 {
115 case TARGET_WAITKIND_EXITED:
116 return "EXITED";
117 case TARGET_WAITKIND_STOPPED:
118 return "STOPPED";
119 case TARGET_WAITKIND_SIGNALLED:
120 return "SIGNALLED";
121 case TARGET_WAITKIND_LOADED:
122 return "LOADED";
123 case TARGET_WAITKIND_FORKED:
124 return "FORKED";
125 case TARGET_WAITKIND_VFORKED:
126 return "VFORKED";
127 case TARGET_WAITKIND_EXECD:
128 return "EXECD";
129 case TARGET_WAITKIND_VFORK_DONE:
130 return "VFORK_DONE";
131 case TARGET_WAITKIND_SYSCALL_ENTRY:
132 return "SYSCALL_ENTRY";
133 case TARGET_WAITKIND_SYSCALL_RETURN:
134 return "SYSCALL_RETURN";
135 case TARGET_WAITKIND_SPURIOUS:
136 return "SPURIOUS";
137 case TARGET_WAITKIND_IGNORE:
138 return "IGNORE";
139 case TARGET_WAITKIND_NO_HISTORY:
140 return "NO_HISTORY";
141 case TARGET_WAITKIND_NO_RESUMED:
142 return "NO_RESUMED";
143 case TARGET_WAITKIND_THREAD_CREATED:
144 return "THREAD_CREATED";
145 case TARGET_WAITKIND_THREAD_EXITED:
146 return "THREAD_EXITED";
147 };
148 #pragma GCC diagnostic pop
149
150 gdb_assert_not_reached ("invalid target_waitkind value: %d\n", (int) kind);
151 }
152
153 struct target_waitstatus
154 {
155 /* Default constructor. */
156 target_waitstatus () = default;
157
158 /* Copy constructor. */
159
160 target_waitstatus (const target_waitstatus &other)
161 {
162 m_kind = other.m_kind;
163 m_value = other.m_value;
164
165 if (m_kind == TARGET_WAITKIND_EXECD)
166 m_value.execd_pathname = xstrdup (m_value.execd_pathname);
167 }
168
169 /* Move constructor. */
170
171 target_waitstatus (target_waitstatus &&other)
172 {
173 m_kind = other.m_kind;
174 m_value = other.m_value;
175
176 if (m_kind == TARGET_WAITKIND_EXECD)
177 other.m_value.execd_pathname = nullptr;
178
179 other.reset ();
180 }
181
182 /* Copy assignment operator. */
183
184 target_waitstatus &operator= (const target_waitstatus &rhs)
185 {
186 this->reset ();
187 m_kind = rhs.m_kind;
188 m_value = rhs.m_value;
189
190 if (m_kind == TARGET_WAITKIND_EXECD)
191 m_value.execd_pathname = xstrdup (m_value.execd_pathname);
192
193 return *this;
194 }
195
196 /* Move assignment operator. */
197
198 target_waitstatus &operator= (target_waitstatus &&rhs)
199 {
200 this->reset ();
201 m_kind = rhs.m_kind;
202 m_value = rhs.m_value;
203
204 if (m_kind == TARGET_WAITKIND_EXECD)
205 rhs.m_value.execd_pathname = nullptr;
206
207 rhs.reset ();
208
209 return *this;
210 }
211
212 /* Destructor. */
213
214 ~target_waitstatus ()
215 {
216 this->reset ();
217 }
218
219 /* Setters: set the wait status kind plus any associated data. */
220
221 void set_exited (int exit_status)
222 {
223 this->reset ();
224 m_kind = TARGET_WAITKIND_EXITED;
225 m_value.exit_status = exit_status;
226 }
227
228 void set_stopped (gdb_signal sig)
229 {
230 this->reset ();
231 m_kind = TARGET_WAITKIND_STOPPED;
232 m_value.sig = sig;
233 }
234
235 void set_signalled (gdb_signal sig)
236 {
237 this->reset ();
238 m_kind = TARGET_WAITKIND_SIGNALLED;
239 m_value.sig = sig;
240 }
241
242 void set_loaded ()
243 {
244 this->reset ();
245 m_kind = TARGET_WAITKIND_LOADED;
246 }
247
248 void set_forked (ptid_t child_ptid)
249 {
250 this->reset ();
251 m_kind = TARGET_WAITKIND_FORKED;
252 m_value.child_ptid = child_ptid;
253 }
254
255 void set_vforked (ptid_t child_ptid)
256 {
257 this->reset ();
258 m_kind = TARGET_WAITKIND_VFORKED;
259 m_value.child_ptid = child_ptid;
260 }
261
262 void set_execd (gdb::unique_xmalloc_ptr<char> execd_pathname)
263 {
264 this->reset ();
265 m_kind = TARGET_WAITKIND_EXECD;
266 m_value.execd_pathname = execd_pathname.release ();
267 }
268
269 void set_vfork_done ()
270 {
271 this->reset ();
272 m_kind = TARGET_WAITKIND_VFORK_DONE;
273 }
274
275 void set_syscall_entry (int syscall_number)
276 {
277 this->reset ();
278 m_kind = TARGET_WAITKIND_SYSCALL_ENTRY;
279 m_value.syscall_number = syscall_number;
280 }
281
282 void set_syscall_return (int syscall_number)
283 {
284 this->reset ();
285 m_kind = TARGET_WAITKIND_SYSCALL_RETURN;
286 m_value.syscall_number = syscall_number;
287 }
288
289 void set_spurious ()
290 {
291 this->reset ();
292 m_kind = TARGET_WAITKIND_SPURIOUS;
293 }
294
295 void set_ignore ()
296 {
297 this->reset ();
298 m_kind = TARGET_WAITKIND_IGNORE;
299 }
300
301 void set_no_history ()
302 {
303 this->reset ();
304 m_kind = TARGET_WAITKIND_NO_HISTORY;
305 }
306
307 void set_no_resumed ()
308 {
309 this->reset ();
310 m_kind = TARGET_WAITKIND_NO_RESUMED;
311 }
312
313 void set_thread_created ()
314 {
315 this->reset ();
316 m_kind = TARGET_WAITKIND_THREAD_CREATED;
317 }
318
319 void set_thread_exited (int exit_status)
320 {
321 this->reset ();
322 m_kind = TARGET_WAITKIND_THREAD_EXITED;
323 m_value.exit_status = exit_status;
324 }
325
326 /* Get the kind of this wait status. */
327
328 target_waitkind kind () const
329 {
330 return m_kind;
331 }
332
333 /* Getters for the associated data.
334
335 Getters can only be used if the wait status is of the appropriate kind.
336 See the setters above or the assertions below to know which data is
337 associated to which kind. */
338
339 int exit_status () const
340 {
341 gdb_assert (m_kind == TARGET_WAITKIND_EXITED
342 || m_kind == TARGET_WAITKIND_THREAD_EXITED);
343 return m_value.exit_status;
344 }
345
346 gdb_signal sig () const
347 {
348 gdb_assert (m_kind == TARGET_WAITKIND_STOPPED
349 || m_kind == TARGET_WAITKIND_SIGNALLED);
350 return m_value.sig;
351 }
352
353 ptid_t child_ptid () const
354 {
355 gdb_assert (m_kind == TARGET_WAITKIND_FORKED
356 || m_kind == TARGET_WAITKIND_VFORKED);
357 return m_value.child_ptid;
358 }
359
360 const char *execd_pathname () const
361 {
362 gdb_assert (m_kind == TARGET_WAITKIND_EXECD);
363 return m_value.execd_pathname;
364 }
365
366 int syscall_number () const
367 {
368 gdb_assert (m_kind == TARGET_WAITKIND_SYSCALL_ENTRY
369 || m_kind == TARGET_WAITKIND_SYSCALL_RETURN);
370 return m_value.syscall_number;
371 }
372
373 /* Return a pretty printed form of target_waitstatus.
374
375 This is only meant to be used in debug messages, not for user-visible
376 messages. */
377 std::string to_string () const;
378
379 private:
380 /* Reset the wait status to its original state. */
381 void reset ()
382 {
383 if (m_kind == TARGET_WAITKIND_EXECD)
384 xfree (m_value.execd_pathname);
385
386 m_kind = TARGET_WAITKIND_IGNORE;
387 }
388
389 target_waitkind m_kind = TARGET_WAITKIND_IGNORE;
390
391 /* Additional information about the event. */
392 union
393 {
394 /* Exit status */
395 int exit_status;
396 /* Signal number */
397 enum gdb_signal sig;
398 /* Forked child pid */
399 ptid_t child_ptid;
400 /* execd pathname */
401 char *execd_pathname;
402 /* Syscall number */
403 int syscall_number;
404 } m_value;
405 };
406
407 /* Extended reasons that can explain why a target/thread stopped for a
408 trap signal. */
409
410 enum target_stop_reason
411 {
412 /* Either not stopped, or stopped for a reason that doesn't require
413 special tracking. */
414 TARGET_STOPPED_BY_NO_REASON,
415
416 /* Stopped by a software breakpoint. */
417 TARGET_STOPPED_BY_SW_BREAKPOINT,
418
419 /* Stopped by a hardware breakpoint. */
420 TARGET_STOPPED_BY_HW_BREAKPOINT,
421
422 /* Stopped by a watchpoint. */
423 TARGET_STOPPED_BY_WATCHPOINT,
424
425 /* Stopped by a single step finishing. */
426 TARGET_STOPPED_BY_SINGLE_STEP
427 };
428
429 #endif /* TARGET_WAITSTATUS_H */