473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to debug macro?

Hi,

It is benifitical to use macro in certain cases.

http://www.boost.org/doc/libs/1_35_0...doc/index.html

However, I found that it is not easy to debug a macro. For example,
for the following program, I can not trace into the last macro in gdb.

In this sense, if I want to debug the code easily, should I avoid
using macros. Are there any better way to debug macros?

Thanks,
Peng

$ cat main.cc
#include <boost/typeof/typeof.hpp>
#include <iostream>

#define MACRO_DEF \
class A { \
public: \
A(int a) : _a(a) { } \
int the_a() const { return _a; } \
private: \
int _a; \
};

MACRO_DEF

#define MACRO \
A a(1);\
std::cout << a.the_a() << std::endl;

int main() {
MACRO
}
$gdb main-g.exe
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "x86_64-linux-gnu"...Using host
libthread_db library "/lib/libthread_db.so.1".

(gdb) start
Breakpoint 1 at 0x40081c: file main.cc, line 20.
main () at main.cc:20
20 MACRO
(gdb) s
A (this=0x7fffb8f0de80, a=1) at main.cc:13
13 MACRO_DEF
(gdb) s
A::the_a (this=0x7fffb8f0de80) at main.cc:13
13 MACRO_DEF
(gdb) s
1
main () at main.cc:21
21 }
(gdb) n
0x00002afcf215b4ca in __libc_start_main () from /lib/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
(gdb)
Jun 29 '08 #1
5 10544
Peng Yu wrote:
Hi,

It is benifitical to use macro in certain cases.

http://www.boost.org/doc/libs/1_35_0...doc/index.html

However, I found that it is not easy to debug a macro.
Which is why they are best avoided. With the exception of passing file
name and line number to debug logs, I don't think I've used a macro in
C++ in the past decade.

--
Ian Collins.
Jun 29 '08 #2
Peng Yu wrote:
Hi,

It is benifitical to use macro in certain cases.

http://www.boost.org/doc/libs/1_35_0...doc/index.html

However, I found that it is not easy to debug a macro. For example,
for the following program, I can not trace into the last macro in gdb.

In this sense, if I want to debug the code easily, should I avoid
using macros. Are there any better way to debug macros?
Look at the output of the preprocessor. I think most compilers allow you to
do that. This way, you can see what the macro expands to.
Best

Kai-Uwe Bux
Jun 29 '08 #3
It is benifitical to use macro in certain cases.

Use macros for...

- conditional compilation
- token pasting
- stringerization

but, naturally, try to avoid _those_ things unless the alternatives are
worse.
Jun 29 '08 #4
On Jun 28, 9:23*pm, Peng Yu <PengYu...@gmail.comwrote:
It is benifitical to use macro in certain cases.
Perhaps, but the example below certainly isn't one of them.
>
#define MACRO_DEF \
class A { \
public: \
A(int a) : _a(a) { } \
int the_a() const { return _a; } \
private: \
int _a; \
};

MACRO_DEF

#define MACRO \
A a(1);\
std::cout << a.the_a() << std::endl;
However, I found that it is not easy to debug a macro. For example,
for the following program, I can not trace into the last macro in gdb.
If you (who wrote the macro) have trouble debugging it, imagine the
difficulties that the maintenance programmer will face - simply to
understand what the macro is supposed to do. After all, what would you
think if you had to debug a C++ program whose main() function looked
like the main() in your program:
int main() {
* MACRO

}
Having to deal with code like this, in my experience, would be enough
to convince most programmers that they really should be working -
somewhere else.

Greg

Jun 30 '08 #5
On Jun 30, 12:51 am, Greg Herlihy <gre...@mac.comwrote:
On Jun 28, 9:23 pm, Peng Yu <PengYu...@gmail.comwrote:
It is benifitical to use macro in certain cases.

Perhaps, but the example below certainly isn't one of them.


#define MACRO_DEF \
class A { \
public: \
A(int a) : _a(a) { } \
int the_a() const { return _a; } \
private: \
int _a; \
};
MACRO_DEF
#define MACRO \
A a(1);\
std::cout << a.the_a() << std::endl;
However, I found that it is not easy to debug a macro. For example,
for the following program, I can not trace into the last macro in gdb.

If you (who wrote the macro) have trouble debugging it, imagine the
difficulties that the maintenance programmer will face - simply to
understand what the macro is supposed to do. After all, what would you
think if you had to debug a C++ program whose main() function looked
like the main() in your program:
int main() {
MACRO
}

Having to deal with code like this, in my experience, would be enough
to convince most programmers that they really should be working -
somewhere else.
Hi Greg,

I guest you misunderstood my OP. I have never said that my example is
the case that I should use macro.

I just raised that example to show the difficulty of debugging it. It
is not at all similar to the real code that I'm pondering whether I
should use macro or not.

I originally considered macro because there are somewhat redundancies
(not exactly the same) in that code that can not be refactored in with
extract method, etc. However, as macro is hard to debug, I end up with
writing a code generator to generated the somewhat redundant code.

Thanks,
Peng
Jun 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mac | last post by:
Is there a way to mimic the behaviour of C/C++'s preprocessor for macros? The problem: a lot of code like this: def foo(): # .... do some stuff if debug: emit_dbg_obj(DbgObjFoo(a,b,c)) #...
2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
3
by: Michael | last post by:
OK Guys, lets say that I've got some code that I want from time when I'm debuging but want to be able to turn off quickly. At the moment I'm doing: #define DEBUG_TEXTURE #define DEBUG_BSP...
3
by: Thomas Lorenz | last post by:
Hello NG, I'm working on a larger C++ project with it's own debug functionality. The typical debug call looks like this: DEBUG(WARNING, "An error " << getErrorCategory() << " occured."); ...
6
by: Roman Mashak | last post by:
Hello, All! I'm trying to set up macro: .... int main(int argc, char *argv, char *envp) { .... if ( argc > 1 ) { if ( strcmp(argv, "-d") == 0 ) {
6
by: pinkfloydhomer | last post by:
I want to have debugging macro or function that I can use like: DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4); just as with printf. I want to avoid implementing this functionality...
6
by: pauldepstein | last post by:
To help me debug, I am writing a lot of information into a stream which I call debug. However, because of the large amount of time taken to print this information, I only want this printed while...
4
by: herc | last post by:
For some reason when I use Debug.WriteLine(), it does not send anything to the output window any more. I have noticed this in C#, VB.Net, and in Unmanaged C++. Any thoughts?
9
by: vivek | last post by:
Hi i have used some debug macro and called several times(hundreds) in the code. The purpose of the macro was to print the values at that time to the screen Now that i have finished...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.