1 /* backtrace_linux - backtrace displaying on linux platform */
2 /* This file is included by ex.cpp on need (have execinfo.h, popen & addrline)*/
4 /* Copyright (c) 2008-2018. The SimGrid Team. All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
16 #include <boost/algorithm/string.hpp>
22 /* This file is to be included in ex.cpp, so the following headers are not mandatory, but it's to make sure that eclipse see them too */
23 #include <xbt/string.hpp>
24 #include <xbt/backtrace.hpp>
27 #include "xbt/module.h" /* xbt_binary_name */
28 #include "src/xbt_modinter.h" /* backtrace initialization headers */
29 /* end of "useless" inclusions */
31 size_t xbt_backtrace_current(xbt_backtrace_location_t* loc, std::size_t count)
33 std::size_t used = backtrace(loc, count);
35 std::fprintf(stderr, "The backtrace() function failed, which probably means that the memory is exhausted\n.");
36 std::fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory\n.");
37 std::fprintf(stderr, "Please go fix the memleaks\n");
46 /** Find the path of the binary file from the name found in argv */
47 static std::string get_binary_path()
51 if (xbt_binary_name == nullptr)
54 // We found it, we are happy:
55 if (stat(xbt_binary_name, &stat_buf) == 0)
56 return xbt_binary_name;
58 // Not found, look in the PATH:
59 char* path = getenv("PATH");
62 XBT_DEBUG("Looking in the PATH: %s\n", path);
63 std::vector<std::string> path_list;
64 // TODO, on Windows, this is ";"
65 boost::split(path_list, path, boost::is_any_of(":"));
66 for (std::string const& path_item : path_list) {
67 std::string binary_name = simgrid::xbt::string_printf(
68 "%s/%s", path_item.c_str(), xbt_binary_name);
69 bool found = (stat(binary_name.c_str(), &stat_buf) == 0);
70 XBT_DEBUG("Looked in the PATH for the binary. %s %s",
71 found ? "Found" : "Not found",
81 //FIXME: This code could be greatly improved/simplifyied with
82 // http://cairo.sourcearchive.com/documentation/1.9.4/backtrace-symbols_8c-source.html
83 std::vector<std::string> resolve_backtrace(xbt_backtrace_location_t const* loc, std::size_t count)
85 std::vector<std::string> result;
90 if (xbt_binary_name == nullptr)
91 XBT_WARN("XBT not initialized, the backtrace will not be resolved.");
93 // Drop the first one:
96 char** backtrace_syms = backtrace_symbols(loc, count);
97 std::string binary_name = get_binary_path();
99 if (binary_name.empty()) {
100 for (std::size_t i = 0; i < count; i++)
101 result.push_back(simgrid::xbt::string_printf("%p", loc[i]));
105 // Create the system command for add2line:
106 std::ostringstream stream;
107 stream << ADDR2LINE << " -f -e " << binary_name << ' ';
108 std::vector<std::string> addrs(count);
109 for (std::size_t i = 0; i < count; i++) {
110 /* retrieve this address */
111 XBT_DEBUG("Retrieving address number %zu from '%s'", i, backtrace_syms[i]);
113 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
114 char* p = strchr(buff, ']');
116 if (strcmp(buff, "(nil)"))
120 XBT_DEBUG("Set up a new address: %zu, '%s'", i, addrs[i].c_str());
121 /* Add it to the command line args */
122 stream << addrs[i] << ' ';
124 std::string cmd = stream.str();
126 /* size (in char) of pointers on this arch */
127 int addr_len = addrs[0].size();
129 XBT_VERB("Fire a first command: '%s'", cmd.c_str());
130 FILE* pipe = popen(cmd.c_str(), "r");
131 xbt_assert(pipe, "Cannot fork addr2line to display the backtrace");
133 /* To read the output of addr2line */
134 char line_func[1024];
136 for (std::size_t i = 0; i < count; i++) {
137 XBT_DEBUG("Looking for symbol %zu, addr = '%s'", i, addrs[i].c_str());
138 if (fgets(line_func, 1024, pipe)) {
139 line_func[strlen(line_func) - 1] = '\0';
141 XBT_VERB("Cannot run fgets to look for symbol %zu, addr %s", i, addrs[i].c_str());
142 strncpy(line_func, "???", 4);
144 if (fgets(line_pos, 1024, pipe)) {
145 line_pos[strlen(line_pos) - 1] = '\0';
147 XBT_VERB("Cannot run fgets to look for symbol %zu, addr %s", i, addrs[i].c_str());
148 strncpy(line_pos, backtrace_syms[i],1024);
151 if (strcmp("??", line_func) != 0) {
152 auto name = simgrid::xbt::demangle(line_func);
153 XBT_DEBUG("Found static symbol %s at %s", name.get(), line_pos);
154 result.push_back(simgrid::xbt::string_printf(
155 "%s at %s, %p", name.get(), line_pos, loc[i]
158 /* Damn. The symbol is in a dynamic library. Let's get wild */
160 unsigned long int offset = 0;
163 /* let's look for the offset of this library in our addressing space */
164 std::string maps_name = std::string("/proc/") + std::to_string(getpid()) + "/maps";
165 std::ifstream maps(maps_name);
167 XBT_CRITICAL("open(\"%s\") failed: %s", maps_name.c_str(), strerror(errno));
171 unsigned long int addr = std::stoul(addrs[i], &pos, 16);
172 if (pos != addrs[i].length()) {
173 XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)", addrs[i].c_str(), addr);
175 XBT_DEBUG("addr=%s (as string) =%#lx (as number)", addrs[i].c_str(), addr);
178 unsigned long int first;
179 unsigned long int last;
181 std::string maps_buff;
182 if (not std::getline(maps, maps_buff))
185 XBT_DEBUG("map line: %s", maps_buff.c_str());
187 first = std::stoul(maps_buff, &pos, 16);
188 maps_buff.erase(0, pos + 1);
189 last = std::stoul(maps_buff, nullptr, 16);
190 if (first < addr && addr < last) {
195 XBT_DEBUG("%#lx in [%#lx-%#lx]", addr, first, last);
196 XBT_DEBUG("Symbol found, map lines not further displayed (even if looking for next ones)");
203 XBT_VERB("Problem while reading the maps file. Following backtrace will be mangled.");
204 XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]);
205 result.push_back(simgrid::xbt::string_printf("?? (%s)", backtrace_syms[i]));
209 /* Ok, Found the offset of the maps line containing the searched symbol.
210 We now need to substract this from the address we got from backtrace.
213 addrs[i] = simgrid::xbt::string_printf("0x%0*lx", addr_len - 2, addr - offset);
214 XBT_DEBUG("offset=%#lx new addr=%s", offset, addrs[i].c_str());
216 /* Got it. We have our new address. Let's get the library path and we are set */
217 std::string p(backtrace_syms[i]);
219 /* library path not displayed in the map file either... */
220 snprintf(line_func, 3, "??");
222 size_t p2 = p.find_first_of("( ");
223 if (p2 != std::string::npos)
226 /* Here we go, fire an addr2line up */
227 std::string subcmd = std::string(ADDR2LINE) + " -f -e " + p + " " + addrs[i];
228 XBT_VERB("Fire another command: '%s'", subcmd.c_str());
229 FILE* subpipe = popen(subcmd.c_str(), "r");
231 xbt_die("Cannot fork addr2line to display the backtrace");
233 if (fgets(line_func, 1024, subpipe)) {
234 line_func[strlen(line_func) - 1] = '\0';
236 XBT_VERB("Cannot read result of subcommand %s", subcmd.c_str());
237 strncpy(line_func, "???", 4);
239 if (fgets(line_pos, 1024, subpipe)) {
240 line_pos[strlen(line_pos) - 1] = '\0';
242 XBT_VERB("Cannot read result of subcommand %s", subcmd.c_str());
243 strncpy(line_pos, backtrace_syms[i],1024);
248 /* check whether the trick worked */
249 if (strcmp("??", line_func)) {
250 auto name = simgrid::xbt::demangle(line_func);
251 XBT_DEBUG("Found dynamic symbol %s at %s", name.get(), line_pos);
252 result.push_back(simgrid::xbt::string_printf(
253 "%s at %s, %p", name.get(), line_pos, loc[i]));
255 /* damn, nothing to do here. Let's print the raw address */
256 XBT_DEBUG("Dynamic symbol not found. Raw address = %s", backtrace_syms[i]);
257 result.push_back(simgrid::xbt::string_printf(
258 "?? at %s", backtrace_syms[i]));
263 /* Mask the bottom of the stack */
264 const char* const breakers[] = {
266 "_ZN7simgrid6kernel7context13ThreadContext7wrapperE", // simgrid::kernel::context::ThreadContext::wrapper
267 "_ZN7simgrid6kernel7context8UContext7wrapperE" // simgrid::kernel::context::UContext::wrapper
269 bool do_break = false;
270 for (const char* b : breakers) {
271 if (strncmp(b, line_func, strlen(b)) == 0) {
280 free(backtrace_syms);