Watchpoint on an unloaded shared library(1)

Emi SUZUKI
2008-11-20T12:08:02Z

Hello members,

I've now faced three issues related to a watchpoint on an unloaded
shared library: a segfault causes on GDB when referring to a
watchpoint which is invalid at that time. Now I will report them
separately.

To begin with, I will provide a sample to reproduce all the issues:

-----------------------------
dl-main.c:

#include <dlfcn.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>

static void (*sample) (void);

int
main (void)
{
void *handle;

if ((handle = dlopen("./libsample.so", RTLD_LAZY)) == NULL)
errx(2, "dlopen(): %s", dlerror());

if ((sample = dlsym(handle, "sample")) == NULL)
errx(2, "dlsym(): %s", dlerror());

sample ();

if (dlclose(handle) < 0)
errx(2, "dlclose(): %s", dlerror());

return 0;
}

-----------------------------
sample.c:

#include <stdio.h>

int sample_glob = 1;

void
sample (void)
{
puts ("sample of shared library");
++sample_glob;
}

-----------------------------
Build:

$ gcc -c -g -Wall sample.c
$ gcc -o libsample.so -shared sample.o
$ gcc -c -g -Wall dl-test.c
$ gcc -o dl-test dl-test.o -ldl

-----------------------------

And the first issue:

-----------------------------
$ gdb ./dl-test
GNU gdb (GDB) 6.8.50.20081114-cvs
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) start
Temporary breakpoint 1 at 0x80484e5: file dl-test.c, line 13.
Starting program: /home/suzuki/test/dl-test

Temporary breakpoint 1, main () at dl-test.c:14
13 if ((handle = dlopen("./libsample.so", RTLD_LAZY)) == NULL)
(gdb) next
16 if ((sample = dlsym(handle, "sample")) == NULL)
(gdb) watch sample_glob
Hardware watchpoint 2: sample_glob
(gdb) continue
Continuing.
sample of shared library
Hardware watchpoint 2: sample_glob

Old value = 1
New value = 2
sample () at sample.c:10
10 }
(gdb) disable 2
(gdb) c
Continuing.

Program exited normally.
(gdb) start
Temporary breakpoint 3 at 0x80484e5: file dl-test.c, line 13.
Starting program: /homer/suzuki/test/dl-test
Error in re-setting breakpoint 2: No symbol "sample_glob" in current context.
Error in re-setting breakpoint 2: No symbol "sample_glob" in current context.
Error in re-setting breakpoint 2: No symbol "sample_glob" in current context.
Error in re-setting breakpoint 2: No symbol "sample_glob" in current context.

Temporary breakpoint 3, main () at dl-test.c:13
13 if ((handle = dlopen("./libsample.so", RTLD_LAZY)) == NULL)
(gdb) enable 2
sample of shared library
Segmentation fault

$
-----------------------------

The cause is rather simple: the pointer to struct expression in struct
breakpoint (`bpt->exp') is set to NULL in breakpoint_re_set, when the
program was restarted and the shared library in which the watchpoint
expression is valid has not been loaded yet. However,
do_enable_breakpoint does not care about it.

The patch below addresses to the issue. Is that OK?

2008-11-20 Emi Suzuki <emi-suzuki@tjsys.co.jp>

* breakpoint.c (do_enable_breakpoint): Inform the user and
return from the function if the expression of a watchpoint is
invalid and cannot be updated.

diff src/gdb/breakpoint.c.orig src/gdb/breakpoint.c
--- src/gdb/breakpoint.c.orig 2008-11-20 18:52:13.000000000 +0900
+++ src/gdb/breakpoint.c 2008-11-20 18:52:56.000000000 +0900
@@ -7756,6 +7756,18 @@ is valid is not currently in scope.\n"),
}
select_frame (fr);
}
+
+ if (bpt->exp == NULL)
+ {
+ char *s = bpt->exp_string;
+ if (!gdb_parse_exp_1 (&s, bpt->exp_valid_block, 0, &bpt->exp))
+ {
+ printf_filtered (_("\
+Cannot enable watchpoint %d because the block in which its expression\n\
+is valid is not exist.\n"), bpt->number);
+ return;
+ }
+ }

if (bpt->val)
value_free (bpt->val);

My best regards,