Imported Upstream version 1.1
[platform/upstream/libunwind.git] / tests / test-ptrace-misc.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2004 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26 #include "compiler.h"
27
28 #include <signal.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <sys/types.h>
35
36 pid_t self;
37 int global[64];
38
39 int
40 func (int arg)
41 {
42   int sum = 0, i, max, arr[1024];
43
44   if (arg == 0)
45     {
46       sum = global[2];
47       sum += sum + sum * getppid ();
48       return sum;
49     }
50   else
51     {
52       max = arg;
53       if (max >= 64)
54         max = 64;
55
56       for (i = 0; i < max; ++i)
57         arr[i] = func (arg - 1);
58
59       for (i = 0; i < max; ++i)
60         if (arr[i] > 16)
61           sum += arr[i];
62         else
63           sum -= arr[i];
64     }
65   return sum;
66 }
67
68 int
69 bar (int v)
70 {
71   extern long f (long);
72   int arr[1] = { v };
73   uintptr_t r;
74
75   /* This is a vain attempt to use up lots of registers to force
76      the frame-chain info to be saved on the memory stack on ia64.
77      It happens to work with gcc v3.3.4 and gcc v3.4.1 but perhaps
78      not with any other compiler.  */
79   r = (uintptr_t) malloc(f (arr[0])
80        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
81        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
82        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
83        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
84        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
85        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
86        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
87        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
88        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
89        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
90        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
91        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
92        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
93        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
94        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
95        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v)
96        + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + f (v))
97        ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
98        )))))))))))))))))))))))))))))))))))))))))))))))))))))));
99   if (r < 2)
100     v = r;
101
102   kill (self, SIGUSR1); /* tell test-ptrace to start single-stepping */
103   v = func (v);
104   kill (self, SIGUSR2); /* tell test-ptrace to stop single-stepping */
105   return v;
106 }
107
108 int
109 main (int argc, char **argv UNUSED)
110 {
111   int val = argc;
112
113   signal (SIGUSR1, SIG_IGN);
114   signal (SIGUSR2, SIG_IGN);
115
116   self = getpid ();
117
118   printf ("sum = %d\n", bar (val));
119   return 0;
120 }