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