Hide getting at the current process's PID behind a target method. From: Pedro Alves --- gdb/linux-nat.c | 18 ++++++++++++++++-- gdb/remote.c | 26 ++++++++++++++++++++++++++ gdb/target.c | 5 +++++ gdb/target.h | 4 ++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 17236ec..1d06352 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -5007,6 +5007,16 @@ make_cleanup_target_file_close (int fd) return make_cleanup (do_target_file_close, fdp); } +static int +linux_nat_current_process_id (LONGEST *pid) +{ + if (!target_has_execution) + noprocess (); + + *pid = ptid_get_pid (inferior_ptid); + return 0; +} + static void linux_nat_info_proc_cmd_1 (char *args, enum info_proc_what what, int from_tty) { @@ -5038,11 +5048,13 @@ linux_nat_info_proc_cmd_1 (char *args, enum info_proc_what what, int from_tty) if (!explicit_pid) { + LONGEST tpid; + if (!target_has_execution) error (_("No current process: you must name one.")); - if (current_inferior ()->fake_pid_p) + if (target_current_process_id (&tpid) == -1) error (_("Can't determine the current process's PID: you must name one.")); - pid = ptid_get_pid (inferior_ptid); + pid = tpid; } printf_filtered (_("process %ld\n"), pid); @@ -6077,6 +6089,8 @@ linux_nat_add_target (struct target_ops *t) t->to_file_pread = linux_nat_file_pread; t->to_file_close = linux_nat_file_close; + t->to_current_process_id = linux_nat_current_process_id; + /* We don't change the stratum; this target will sit at process_stratum and thread_db will set at thread_stratum. This is a little strange, since this is a multi-threaded-capable diff --git a/gdb/remote.c b/gdb/remote.c index e78853c..2bd7d13 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -9779,6 +9779,30 @@ remote_supports_multi_process (void) return rs->extended && remote_multi_process_p (rs); } +/* Return in *PID the current inferior's target process ID. Returns 0 + on success, and -1 if not possible to determine the target PID. + Throws an error if there is no current inferior. */ + +static int +remote_current_process_id (LONGEST *pid) +{ + if (!target_has_execution) + noprocess (); + + /* Add query to remote target side here if/when necessary. */ + + /* Default to assuming there's a 1:1 mapping between RSP process ID + and target process ID. */ + + /* With no multi-process extensions, we have nothing to fall back + to. */ + if (current_inferior ()->fake_pid_p) + return -1; + + *pid = ptid_get_pid (inferior_ptid); + return 0; +} + int remote_supports_cond_tracepoints (void) { @@ -10715,6 +10739,8 @@ Specify the serial device it is connected to\n\ remote_ops.to_file_pread = remote_hostio_pread; remote_ops.to_file_close = remote_hostio_close; remote_ops.to_file_unlink = remote_hostio_unlink; + + remote_ops.to_current_process_id = remote_current_process_id; } /* Set up the extended remote vector by making a copy of the standard diff --git a/gdb/target.c b/gdb/target.c index 74238a5..1422117 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -704,6 +704,8 @@ update_current_target (void) INHERIT (to_file_close, t); INHERIT (to_file_unlink, t); + INHERIT (to_current_process_id, t); + INHERIT (to_magic, t); /* Do not inherit to_memory_map. */ /* Do not inherit to_flash_erase. */ @@ -950,6 +952,9 @@ update_current_target (void) de_fault (to_file_unlink, (int (*) (const char *, int *)) tcomplain); + de_fault (to_current_process_id, + (int (*) (LONGEST *)) + tcomplain); #undef de_fault diff --git a/gdb/target.h b/gdb/target.h index 96176a5..449be04 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -821,6 +821,7 @@ struct target_ops occurs (and set *TARGET_ERRNO). */ int (*to_file_unlink) (const char *filename, int *target_errno); + int (*to_current_process_id) (LONGEST *pid); int to_magic; /* Need sub-structure for target machine related rather than comm related? */ @@ -1789,6 +1790,9 @@ extern void update_target_permissions (void); #define target_file_unlink(filename, target_errno) \ (*current_target.to_file_close) (filename, target_errno) +#define target_current_process_id(PID) \ + (*current_target.to_current_process_id) (PID) + /* Imported from machine dependent code. */