GDB and memory violations

Pietro Cerutti gahr at gahr.ch
Wed May 16 10:35:27 UTC 2007


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 GDB.

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

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

   char *tok;

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

   tok = 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 = "Hello, World!";
(gdb) next
10         tok = strtok(input1, " ");
(gdb)
11         if(tok) printf("%s\n", tok);
(gdb)
Hello,
13         tok = 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,

-- 
Pietro Cerutti

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20070516/13ae19a6/signature.pgp


More information about the freebsd-hackers mailing list