Monday 9 December 2013

Debugging macros


Revisiting C was another by product of working on boa.
It was not that there were complex constructs which we had difficulty comprehending but it was while implementing simple ones did we realize once again the beauty of C.

Especially during playing along with macros!

Macros being really simple in their definition and working, are at the same time most difficult to debug. That's what we felt, when we overlooked the simple macros when we got few errors.
Not compilation ones, they are the simplest to fix!

Upon adding our code and attempting to run boa for the second time, it ran and accepted requests.
Not a bad thing at all or on second thoughts, was it? This was better than the previous time where it would just exit upon attempting to connect.

Enter GDB.
THE most helpful tool. It works wonders. No need of adding extra printf statements everywhere just to check where exactly the program goes wrong.

And guess what?! The point to be pinpointed was, in a macro. A very simple and silly mistake. Perhaps it required a fresher mind to figure out.
Macros perform simple code substitution.
That's what we did. But a simple mapping doesn't always work out.

Simple mapping from the return value of a function call to a value to be set inside a macro.

#define EPOLL_ISSET(args..) {\
int i=0;\
for(i=0; i<count; ++i) \
  if(condition) {\
     return_val = 1; \
     break; \
} \
return_val = 0;\   //always sets return val to 0
}


#define EPOLL_ISSET(args..) {\
int i=0;\
return_val = 0;\   //SHOULD BE HERE
for(i=0; i<count; ++i) \
  if(condition) {\
     return_val = 1; \
     break; \
} \
}

This is what happens when mindless mapping is done. This was between a return statement returning a value from the function and the setting of a variable inside a macro!
The first thought was to write a function for the same. But it was avoided considering the manner in which boa is structured as such. An overhead avoided!

Time saved is time gained.

GDB With macros


No comments:

Post a Comment