{"id":6701,"date":"2022-12-20T19:33:27","date_gmt":"2022-12-20T22:33:27","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/backtrace-man3\/"},"modified":"2022-12-20T19:33:27","modified_gmt":"2022-12-20T22:33:27","slug":"backtrace-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/backtrace-man3\/","title":{"rendered":"BACKTRACE (man3)"},"content":{"rendered":"<h1 align=\"center\">BACKTRACE<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">backtrace, backtrace_symbols, backtrace_symbols_fd \u2212 support for application self-debugging<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <execinfo.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>int backtrace(void **<\/b><i>buffer<\/i><b>, int<\/b> <i>size<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>char **backtrace_symbols(void *const *<\/b><i>buffer<\/i><b>, int<\/b> <i>size<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>void backtrace_symbols_fd(void *const *<\/b><i>buffer<\/i><b>, int<\/b> <i>size<\/i><b>, int<\/b> <i>fd<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>backtrace<\/b>() returns a backtrace for the calling program, in the array pointed to by <i>buffer<\/i>. A backtrace is the series of currently active function calls for the program. Each item in the array pointed to by <i>buffer<\/i> is of type <i>void\u00a0*<\/i>, and is the return address from the corresponding stack frame. The <i>size<\/i> argument specifies the maximum number of addresses that can be stored in <i>buffer<\/i>. If the backtrace is larger than <i>size<\/i>, then the addresses corresponding to the <i>size<\/i> most recent function calls are returned; to obtain the complete backtrace, make sure that <i>buffer<\/i> and <i>size<\/i> are large enough.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Given the set of addresses returned by <b>backtrace<\/b>() in <i>buffer<\/i>, <b>backtrace_symbols<\/b>() translates the addresses into an array of strings that describe the addresses symbolically. The <i>size<\/i> argument specifies the number of addresses in <i>buffer<\/i>. The symbolic representation of each address consists of the function name (if this can be determined), a hexadecimal offset into the function, and the actual return address (in hexadecimal). The address of the array of string pointers is returned as the function result of <b>backtrace_symbols<\/b>(). This array is <b>malloc<\/b>(3)ed by <b>backtrace_symbols<\/b>(), and must be freed by the caller. (The strings pointed to by the array of pointers need not and should not be freed.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>backtrace_symbols_fd<\/b>() takes the same <i>buffer<\/i> and <i>size<\/i> arguments as <b>backtrace_symbols<\/b>(), but instead of returning an array of strings to the caller, it writes the strings, one per line, to the file descriptor <i>fd<\/i>. <b>backtrace_symbols_fd<\/b>() does not call <b>malloc<\/b>(3), and so can be employed in situations where the latter function might fail, but see NOTES.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>backtrace<\/b>() returns the number of addresses returned in <i>buffer<\/i>, which is not greater than <i>size<\/i>. If the return value is less than <i>size<\/i>, then the full backtrace was stored; if it is equal to <i>size<\/i>, then it may have been truncated, in which case the addresses of the oldest stack frames are not returned.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">On success, <b>backtrace_symbols<\/b>() returns a pointer to the array <b>malloc<\/b>(3)ed by the call; on error, NULL is returned.<\/p>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>backtrace<\/b>(), <b>backtrace_symbols<\/b>(), and <b>backtrace_symbols_fd<\/b>() are provided in glibc since version 2.1.<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1579261.png\" alt=\"Image grohtml-1579261.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions are GNU extensions.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions make some assumptions about how a function\u2019s return address is stored on the stack. Note the following:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Omission of the frame pointers (as implied by any of <b>gcc<\/b>(1)\u2019s nonzero optimization levels) may cause these assumptions to be violated.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Inlined functions do not have stack frames.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Tail-call optimization causes one stack frame to replace another.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p><b>backtrace<\/b>() and <b>backtrace_symbols_fd<\/b>() don\u2019t call <b>malloc<\/b>() explicitly, but they are part of <i>libgcc<\/i>, which gets loaded dynamically when first used. Dynamic loading usually triggers a call to <b>malloc<\/b>(3). If you need certain calls to these two functions to not allocate memory (in signal handlers, for example), you need to make sure <i>libgcc<\/i> is loaded beforehand.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The symbol names may be unavailable without the use of special linker options. For systems using the GNU linker, it is necessary to use the <i>\u2212rdynamic<\/i> linker option. Note that names of &#8220;static&#8221; functions are not exposed, and won\u2019t be available in the backtrace.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The program below demonstrates the use of <b>backtrace<\/b>() and <b>backtrace_symbols<\/b>(). The following shell session shows what we might see when running the program:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>cc \u2212rdynamic prog.c \u2212o prog<\/b> <br \/> $ <b>.\/prog 3<\/b> <br \/> backtrace() returned 8 addresses <br \/> .\/prog(myfunc3+0x5c) [0x80487f0] <br \/> .\/prog [0x8048871] <br \/> .\/prog(myfunc+0x21) [0x8048894] <br \/> .\/prog(myfunc+0x1a) [0x804888d] <br \/> .\/prog(myfunc+0x1a) [0x804888d] <br \/> .\/prog(main+0x65) [0x80488fb] <br \/> \/lib\/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c] <br \/> .\/prog [0x8048711]<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source<\/b> <br \/> #include <execinfo.h> <br \/> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <unistd.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define BT_BUF_SIZE 100<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">void <br \/> myfunc3(void) <br \/> { <br \/> int nptrs; <br \/> void *buffer[BT_BUF_SIZE]; <br \/> char **strings;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">nptrs = backtrace(buffer, BT_BUF_SIZE); <br \/> printf(&#8220;backtrace() returned %d addressesn&#8221;, nptrs);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) <br \/> would produce similar output to the following: *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">strings = backtrace_symbols(buffer, nptrs); <br \/> if (strings == NULL) { <br \/> perror(&#8220;backtrace_symbols&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (int j = 0; j < nptrs; j++) <br \/> printf(&#8220;%sn&#8221;, strings[j]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">free(strings); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static void \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var &#8220;static&#8221; means don&#8217;t export the symbol&#8230; bodies\/ usr\/ <br \/> myfunc2(void) <br \/> { <br \/> myfunc3(); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">void <br \/> myfunc(int ncalls) <br \/> { <br \/> if (ncalls > 1) <br \/> myfunc(ncalls \u2212 1); <br \/> else <br \/> myfunc2(); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> if (argc != 2) { <br \/> fprintf(stderr, &#8220;%s num\u2212callsn&#8221;, argv[0]); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">myfunc(atoi(argv[1])); <br \/> exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>addr2line<\/b>(1), <b>gcc<\/b>(1), <b>gdb<\/b>(1), <b>ld<\/b>(1), <b>dlopen<\/b>(3), <b>malloc<\/b>(3)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  backtrace, backtrace_symbols, backtrace_symbols_fd \u2212 support for application self-debugging <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,3079,3007],"class_list":["post-6701","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-backtrace","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6701"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6701\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}