[v3] Add a test for the gcore script

Message ID 20240423104620.1621814-1-ahajkova@khirnov.net
State New
Headers
Series [v3] Add a test for the gcore script |

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

Alexandra Hájková April 23, 2024, 10:45 a.m. UTC
  From: Alexandra Hájková <ahajkova@redhat.com>

It also tests the gcore script being run without its accessible
terminal.

This test was written by Jan Kratochvil a long time ago. I modernized
the test making it use various procs from lib/gdb.exp, reoirganising it
and added some comments.

Modify the gcore script to make it possible to pass the --data-directory to
it. This prevents a lot of these warnings:

Python Exception <class 'AttributeError'>: module 'gdb' has no attribute
'_handle_missing_debuginfo'

Tested by using make check-all-boards.

Co-Authored-By: Jan Kratochvil
---
v3: - do not call sh explicitly to avoid "Syntax error: "(" unexpected"

 gdb/gcore.in                       |  9 ++-
 gdb/testsuite/gdb.base/gcorebg.c   | 85 +++++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/gcorebg.exp | 88 ++++++++++++++++++++++++++++++
 gdb/testsuite/lib/gdb.exp          | 17 ++++++
 4 files changed, 198 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.base/gcorebg.c
 create mode 100644 gdb/testsuite/gdb.base/gcorebg.exp
  

Comments

Andrew Burgess April 29, 2024, 12:09 p.m. UTC | #1
Alexandra Hájková <ahajkova@khirnov.net> writes:

> From: Alexandra Hájková <ahajkova@redhat.com>
>
> It also tests the gcore script being run without its accessible
> terminal.
>
> This test was written by Jan Kratochvil a long time ago. I modernized
> the test making it use various procs from lib/gdb.exp, reoirganising it
> and added some comments.
>
> Modify the gcore script to make it possible to pass the --data-directory to
> it. This prevents a lot of these warnings:
>
> Python Exception <class 'AttributeError'>: module 'gdb' has no attribute
> '_handle_missing_debuginfo'
>
> Tested by using make check-all-boards.
>
> Co-Authored-By: Jan Kratochvil
> ---
> v3: - do not call sh explicitly to avoid "Syntax error: "(" unexpected"
>
>  gdb/gcore.in                       |  9 ++-
>  gdb/testsuite/gdb.base/gcorebg.c   | 85 +++++++++++++++++++++++++++++
>  gdb/testsuite/gdb.base/gcorebg.exp | 88 ++++++++++++++++++++++++++++++
>  gdb/testsuite/lib/gdb.exp          | 17 ++++++
>  4 files changed, 198 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.base/gcorebg.c
>  create mode 100644 gdb/testsuite/gdb.base/gcorebg.exp
>
> diff --git a/gdb/gcore.in b/gdb/gcore.in
> index 982c854eb70..291b807bed9 100644
> --- a/gdb/gcore.in
> +++ b/gdb/gcore.in
> @@ -27,7 +27,9 @@ prefix=core
>  # to ensure gdb dumps all mappings (OS dependent).
>  dump_all_cmds=()
>  
> -while getopts :ao: opt; do
> +DATA_DIRECTORY_OPT=""

All other variables are lower case, so think this should be too.

> +
> +while getopts :ao:d: opt; do
>      case "$opt" in
>          a)
>              case "$OSTYPE" in
> @@ -40,8 +42,12 @@ while getopts :ao: opt; do
>          o)
>              prefix=$OPTARG
>              ;;
> +        d)
> +            DATA_DIRECTORY_OPT="--data-directory $OPTARG"

I've been thinking about filenames containing spaces recently.  I wonder
if we should create data_directory_opt as an array, like we do with
dump_all_cmds?

So initialise it as:

  data_directory_opt=()

And here we'd do:

  data_directory_opt=("--data-directory", "$OPTARG")

Then, when we use it we can do:

  "${data_directory_opt[@]}"

which will enclose each array element in quotes.  In this way, I think
that if OPTARG contains a space, we should still correctly forward the
string to GDB.

> +            ;;
>          *)
>              echo "usage:  @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]"
> +            echo "DATA_DIRECTORY_OPT: $DATA_DIRECTORY_OPT"

I think this line was likely debug?  But that points out that you should
have updated the usage line.

You should also update the documentation I think, there's certainly some
docs for the gcore script, so they'll need updating.

