From owner-freebsd-hackers@freebsd.org Thu Jul 6 21:12:56 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B798AD920DD for ; Thu, 6 Jul 2017 21:12:56 +0000 (UTC) (envelope-from marcnarc@xiplink.com) Received: from smtp93.ord1c.emailsrvr.com (smtp93.ord1c.emailsrvr.com [108.166.43.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 9ADF27FD0D for ; Thu, 6 Jul 2017 21:12:56 +0000 (UTC) (envelope-from marcnarc@xiplink.com) Received: from smtp28.relay.ord1c.emailsrvr.com (localhost [127.0.0.1]) by smtp28.relay.ord1c.emailsrvr.com (SMTP Server) with ESMTP id D925940378; Thu, 6 Jul 2017 17:02:59 -0400 (EDT) X-Auth-ID: mbranchaud@xiplink.com Received: by smtp28.relay.ord1c.emailsrvr.com (Authenticated sender: mbranchaud-AT-xiplink.com) with ESMTPSA id B066F40321; Thu, 6 Jul 2017 17:02:59 -0400 (EDT) X-Sender-Id: mbranchaud@xiplink.com Received: from [10.10.1.32] ([UNAVAILABLE]. [192.252.130.194]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA) by 0.0.0.0:465 (trex/5.7.12); Thu, 06 Jul 2017 17:02:59 -0400 To: freebsd-hackers@freebsd.org From: Marc Branchaud Subject: libexecinfo backtrace() in a signal handler Message-ID: <4d662753-98bc-1275-9394-0cda95eedc65@xiplink.com> Date: Thu, 6 Jul 2017 17:02:59 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Thu, 06 Jul 2017 21:37:29 +0000 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Jul 2017 21:12:56 -0000 Howdy, (Please CC replies to me, as I am not subscribed. Apologies if I'm in the wrong forum!) I'm trying to use libexecinfo's backtrace() in a SIGSEGV handler. It only finds one single frame in the stack, for the signal handler function. (Outside of the signal handler backtrace() does return the full stack from the call point.) Is there any way to get the stack from where the SIGSEGV arose? A few hours of Googling has proved fruitless. My overall goal is to intercept core-dumping signals to try to save a textual backtrace instead of a full dump (as they can be quite large in my application). So I'd also like to catch SIGBUS, and maybe SIGFPE too. Details: FreeBSD 9 (yes, ouch, I know, but I see the same problem in 10) libexecinfo is copied from STABLE-10 amd64 gcc Here's a little test app. Even when compiled with -O0, backtrace() still only finds the frame for the handler() function. ----->8----->8----->8----->8------>8---->8----->8----->8----- #include #include #include #include #include void handler(int sig) { void *frames[10]; size_t size = backtrace(frames, sizeof(frames)); fprintf(stderr, "Signal %d:\n", sig); fprintf(stderr, "%zu FRAMES:\n", size); backtrace_symbols_fd(frames, size, STDERR_FILENO); exit(1); } void baz() { int *foo = (int*)-1; printf("%d\n", *foo); } void bar() { baz(); } void foo() { bar(); } int main(int argc, char **argv) { signal(SIGSEGV, handler); foo(); return 0; } ----->8----->8----->8----->8------>8---->8----->8----->8----- Thanks! M.