From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 62929 invoked by alias); 23 Apr 2017 16:21:21 -0000 Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org Received: (qmail 62912 invoked by uid 89); 23 Apr 2017 16:21:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_SOFTFAIL autolearn=no version=3.3.2 spammy=HTo:U*richard, reaches, Hx-languages-length:2361, Best X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 23 Apr 2017 16:21:18 +0000 Received: by simark.ca (Postfix, from userid 33) id E4FC91E4A4; Sun, 23 Apr 2017 12:21:17 -0400 (EDT) To: Richard Szibele Subject: Re: GDB 7.12.1: Strange "stepping" behavior X-PHP-Originating-Script: 33:rcube.php MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Sun, 23 Apr 2017 16:21:00 -0000 From: Simon Marchi Cc: gdb@sourceware.org In-Reply-To: References: Message-ID: X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.2.4 X-IsSubscribed: yes X-SW-Source: 2017-04/txt/msg00032.txt.bz2 On 2017-04-22 19:06, Richard Szibele wrote: > Hello everyone, > > I am experiencing strange stepping behavior with GDB 7.12.1 and a > program compiled with g++ (GCC) 5.4.0 which I can demonstrate with a > simple example: > > > #include > #include > > int main() > { > auto ptr = std::shared_ptr(new int); > *ptr = 100; > std::cout << *ptr << std::endl; > return 0; > } > > > I've compiled the above with the following g++ flags: > > g++ -std=c++14 -g -O0 main.cpp > > and then run gdb on the resulting executable. > > When I step over using "next" I end up jumping back and forth, rather > than a simple linear top-down progression in the source code. I've > read that this is due to compiler optimizations, but as I've supplied > the flags -g and -O0, I do not believe this should happen. > > Is this a bug or am I doing something wrong? > > Best Regards, > Richard Szibele Hi Richard, You probably see this sequence: Temporary breakpoint 1, main () at test.cpp:6 6 auto ptr = std::shared_ptr(new int); (gdb) n 7 *ptr = 100; (gdb) n 8 std::cout << *ptr << std::endl; (gdb) n 100 9 return 0; (gdb) n 6 auto ptr = std::shared_ptr(new int); (gdb) n 10 } It's jumping back to the declaration of "ptr" just before exiting the scope of the main function. This can be surprising at first, but is perfectly normal given the implementation of next/step. The way step works is equivalent to this. The instruction you are stopped at currently belongs (was generated from) a particular source line. The step command executes instructions until it reaches an instruction that belongs to a different source line. next is the same except it doesn't go into function calls. The simple fact that there's a variable of type std::shared_ptr declared in your scope means that the compiler must generate some code to call the destructor of that variable. This code is after the "return 0", and was generated from the declaration of ptr. That's why after "return 0" it jumps to "auto ptr = ...". You can look at the instructions generated by the compiler using "objdump -S a.out". For reference, here's what I get: https://pastebin.com/raw/rYPzbbeQ If you were to debug optimized code (you should give it a try), you'd see that it jumps in a much more erratic and unexplainable way. Simon