Sorry if this question is poorly phrased, but it's a weird problem and I'm not sure entirely how to explain it. I wrote some stupid code involving macros that had a stupid error, but even though I solved the problem, gdb didn't help very much. I thought the problem might be in how I wrote the macros (it wasn't), so I used the -E flag so I could inspect the code and debug it without the macros. Then this happened:
/media/sf_Mint-Shared/C $ clang switch.c -E > switch_e.c
/media/sf_Mint-Shared/C $ clang switch_e.c -g -O0 -o switch
/media/sf_Mint-Shared/C $ gdb switch
[snip]
(gdb) run
Starting program: /media/sf_Mint-Shared/C/switch
Enter your favorite fruit: apple
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b6f2f2 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) l
6 #define SWITCH(t) strcpy(switch_str, t);
7 #define CASE(c, d) if (!strcmp(switch_str, (c))) {d} else
8 #define ELSE(d) {d}
9 #define ENDSWITCH() switch_str = NULL;
10
11 int main()
12 {
13 char fruit[20];
14
15 printf("Enter your favorite fruit: ");
(gdb) l 26
21 scanf("%s", fruit);
22 to_lower(fruit);
23
24 SWITCH(0, fruit)
25 CASE(0, "apple", puts("Apples are delicious!");)
26 CASE(0, "pear", puts("Pears are alright");)
27 CASE(0, "banana", puts("Ew, bananas are gross");)
28 ELSE(puts("Sorry, I don't know that fruit.");)
29 ENDSWITCH(0)
30
In case it isn't obvious, the macros are still present, despite the fact that I compiled the executable from the fully preprocessed source. Here's the (truncated) code that went into the processor:
// Includes truncated
static char *switch_str;
int main()
{
char fruit[20];
printf("Enter your favorite fruit: ");
scanf("%s", fruit);
strcpy(switch_str, fruit);
if (!strcmp(switch_str, ("apple"))) {puts("Apples are delicious!");} else
if (!strcmp(switch_str, ("pear"))) {puts("Pears are alright");} else
if (!strcmp(switch_str, ("banana"))) {puts("Ew, bananas are gross");} else
{puts("Sorry, I don't know that fruit.");}
switch_str = ((void *)0);
return 0;
}
How are the original macros persisting even after preprocessing and how can I debug with them expanded?
The gcc preprocessor inserts #line
directives to be able to correlate preprocessed lines to the original line. The gcc compiler uses these directives, both to produce error messages and for debugging information. Consequently, gdb is instructed to look at the original file for source information, even if you run the preprocessor manually.
You can use the -P
option to cpp in order to suppress #line
directives.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.