From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 112508 invoked by alias); 17 Mar 2015 11:06:08 -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 112498 invoked by uid 89); 17 Mar 2015 11:06:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=2.3 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,KAM_FROM_URIBL_PCCC,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=no version=3.3.2 X-HELO: mail-pa0-f53.google.com Received: from mail-pa0-f53.google.com (HELO mail-pa0-f53.google.com) (209.85.220.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Tue, 17 Mar 2015 11:06:05 +0000 Received: by padcy3 with SMTP id cy3so6746741pad.3 for ; Tue, 17 Mar 2015 04:06:03 -0700 (PDT) X-Received: by 10.70.123.131 with SMTP id ma3mr121791547pdb.16.1426590363557; Tue, 17 Mar 2015 04:06:03 -0700 (PDT) Received: from E107787-LIN (gcc1-power7.osuosl.org. [140.211.15.137]) by mx.google.com with ESMTPSA id r8sm21857847pdp.10.2015.03.17.04.06.01 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 17 Mar 2015 04:06:02 -0700 (PDT) From: Yao Qi To: Zhang Zhen Cc: , Subject: Re: The return address of strtok is out of bounds in gdb References: <5507E977.2030003@huawei.com> Date: Tue, 17 Mar 2015 11:06:00 -0000 In-Reply-To: <5507E977.2030003@huawei.com> (Zhang Zhen's message of "Tue, 17 Mar 2015 16:44:39 +0800") Message-ID: <86k2yfg9yi.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2015-03/txt/msg00059.txt.bz2 Zhang Zhen writes: > I found a problem with gdb-7.9 on my x86_64 machine. > The return address is out of bounds by calling call strtok in gdb. > But if we enter 'n', the return address is correct. > I want to know this is a bug ? If so, how to resolve it ? It is not a bug, IMO. > > It is easily reproduced as follows: > > Fs-Server:/opt/zhangzhen/gdb-7.9 # ./gdb/gdb -q ../strtok_test > Reading symbols from ../strtok_test...done. > (gdb) b 12 > Breakpoint 1 at 0x4005c7: file strtok_test.c, line 12. > (gdb) r > Starting program: /opt/zhangzhen/strtok_test > > Breakpoint 1, main (argc=3D1, argv=3D0x7fffffffe358) at strtok_test.c:12 > 12 p1 =3D strtok(a0, se); > (gdb) p p1 > $1 =3D 0x0 > (gdb) p p1 =3D strtok(a0, se) > $2 =3D 0xffffffffffffe260 You are doing an "inferior call" https://sourceware.org/gdb/onlinedocs/gdb/Calling.html here. In order to support inferior call, GDB needs to create a new frame, get the function's signature (return value and arguments), prepare the arguments in the right place (registers or stack) as well as return address, and resume the programme, wait for the function call finished. In your case, I suspect GDB prepares the incorrect arguments for function strtok due to lack of debugging information, so you'll see the error. You can get your libc debug info installed, or wrap up strktok like this in your program, char * my_strtok(char *str, const char *delim) { return strtok (str, delim); } and in gdb, (gdb) p p1 =3D my_strtok(a0, se) --=20 Yao (=E9=BD=90=E5=B0=A7)