1 #!/usr/bin/ksh
   2 #
   3 # This file and its contents are supplied under the terms of the
   4 # Common Development and Distribution License ("CDDL"), version 1.0.
   5 # You may only use this file in accordance with the terms of version
   6 # 1.0 of the CDDL.
   7 #
   8 # A full copy of the text of the CDDL should have accompanied this
   9 # source.  A copy of the CDDL is also available via the Internet at
  10 # http://www.illumos.org/license/CDDL.
  11 #
  12 # Copyright (c) 2019, Joyent, Inc.
  13 #
  14 
  15 set -e
  16 
  17 result=0
  18 
  19 progname=$(basename $0)
  20 
  21 fail()
  22 {
  23         echo "Failed: $*" 2>&1
  24         result=1
  25 }
  26 
  27 fail_no_debug()
  28 {
  29         cmd="$@"
  30         set +e
  31         out=$($ctf_convert $cmd 2>&1)
  32 
  33         if [[ $? -eq 0 ]]; then
  34                 fail "$cmd succeeded but should have failed"
  35                 set -e
  36                 return;
  37         fi
  38 
  39         set -e
  40 
  41         if echo "$out" | grep "is missing debug info" >/dev/null; then
  42                 return;
  43         fi
  44 
  45         if echo "$out" | grep "does not contain DWARF data" >/dev/null; then
  46                 return;
  47         fi
  48         fail "$cmd: incorrect output $out"
  49 }
  50 
  51 has_ctf()
  52 {
  53         for f in "$@"; do
  54                 if ! elfdump -c -N .SUNW_ctf "$f" |
  55                     grep '.SUNW_ctf' >/dev/null; then
  56                         fail "$f lacks CTF section"
  57                         return
  58                 fi
  59         done
  60 }
  61 
  62 cat <<EOF >file1.c
  63 #include <stdio.h>
  64 struct foo { int a; };
  65 int main(void) { struct foo foo = { 4 }; printf("%d\n", foo.a); }
  66 EOF
  67 
  68 cat <<EOF >file2.c
  69 #include <stdio.h>
  70 char myfunc(int a) { printf("%d\n", a); }
  71 EOF
  72 
  73 cat <<EOF >file3.cc
  74 struct bar { char *tar; };
  75 void mycxxfunc(char *c) { c[0] = '9'; };
  76 EOF
  77 
  78 cat <<EOF >file4.s
  79 .globl caller
  80 .type caller,@function
  81 caller:
  82         movl 4(%ebp), %eax
  83         ret
  84 EOF
  85 
  86 echo "$progname: An empty file should fail conversion due to no DWARF"
  87 echo >emptyfile.c
  88 
  89 $ctf_cc -c -o emptyfile.o emptyfile.c
  90 fail_no_debug emptyfile.o
  91 $ctf_cc -c -o emptyfile.o emptyfile.c
  92 $ctf_convert -m emptyfile.o
  93 
  94 $ctf_cc $ctf_debugflags -c -o emptyfile.o emptyfile.c
  95 fail_no_debug emptyfile.o
  96 $ctf_cc $ctf_debugflags -c -o emptyfile.o emptyfile.c
  97 $ctf_convert -m emptyfile.o
  98 
  99 echo "$progname: A file missing DWARF should fail conversion"
 100 
 101 $ctf_cc -c -o file1.o file1.c
 102 fail_no_debug file1.o
 103 $ctf_cc -c -o file1.o file1.c
 104 $ctf_convert -m file1.o
 105 
 106 echo "$progname: A binary with DWARF but 0 debug dies should fail conversion"
 107 
 108 $ctf_cc -o mybin file1.c
 109 fail_no_debug mybin
 110 $ctf_cc -o mybin file1.c
 111 $ctf_convert -m mybin
 112 
 113 echo "$progname: One C file missing DWARF should fail ctfconvert"
 114 
 115 $ctf_cc -c -o file1.o file1.c
 116 $ctf_cc $ctf_debugflags -c -o file2.o file2.c
 117 ld -r -o files.o file2.o file1.o
 118 fail_no_debug files.o
 119 ld -r -o files.o file2.o file1.o
 120 $ctf_convert -m files.o
 121 has_ctf files.o
 122 
 123 echo "$progname: One .cc file missing DWARF should pass"
 124 
 125 $ctf_cc $ctf_debugflags -c -o file1.o file1.c
 126 $ctf_cc $ctf_debugflags -c -o file2.o file2.c
 127 $ctf_cxx -c -o file3.o file3.cc
 128 ld -r -o files.o file1.o file2.o file3.o
 129 $ctf_convert files.o
 130 has_ctf files.o
 131 
 132 echo "$progname: One .s file missing DWARF should pass"
 133 $ctf_cc $ctf_debugflags -c -o file1.o file1.c
 134 $ctf_cc $ctf_debugflags -c -o file2.o file2.c
 135 $ctf_as -o file4.o file4.s
 136 $ctf_cc -o mybin file1.o file2.o file4.o
 137 $ctf_convert mybin
 138 has_ctf mybin
 139 
 140 echo "result is $result"
 141 exit $result