>              exit 2
>              ;;
>      esac
> @@ -98,6 +104,7 @@ do
>  	# `</dev/null' to avoid touching interactive terminal if it is
>  	# available but not accessible as GDB would get stopped on SIGTTIN.
>  	"$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \
> +            $DATA_DIRECTORY_OPT \

This is where I'd propose using:

  "${data_directory_opt[@]}"

to handle paths containing whitespace.

>  	    --nx --batch --readnever -iex 'set debuginfod enabled off' \
>  	    -ex "set pagination off" -ex "set height 0" -ex "set width 0" \
>  	    "${dump_all_cmds[@]}" \
> diff --git a/gdb/testsuite/gdb.base/gcorebg.c b/gdb/testsuite/gdb.base/gcorebg.c
> new file mode 100644
> index 00000000000..a56ca743a98
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/gcorebg.c
> @@ -0,0 +1,85 @@
> +/* Copyright 2007-2024 Free Software Foundation, Inc.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#include <stdio.h>
> +#include <sys/types.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <signal.h>
> +#include <string.h>
> +#include <assert.h>
> +
> +/* Expects 4 arguments:
> +
> +   1. Either 'standard' or 'detached', where 'standard' tests
> +   a general gcore script spawn with its controlling terminal available
> +   and 'detached' tests gcore script spawn without its controlling
> +   terminal available.
> +   2. Path to the gcore script
> +   3. Path to the data-directory to pass to the gcore script
> +   4. The core file output name
> +
> +*/

Full stop at the end of each list entry, like you have for #1, and the
final '*/' should be on the line for entry number 4.

