File: test-ptrace.c

package info (click to toggle)
libunwind 1.1-3.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,540 kB
  • ctags: 5,665
  • sloc: ansic: 31,521; sh: 18,062; asm: 1,851; makefile: 866; cpp: 118
file content (362 lines) | stat: -rw-r--r-- 8,696 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* libunwind - a platform-independent unwind library
   Copyright (C) 2003-2004 Hewlett-Packard Co
	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include <config.h>

#ifdef HAVE_TTRACE

int
main (void)
{
  printf ("FAILURE: ttrace() not supported yet\n");
  return -1;
}

#else /* !HAVE_TTRACE */

#include <errno.h>
#include <fcntl.h>
#include <libunwind-ptrace.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/ptrace.h>
#include <sys/wait.h>

extern char **environ;

static const int nerrors_max = 100;

int nerrors;
int verbose;
int print_names = 1;

enum
  {
    INSTRUCTION,
    SYSCALL,
    TRIGGER
  }
trace_mode = SYSCALL;

#define panic(args...)						\
	do { fprintf (stderr, args); ++nerrors; } while (0)

static unw_addr_space_t as;
static struct UPT_info *ui;

static int killed;

void
do_backtrace (void)
{
  unw_word_t ip, sp, start_ip = 0, off;
  int n = 0, ret;
  unw_proc_info_t pi;
  unw_cursor_t c;
  char buf[512];
  size_t len;

  ret = unw_init_remote (&c, as, ui);
  if (ret < 0)
    panic ("unw_init_remote() failed: ret=%d\n", ret);

  do
    {
      if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0
	  || (ret = unw_get_reg (&c, UNW_REG_SP, &sp)) < 0)
	panic ("unw_get_reg/unw_get_proc_name() failed: ret=%d\n", ret);

      if (n == 0)
	start_ip = ip;

      buf[0] = '\0';
      if (print_names)
	unw_get_proc_name (&c, buf, sizeof (buf), &off);

      if (verbose)
	{
	  if (off)
	    {
	      len = strlen (buf);
	      if (len >= sizeof (buf) - 32)
		len = sizeof (buf) - 32;
	      sprintf (buf + len, "+0x%lx", (unsigned long) off);
	    }
	  printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
	}

      if ((ret = unw_get_proc_info (&c, &pi)) < 0)
	panic ("unw_get_proc_info(ip=0x%lx) failed: ret=%d\n", (long) ip, ret);
      else if (verbose)
	printf ("\tproc=%016lx-%016lx\n\thandler=%lx lsda=%lx",
		(long) pi.start_ip, (long) pi.end_ip,
		(long) pi.handler, (long) pi.lsda);

#if UNW_TARGET_IA64
      {
	unw_word_t bsp;

	if ((ret = unw_get_reg (&c, UNW_IA64_BSP, &bsp)) < 0)
	  panic ("unw_get_reg() failed: ret=%d\n", ret);
	else if (verbose)
	  printf (" bsp=%lx", bsp);
      }
#endif
      if (verbose)
	printf ("\n");

      ret = unw_step (&c);
      if (ret < 0)
	{
	  unw_get_reg (&c, UNW_REG_IP, &ip);
	  panic ("FAILURE: unw_step() returned %d for ip=%lx (start ip=%lx)\n",
		 ret, (long) ip, (long) start_ip);
	}

      if (++n > 64)
	{
	  /* guard against bad unwind info in old libraries... */
	  panic ("too deeply nested---assuming bogus unwind (start ip=%lx)\n",
		 (long) start_ip);
	  break;
	}
      if (nerrors > nerrors_max)
        {
	  panic ("Too many errors (%d)!\n", nerrors);
	  break;
	}
    }
  while (ret > 0);

  if (ret < 0)
    panic ("unwind failed with ret=%d\n", ret);

  if (verbose)
    printf ("================\n\n");
}

static pid_t target_pid;
static void target_pid_kill (void)
{
  kill (target_pid, SIGKILL);
}

