[2/3] Use gdb_dir_up in linux_proc_attach_tgid_threads

Message ID 20231003-attach-bug-v1-2-f5de2e583c5d@adacore.com
State New
Headers
Series Fix "attach" infinite loop |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom Tromey Oct. 3, 2023, 5:56 p.m. UTC
  This changes linux_proc_attach_tgid_threads to use gdb_dir_up.  This
makes it robust against exceptions.
---
 gdb/nat/linux-procfs.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
  

Comments

Simon Marchi Oct. 4, 2023, 1:30 a.m. UTC | #1
On 2023-10-03 13:56, Tom Tromey via Gdb-patches wrote:
> This changes linux_proc_attach_tgid_threads to use gdb_dir_up.  This
> makes it robust against exceptions.
> ---
>  gdb/nat/linux-procfs.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
> index 95ecae443e4..c7d8ba2bd67 100644
> --- a/gdb/nat/linux-procfs.c
> +++ b/gdb/nat/linux-procfs.c
> @@ -275,7 +275,6 @@ void
>  linux_proc_attach_tgid_threads (pid_t pid,
>  				linux_proc_attach_lwp_func attach_lwp)
>  {
> -  DIR *dir;
>    char pathname[128];
>    int new_threads_found;
>    int iterations;
> @@ -284,7 +283,7 @@ linux_proc_attach_tgid_threads (pid_t pid,
>      return;
>  
>    xsnprintf (pathname, sizeof (pathname), "/proc/%ld/task", (long) pid);
> -  dir = opendir (pathname);
> +  gdb_dir_up dir (opendir (pathname));
>    if (dir == NULL)
>      {
>        warning (_("Could not open %s."), pathname);
> @@ -300,7 +299,7 @@ linux_proc_attach_tgid_threads (pid_t pid,
>        struct dirent *dp;
>  
>        new_threads_found = 0;
> -      while ((dp = readdir (dir)) != NULL)
> +      while ((dp = readdir (dir.get ())) != NULL)
>  	{
>  	  unsigned long lwp;
>  
> @@ -321,10 +320,8 @@ linux_proc_attach_tgid_threads (pid_t pid,
>  	  iterations = -1;
>  	}
>  
> -      rewinddir (dir);
> +      rewinddir (dir.get ());
>      }
> -
> -  closedir (dir);
>  }
>  
>  /* See linux-procfs.h.  */
> 

Approved-By: Simon Marchi <simon.marchi@efficios.com>

If you want to have more fun, I think it would be nice to add a
gdb_opendir function that wraps opendir and returns a gdb_dir_up.

Simon
  

Patch

diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index 95ecae443e4..c7d8ba2bd67 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -275,7 +275,6 @@  void
 linux_proc_attach_tgid_threads (pid_t pid,
 				linux_proc_attach_lwp_func attach_lwp)
 {
-  DIR *dir;
   char pathname[128];
   int new_threads_found;
   int iterations;
@@ -284,7 +283,7 @@  linux_proc_attach_tgid_threads (pid_t pid,
     return;
 
   xsnprintf (pathname, sizeof (pathname), "/proc/%ld/task", (long) pid);
-  dir = opendir (pathname);
+  gdb_dir_up dir (opendir (pathname));
   if (dir == NULL)
     {
       warning (_("Could not open %s."), pathname);
@@ -300,7 +299,7 @@  linux_proc_attach_tgid_threads (pid_t pid,
       struct dirent *dp;
 
       new_threads_found = 0;
-      while ((dp = readdir (dir)) != NULL)
+      while ((dp = readdir (dir.get ())) != NULL)
 	{
 	  unsigned long lwp;
 
@@ -321,10 +320,8 @@  linux_proc_attach_tgid_threads (pid_t pid,
 	  iterations = -1;
 	}
 
-      rewinddir (dir);
+      rewinddir (dir.get ());
     }
-
-  closedir (dir);
 }
 
 /* See linux-procfs.h.  */