> +
> +int
> +main (int argc, char **argv)
> +{
> +  pid_t pid = 0;
> +  pid_t ppid;
> +  char buf[1024*2 + 500];
> +  int gotint, res;
> +  int fd[2];
> +
> +  assert (argc == 5);
> +
> +  if (pipe(fd) == -1)
> +    {
> +      perror ("pipe err\n");
> +      exit (1);
> +    }
> +  pid = fork ();
> +
> +  switch (pid)
> +    {
> +    case 0:
> +      close (fd[0]);
> +      if (strcmp (argv[1], "detached") == 0)
> +	setpgrp ();
> +      ppid = getppid ();
> +      gotint = snprintf (buf, sizeof (buf), "%s -d %s -o %s %d",
> +			 argv[2], argv[3], argv[4], (int) ppid);
> +      assert (gotint < sizeof (buf));
> +      res = system (buf);
> +      assert (res != -1);
> +      close(fd[1]);
> +      break;
> +
> +    case -1:
> +      close (fd[0]);
> +      close (fd[1]);
> +      perror ("fork err\n");
> +      exit (1);
> +      break;
> +
> +    default:
> +      close (fd[1]);

In Pedro's review of V1 he suggested adding some comments of the flow of
control.  The implementation has changed since V1, but I do think it
wouldn't hurt to add a comment here:

  /* Wait here until the child is done with gcore-ing us.  When the
     child has finished it will close its end of the pipe and this read
     call will return.  */

> +      res = read (fd[0], buf, 1);
> +      assert (res == 0);
> +      close (fd[0]);
> +      break;
> +    }
> +
> +  return 0;
> +}
> diff --git a/gdb/testsuite/gdb.base/gcorebg.exp b/gdb/testsuite/gdb.base/gcorebg.exp
> new file mode 100644
> index 00000000000..6269988133c
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/gcorebg.exp
> @@ -0,0 +1,88 @@
> +# Copyright 2007-2024 Free Software Foundation, Inc.
> +#
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +#
> +# This is a test for the gcore script (not the gcore command form
> +# inside GDB).  It also tests the gcore script being run without its
> +# accessible terminal.
> +
> +standard_testfile
> +require {!is_remote host}
> +require {!is_remote target}
> +require has_gcore_script
> +
> +set corefile [standard_output_file ${testfile}.core]
> +
> +if {[build_executable "failed to build" $testfile ${srcfile}] == -1 } {
> +     return -1
> +}
> +
> +# Cleanup.
> +
> +proc core_clean {} {
> +    global corefile
> +
> +    foreach file [glob -nocomplain [join [list $corefile *] ""]] {
> +	verbose "Delete file $file" 1
> +	remote_file target delete $file
> +    }
> +}
> +core_clean
> +
> +# Generate the core file.
> +proc test_body { detached } {
> +    global binfile
> +    global GCORE
> +    global corefile
> +    global GDB_DATA_DIRECTORY
> +
> +    with_test_prefix "detached = $detached" {
> +	# We can't use gdb_test_multiple here because GDB is not started.
> +	set res [remote_spawn target "$binfile $detached $GCORE $GDB_DATA_DIRECTORY $corefile"]
> +	if { $res < 0 || $res == "" } {
> +	    fail "Spawning gcore"
> +	    return 1
> +	}
> +	pass "Spawned gcore"
> +
> +	set saw_corefile_created false
> +	set testname "Spawned gcore finished"
> +	remote_expect target 20 {
> +	    timeout {
> +		fail "$testname (timeout)"
> +		remote_exec target "kill -9 -[exp_pid -i $res]"
> +		return
> +	    }
> +	    -re "Saved corefile \[^\r\n\]+\r\n" {
> +		set saw_corefile_created true
> +		exp_continue
> +	    }
> +	    eof {
> +		gdb_assert { $saw_corefile_created } $testname
> +	    }
> +	}
> +
> +	gdb_assert {1 == [llength [glob -nocomplain [join [list $corefile *] ""]]]} "Core file generated by gcore"
> +	core_clean
> +    }
> +}
> +
> +# First a general gcore script spawn with its controlling terminal available.
> +
> +test_body standard
> +
> +# And now gcore script spawn without its controlling terminal available.
> +# It is spawned through `gcorebg.c' using setpgrp ().
> +
> +test_body detached
> diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
> index cbd37fd3094..6b1c646bd23 100644
> --- a/gdb/testsuite/lib/gdb.exp
> +++ b/gdb/testsuite/lib/gdb.exp
> @@ -158,6 +158,23 @@ load_lib gdb-utils.exp
>  load_lib memory.exp
>  load_lib check-test-names.exp
>  
> +# The path to the GCORE script to test.
> +global GCORE
> +if ![info exists GCORE] {

Please add {...} around the if expression:

  if {![info exists GCORE]} {

Thanks,
Andrew

> +    set GCORE [findfile $base_dir/../../gdb/gcore]
> +} else {
> +    set GCORE ""
> +}
> +verbose "using GCORE = $GCORE" 2
> +
> +proc has_gcore_script {} {
> +    if {[info exists ::GCORE]} {
> +	return 1
> +    } else {
> +	return 0
> +    }
> +}
> +
>  # The path to the GDB binary to test.
>  global GDB
>  
> -- 
> 2.44.0
  

Patch

diff --git a/gdb/gcore.in b/gdb/gcore.in
index 982c854eb70..291b807bed9 100644
--- a/gdb/gcore.in
+++ b/gdb/gcore.in
@@ -27,7 +27,9 @@  prefix=core
 # to ensure gdb dumps all mappings (OS dependent).
 dump_all_cmds=()
 
-while getopts :ao: opt; do
+DATA_DIRECTORY_OPT=""
+
+while getopts :ao:d: opt; do
     case "$opt" in
         a)
             case "$OSTYPE" in
@@ -40,8 +42,12 @@  while getopts :ao: opt; do
         o)
             prefix=$OPTARG
             ;;
+        d)
+            DATA_DIRECTORY_OPT="--data-directory $OPTARG"
+            ;;
         *)
             echo "usage:  @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]"
+            echo "DATA_DIRECTORY_OPT: $DATA_DIRECTORY_OPT"
             exit 2
             ;;
     esac
