| 1720 | #ifdef HAVE_BACKTRACE |
| 1721 | |
| 1722 | void backtrace_symbols_demangle_fd(void **trace, size_t csym, int fd) |
| 1723 | { |
| 1724 | char **syms = backtrace_symbols(trace, csym); |
| 1725 | char symbuf[1024]; |
| 1726 | for (size_t itrace = 0; itrace < csym; ++itrace) |
| 1727 | { |
| 1728 | int status = 0; |
| 1729 | |
| 1730 | // First find the symbol (preceded by a '(') |
| 1731 | char *pchSymStart = syms[itrace]; |
| 1732 | while (*pchSymStart != '(' && *pchSymStart != '\0') |
| 1733 | ++pchSymStart; |
| 1734 | if (*pchSymStart != '\0') |
| 1735 | ++pchSymStart; // skip the '(' |
| 1736 | char *pchSymEnd = pchSymStart; |
| 1737 | while (*pchSymEnd != '+' && *pchSymEnd != '\0') |
| 1738 | ++pchSymEnd; |
| 1739 | |
| 1740 | if ((pchSymEnd - pchSymStart) < 1023) |
| 1741 | { |
| 1742 | memcpy(symbuf, pchSymStart, pchSymEnd - pchSymStart); |
| 1743 | symbuf[pchSymEnd - pchSymStart] = '\0'; |
| 1744 | char *sz = abi::__cxa_demangle(symbuf, nullptr, nullptr, &status); |
| 1745 | if (status == 0) |
| 1746 | { |
| 1747 | safe_write(fd, syms[itrace], pchSymStart - syms[itrace]); |
| 1748 | safe_write(fd, sz, strlen(sz)); |
| 1749 | safe_write(fd, pchSymEnd, (syms[itrace] + strlen(syms[itrace])-pchSymEnd)); |
| 1750 | } |
| 1751 | else |
| 1752 | { |
| 1753 | safe_write(fd, syms[itrace], strlen(syms[itrace])); |
| 1754 | } |
| 1755 | free(sz); |
| 1756 | } |
| 1757 | else { |
| 1758 | safe_write(fd, syms[itrace], strlen(syms[itrace])); |
| 1759 | } |
| 1760 | safe_write(fd, "\n", 1); |
| 1761 | } |
| 1762 | free(syms); |
| 1763 | } |
| 1764 | |
| 1765 | /* Logs the stack trace using the backtrace() call. This function is designed |
| 1766 | * to be called from signal handlers safely. |
no test coverage detected