2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "backtrace.h"
29 VLOG_DEFINE_THIS_MODULE(backtrace);
34 backtrace_capture(struct backtrace *b)
36 void *frames[BACKTRACE_MAX_FRAMES];
39 b->n_frames = backtrace(frames, BACKTRACE_MAX_FRAMES);
40 for (i = 0; i < b->n_frames; i++) {
41 b->frames[i] = (uintptr_t) frames[i];
48 static const char file_name[] = "/proc/self/maps";
53 f = fopen(file_name, "r");
55 VLOG_WARN("opening %s failed: %s", file_name, strerror(errno));
59 for (line_number = 1; fgets(line, sizeof line, f); line_number++) {
60 if (strstr(line, "[stack]")) {
62 if (sscanf(line, "%*x-%"SCNxPTR, &end) != 1) {
63 VLOG_WARN("%s:%d: parse error", file_name, line_number);
72 VLOG_WARN("%s: no stack found", file_name);
79 static uintptr_t high;
81 high = get_max_stack();
89 uintptr_t low = (uintptr_t) &low;
96 uintptr_t address = (uintptr_t) p;
97 return address >= stack_low() && address < stack_high();
101 backtrace_capture(struct backtrace *backtrace)
107 for (frame = __builtin_frame_address(1);
108 frame != NULL && in_stack(frame) && frame[0] != NULL
109 && n < BACKTRACE_MAX_FRAMES;
112 backtrace->frames[n++] = (uintptr_t) frame[1];
114 backtrace->n_frames = n;
116 #else /* !HAVE_BACKTRACE && !__GNUC__ */
118 backtrace_capture(struct backtrace *backtrace)
120 backtrace->n_frames = 0;