From 200fd2874d2c147539f756b29e15ebbafa81dfcf Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Fri, 12 Nov 2021 11:18:58 +0000 Subject: [PATCH] gdb: make post_startup_inferior a virtual method on inf_ptrace_target While working on a later patch that required me to understand how GDB starts up inferiors, I was confused by the target_ops::post_startup_inferior method. The post_startup_inferior target function is only called from inf_ptrace_target::create_inferior. Part of the target class hierarchy looks like this: inf_child_target | '-- inf_ptrace_target | |-- linux_nat_target | |-- fbsd_nat_target | |-- nbsd_nat_target | |-- obsd_nat_target | '-- rs6000_nat_target Every sub-class of inf_ptrace_target, except rs6000_nat_target, implements ::post_startup_inferior. The rs6000_nat_target picks up the implementation of ::post_startup_inferior not from inf_ptrace_target, but from inf_child_target. No descendent of inf_child_target, outside the inf_ptrace_target sub-tree, implements ::post_startup_inferior, which isn't really surprising, as they would never see the method called (remember, the method is only called from inf_ptrace_target::create_inferior). What I find confusing is the role inf_child_target plays in implementing, what is really a helper function for just one of its descendents. In this commit I propose that we formally make ::post_startup_inferior a helper function of inf_ptrace_target. To do this I will remove the ::post_startup_inferior from the target_ops API, and instead make this a protected, pure virtual function on inf_ptrace_target. I'll remove the empty implementation of ::post_startup_inferior from the inf_child_target class, and add a new empty implementation to the rs6000_nat_target class. All the other descendents of inf_ptrace_target already provide an implementation of this method and so don't need to change beyond making the method protected within their class declarations. To me, this makes much more sense now. The helper function, which is only called from within the inf_ptrace_target class, is now a part of the inf_ptrace_target class. The only way in which this change is visible to a user is if the user turns on 'set debug target 1'. With this debug flag on, prior to this patch the user would see something like: -> native->post_startup_inferior (...) <- native->post_startup_inferior (2588939) After this patch these lines are no longer present, as the post_startup_inferior is no longer a top level target method. For me, this is an acceptable change. --- gdb/aarch64-linux-nat.c | 2 +- gdb/fbsd-nat.c | 2 +- gdb/fbsd-nat.h | 5 ++++- gdb/inf-child.c | 7 ------- gdb/inf-child.h | 2 -- gdb/inf-ptrace.c | 2 +- gdb/inf-ptrace.h | 11 +++++++++++ gdb/linux-nat.c | 2 ++ gdb/linux-nat.h | 6 ++++-- gdb/netbsd-nat.c | 2 +- gdb/netbsd-nat.h | 4 +++- gdb/obsd-nat.c | 2 ++ gdb/obsd-nat.h | 5 +++-- gdb/rs6000-aix-nat.c | 5 +++++ gdb/target-delegates.c | 23 ----------------------- gdb/target.c | 8 -------- gdb/target.h | 14 -------------- gdb/x86-linux-nat.c | 2 ++ gdb/x86-linux-nat.h | 7 ++++--- 19 files changed, 44 insertions(+), 67 deletions(-) diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c index aa42365d25d..0ca386331e5 100644 --- a/gdb/aarch64-linux-nat.c +++ b/gdb/aarch64-linux-nat.c @@ -694,7 +694,7 @@ ps_get_thread_area (struct ps_prochandle *ph, } -/* Implement the "post_startup_inferior" target_ops method. */ +/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ void aarch64_linux_nat_target::post_startup_inferior (ptid_t ptid) diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c index e90aa12e442..e84dafdad9d 100644 --- a/gdb/fbsd-nat.c +++ b/gdb/fbsd-nat.c @@ -1548,7 +1548,7 @@ fbsd_nat_target::remove_vfork_catchpoint (int pid) } #endif -/* Implement the "post_startup_inferior" target_ops method. */ +/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ void fbsd_nat_target::post_startup_inferior (ptid_t pid) diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h index 34d7e6d25ba..863c0a4e190 100644 --- a/gdb/fbsd-nat.h +++ b/gdb/fbsd-nat.h @@ -76,7 +76,6 @@ public: ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override; - void post_startup_inferior (ptid_t) override; void post_attach (int) override; #ifdef USE_SIGTRAP_SIGINFO @@ -106,6 +105,10 @@ public: bool supports_disable_randomization () override; +protected: + + void post_startup_inferior (ptid_t) override; + private: /* Helper routines for use in fetch_registers and store_registers in subclasses. These routines fetch and store a single set of diff --git a/gdb/inf-child.c b/gdb/inf-child.c index b65bbf30074..949f530e128 100644 --- a/gdb/inf-child.c +++ b/gdb/inf-child.c @@ -202,13 +202,6 @@ inf_child_target::maybe_unpush_target () current_inferior ()->unpush_target (this); } -void -inf_child_target::post_startup_inferior (ptid_t ptid) -{ - /* This target doesn't require a meaningful "post startup inferior" - operation by a debugger. */ -} - bool inf_child_target::can_run () { diff --git a/gdb/inf-child.h b/gdb/inf-child.h index 1e009b6b74a..ce8feaf05bd 100644 --- a/gdb/inf-child.h +++ b/gdb/inf-child.h @@ -55,8 +55,6 @@ public: void interrupt () override; void pass_ctrlc () override; - void post_startup_inferior (ptid_t) override; - void follow_exec (inferior *follow_inf, ptid_t ptid, const char *execd_pathname) override; diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c index 2e7a03c63f5..bbf38c0aeac 100644 --- a/gdb/inf-ptrace.c +++ b/gdb/inf-ptrace.c @@ -103,7 +103,7 @@ inf_ptrace_target::create_inferior (const char *exec_file, /* On some targets, there must be some explicit actions taken after the inferior has been started up. */ - target_post_startup_inferior (ptid); + post_startup_inferior (ptid); } /* Clean up a rotting corpse of an inferior after it died. */ diff --git a/gdb/inf-ptrace.h b/gdb/inf-ptrace.h index 8aded9b60db..a4ad0969075 100644 --- a/gdb/inf-ptrace.h +++ b/gdb/inf-ptrace.h @@ -60,6 +60,17 @@ struct inf_ptrace_target : public inf_child_target protected: /* Cleanup the inferior after a successful ptrace detach. */ void detach_success (inferior *inf); + + /* Some targets don't allow us to request notification of inferior events + such as fork and vfork immediately after the inferior is created. + (This is because of how gdb creates inferiors via invoking a shell to + do it. In such a scenario, if the shell init file has commands in it, + the shell will fork and exec for each of those commands, and we will + see each such fork event. Very bad.) + + Such targets will supply an appropriate definition for this + function. */ + virtual void post_startup_inferior (ptid_t ptid) = 0; }; #ifndef __NetBSD__ diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 9d4b05a2167..20aa4a1322d 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -411,6 +411,8 @@ linux_nat_target::post_attach (int pid) linux_init_ptrace_procfs (pid, 1); } +/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ + void linux_nat_target::post_startup_inferior (ptid_t ptid) { diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h index 95e26b7ee46..116eb7ed51b 100644 --- a/gdb/linux-nat.h +++ b/gdb/linux-nat.h @@ -129,8 +129,6 @@ public: char *pid_to_exec_file (int pid) override; - void post_startup_inferior (ptid_t) override; - void post_attach (int) override; void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override; @@ -189,6 +187,10 @@ public: /* SIGTRAP-like breakpoint status events recognizer. The default recognizes SIGTRAP only. */ virtual bool low_status_is_event (int status); + +protected: + + void post_startup_inferior (ptid_t) override; }; /* The final/concrete instance. */ diff --git a/gdb/netbsd-nat.c b/gdb/netbsd-nat.c index 7dfc586fc7b..1eb2c769411 100644 --- a/gdb/netbsd-nat.c +++ b/gdb/netbsd-nat.c @@ -132,7 +132,7 @@ nbsd_add_threads (nbsd_nat_target *target, pid_t pid) netbsd_nat::for_each_thread (pid, fn); } -/* Implement the "post_startup_inferior" target_ops method. */ +/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ void nbsd_nat_target::post_startup_inferior (ptid_t ptid) diff --git a/gdb/netbsd-nat.h b/gdb/netbsd-nat.h index 43d60eb3415..ad83f2036d4 100644 --- a/gdb/netbsd-nat.h +++ b/gdb/netbsd-nat.h @@ -32,7 +32,6 @@ struct nbsd_nat_target : public inf_ptrace_target bool thread_alive (ptid_t ptid) override; const char *thread_name (struct thread_info *thr) override; - void post_startup_inferior (ptid_t ptid) override; void post_attach (int pid) override; void update_thread_list () override; std::string pid_to_str (ptid_t ptid) override; @@ -57,6 +56,9 @@ struct nbsd_nat_target : public inf_ptrace_target ULONGEST *xfered_len) override; bool supports_dumpcore () override; void dumpcore (const char *filename) override; + +protected: + void post_startup_inferior (ptid_t ptid) override; }; #endif /* netbsd-nat.h */ diff --git a/gdb/obsd-nat.c b/gdb/obsd-nat.c index c70820d60d0..2340dfed1c3 100644 --- a/gdb/obsd-nat.c +++ b/gdb/obsd-nat.c @@ -145,6 +145,8 @@ obsd_nat_target::post_attach (int pid) obsd_enable_proc_events (pid); } +/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ + void obsd_nat_target::post_startup_inferior (ptid_t pid) { diff --git a/gdb/obsd-nat.h b/gdb/obsd-nat.h index 1f74b9984c5..5ebbf61db40 100644 --- a/gdb/obsd-nat.h +++ b/gdb/obsd-nat.h @@ -35,9 +35,10 @@ class obsd_nat_target : public inf_ptrace_target int remove_fork_catchpoint (int) override; - void post_startup_inferior (ptid_t) override; - void post_attach (int) override; + +protected: + void post_startup_inferior (ptid_t) override; }; #endif /* obsd-nat.h */ diff --git a/gdb/rs6000-aix-nat.c b/gdb/rs6000-aix-nat.c index d74211f048e..682e4e9e75f 100644 --- a/gdb/rs6000-aix-nat.c +++ b/gdb/rs6000-aix-nat.c @@ -91,6 +91,11 @@ public: ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override; +protected: + + void post_startup_inferior (ptid_t ptid) override + { /* Nothing. */ } + private: enum target_xfer_status xfer_shared_libraries (enum target_object object, diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c index fb9c78a5f79..9636e3212bc 100644 --- a/gdb/target-delegates.c +++ b/gdb/target-delegates.c @@ -51,7 +51,6 @@ struct dummy_target : public target_ops void terminal_info (const char *arg0, int arg1) override; void kill () override; void load (const char *arg0, int arg1) override; - void post_startup_inferior (ptid_t arg0) override; int insert_fork_catchpoint (int arg0) override; int remove_fork_catchpoint (int arg0) override; int insert_vfork_catchpoint (int arg0) override; @@ -226,7 +225,6 @@ struct debug_target : public target_ops void terminal_info (const char *arg0, int arg1) override; void kill () override; void load (const char *arg0, int arg1) override; - void post_startup_inferior (ptid_t arg0) override; int insert_fork_catchpoint (int arg0) override; int remove_fork_catchpoint (int arg0) override; int insert_vfork_catchpoint (int arg0) override; @@ -1393,27 +1391,6 @@ debug_target::load (const char *arg0, int arg1) fputs_unfiltered (")\n", gdb_stdlog); } -void -target_ops::post_startup_inferior (ptid_t arg0) -{ - this->beneath ()->post_startup_inferior (arg0); -} - -void -dummy_target::post_startup_inferior (ptid_t arg0) -{ -} - -void -debug_target::post_startup_inferior (ptid_t arg0) -{ - fprintf_unfiltered (gdb_stdlog, "-> %s->post_startup_inferior (...)\n", this->beneath ()->shortname ()); - this->beneath ()->post_startup_inferior (arg0); - fprintf_unfiltered (gdb_stdlog, "<- %s->post_startup_inferior (", this->beneath ()->shortname ()); - target_debug_print_ptid_t (arg0); - fputs_unfiltered (")\n", gdb_stdlog); -} - int target_ops::insert_fork_catchpoint (int arg0) { diff --git a/gdb/target.c b/gdb/target.c index 06a21c46a19..65d2a1ca514 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -302,14 +302,6 @@ target_files_info () /* See target.h. */ -void -target_post_startup_inferior (ptid_t ptid) -{ - return current_inferior ()->top_target ()->post_startup_inferior (ptid); -} - -/* See target.h. */ - int target_insert_fork_catchpoint (int pid) { diff --git a/gdb/target.h b/gdb/target.h index e709b7d7cfd..4736d32e78c 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -626,8 +626,6 @@ struct target_ops virtual bool can_create_inferior (); virtual void create_inferior (const char *, const std::string &, char **, int); - virtual void post_startup_inferior (ptid_t) - TARGET_DEFAULT_IGNORE (); virtual int insert_fork_catchpoint (int) TARGET_DEFAULT_RETURN (1); virtual int remove_fork_catchpoint (int) @@ -1688,18 +1686,6 @@ extern void target_kill (void); extern void target_load (const char *arg, int from_tty); -/* Some targets (such as ttrace-based HPUX) don't allow us to request - notification of inferior events such as fork and vork immediately - after the inferior is created. (This because of how gdb gets an - inferior created via invoking a shell to do it. In such a scenario, - if the shell init file has commands in it, the shell will fork and - exec for each of those commands, and we will see each such fork - event. Very bad.) - - Such targets will supply an appropriate definition for this function. */ - -extern void target_post_startup_inferior (ptid_t ptid); - /* On some targets, we can catch an inferior fork or vfork event when it occurs. These functions insert/remove an already-created catchpoint for such events. They return 0 for success, 1 if the diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index adea1ad0092..71769085493 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -81,6 +81,8 @@ x86_linux_nat_target::~x86_linux_nat_target () { } +/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */ + void x86_linux_nat_target::post_startup_inferior (ptid_t ptid) { diff --git a/gdb/x86-linux-nat.h b/gdb/x86-linux-nat.h index a8123f76491..e1f232adae8 100644 --- a/gdb/x86-linux-nat.h +++ b/gdb/x86-linux-nat.h @@ -29,9 +29,6 @@ struct x86_linux_nat_target : public x86_nat_target { virtual ~x86_linux_nat_target () override = 0; - /* Override the GNU/Linux inferior startup hook. */ - void post_startup_inferior (ptid_t) override; - /* Add the description reader. */ const struct target_desc *read_description () override; @@ -73,6 +70,10 @@ struct x86_linux_nat_target : public x86_nat_target void low_delete_thread (struct arch_lwp_info *lwp) override { x86_linux_delete_thread (lwp); } + +protected: + /* Override the GNU/Linux inferior startup hook. */ + void post_startup_inferior (ptid_t) override; }; -- 2.30.2