1/*
2 * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $Id: backtrace_test.c,v 1.4 2009/09/02 23:48:01 tbox Exp $ */
18
19#include <config.h>
20
21#include <stdio.h>
22#include <string.h>
23
24#include <isc/backtrace.h>
25#include <isc/result.h>
26
27const char *expected_symbols[] = {
28	"func3",
29	"func2",
30	"func1",
31	"main"
32};
33
34static int
35func3() {
36	void *tracebuf[16];
37	int i, nframes;
38	int error = 0;
39	const char *fname;
40	isc_result_t result;
41	unsigned long offset;
42
43	result = isc_backtrace_gettrace(tracebuf, 16, &nframes);
44	if (result != ISC_R_SUCCESS) {
45		printf("isc_backtrace_gettrace failed: %s\n",
46		       isc_result_totext(result));
47		return (1);
48	}
49
50	if (nframes < 4)
51		error++;
52
53	for (i = 0; i < 4 && i < nframes; i++) {
54		fname = NULL;
55		result = isc_backtrace_getsymbol(tracebuf[i], &fname, &offset);
56		if (result != ISC_R_SUCCESS) {
57			error++;
58			continue;
59		}
60		if (strcmp(fname, expected_symbols[i]) != 0)
61			error++;
62	}
63
64	if (error) {
65		printf("Unexpected result:\n");
66		printf("  # of frames: %d (expected: at least 4)\n", nframes);
67		printf("  symbols:\n");
68		for (i = 0; i < nframes; i++) {
69			fname = NULL;
70			result = isc_backtrace_getsymbol(tracebuf[i], &fname,
71							 &offset);
72			if (result == ISC_R_SUCCESS)
73				printf("  [%d] %s\n", i, fname);
74			else {
75				printf("  [%d] getsymbol failed: %s\n", i,
76				       isc_result_totext(result));
77			}
78		}
79	}
80
81	return (error);
82}
83
84static int
85func2() {
86	return (func3());
87}
88
89static int
90func1() {
91	return (func2());
92}
93
94int
95main() {
96	return (func1());
97}
98