Header files with enums instead of defines?

Peter Edwards peadar.edwards at gmail.com
Wed Dec 22 05:00:51 PST 2004


> The Single Unix Specification goes to great pains to repeat over and
> over again that the error codes are 'symbolic constants', which IMHO
> may be taken to mean either a #define'd macro or an enum value.
> I, too, went to check with more than half a hunch that it would mandate
> that the error codes be macros, but it turned out it doesn't :)

But "errno" itself is "int", so even if the constants for the
individual errno values were defined by an enumeration, that type
information would be lost to the debugger when looking at errno
itself, defeating the original benefit of having the symbolic names
available in the debugger. As an alternative to Peter Jeremy's
suggestion of using a GDB macro, you could, of course, define a type
as:
typedef enum {
    err_EPERM = EPERM,
    err_ENOENT = ENOENT,
    /* .... */
} errno_t

Then within gdb:

Breakpoint 1, main (argc=1, argv=0xbfbfe55c) at e.c:21
21          int rc = write(-1, "X", 1);
(gdb) n
22          pause();
(gdb) p (errno_t)errno
$1 = err_EBADF
(gdb) 

(Note if you actually try this, you need to define at least one object
of the errno_t type in your program to generate the type in the
executable output.)

That should work in other symbolic debuggers beyond gdb.


More information about the freebsd-arch mailing list