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