5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
6 * Copyright (C) 2016 Yann E. MORIN <yann.morin.1998@free.fr>. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 void print_backtrace(const char* program_path, const char* program_exec,
42 int outfd[2], infd[2];
49 pathlen = strlen(program_path);
51 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
77 dup2(outfd[0], STDIN_FILENO);
78 dup2(infd[1], STDOUT_FILENO);
80 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
88 connman_error("++++++++ backtrace ++++++++");
90 for (i = offset; i < n_ptrs - 1; i++) {
92 char addr[20], buf[PATH_MAX * 2];
96 dladdr(frames[i], &info);
98 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
102 written = write(outfd[1], addr, len);
106 len = read(infd[0], buf, sizeof(buf) - 1);
112 pos = strchr(buf, '\n');
114 connman_error("Error in backtrace format");
120 if (strcmp(buf, "??") == 0) {
121 connman_error("#%-2u %p in %s", i - offset,
122 frames[i], info.dli_fname);
126 ptr = strchr(pos, '\n');
128 connman_error("Error in backtrace format");
134 if (strncmp(pos, program_path, pathlen) == 0)
137 connman_error("#%-2u %p in %s() at %s", i - offset,
138 frames[i], buf, pos);
141 connman_error("+++++++++++++++++++++++++++");