From: brobecke Date: Wed, 8 Jun 2011 16:56:11 +0000 (+0000) Subject: py-inferior.exp: Make sure local var is allocated on the stack. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=13cd44c7f306359ce4e9048aaed6ef4c2834efea;p=pf3gnuchains%2Fpf3gnuchains4x.git py-inferior.exp: Make sure local var is allocated on the stack. The testcase, at some point, is trying to change the contents of a string that was defined as follow: char *str = "hello, testsuite"; The problem is that the string is constant, and str is never used to change the contents of the string in the program, so the compiler is free to allocate it in a read-only section. This is what happens on x86-windows, for instance. As a result, trying to change the contents of the string during the `python gdb.inferiors()[0].write_memory (addr, str)' results in the following error: (gdb) python gdb.inferiors()[0].write_memory (addr, str) gdb: write target memory, 5 bytes at 0x00403064 Traceback (most recent call last): File "", line 1, in gdb.MemoryError: Cannot access memory at address 0x403064 Error while executing Python code. This patch prevents this from happening by declaring str as an array rather than a pointer. gdb/testsuite/ChangeLog: * gdb.python/py-inferior.c (f2): Make str an array rather than a pointer. * gdb.python/py-inferior.exp: Adjust testcase accordingly. --- diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 515f0eb434..b192a47e39 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2011-06-08 Joel Brobecker + + * gdb.python/py-inferior.c (f2): Make str an array rather + than a pointer. + * gdb.python/py-inferior.exp: Adjust testcase accordingly. + 2011-06-07 Jan Kratochvil * gdb.base/async-shell.c: New file. diff --git a/gdb/testsuite/gdb.python/py-inferior.c b/gdb/testsuite/gdb.python/py-inferior.c index dd83ffc476..04ec476926 100644 --- a/gdb/testsuite/gdb.python/py-inferior.c +++ b/gdb/testsuite/gdb.python/py-inferior.c @@ -17,7 +17,11 @@ static int search_buf_size; int f2 (int a) { - char *str = "hello, testsuite"; + /* We use a `char[]' type below rather than the typical `char *' + to make sure that `str' gets allocated on the stack. Otherwise, + the compiler may place the "hello, testsuite" string inside + a read-only section, preventing us from over-writing it from GDB. */ + char str[] = "hello, testsuite"; puts (str); /* Break here. */ diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 42ca920a82..b853c791b4 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -75,7 +75,7 @@ gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5)" gdb_py_test_silent_cmd "python str\[1\] = 'a'" "change str" 0 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \ "write str" 1 -gdb_test "print str" " = 0x\[\[:xdigit:\]\]+ \"hallo, testsuite\"" \ +gdb_test "print str" " = \"hallo, testsuite\"" \ "ensure str was changed in the inferior" # Test memory search.