summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Kratochvil <jan.kratochvil@redhat.com>2010-02-15 17:35:48 +0000
committerJan Kratochvil <jan.kratochvil@redhat.com>2010-02-15 17:35:48 +0000
commita5c67f4b5ee3e522ccfacc2ff800cd42e9243c68 (patch)
treeee7923ad66d1c691d915bf41ec614166ca685f04
parentd8be1ca0736a3ff60225468bdefbc9556c97aaaf (diff)
downloadgdb-a5c67f4b5ee3e522ccfacc2ff800cd42e9243c68.tar.gz
gdb/
* defs.h (parse_pid_to_attach): New. * utils.c (parse_pid_to_attach): New. * darwin-nat.c (darwin_attach): Replace ARGS parsing by parse_pid. * gnu-nat.c (gnu_attach): Likewise. * nto-procfs.c (procfs_attach): Likewise. * procfs.c (procfs_attach): Likewise. * windows-nat.c (windows_attach): Likewise. * inf-ptrace.c (inf_ptrace_attach): Likewise. Remove variable dummy. * inf-ttrace.c (inf_ttrace_attach): Likewise. * remote.c (extended_remote_attach_1): Likewise. New comment on getpid check. gdb/testsuite/ * gdb.base/attach.exp (attach to nonsense is prohibited): Make the "Illegal process-id" expect string more exact. (attach to digits-starting nonsense is prohibited): New.
-rw-r--r--gdb/ChangeLog14
-rw-r--r--gdb/darwin-nat.c7
-rw-r--r--gdb/defs.h2
-rw-r--r--gdb/gnu-nat.c7
-rw-r--r--gdb/inf-ptrace.c10
-rw-r--r--gdb/inf-ttrace.c9
-rw-r--r--gdb/nto-procfs.c5
-rw-r--r--gdb/procfs.c4
-rw-r--r--gdb/remote.c11
-rw-r--r--gdb/testsuite/ChangeLog6
-rw-r--r--gdb/testsuite/gdb.base/attach.exp23
-rw-r--r--gdb/utils.c20
-rw-r--r--gdb/windows-nat.c5
13 files changed, 76 insertions, 47 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 43442aedb65..ca892e99385 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,17 @@
+2010-02-15 Jan Kratochvil <jan.kratochvil@redhat.com>
+
+ * defs.h (parse_pid_to_attach): New.
+ * utils.c (parse_pid_to_attach): New.
+ * darwin-nat.c (darwin_attach): Replace ARGS parsing by parse_pid.
+ * gnu-nat.c (gnu_attach): Likewise.
+ * nto-procfs.c (procfs_attach): Likewise.
+ * procfs.c (procfs_attach): Likewise.
+ * windows-nat.c (windows_attach): Likewise.
+ * inf-ptrace.c (inf_ptrace_attach): Likewise. Remove variable dummy.
+ * inf-ttrace.c (inf_ttrace_attach): Likewise.
+ * remote.c (extended_remote_attach_1): Likewise. New comment on getpid
+ check.
+
2010-02-14 Masaki Muranaka <monaka@monami-software.com>
* MAINTAINERS: Add myself for write after approval privileges.
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 535720df172..40b9f3fdf00 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1514,12 +1514,9 @@ darwin_attach (struct target_ops *ops, char *args, int from_tty)
struct inferior *inf;
kern_return_t kret;
- if (!args)
- error_no_arg (_("process-id to attach"));
+ pid = parse_pid_to_attach (args);
- pid = atoi (args);
-
- if (pid == getpid ()) /* Trying to masturbate? */
+ if (pid == getpid ()) /* Trying to masturbate? */
error (_("I refuse to debug myself!"));
if (from_tty)
diff --git a/gdb/defs.h b/gdb/defs.h
index 770d1719428..eee7b39ae96 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -426,6 +426,8 @@ int compare_positive_ints (const void *ap, const void *bp);
extern const char *gdb_bfd_errmsg (bfd_error_type error_tag, char **matching);
+extern int parse_pid_to_attach (char *args);
+
/* From demangle.c */
extern void set_demangling_style (char *);
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 2750f9d5bd4..51dde6f3511 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2139,12 +2139,9 @@ gnu_attach (struct target_ops *ops, char *args, int from_tty)
struct inf *inf = cur_inf ();
struct inferior *inferior;
- if (!args)
- error_no_arg (_("process-id to attach"));
-
- pid = atoi (args);
+ pid = parse_pid_to_attach (args);
- if (pid == getpid ()) /* Trying to masturbate? */
+ if (pid == getpid ()) /* Trying to masturbate? */
error (_("I refuse to debug myself!"));
if (from_tty)
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index f8050d46a5e..ba9d8a8aec9 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -187,17 +187,9 @@ inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty)
{
char *exec_file;
pid_t pid;
- char *dummy;
struct inferior *inf;
- if (!args)
- error_no_arg (_("process-id to attach"));
-
- dummy = args;
- pid = strtol (args, &dummy, 0);
- /* Some targets don't set errno on errors, grrr! */
- if (pid == 0 && args == dummy)
- error (_("Illegal process-id: %s."), args);
+ pid = parse_pid_to_attach (args);
if (pid == getpid ()) /* Trying to masturbate? */
error (_("I refuse to debug myself!"));
diff --git a/gdb/inf-ttrace.c b/gdb/inf-ttrace.c
index c9ab548c742..708a07c5875 100644
--- a/gdb/inf-ttrace.c
+++ b/gdb/inf-ttrace.c
@@ -691,17 +691,10 @@ inf_ttrace_attach (struct target_ops *ops, char *args, int from_tty)
{
char *exec_file;
pid_t pid;
- char *dummy;
ttevent_t tte;
struct inferior *inf;
- if (!args)
- error_no_arg (_("process-id to attach"));
-
- dummy = args;
- pid = strtol (args, &dummy, 0);
- if (pid == 0 && args == dummy)
- error (_("Illegal process-id: %s."), args);
+ pid = parse_pid_to_attach (args);
if (pid == getpid ()) /* Trying to masturbate? */
error (_("I refuse to debug myself!"));
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index d8f3c91a827..3d62ff8a87f 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -619,10 +619,7 @@ procfs_attach (struct target_ops *ops, char *args, int from_tty)
int pid;
struct inferior *inf;
- if (!args)
- error_no_arg (_("process-id to attach"));
-
- pid = atoi (args);
+ pid = parse_pid_to_attach (args);
if (pid == getpid ())
error (_("Attaching GDB to itself is not a good idea..."));
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 1dd39b2e7fc..6413ed039bf 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3606,10 +3606,8 @@ procfs_attach (struct target_ops *ops, char *args, int from_tty)
char *exec_file;
int pid;
- if (!args)
- error_no_arg (_("process-id to attach"));
+ pid = parse_pid_to_attach (args);
- pid = atoi (args);
if (pid == getpid ())
error (_("Attaching GDB to itself is not a good idea..."));
diff --git a/gdb/remote.c b/gdb/remote.c
index 709e424ac27..6b1a27bf4fa 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3857,17 +3857,12 @@ extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty)
{
struct remote_state *rs = get_remote_state ();
int pid;
- char *dummy;
char *wait_status = NULL;
- if (!args)
- error_no_arg (_("process-id to attach"));
+ pid = parse_pid_to_attach (args);
- dummy = args;
- pid = strtol (args, &dummy, 0);
- /* Some targets don't set errno on errors, grrr! */
- if (pid == 0 && args == dummy)
- error (_("Illegal process-id: %s."), args);
+ /* Remote PID can be freely equal to getpid, do not check it here the same
+ way as in other targets. */
if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
error (_("This target does not support attaching to a process"));
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 897740418a5..6196c587db3 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2010-02-15 Jan Kratochvil <jan.kratochvil@redhat.com>
+
+ * gdb.base/attach.exp (attach to nonsense is prohibited): Make the
+ "Illegal process-id" expect string more exact.
+ (attach to digits-starting nonsense is prohibited): New.
+
2010-02-13 Jan Kratochvil <jan.kratochvil@redhat.com>
* gdb.base/prelink.exp (set verbose on): New.
diff --git a/gdb/testsuite/gdb.base/attach.exp b/gdb/testsuite/gdb.base/attach.exp
index 8729d581be0..a507e3b291b 100644
--- a/gdb/testsuite/gdb.base/attach.exp
+++ b/gdb/testsuite/gdb.base/attach.exp
@@ -93,7 +93,28 @@ proc do_attach_tests {} {
set test "attach to nonsense is prohibited"
gdb_test_multiple "attach abc" "$test" {
- -re "Illegal process-id: abc.*$gdb_prompt $" {
+ -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
+ pass "$test"
+ }
+ -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
+ # Response expected from /proc-based systems.
+ pass "$test"
+ }
+ -re "Can't attach to process..*$gdb_prompt $" {
+ # Response expected on Cygwin
+ pass "$test"
+ }
+ -re "Attaching to.*$gdb_prompt $" {
+ fail "$test (bogus pid allowed)"
+ }
+ }
+
+ # Verify that we cannot attach to nonsense even if its initial part is
+ # a valid PID.
+
+ set test "attach to digits-starting nonsense is prohibited"
+ gdb_test_multiple "attach ${testpid}x" "$test" {
+ -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
pass "$test"
}
-re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
diff --git a/gdb/utils.c b/gdb/utils.c
index 33c195a41dc..52596ca7068 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3648,6 +3648,26 @@ gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
return ret;
}
+/* Return ARGS parsed as a valid pid, or throw an error. */
+
+int
+parse_pid_to_attach (char *args)
+{
+ unsigned long pid;
+ char *dummy;
+
+ if (!args)
+ error_no_arg (_("process-id to attach"));
+
+ dummy = args;
+ pid = strtoul (args, &dummy, 0);
+ /* Some targets don't set errno on errors, grrr! */
+ if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)])
+ error (_("Illegal process-id: %s."), args);
+
+ return pid;
+}
+
/* Provide a prototype to silence -Wmissing-prototypes. */
extern initialize_file_ftype _initialize_utils;
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 9ac2b2443ff..e51d30b51af 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1691,8 +1691,7 @@ windows_attach (struct target_ops *ops, char *args, int from_tty)
BOOL ok;
DWORD pid;
- if (!args)
- error_no_arg (_("process-id to attach"));
+ pid = parse_pid_to_attach (args);
if (set_process_privilege (SE_DEBUG_NAME, TRUE) < 0)
{
@@ -1700,8 +1699,6 @@ windows_attach (struct target_ops *ops, char *args, int from_tty)
printf_unfiltered ("This can cause attach to fail on Windows NT/2K/XP\n");
}
- pid = strtoul (args, 0, 0); /* Windows pid */
-
windows_init_thread_list ();
ok = DebugActiveProcess (pid);
saw_create = 0;