1 /* Tests that Valgrind coredump support works correctly by producing
2 a core dump analyzable by mdb. */
3
4 #include <stdio.h>
5 #include <sys/types.h>
6
7 __attribute__((noinline))
inner(void)8 static void inner(void)
9 {
10 uint16_t cs, ds, ss, es, fs, gs;
11
12 /* Print segment registers as they cannot be set to arbitrary
13 values. */
14 __asm__ __volatile__(
15 "movw %%cs, %0\n"
16 "movw %%ds, %1\n"
17 "movw %%ss, %2\n"
18 "movw %%es, %3\n"
19 "movw %%fs, %4\n"
20 "movw %%gs, %5\n"
21 : "=m" (cs), "=m" (ds), "=m" (ss), "=m" (es), "=m" (fs), "=m" (gs));
22 printf("cs=%#x ds=%#x ss=%#x es=%#x fs=%#x gs=%#x\n",
23 cs, ds, ss, es, fs, gs);
24
25 /* Set other registers to apriori known values. */
26 __asm__ __volatile__(
27 "movl $0x101, %%eax\n"
28 "movl $0x102, %%ebx\n"
29 "movl $0x103, %%ecx\n"
30 "movl $0x104, %%edx\n"
31 "movl $0x105, %%esi\n"
32 "movl $0x106, %%edi\n"
33 // not %ebp as mdb is then not able to reconstruct stack trace
34 "movl $0x108, %%esp\n"
35 "movl $0x1234, (%%eax)\n" // should cause SEGV here
36 "ud2" // should never get here
37 : // no output registers
38 : // no input registers
39 : "memory", "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%esp");
40 }
41
42 __attribute__((noinline))
outer(void)43 static void outer(void)
44 {
45 inner();
46 }
47
main(int argc,const char * argv[])48 int main(int argc, const char *argv[])
49 {
50 outer();
51 return 0;
52 }
53