LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to debug macros under gdb (https://www.linuxquestions.org/questions/programming-9/how-to-debug-macros-under-gdb-814020/)

dayalan_cse 06-14-2010 04:38 AM

how to debug macros under gdb
 
Hello All,

It seems GDB can not expand or show the macro expansion or its definition under the GDB debug to while debugging the program.

Does anyone has any information on how to debug macros under GDB? Please let me know.


Sample Code:

#include <stdio.h>
#include "sample.h"
#define M 42
#define ADD(x) (M + x)

main ()
{
#define N 28
printf ("Hello, world!\n");
#undef N
printf ("We're so creative.\n");
#define N 1729
printf ("Goodbye, world!\n");
}
Compilation:
gcc -gdwarf-2 -g3 sample.c -o sample
GDB message for the command: " info macro ADD "

The symbol `ADD' has no definition as a C/C++ preprocessor macro
at sample.c:5
included at /usr/include/stdio.h:749

TheIndependentAquarius 06-14-2010 08:05 AM

Have a look here: http://www.delorie.com/gnu/docs/gdb/gdb_70.html

dayalan_cse 06-14-2010 11:39 AM

Quote:

Originally Posted by anishakaul (Post 4003054)

Hi Anishakaul,

Thank you for your information.

I tried the below:

gdb> info macro ADD

The symbol `ADD' has no definition as a C/C++ preprocessor macro
at sample.c:5
included at /usr/include/stdio.h:749

I could not "display" or "expand" the macro definition under GDB.

johnsfine 06-14-2010 12:32 PM

Quote:

Originally Posted by dayalan_cse (Post 4003261)
Thank you for your information.
...
I could not "display" or "expand" the macro definition under GDB.

Did you follow from that link to the page it links which tells you the required change to your compile command needed to make any of that work?

http://www.delorie.com/gnu/docs/gdb/gdb_17.html

Especially the phrase there:
Quote:

Version 3.1 of GCC, the GNU C compiler, provides macro information if you specify the options `-gdwarf-2' and `-g3'
I haven't tried any of that myself, so I would guess from that documentation that GCC versions earlier than 3.1 lack the feature and GCC versions later than 3.1 probably still support that feature.

Assuming you compiled without those extra switches, GDB obviously can't use information that GCC didn't make available to it.

dayalan_cse 06-14-2010 10:36 PM

how to debug macros under gdb
 
Quote:

Originally Posted by johnsfine (Post 4003317)
Did you follow from that link to the page it links which tells you the required change to your compile command needed to make any of that work?

http://www.delorie.com/gnu/docs/gdb/gdb_17.html

Especially the phrase there:


I haven't tried any of that myself, so I would guess from that documentation that GCC versions earlier than 3.1 lack the feature and GCC versions later than 3.1 probably still support that feature.

Assuming you compiled without those extra switches, GDB obviously can't use information that GCC didn't make available to it.

Hi Johnsfine,

Thanks for the information.

My compilation switches:

gcc -gdwarf-2 -g3 sample.c -o sample

My gcc compiler version: 3.4.6

GDB still does not show the information about the macro while i was debugging.

ntubski 06-17-2010 02:06 PM

Did you call the list command first? The page says
Quote:

GDB uses the current listing position to decide which macro definitions are in scope:
I missed that the first time I skimmed through, it seems like surprising behaviour.
Code:

~/tmp/macro$ gdb -q sample
Reading symbols from /home/npostavs/tmp/macro/sample...done.
(gdb) info macro ADD
The symbol `ADD' has no definition as a C/C++ preprocessor macro
at <user-defined>:-1
(gdb) list main
3       
4        #define M 42
5        #define ADD(x) (M + x)
6       
7        main ()
8        {
9        #define N 28
10          printf ("Hello, world!\n");
11        #undef N
12          printf ("We're so creative.\n");
(gdb) info macro ADD
Defined at /home/npostavs/tmp/macro/sample.c:5
#define ADD(x) (M + x)
(gdb)


dayalan_cse 06-29-2010 06:24 AM

how to debug macros under gdb
 
Quote:

Originally Posted by ntubski (Post 4006803)
Did you call the list command first? The page says

I missed that the first time I skimmed through, it seems like surprising behaviour.
Code:

~/tmp/macro$ gdb -q sample
Reading symbols from /home/npostavs/tmp/macro/sample...done.
(gdb) info macro ADD
The symbol `ADD' has no definition as a C/C++ preprocessor macro
at <user-defined>:-1
(gdb) list main
3       
4        #define M 42
5        #define ADD(x) (M + x)
6       
7        main ()
8        {
9        #define N 28
10          printf ("Hello, world!\n");
11        #undef N
12          printf ("We're so creative.\n");
(gdb) info macro ADD
Defined at /home/npostavs/tmp/macro/sample.c:5
#define ADD(x) (M + x)
(gdb)



Hi,

Thanks for the information.

I still dont see macro definition under GDB after listing main.

GDB version used: 6.3.0.0-1.153

What command line options you have used to build your sample.c?
What GDB version you are using?

Thanks,
Deenadayalan

ntubski 06-29-2010 09:24 AM

GNU gdb (GDB) 7.0.1-debian
gcc (Debian 4.4.4-5) 4.4.4

Code:

gcc -g3 -o sample sample.c
also works with
gcc -gdwarf-2 -g3 -o sample sample.c



All times are GMT -5. The time now is 12:29 AM.