gdb/
authorJan Kratochvil <jan.kratochvil@redhat.com>
Mon, 15 Feb 2010 17:35:50 +0000 (17:35 +0000)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Mon, 15 Feb 2010 17:35:50 +0000 (17:35 +0000)
* 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.

13 files changed:
gdb/ChangeLog
gdb/darwin-nat.c
gdb/defs.h
gdb/gnu-nat.c
gdb/inf-ptrace.c
gdb/inf-ttrace.c
gdb/nto-procfs.c
gdb/procfs.c
gdb/remote.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/attach.exp
gdb/utils.c
gdb/windows-nat.c

index 43442aedb656c6413a6089a4b6fcc1ad32de78be..ca892e99385780551abf8a981671459b6ecd12c0 100644 (file)
@@ -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.
index 535720df17264d731e4d6a31caaa242ca124529e..40b9f3fdf000c9ccc7ea62cc7b18539f61cba7f3 100644 (file)
@@ -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)
index 770d1719428e38f6cd068579ada211e69ecceb54..eee7b39ae96daaa75c59ac616b50e528b81cb4df 100644 (file)
@@ -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 *);
index 2750f9d5bd49adb547e023f08a36daf861a6f740..51dde6f35110051281d1048fcd08988d8528e2c3 100644 (file)
@@ -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)
index f8050d46a5eda510d91977f6a2ebd8664a5ccb60..ba9d8a8aec9e863cf6c416744b7569a4c47f2f63 100644 (file)
@@ -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!"));
index c9ab548c742193144081eb11b0dcafd66de0d3dc..708a07c587597ed811aab203505f2a292fcbee90 100644 (file)
@@ -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!"));
index d8f3c91a8274490b1a0bb9657130418263c680f6..3d62ff8a87fad7d7f0063131cf883e3bb25b3d15 100644 (file)
@@ -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..."));
index 1dd39b2e7fc9bbab80bff06916e58905c48b35e1..6413ed039bf63cb9b88a8b7418fbc1e2e1c96d36 100644 (file)
@@ -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..."));
 
index 709e424ac276479753cf42dafb2e882ea5183a86..6b1a27bf4fac96b1978f92f5ddcb1386b27188a3 100644 (file)
@@ -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"));
index 897740418a5690db93c062dd7aecfd99932689db..6196c587db376ecc7050bdf8871f9e33ce7ffece 100644 (file)
@@ -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.
index 8729d581be0ffab4dd1fd3e301d178314bb9c22b..a507e3b291b9c2d2fff460ef0d001209ae4719a4 100644 (file)
@@ -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 $" {
index 33c195a41dcebc22dd71e2b343a582b3a25a2351..52596ca7068e8dd6ef12f35e8950467b6265ec8a 100644 (file)
@@ -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;
 
index 9ac2b2443ff00bd177c5a8f01f73ed36daf405e8..e51d30b51af9a66f05691d6f7aacf7f63a3cf67f 100644 (file)
@@ -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;