gdb: maintain ptid -> thread map, optimize find_thread_ptid
[binutils-gdb.git] / gdb / inferior.h
1 /* Variables that describe the inferior process running under GDB:
2 Where it is, why it stopped, and how to step it.
3
4 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #if !defined (INFERIOR_H)
22 #define INFERIOR_H 1
23
24 #include <exception>
25 #include <list>
26
27 struct target_waitstatus;
28 struct frame_info;
29 struct ui_file;
30 struct type;
31 struct gdbarch;
32 struct regcache;
33 struct ui_out;
34 struct terminal_info;
35 struct target_desc_info;
36 struct inferior;
37 struct thread_info;
38
39 /* For bpstat. */
40 #include "breakpoint.h"
41
42 /* For enum gdb_signal. */
43 #include "target.h"
44
45 /* For struct frame_id. */
46 #include "frame.h"
47
48 /* For gdb_environ. */
49 #include "gdbsupport/environ.h"
50
51 #include "progspace.h"
52 #include "registry.h"
53
54 #include "symfile-add-flags.h"
55 #include "gdbsupport/refcounted-object.h"
56 #include "gdbsupport/forward-scope-exit.h"
57 #include "gdbsupport/gdb_unique_ptr.h"
58 #include "gdbsupport/intrusive_list.h"
59
60 #include "gdbsupport/common-inferior.h"
61 #include "gdbthread.h"
62
63 #include "process-stratum-target.h"
64 #include "displaced-stepping.h"
65
66 #include <unordered_map>
67
68 struct infcall_suspend_state;
69 struct infcall_control_state;
70
71 extern void restore_infcall_suspend_state (struct infcall_suspend_state *);
72 extern void restore_infcall_control_state (struct infcall_control_state *);
73
74 /* A deleter for infcall_suspend_state that calls
75 restore_infcall_suspend_state. */
76 struct infcall_suspend_state_deleter
77 {
78 void operator() (struct infcall_suspend_state *state) const
79 {
80 try
81 {
82 restore_infcall_suspend_state (state);
83 }
84 catch (const gdb_exception_error &e)
85 {
86 /* If we are restoring the inferior state due to an exception,
87 some error message will be printed. So, only warn the user
88 when we cannot restore during normal execution. */
89 bool unwinding;
90 #if __cpp_lib_uncaught_exceptions
91 unwinding = std::uncaught_exceptions () > 0;
92 #else
93 unwinding = std::uncaught_exception ();
94 #endif
95 if (!unwinding)
96 warning (_("Failed to restore inferior state: %s"), e.what ());
97 }
98 }
99 };
100
101 /* A unique_ptr specialization for infcall_suspend_state. */
102 typedef std::unique_ptr<infcall_suspend_state, infcall_suspend_state_deleter>
103 infcall_suspend_state_up;
104
105 extern infcall_suspend_state_up save_infcall_suspend_state ();
106
107 /* A deleter for infcall_control_state that calls
108 restore_infcall_control_state. */
109 struct infcall_control_state_deleter
110 {
111 void operator() (struct infcall_control_state *state) const
112 {
113 restore_infcall_control_state (state);
114 }
115 };
116
117 /* A unique_ptr specialization for infcall_control_state. */
118 typedef std::unique_ptr<infcall_control_state, infcall_control_state_deleter>
119 infcall_control_state_up;
120
121 extern infcall_control_state_up save_infcall_control_state ();
122
123 extern void discard_infcall_suspend_state (struct infcall_suspend_state *);
124 extern void discard_infcall_control_state (struct infcall_control_state *);
125
126 extern readonly_detached_regcache *
127 get_infcall_suspend_state_regcache (struct infcall_suspend_state *);
128
129 extern void set_sigint_trap (void);
130
131 extern void clear_sigint_trap (void);
132
133 /* Collected pid, tid, etc. of the debugged inferior. When there's
134 no inferior, inferior_ptid.pid () will be 0. */
135
136 extern ptid_t inferior_ptid;
137
138 extern void generic_mourn_inferior (void);
139
140 extern CORE_ADDR unsigned_pointer_to_address (struct gdbarch *gdbarch,
141 struct type *type,
142 const gdb_byte *buf);
143 extern void unsigned_address_to_pointer (struct gdbarch *gdbarch,
144 struct type *type, gdb_byte *buf,
145 CORE_ADDR addr);
146 extern CORE_ADDR signed_pointer_to_address (struct gdbarch *gdbarch,
147 struct type *type,
148 const gdb_byte *buf);
149 extern void address_to_signed_pointer (struct gdbarch *gdbarch,
150 struct type *type, gdb_byte *buf,
151 CORE_ADDR addr);
152
153 extern void reopen_exec_file (void);
154
155 /* From misc files */
156
157 extern void default_print_registers_info (struct gdbarch *gdbarch,
158 struct ui_file *file,
159 struct frame_info *frame,
160 int regnum, int all);
161
162 /* Default implementation of gdbarch_print_float_info. Print
163 the values of all floating point registers. */
164
165 extern void default_print_float_info (struct gdbarch *gdbarch,
166 struct ui_file *file,
167 struct frame_info *frame,
168 const char *args);
169
170 extern void child_terminal_info (struct target_ops *self, const char *, int);
171
172 extern void child_terminal_ours (struct target_ops *self);
173
174 extern void child_terminal_ours_for_output (struct target_ops *self);
175
176 extern void child_terminal_inferior (struct target_ops *self);
177
178 extern void child_terminal_save_inferior (struct target_ops *self);
179
180 extern void child_terminal_init (struct target_ops *self);
181
182 extern void child_terminal_init_with_pgrp (int pgrp);
183
184 extern void child_pass_ctrlc (struct target_ops *self);
185
186 extern void child_interrupt (struct target_ops *self);
187
188 /* From fork-child.c */
189
190 /* Helper function to call STARTUP_INFERIOR with PID and NUM_TRAPS.
191 This function already calls set_executing. Return the ptid_t from
192 STARTUP_INFERIOR. */
193 extern ptid_t gdb_startup_inferior (pid_t pid, int num_traps);
194
195 /* From infcmd.c */
196
197 /* Initial inferior setup. Determines the exec file is not yet known,
198 takes any necessary post-attaching actions, fetches the target
199 description and syncs the shared library list. */
200
201 extern void setup_inferior (int from_tty);
202
203 extern void post_create_inferior (int from_tty);
204
205 extern void attach_command (const char *, int);
206
207 extern const char *get_inferior_args (void);
208
209 extern void set_inferior_args (const char *);
210
211 extern void set_inferior_args_vector (int, char **);
212
213 extern void registers_info (const char *, int);
214
215 extern void continue_1 (int all_threads);
216
217 extern void interrupt_target_1 (bool all_threads);
218
219 using delete_longjmp_breakpoint_cleanup
220 = FORWARD_SCOPE_EXIT (delete_longjmp_breakpoint);
221
222 extern void detach_command (const char *, int);
223
224 extern void notice_new_inferior (struct thread_info *, bool, int);
225
226 extern struct value *get_return_value (struct value *function,
227 struct type *value_type);
228
229 /* Prepare for execution command. TARGET is the target that will run
230 the command. BACKGROUND determines whether this is a foreground
231 (synchronous) or background (asynchronous) command. */
232
233 extern void prepare_execution_command (struct target_ops *target,
234 int background);
235
236 /* Nonzero if stopped due to completion of a stack dummy routine. */
237
238 extern enum stop_stack_kind stop_stack_dummy;
239
240 /* Nonzero if program stopped due to a random (unexpected) signal in
241 inferior process. */
242
243 extern int stopped_by_random_signal;
244
245 /* Print notices on inferior events (attach, detach, etc.), set with
246 `set print inferior-events'. */
247 extern bool print_inferior_events;
248
249 /* Anything but NO_STOP_QUIETLY means we expect a trap and the caller
250 will handle it themselves. STOP_QUIETLY is used when running in
251 the shell before the child program has been exec'd and when running
252 through shared library loading. STOP_QUIETLY_REMOTE is used when
253 setting up a remote connection; it is like STOP_QUIETLY_NO_SIGSTOP
254 except that there is no need to hide a signal. */
255
256 /* STOP_QUIETLY_NO_SIGSTOP is used to handle a tricky situation with attach.
257 When doing an attach, the kernel stops the debuggee with a SIGSTOP.
258 On newer GNU/Linux kernels (>= 2.5.61) the handling of SIGSTOP for
259 a ptraced process has changed. Earlier versions of the kernel
260 would ignore these SIGSTOPs, while now SIGSTOP is treated like any
261 other signal, i.e. it is not muffled.
262
263 If the gdb user does a 'continue' after the 'attach', gdb passes
264 the global variable stop_signal (which stores the signal from the
265 attach, SIGSTOP) to the ptrace(PTRACE_CONT,...) call. This is
266 problematic, because the kernel doesn't ignore such SIGSTOP
267 now. I.e. it is reported back to gdb, which in turn presents it
268 back to the user.
269
270 To avoid the problem, we use STOP_QUIETLY_NO_SIGSTOP, which allows
271 gdb to clear the value of stop_signal after the attach, so that it
272 is not passed back down to the kernel. */
273
274 enum stop_kind
275 {
276 NO_STOP_QUIETLY = 0,
277 STOP_QUIETLY,
278 STOP_QUIETLY_REMOTE,
279 STOP_QUIETLY_NO_SIGSTOP
280 };
281
282 \f
283 /* Possible values for gdbarch_call_dummy_location. */
284 #define ON_STACK 1
285 #define AT_ENTRY_POINT 4
286
287 /* Base class for target-specific inferior data. */
288
289 struct private_inferior
290 {
291 virtual ~private_inferior () = 0;
292 };
293
294 /* Inferior process specific part of `struct infcall_control_state'.
295
296 Inferior thread counterpart is `struct thread_control_state'. */
297
298 struct inferior_control_state
299 {
300 inferior_control_state ()
301 : stop_soon (NO_STOP_QUIETLY)
302 {
303 }
304
305 explicit inferior_control_state (enum stop_kind when)
306 : stop_soon (when)
307 {
308 }
309
310 /* See the definition of stop_kind above. */
311 enum stop_kind stop_soon;
312 };
313
314 /* Return a pointer to the current inferior. */
315 extern inferior *current_inferior ();
316
317 extern void set_current_inferior (inferior *);
318
319 /* Switch inferior (and program space) to INF, and switch to no thread
320 selected. */
321 extern void switch_to_inferior_no_thread (inferior *inf);
322
323 /* GDB represents the state of each program execution with an object
324 called an inferior. An inferior typically corresponds to a process
325 but is more general and applies also to targets that do not have a
326 notion of processes. Each run of an executable creates a new
327 inferior, as does each attachment to an existing process.
328 Inferiors have unique internal identifiers that are different from
329 target process ids. Each inferior may in turn have multiple
330 threads running in it.
331
332 Inferiors are intrusively refcounted objects. Unlike thread
333 objects, being the user-selected inferior is considered a strong
334 reference and is thus accounted for in the inferior object's
335 refcount (see set_current_inferior). When GDB needs to remember
336 the selected inferior to later restore it, GDB temporarily bumps
337 the inferior object's refcount, to prevent something deleting the
338 inferior object before reverting back (e.g., due to a
339 "remove-inferiors" command (see
340 scoped_restore_current_inferior). All other inferior
341 references are considered weak references. Inferiors are always
342 listed exactly once in the inferior list, so placing an inferior in
343 the inferior list is an implicit, not counted strong reference. */
344
345 class inferior : public refcounted_object,
346 public intrusive_list_node<inferior>
347 {
348 public:
349 explicit inferior (int pid);
350 ~inferior ();
351
352 /* Returns true if we can delete this inferior. */
353 bool deletable () const { return refcount () == 0; }
354
355 /* Push T in this inferior's target stack. */
356 void push_target (struct target_ops *t)
357 { m_target_stack.push (t); }
358
359 /* An overload that deletes the target on failure. */
360 void push_target (target_ops_up &&t)
361 {
362 m_target_stack.push (t.get ());
363 t.release ();
364 }
365
366 /* Unpush T from this inferior's target stack. */
367 int unpush_target (struct target_ops *t);
368
369 /* Returns true if T is pushed in this inferior's target stack. */
370 bool target_is_pushed (target_ops *t)
371 { return m_target_stack.is_pushed (t); }
372
373 /* Find the target beneath T in this inferior's target stack. */
374 target_ops *find_target_beneath (const target_ops *t)
375 { return m_target_stack.find_beneath (t); }
376
377 /* Return the target at the top of this inferior's target stack. */
378 target_ops *top_target ()
379 { return m_target_stack.top (); }
380
381 /* Return the target at process_stratum level in this inferior's
382 target stack. */
383 struct process_stratum_target *process_target ()
384 { return (process_stratum_target *) m_target_stack.at (process_stratum); }
385
386 /* Return the target at STRATUM in this inferior's target stack. */
387 target_ops *target_at (enum strata stratum)
388 { return m_target_stack.at (stratum); }
389
390 bool has_execution ()
391 { return target_has_execution (this); }
392
393 /* This inferior's thread list, sorted by creation order. */
394 intrusive_list<thread_info> thread_list;
395
396 /* A map of ptid_t to thread_info*, for average O(1) ptid_t lookup.
397 Exited threads do not appear in the map. */
398 std::unordered_map<ptid_t, thread_info *, hash_ptid> ptid_thread_map;
399
400 /* Returns a range adapter covering the inferior's threads,
401 including exited threads. Used like this:
402
403 for (thread_info *thr : inf->threads ())
404 { .... }
405 */
406 inf_threads_range threads ()
407 { return inf_threads_range (this->thread_list.begin ()); }
408
409 /* Returns a range adapter covering the inferior's non-exited
410 threads. Used like this:
411
412 for (thread_info *thr : inf->non_exited_threads ())
413 { .... }
414 */
415 inf_non_exited_threads_range non_exited_threads ()
416 { return inf_non_exited_threads_range (this->thread_list.begin ()); }
417
418 /* Like inferior::threads(), but returns a range adapter that can be
419 used with range-for, safely. I.e., it is safe to delete the
420 currently-iterated thread, like this:
421
422 for (thread_info *t : inf->threads_safe ())
423 if (some_condition ())
424 delete f;
425 */
426 inline safe_inf_threads_range threads_safe ()
427 { return safe_inf_threads_range (this->thread_list.begin ()); }
428
429 /* Delete all threads in the thread list. If SILENT, exit threads
430 silently. */
431 void clear_thread_list (bool silent);
432
433 /* Continuations-related methods. A continuation is an std::function
434 to be called to finish the execution of a command when running
435 GDB asynchronously. A continuation is executed after any thread
436 of this inferior stops. Continuations are used by the attach
437 command and the remote target when a new inferior is detected. */
438 void add_continuation (std::function<void ()> &&cont);
439 void do_all_continuations ();
440
441 /* Set/get file name for default use for standard in/out in the
442 inferior. On Unix systems, we try to make TERMINAL_NAME the
443 inferior's controlling terminal. If TERMINAL_NAME is nullptr or
444 the empty string, then the inferior inherits GDB's terminal (or
445 GDBserver's if spawning a remote process). */
446 void set_tty (const char *terminal_name);
447 const char *tty ();
448
449 /* Convenient handle (GDB inferior id). Unique across all
450 inferiors. */
451 int num = 0;
452
453 /* Actual target inferior id, usually, a process id. This matches
454 the ptid_t.pid member of threads of this inferior. */
455 int pid = 0;
456 /* True if the PID was actually faked by GDB. */
457 bool fake_pid_p = false;
458
459 /* The highest thread number this inferior ever had. */
460 int highest_thread_num = 0;
461
462 /* State of GDB control of inferior process execution.
463 See `struct inferior_control_state'. */
464 inferior_control_state control;
465
466 /* True if this was an auto-created inferior, e.g. created from
467 following a fork; false, if this inferior was manually added by
468 the user, and we should not attempt to prune it
469 automatically. */
470 bool removable = false;
471
472 /* The address space bound to this inferior. */
473 struct address_space *aspace = NULL;
474
475 /* The program space bound to this inferior. */
476 struct program_space *pspace = NULL;
477
478 /* The arguments string to use when running. */
479 gdb::unique_xmalloc_ptr<char> args;
480
481 /* The size of elements in argv. */
482 int argc = 0;
483
484 /* The vector version of arguments. If ARGC is nonzero,
485 then we must compute ARGS from this (via the target).
486 This is always coming from main's argv and therefore
487 should never be freed. */
488 char **argv = NULL;
489
490 /* The current working directory that will be used when starting
491 this inferior. */
492 gdb::unique_xmalloc_ptr<char> cwd;
493
494 /* The terminal state as set by the last target_terminal::terminal_*
495 call. */
496 target_terminal_state terminal_state = target_terminal_state::is_ours;
497
498 /* Environment to use for running inferior,
499 in format described in environ.h. */
500 gdb_environ environment;
501
502 /* True if this child process was attached rather than forked. */
503 bool attach_flag = false;
504
505 /* If this inferior is a vfork child, then this is the pointer to
506 its vfork parent, if GDB is still attached to it. */
507 inferior *vfork_parent = NULL;
508
509 /* If this process is a vfork parent, this is the pointer to the
510 child. Since a vfork parent is left frozen by the kernel until
511 the child execs or exits, a process can only have one vfork child
512 at a given time. */
513 inferior *vfork_child = NULL;
514
515 /* True if this inferior should be detached when it's vfork sibling
516 exits or execs. */
517 bool pending_detach = false;
518
519 /* True if this inferior is a vfork parent waiting for a vfork child
520 not under our control to be done with the shared memory region,
521 either by exiting or execing. */
522 bool waiting_for_vfork_done = false;
523
524 /* True if we're in the process of detaching from this inferior. */
525 bool detaching = false;
526
527 /* True if setup_inferior wasn't called for this inferior yet.
528 Until that is done, we must not access inferior memory or
529 registers, as we haven't determined the target
530 architecture/description. */
531 bool needs_setup = false;
532
533 /* True when we are reading the library list of the inferior during an
534 attach or handling a fork child. */
535 bool in_initial_library_scan = false;
536
537 /* Private data used by the target vector implementation. */
538 std::unique_ptr<private_inferior> priv;
539
540 /* HAS_EXIT_CODE is true if the inferior exited with an exit code.
541 In this case, the EXIT_CODE field is also valid. */
542 bool has_exit_code = false;
543 LONGEST exit_code = 0;
544
545 /* Default flags to pass to the symbol reading functions. These are
546 used whenever a new objfile is created. */
547 symfile_add_flags symfile_flags = 0;
548
549 /* Info about an inferior's target description (if it's fetched; the
550 user supplied description's filename, if any; etc.). */
551 target_desc_info *tdesc_info = NULL;
552
553 /* The architecture associated with the inferior through the
554 connection to the target.
555
556 The architecture vector provides some information that is really
557 a property of the inferior, accessed through a particular target:
558 ptrace operations; the layout of certain RSP packets; the
559 solib_ops vector; etc. To differentiate architecture accesses to
560 per-inferior/target properties from
561 per-thread/per-frame/per-objfile properties, accesses to
562 per-inferior/target properties should be made through
563 this gdbarch. */
564 struct gdbarch *gdbarch = NULL;
565
566 /* Data related to displaced stepping. */
567 displaced_step_inferior_state displaced_step_state;
568
569 /* Per inferior data-pointers required by other GDB modules. */
570 REGISTRY_FIELDS;
571
572 private:
573 /* The inferior's target stack. */
574 target_stack m_target_stack;
575
576 /* The name of terminal device to use for I/O. */
577 gdb::unique_xmalloc_ptr<char> m_terminal;
578
579 /* The list of continuations. */
580 std::list<std::function<void ()>> m_continuations;
581 };
582
583 /* Keep a registry of per-inferior data-pointers required by other GDB
584 modules. */
585
586 DECLARE_REGISTRY (inferior);
587
588 /* Add an inferior to the inferior list, print a message that a new
589 inferior is found, and return the pointer to the new inferior.
590 Caller may use this pointer to initialize the private inferior
591 data. */
592 extern struct inferior *add_inferior (int pid);
593
594 /* Same as add_inferior, but don't print new inferior notifications to
595 the CLI. */
596 extern struct inferior *add_inferior_silent (int pid);
597
598 extern void delete_inferior (struct inferior *todel);
599
600 /* Delete an existing inferior list entry, due to inferior detaching. */
601 extern void detach_inferior (inferior *inf);
602
603 extern void exit_inferior (inferior *inf);
604
605 extern void exit_inferior_silent (inferior *inf);
606
607 extern void exit_inferior_num_silent (int num);
608
609 extern void inferior_appeared (struct inferior *inf, int pid);
610
611 /* Search function to lookup an inferior of TARG by target 'pid'. */
612 extern struct inferior *find_inferior_pid (process_stratum_target *targ,
613 int pid);
614
615 /* Search function to lookup an inferior of TARG whose pid is equal to
616 'ptid.pid'. */
617 extern struct inferior *find_inferior_ptid (process_stratum_target *targ,
618 ptid_t ptid);
619
620 /* Search function to lookup an inferior by GDB 'num'. */
621 extern struct inferior *find_inferior_id (int num);
622
623 /* Find an inferior bound to PSPACE, giving preference to the current
624 inferior. */
625 extern struct inferior *
626 find_inferior_for_program_space (struct program_space *pspace);
627
628 /* Returns true if the inferior list is not empty. */
629 extern int have_inferiors (void);
630
631 /* Returns the number of live inferiors running on PROC_TARGET (real
632 live processes with execution). */
633 extern int number_of_live_inferiors (process_stratum_target *proc_target);
634
635 /* Returns true if there are any live inferiors in the inferior list
636 (not cores, not executables, real live processes). */
637 extern int have_live_inferiors (void);
638
639 /* Save/restore the current inferior. */
640
641 class scoped_restore_current_inferior
642 {
643 public:
644 scoped_restore_current_inferior ()
645 : m_saved_inf (current_inferior ())
646 {}
647
648 ~scoped_restore_current_inferior ()
649 { set_current_inferior (m_saved_inf); }
650
651 DISABLE_COPY_AND_ASSIGN (scoped_restore_current_inferior);
652
653 private:
654 inferior *m_saved_inf;
655 };
656
657
658 /* Traverse all inferiors. */
659
660 extern intrusive_list<inferior> inferior_list;
661
662 /* Pull in the internals of the inferiors ranges and iterators. Must
663 be done after struct inferior is defined. */
664 #include "inferior-iter.h"
665
666 /* Return a range that can be used to walk over all inferiors
667 inferiors, with range-for, safely. I.e., it is safe to delete the
668 currently-iterated inferior. When combined with range-for, this
669 allow convenient patterns like this:
670
671 for (inferior *inf : all_inferiors_safe ())
672 if (some_condition ())
673 delete inf;
674 */
675
676 inline all_inferiors_safe_range
677 all_inferiors_safe ()
678 {
679 return {};
680 }
681
682 /* Returns a range representing all inferiors, suitable to use with
683 range-for, like this:
684
685 for (inferior *inf : all_inferiors ())
686 [...]
687 */
688
689 inline all_inferiors_range
690 all_inferiors (process_stratum_target *proc_target = nullptr)
691 {
692 return all_inferiors_range (proc_target, inferior_list);
693 }
694
695 /* Return a range that can be used to walk over all inferiors with PID
696 not zero, with range-for. */
697
698 inline all_non_exited_inferiors_range
699 all_non_exited_inferiors (process_stratum_target *proc_target = nullptr)
700 {
701 return all_non_exited_inferiors_range (proc_target, inferior_list);
702 }
703
704 /* Prune away automatically added inferiors that aren't required
705 anymore. */
706 extern void prune_inferiors (void);
707
708 extern int number_of_inferiors (void);
709
710 extern struct inferior *add_inferior_with_spaces (void);
711
712 /* Print the current selected inferior. */
713 extern void print_selected_inferior (struct ui_out *uiout);
714
715 #endif /* !defined (INFERIOR_H) */