2011-11-10

Debug utility function caveats

What's wrong with the following code assuming there is no danger of null dereference?

int debug;
module_param(debug, int, 0644);

...

void dump_xyz_info(struct xyz *obj)
{
    if (!(debug & DEBUG_INFO))
        return;

    dev_info(mydev, "[a=%d b=%d c=%d]\n", obj->a, obj->b, obj->c);
}
While I though it was a good idea to incorporate the debug flag check in my debug utility method, I learned later that it prevents me from using this method for debugging, and having to resort to ugly tricks like this:

void dump_all_info(struct the_obj *obj)
{
    int old_debug = debug;
    debug = DEBUG_INFO;
    dump_xyz_info(obj->xyz);
    dump_pqr_info(obj->pqr);
    debug = old_debug;
}
OK, so I did not actually do this and fixed the issue at the first place. But how many times have we seen ugly tricks in the code like the above? Now I cherish expressions like:

if (debug & DEBUG_INFO)
    dump_xyz_info(obj->xyz);
Cheers!

No comments:

Post a Comment