int
main (int argc, char **argv)
{
  int status, pid, pending_sig, optind = 1, state = 1;

  as = unw_create_addr_space (&_UPT_accessors, 0);
  if (!as)
    panic ("unw_create_addr_space() failed");

  if (argc == 1)
    {
      static char *args[] = { "self", "/bin/ls", "/usr", NULL };

      /* automated test case */
      argv = args;

      /* Unless the args array is 'walked' the child
         process is unable to access it and dies with a segfault */
      fprintf(stderr, "Automated test (%s,%s,%s,%s)\n",
              args[0],args[1],args[2],args[3]);
    }
  else if (argc > 1)
    while (argv[optind][0] == '-')
      {
	if (strcmp (argv[optind], "-v") == 0)
	  ++optind, verbose = 1;
	else if (strcmp (argv[optind], "-i") == 0)
	  ++optind, trace_mode = INSTRUCTION;	/* backtrace at each insn */
	else if (strcmp (argv[optind], "-s") == 0)
	  ++optind, trace_mode = SYSCALL;	/* backtrace at each syscall */
	else if (strcmp (argv[optind], "-t") == 0)
	  /* Execute until raise(SIGUSR1), then backtrace at each insn
	     until raise(SIGUSR2).  */
	  ++optind, trace_mode = TRIGGER;
	else if (strcmp (argv[optind], "-c") == 0)
	  /* Enable caching of unwind-info.  */
	  ++optind, unw_set_caching_policy (as, UNW_CACHE_GLOBAL);
	else if (strcmp (argv[optind], "-n") == 0)
	  /* Don't look-up and print symbol names.  */
	  ++optind, print_names = 0;
	else
	  fprintf(stderr, "unrecognized option: %s\n", argv[optind++]);
        if (optind >= argc)
          break;
      }

  target_pid = fork ();
  if (!target_pid)
    {
      /* child */

      if (!verbose)
	dup2 (open ("/dev/null", O_WRONLY), 1);

#if HAVE_DECL_PTRACE_TRACEME
      ptrace (PTRACE_TRACEME, 0, 0, 0);
#elif HAVE_DECL_PT_TRACE_ME
      ptrace (PT_TRACE_ME, 0, 0, 0);
#else
#error Trace me
#endif

      if ((argc > 1) && (optind == argc)) {
        fprintf(stderr, "Need to specify a command line for the child\n");
        exit (-1);
      }
      execve (argv[optind], argv + optind, environ);
      _exit (-1);
    }
  atexit (target_pid_kill);

  ui = _UPT_create (target_pid);

  while (nerrors <= nerrors_max)
    {
      pid = wait4 (-1, &status, 0, NULL);
      if (pid == -1)
	{
	  if (errno == EINTR)
	    continue;

	  panic ("wait4() failed (errno=%d)\n", errno);
	}
      pending_sig = 0;
      if (WIFSIGNALED (status) || WIFEXITED (status)
	  || (WIFSTOPPED (status) && WSTOPSIG (status) != SIGTRAP))
	{
	  if (WIFEXITED (status))
	    {
	      if (WEXITSTATUS (status) != 0)
		panic ("child's exit status %d\n", WEXITSTATUS (status));
	      break;
	    }
	  else if (WIFSIGNALED (status))
	    {
	      if (!killed)
		panic ("child terminated by signal %d\n", WTERMSIG (status));
	      break;
	    }
	  else
	    {
	      pending_sig = WSTOPSIG (status);
	      /* Avoid deadlock:  */
	      if (WSTOPSIG (status) == SIGKILL)
	        break;
	      if (trace_mode == TRIGGER)
		{
		  if (WSTOPSIG (status) == SIGUSR1)
		    state = 0;
		  else if  (WSTOPSIG (status) == SIGUSR2)
		    state = 1;
		}
	      if (WSTOPSIG (status) != SIGUSR1 && WSTOPSIG (status) != SIGUSR2)
	        {
		  static int count = 0;

		  if (count++ > 100)
		    {
		      panic ("Too many child unexpected signals (now %d)\n",
			     WSTOPSIG (status));
			killed = 1;
		    }
	        }
	    }
	}

      switch (trace_mode)
	{
	case TRIGGER:
	  if (state)
#if HAVE_DECL_PTRACE_CONT
	    ptrace (PTRACE_CONT, target_pid, 0, 0);
#elif HAVE_DECL_PT_CONTINUE
	    ptrace (PT_CONTINUE, target_pid, (caddr_t)1, 0);
#else
#error Port me
#endif
	  else
	    {
	      do_backtrace ();
#if HAVE_DECL_PTRACE_SINGLESTEP
	      ptrace (PTRACE_SINGLESTEP, target_pid, 0, pending_sig);
#elif HAVE_DECL_PT_STEP
	      ptrace (PT_STEP, target_pid, (caddr_t)1, pending_sig);
#else
#error Singlestep me
#endif
	    }
	  break;

	case SYSCALL:
	  if (!state)
	    do_backtrace ();
	  state ^= 1;
#if HAVE_DECL_PTRACE_SYSCALL
	  ptrace (PTRACE_SYSCALL, target_pid, 0, pending_sig);
#elif HAVE_DECL_PT_SYSCALL
	  ptrace (PT_SYSCALL, target_pid, (caddr_t)1, pending_sig);
#else
#error Syscall me
#endif
	  break;

	case INSTRUCTION:
	  do_backtrace ();
#if HAVE_DECL_PTRACE_SINGLESTEP
	      ptrace (PTRACE_SINGLESTEP, target_pid, 0, pending_sig);
#elif HAVE_DECL_PT_STEP
	      ptrace (PT_STEP, target_pid, (caddr_t)1, pending_sig);
#else
#error Singlestep me
#endif
	  break;
	}
      if (killed)
        kill (target_pid, SIGKILL);
    }

  _UPT_destroy (ui);
  unw_destroy_addr_space (as);

  if (nerrors)
    {
      printf ("FAILURE: detected %d errors\n", nerrors);
      exit (-1);
    }
  if (verbose)
    printf ("SUCCESS\n");

  return 0;
}

#endif /* !HAVE_TTRACE */