xref: /openbsd-src/gnu/usr.bin/perl/t/op/dump.t (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1b8851fccSafresh1#!./perl
2b8851fccSafresh1
3b8851fccSafresh1# Minimally test if dump() behaves as expected
4b8851fccSafresh1
5b8851fccSafresh1BEGIN {
6b8851fccSafresh1    chdir 't' if -d 't';
7b8851fccSafresh1    require './test.pl';
85759b3d2Safresh1    set_up_inc( qw(. ../lib) );
9b8851fccSafresh1    skip_all_if_miniperl();
10b8851fccSafresh1}
11b8851fccSafresh1
12b8851fccSafresh1use Config;
13b8851fccSafresh1use File::Temp qw(tempdir);
14b8851fccSafresh1use Cwd qw(getcwd);
15b8851fccSafresh1use File::Spec;
16b8851fccSafresh1
17b8851fccSafresh1skip_all("only tested on devel builds")
18b8851fccSafresh1  unless $Config{usedevel};
19b8851fccSafresh1
20b8851fccSafresh1# there may be other operating systems where it makes sense, but
21b8851fccSafresh1# there are some where it isn't, so limit the platforms we test
22b8851fccSafresh1# this on. Also this needs to be a platform that fully supports
23b8851fccSafresh1# fork() and waitpid().
24b8851fccSafresh1
25b8851fccSafresh1skip_all("no point in dumping on $^O")
265759b3d2Safresh1  unless $^O =~ /^(linux|.*bsd|solaris|darwin)$/;
27b8851fccSafresh1
28*eac174f2Safresh1skip_all("GH 18847: excessive writes to /var/log/messages on FreeBSD")
29*eac174f2Safresh1    if $^O eq 'freebsd';
30*eac174f2Safresh1
31b8851fccSafresh1skip_all("avoid coredump under ASan")
32b8851fccSafresh1  if  $Config{ccflags} =~ /-fsanitize=/;
33b8851fccSafresh1
34b8851fccSafresh1# execute in a work directory so File::Temp can clean up core dumps
35b8851fccSafresh1my $tmp = tempdir(CLEANUP => 1);
36b8851fccSafresh1
37b8851fccSafresh1my $start = getcwd;
38b8851fccSafresh1
39b8851fccSafresh1# on systems which don't make $^X absolute which_perl() in test.pl won't
40b8851fccSafresh1# return an absolute path, so once we change directories it can't
41b8851fccSafresh1# find ./perl, resulting in test failures
42b8851fccSafresh1$^X = File::Spec->rel2abs($^X);
43b8851fccSafresh1
44b8851fccSafresh1chdir $tmp
45b8851fccSafresh1  or skip_all("Cannot chdir to work directory");
46b8851fccSafresh1
47b8851fccSafresh1plan(2);
48b8851fccSafresh1
49b8851fccSafresh1# Depending on how perl is built, there may be extraneous stuff on stderr
50b8851fccSafresh1# such as "Aborted", which isn't caught by the '2>&1' that
51b46d8ef2Safresh1# fresh_perl_like() does. So execute each CORE::dump() in a sub-process.
52b8851fccSafresh1#
53b8851fccSafresh1# In detail:
54b8851fccSafresh1# fresh_perl_like() ends up doing a `` which invokes a shell with 2 args:
55b8851fccSafresh1#
56b8851fccSafresh1#   "sh", "-c", "perl /tmp/foo 2>&1"
57b8851fccSafresh1#
58b46d8ef2Safresh1# When the perl process coredumps after calling CORE::dump(), the parent
59b8851fccSafresh1# sh sees that the exit of the child flags a coredump and so prints
60b8851fccSafresh1# something like the following to stderr:
61b8851fccSafresh1#
62b8851fccSafresh1#    sh: line 1: 17605 Aborted (core dumped)
63b8851fccSafresh1#
64b8851fccSafresh1# Note that the '2>&1' only applies to the perl process, not to the sh
65b8851fccSafresh1# command itself.
66b8851fccSafresh1# By do the dump in a child, the parent perl process exits back to sh with
67b8851fccSafresh1# a normal exit value, so sh won't complain.
68b8851fccSafresh1
695759b3d2Safresh1# An unqualified dump() will give a deprecation warning. Usually, we'd
705759b3d2Safresh1# do a "no warnings 'deprecated'" to shut this off, but since we have
715759b3d2Safresh1# chdirred to /tmp, a 'no' won't find the pragma. Hence the fiddling with
725759b3d2Safresh1# $SIG{__WARN__}.
735759b3d2Safresh1
74b8851fccSafresh1fresh_perl_like(<<'PROG', qr/\AA(?!B\z)/, {}, "plain dump quits");
755759b3d2Safresh1BEGIN {$SIG {__WARN__} = sub {1;}}
76b8851fccSafresh1++$|;
77b8851fccSafresh1my $pid = fork;
78b8851fccSafresh1die "fork: $!\n" unless defined $pid;
79b8851fccSafresh1if ($pid) {
80b8851fccSafresh1    # parent
81b8851fccSafresh1    waitpid($pid, 0);
82b8851fccSafresh1}
83b8851fccSafresh1else {
84b8851fccSafresh1    # child
85b8851fccSafresh1    print qq(A);
86b46d8ef2Safresh1    CORE::dump;
87b8851fccSafresh1    print qq(B);
88b8851fccSafresh1}
89b8851fccSafresh1PROG
90b8851fccSafresh1
91b46d8ef2Safresh1fresh_perl_like(<<'PROG', qr/A(?!B\z)/, {}, "CORE::dump with label quits"); BEGIN {$SIG {__WARN__} = sub {1;}}
92b8851fccSafresh1++$|;
93b8851fccSafresh1my $pid = fork;
94b8851fccSafresh1die "fork: $!\n" unless defined $pid;
95b8851fccSafresh1if ($pid) {
96b8851fccSafresh1    # parent
97b8851fccSafresh1    waitpid($pid, 0);
98b8851fccSafresh1}
99b8851fccSafresh1else {
100b8851fccSafresh1    print qq(A);
101b46d8ef2Safresh1    CORE::dump foo;
102b8851fccSafresh1    foo:
103b8851fccSafresh1    print qq(B);
104b8851fccSafresh1}
105b8851fccSafresh1PROG
106b8851fccSafresh1
107b8851fccSafresh1END {
108b8851fccSafresh1  chdir $start if defined $start;
109b8851fccSafresh1}
110