blob: 34d35197cfb0a23362b31b117a58ce18b325a3de [file] [log] [blame]
Christopher Ferris7fb22872013-09-27 12:43:15 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <stdbool.h>
20#include <string.h>
21#include <sys/types.h>
22#include <signal.h>
23#include <sys/ptrace.h>
24#include <sys/wait.h>
25#include <unistd.h>
26#include <inttypes.h>
27
28#include <backtrace/backtrace.h>
29
30#define FINISH(pid) dump_frames(&backtrace); if (pid < 0) exit(1); else return false;
31
32// Prototypes for functions in the test library.
33int test_level_one(int, int, int, int, bool (*)(pid_t));
34
35int test_recursive_call(int, bool (*)(pid_t));
36
37void dump_frames(const backtrace_t* backtrace) {
38 for (size_t i = 0; i < backtrace->num_frames; i++) {
39 printf("%zu ", i);
40 if (backtrace->frames[i].map_name) {
41 printf("%s", backtrace->frames[i].map_name);
42 } else {
43 printf("<unknown>");
44 }
45 if (backtrace->frames[i].proc_name) {
46 printf(" %s", backtrace->frames[i].proc_name);
47 if (backtrace->frames[i].proc_offset) {
48 printf("+%" PRIuPTR, backtrace->frames[i].proc_offset);
49 }
50 }
51 printf("\n");
52 }
53}
54
55bool check_frame(const backtrace_t* backtrace, size_t frame_num,
56 const char* expected_name) {
57 if (backtrace->frames[frame_num].proc_name == NULL) {
58 printf(" Frame %zu function name expected %s, real value is NULL.\n",
59 frame_num, expected_name);
60 return false;
61 }
62 if (strcmp(backtrace->frames[frame_num].proc_name, expected_name) != 0) {
63 printf(" Frame %zu function name expected %s, real value is %s.\n",
64 frame_num, expected_name, backtrace->frames[frame_num].proc_name);
65 return false;
66 }
67 return true;
68}
69
70bool verify_level_backtrace(pid_t pid) {
71 const char* test_type;
72 if (pid < 0) {
73 test_type = "current";
74 } else {
75 test_type = "running";
76 }
77
78 backtrace_t backtrace;
79 if (!backtrace_get_data(&backtrace, pid)) {
80 printf(" backtrace_get_data failed on %s process.\n", test_type);
81 FINISH(pid);
82 }
83
84 if (backtrace.num_frames == 0) {
85 printf(" backtrace_get_data returned no frames for %s process.\n",
86 test_type);
87 FINISH(pid);
88 }
89
90 // Look through the frames starting at the highest to find the
91 // frame we want.
92 size_t frame_num = 0;
93 for (size_t i = backtrace.num_frames-1; i > 2; i--) {
94 if (backtrace.frames[i].proc_name != NULL &&
95 strcmp(backtrace.frames[i].proc_name, "test_level_one") == 0) {
96 frame_num = i;
97 break;
98 }
99 }
100 if (!frame_num) {
101 printf(" backtrace_get_data did not include the test_level_one frame.\n");
102 FINISH(pid);
103 }
104
105 if (!check_frame(&backtrace, frame_num, "test_level_one")) {
106 FINISH(pid);
107 }
108 if (!check_frame(&backtrace, frame_num-1, "test_level_two")) {
109 FINISH(pid);
110 }
111 if (!check_frame(&backtrace, frame_num-2, "test_level_three")) {
112 FINISH(pid);
113 }
114 if (!check_frame(&backtrace, frame_num-3, "test_level_four")) {
115 FINISH(pid);
116 }
117 backtrace_free_data(&backtrace);
118
119 return true;
120}
121
122bool verify_max_backtrace(pid_t pid) {
123 const char* test_type;
124 if (pid < 0) {
125 test_type = "current";
126 } else {
127 test_type = "running";
128 }
129
130 backtrace_t backtrace;
131 if (!backtrace_get_data(&backtrace, pid)) {
132 printf(" backtrace_get_data failed on %s process.\n", test_type);
133 FINISH(pid);
134 }
135
136 if (backtrace.num_frames != MAX_BACKTRACE_FRAMES) {
137 printf(" backtrace_get_data %s process max frame check failed:\n",
138 test_type);
139 printf(" Expected num frames to be %zu, found %zu\n",
140 MAX_BACKTRACE_FRAMES, backtrace.num_frames);
141 FINISH(pid);
142 }
143 backtrace_free_data(&backtrace);
144
145 return true;
146}
147
148void verify_proc_test(pid_t pid, bool (*verify_func)(pid_t)) {
149 printf(" Waiting 5 seconds for process to get to infinite loop.\n");
150 sleep(5);
151 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) {
152 printf("Failed to attach to pid %d\n", pid);
153 kill(pid, SIGKILL);
154 exit(1);
155 }
156 bool pass = verify_func(pid);
157 if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) {
158 printf("Failed to detach from pid %d\n", pid);
159 kill(pid, SIGKILL);
160 exit(1);
161 }
162
163 kill(pid, SIGKILL);
164 int status;
165 if (waitpid(pid, &status, 0) != pid) {
166 printf("Forked process did not terminate properly.\n");
167 exit(1);
168 }
169
170 if (!pass) {
171 exit(1);
172 }
173}
174
175int main() {
176 printf("Running level test on current process...\n");
177 int value = test_level_one(1, 2, 3, 4, verify_level_backtrace);
178 if (value == 0) {
179 printf("This should never happen.\n");
180 exit(1);
181 }
182 printf(" Passed.\n");
183
184 printf("Running max level test on current process...\n");
185 value = test_recursive_call(MAX_BACKTRACE_FRAMES+10, verify_max_backtrace);
186 if (value == 0) {
187 printf("This should never happen.\n");
188 exit(1);
189 }
190 printf(" Passed.\n");
191
192 printf("Running level test on process...\n");
193 pid_t pid;
194 if ((pid = fork()) == 0) {
195 value = test_level_one(1, 2, 3, 4, NULL);
196 if (value == 0) {
197 printf("This should never happen.\n");
198 }
199 exit(1);
200 }
201 verify_proc_test(pid, verify_level_backtrace);
202 printf(" Passed.\n");
203
204 printf("Running max frame test on process...\n");
205 if ((pid = fork()) == 0) {
206 value = test_recursive_call(MAX_BACKTRACE_FRAMES+10, NULL);
207 if (value == 0) {
208 printf("This should never happen.\n");
209 }
210 exit(1);
211 }
212 verify_proc_test(pid, verify_max_backtrace);
213 printf(" Passed.\n");
214
215 printf("All tests passed.\n");
216 return 0;
217}