From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 76336 invoked by alias); 27 Mar 2019 23:47:44 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 76328 invoked by uid 89); 27 Mar 2019 23:47:44 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,KAM_SHORT,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy= X-HELO: mail-wm1-f52.google.com Received: from mail-wm1-f52.google.com (HELO mail-wm1-f52.google.com) (209.85.128.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 27 Mar 2019 23:47:34 +0000 Received: by mail-wm1-f52.google.com with SMTP id z11so1729041wmi.0 for ; Wed, 27 Mar 2019 16:47:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk-org.20150623.gappssmtp.com; s=20150623; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=hSjKh7sYk0ZMQ0WU5TyXKOxPRMOrCdCuhKke34vJLfQ=; b=P10RsLD4qKyN/MgBiKNlf/Nz+7wdfwyAeUKwh/WfncM+Oo+ukLKXSZYAdpEYyP6r2f w3jLhG3QCEasqjgjZLSTtzTHvtCBOA3Kcj+qFYvZroUtfZ1DD93i4KNDWe5brRfvF1Yi 2kmw6y6XL+5QxIP9nztzuyvAxdhXhqi08eXlsuwq+c7u2HzgAh1NkNZ7sbQ/bh3JosUk B2rqtZOy5Nqg9yfgEJLskB9Oh1Z7qAhucY91251UbwXFJiqaZMDt/MuEJaTe60uRxaFU RwL7UuAmudiPBlJNJA0K4b8Pa6r1aSXobfTYLWDzpcLMbrsZWgNKdGx86VIJM7saz21S q5QQ== Return-Path: Received: from [192.168.1.71] (251.98.189.80.dyn.plus.net. [80.189.98.251]) by smtp.gmail.com with ESMTPSA id n13sm26483834wrw.67.2019.03.27.16.47.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Mar 2019 16:47:29 -0700 (PDT) From: Jonny Grant Subject: Re: Recursive SIGSEGV question To: Jonathan Wakely Cc: Florian Weimer , Andrew Haley , Xi Ruoyao , gcc-help References: <1255ee27-882f-ab4e-ea45-ba6f35791b45@jguk.org> <877ecuikq9.fsf@mid.deneb.enyo.de> <835d09ce-752a-c0f7-e5cf-210e855df2ab@jguk.org> <87ef6vkq8a.fsf@mid.deneb.enyo.de> <95ff2a72-47fb-5cc3-5852-08517e3ce76e@redhat.com> <87bm1yho61.fsf@mid.deneb.enyo.de> Message-ID: <490f5b68-a9a9-943e-6485-28c0fd51835d@jguk.org> Date: Wed, 27 Mar 2019 23:51:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------8178D7B2F791DFC2D7837C90" X-SW-Source: 2019-03/txt/msg00198.txt.bz2 This is a multi-part message in MIME format. --------------8178D7B2F791DFC2D7837C90 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 2747 On 27/03/2019 21:34, Jonathan Wakely wrote: > On Wed, 27 Mar 2019 at 21:27, Jonny Grant wrote: >> Hi! >> >> Thank you for your reply and input. >> >> Maybe GCC's "libbacktrace" module could be used? >> >> I was wondering if -fsanitize=address would output a backtrace for the >> C++ exception, but it doesn't seem to. Also it actually prevents the >> core being dumped - that's probably not intended? >> >> Compile without Sanitizer, and it does dump the core to a file at least! >> >> $ export UBSAN_OPTIONS=print_stacktrace=1 > > This is a UBsan option. > >> // g++-8 -fsanitize=address -Wall -o exception exception.cpp > > But you're not using UBsan. > >> #include >> int main() >> { >> std::vector v; >> return v.at(0); >> } >> >> >> $ ./exception >> terminate called after throwing an instance of 'std::out_of_range' >> what(): vector::_M_range_check: __n (which is 0) >= this->size() >> (which is 0) >> Aborted > > There's no undefined behaviour or memory corruption here, so it's not > surprising that UBsan and Asan don't print anything. Ok I see, thank you for pointing this out. I did wonder, as -fsanitize=address seems to inhibit the core dump that is otherwise created by the abort() that appears to be called - is that a known issue? $ g++-8 -Wall -o exception exception.cpp jonny@asus:~/code/crash$ ./exception terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0) Aborted (core dumped) $ Usually I just load the core dump into GDB to take a look at it. > If you want a stack trace for exceptions that terminate the process > then you could install a custom terminate handler which does that. > Libstdc++'s default terminate handler just prints the message above, > which includes the type of the exception and if it's a type derived > from std::exception, the what() string stored in it. Yes, I'd love to have a stack trace for exceptions that terminate the process. Do you know a simple example you can refer me to? I've looked and there are people using boost::stacktrace::stacktrace() but I'd rather not link to boost as a dependency. It would be great if there was a glibc option to do this, or GCC could insert it. Otherwise we each need to insert our own stack tracers... Found this: https://www.gnu.org/software/libc/manual/html_node/Backtraces.html I added this (attached) to a C++ exception handler, but there's no file and line numbers. Other examples resort to calling addr2line! Seems a bit over the top for us each to code our own stack tracer... Or reading symbols etc. Am I asking too much for a general print_backtrace() in libc or elsewhere ? Cheers, Jonny --------------8178D7B2F791DFC2D7837C90 Content-Type: text/x-c++src; name="exception.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="exception.cpp" Content-length: 897 // g++-8 -Wall -g -o exception exception.cpp #include #include int main2(); #include #include #include /* Obtain a backtrace and print it to stdout. https://www.gnu.org/software/libc/manual/html_node/Backtraces.html */ void print_trace (void) { void *array[10]; size_t size; char **strings; size_t i; size = backtrace (array, 10); strings = backtrace_symbols (array, size); printf ("Obtained %zd stack frames.\n", size); for (i = 0; i < size; i++) printf ("symbols %s\n", strings[i]); free (strings); } int main() { try { main2(); } catch( const std::exception &e) { const std::string what(e.what()); std::cout << "Unhandled exception: [" << what << "]\n"; print_trace(); exit(0); } } int main2() { std::vector v; return v.at(0); } --------------8178D7B2F791DFC2D7837C90--