old-cross-binutils/gdb/testsuite/gdb.python/py-inferior.c
Joel Brobecker b5916bbd42 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 "<string>", line 1, in <module>
    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.
2011-06-08 16:56:11 +00:00

51 lines
1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define CHUNK_SIZE 16000 /* same as findcmd.c's */
#define BUF_SIZE (2 * CHUNK_SIZE) /* at least two chunks */
int8_t int8_search_buf[100];
int16_t int16_search_buf[100];
int32_t int32_search_buf[100];
int64_t int64_search_buf[100];
static char *search_buf;
static int search_buf_size;
int f2 (int a)
{
/* 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. */
return ++a;
}
int f1 (int a, int b)
{
return f2(a) + b;
}
static void
init_bufs ()
{
search_buf_size = BUF_SIZE;
search_buf = malloc (search_buf_size);
if (search_buf == NULL)
exit (1);
memset (search_buf, 'x', search_buf_size);
}
int main (int argc, char *argv[])
{
init_bufs ();
return f1 (1, 2);
}