@@ -98,6 +104,7 @@  do
 	# `</dev/null' to avoid touching interactive terminal if it is
 	# available but not accessible as GDB would get stopped on SIGTTIN.
 	"$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \
+            $DATA_DIRECTORY_OPT \
 	    --nx --batch --readnever -iex 'set debuginfod enabled off' \
 	    -ex "set pagination off" -ex "set height 0" -ex "set width 0" \
 	    "${dump_all_cmds[@]}" \
diff --git a/gdb/testsuite/gdb.base/gcorebg.c b/gdb/testsuite/gdb.base/gcorebg.c
new file mode 100644
index 00000000000..a56ca743a98
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcorebg.c
@@ -0,0 +1,85 @@ 
+/* Copyright 2007-2024 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include <assert.h>
+
+/* Expects 4 arguments:
+
+   1. Either 'standard' or 'detached', where 'standard' tests
+   a general gcore script spawn with its controlling terminal available
+   and 'detached' tests gcore script spawn without its controlling
+   terminal available.
+   2. Path to the gcore script
+   3. Path to the data-directory to pass to the gcore script
+   4. The core file output name
+
+*/
+
+int
+main (int argc, char **argv)
+{
+  pid_t pid = 0;
+  pid_t ppid;
+  char buf[1024*2 + 500];
+  int gotint, res;
+  int fd[2];
+
+  assert (argc == 5);
+
+  if (pipe(fd) == -1)
+    {
+      perror ("pipe err\n");
+      exit (1);
+    }
+  pid = fork ();
+
+  switch (pid)
+    {
+    case 0:
+      close (fd[0]);
+      if (strcmp (argv[1], "detached") == 0)
+	setpgrp ();
+      ppid = getppid ();
+      gotint = snprintf (buf, sizeof (buf), "%s -d %s -o %s %d",
+			 argv[2], argv[3], argv[4], (int) ppid);
+      assert (gotint < sizeof (buf));
+      res = system (buf);
+      assert (res != -1);
+      close(fd[1]);
+      break;
+
+    case -1:
+      close (fd[0]);
+      close (fd[1]);
+      perror ("fork err\n");
+      exit (1);
+      break;
+
+    default:
+      close (fd[1]);
+      res = read (fd[0], buf, 1);
+      assert (res == 0);
+      close (fd[0]);
+      break;
+    }
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/gcorebg.exp b/gdb/testsuite/gdb.base/gcorebg.exp
new file mode 100644
index 00000000000..6269988133c
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcorebg.exp
@@ -0,0 +1,88 @@ 
+# Copyright 2007-2024 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# This is a test for the gcore script (not the gcore command form
+# inside GDB).  It also tests the gcore script being run without its
+# accessible terminal.
+
+standard_testfile
+require {!is_remote host}
+require {!is_remote target}
+require has_gcore_script
+
+set corefile [standard_output_file ${testfile}.core]
+
+if {[build_executable "failed to build" $testfile ${srcfile}] == -1 } {
+     return -1
+}
+
+# Cleanup.
+
+proc core_clean {} {
+    global corefile
+
+    foreach file [glob -nocomplain [join [list $corefile *] ""]] {
+	verbose "Delete file $file" 1
+	remote_file target delete $file
+    }
+}
+core_clean
+
+# Generate the core file.
+proc test_body { detached } {
+    global binfile
+    global GCORE
+    global corefile
+    global GDB_DATA_DIRECTORY
+
+    with_test_prefix "detached = $detached" {
+	# We can't use gdb_test_multiple here because GDB is not started.
+	set res [remote_spawn target "$binfile $detached $GCORE $GDB_DATA_DIRECTORY $corefile"]
+	if { $res < 0 || $res == "" } {
+	    fail "Spawning gcore"
+	    return 1
+	}
+	pass "Spawned gcore"
+
+	set saw_corefile_created false
+	set testname "Spawned gcore finished"
+	remote_expect target 20 {
+	    timeout {
+		fail "$testname (timeout)"
+		remote_exec target "kill -9 -[exp_pid -i $res]"
+		return
+	    }
+	    -re "Saved corefile \[^\r\n\]+\r\n" {
+		set saw_corefile_created true
+		exp_continue
+	    }
+	    eof {
+		gdb_assert { $saw_corefile_created } $testname
+	    }
+	}
+
+	gdb_assert {1 == [llength [glob -nocomplain [join [list $corefile *] ""]]]} "Core file generated by gcore"
+	core_clean
+    }
+}
+
+# First a general gcore script spawn with its controlling terminal available.
+
+test_body standard
+
+# And now gcore script spawn without its controlling terminal available.
+# It is spawned through `gcorebg.c' using setpgrp ().
+
+test_body detached
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index cbd37fd3094..6b1c646bd23 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -158,6 +158,23 @@  load_lib gdb-utils.exp
 load_lib memory.exp
 load_lib check-test-names.exp
 
+# The path to the GCORE script to test.
+global GCORE
+if ![info exists GCORE] {
+    set GCORE [findfile $base_dir/../../gdb/gcore]
+} else {
+    set GCORE ""
+}
+verbose "using GCORE = $GCORE" 2
+
+proc has_gcore_script {} {
+    if {[info exists ::GCORE]} {
+	return 1
+    } else {
+	return 0
+    }
+}
+
 # The path to the GDB binary to test.
 global GDB