Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 May 2007 12:02:17 +0200
From:      Pietro Cerutti <gahr@gahr.ch>
To:        freebsd-hackers@freebsd.org
Subject:   GDB and memory violations
Message-ID:  <464AD6A9.8060805@gahr.ch>

next in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig2553F160283B5B63818C8D92
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi list,
I haven't investigated yet, but it seems that FreeBSD's version of GDB
stores string literals in read/write memory, and not on read-only
memory, as I prove below.

Is this behavior known/wanted/erroneous ?

Try to run this code from the command line and from inside GDB.
strtok(3) segfaults when run on the command line, because it tries to
modify the string literal input1, while the program exits normally from G=
DB.

#include <string.h>
#include <stdio.h>

int main()
{
   char *input1 =3D "Hello, World!";

   char *tok;

   tok =3D strtok(input1, " ");
   if(tok) printf("%s\n", tok);

   tok =3D strtok(NULL, " ");
   if(tok) printf("%s\n", tok);

   return(0);

}

$ gcc -ggdb -Wall -o strtok strtok.c
$ ./strtok
Bus error (core dumped)
Exit 138

$ gdb ./strtok
GNU gdb 6.1.1 [FreeBSD]
[snip]GDB copyright and bla bla[/snip]
(gdb) break main
Breakpoint 1 at 0x8048570: file strtok.c, line 6.
(gdb) run
Starting program: /home/piter/strtok

Breakpoint 1, main () at strtok.c:6
6          char *input1 =3D "Hello, World!";
(gdb) next
10         tok =3D strtok(input1, " ");
(gdb)
11         if(tok) printf("%s\n", tok);
(gdb)
Hello,
13         tok =3D strtok(NULL, " ");
(gdb)
14         if(tok) printf("%s\n", tok);
(gdb)
World!
16         return(0);
(gdb)
18      }
(gdb)
0x08048485 in _start ()
(gdb)
Single stepping until exit from function _start,
which has no line number information.

Program exited normally.
(gdb)

Tnx,

--=20
Pietro Cerutti

PGP Public Key ID:
http://gahr.ch/pgp


--------------enig2553F160283B5B63818C8D92
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (FreeBSD)

iD8DBQFGStavwMJqmJVx944RCtXFAKCO1qd57c7BODIqbdXcIOOubBYwqACeNHWD
3RoWfiT6MII9UvqjoYUlOo4=
=5Bw8
-----END PGP SIGNATURE-----

--------------enig2553F160283B5B63818C8D92--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?464AD6A9.8060805>