/Teaching/Operating Systems/Tutorials/Using and creating debug flags

Using and creating debug flags

This tutorial is intended to show you the correct use of debug statements in SWEB. After a quick usage demonstration, we will create a new debug flag ourselves.

Use of debug flags

Apart from using GDB for SWEB debugging, a debug macro can used. It allows you to output any printf()-like character sequence to the emulator console. As you can see from the basic SWEB a plentitude of debug messages is printed. In order to better distinguish between the various messages, debug flags have been introduced. Thereby you can not only hide certain unwanted debug output but you are also able to print related debug messages within a common debug flag (group).

To make use of the debug() function, you first need to include its header file: #include "debug.h".

Now, assuming that you want to print a message using the debug flag SYSCALL you would simply write it this way:

uint32 test_value = 42; 
debug(SYSCALL, "Telling you the answer: %d\n", test_value);

On execution of your debug statement, you will see a colorized output in the emulator console: [SYSCALL ]Telling you the answer: 42

Creating a new debug flag

If you want your own debug flag, you will have to edit two files which are handling all available flags.

Basically, all flags are handled in the file common/include/console/debug.h. They are organized in groups and can be enabled or disabled by or-ing a special flag named OUTPUT_ENABLED. Each flag has a color in which it is printed in the debug console, you can add new colors in the enum AnsiColor and use them in your custom flags. It remains your personal preference how you would like to assign colors to the flags.

For demonstration purposes, we will add a flag named SEMAPHORE. Therefore, open debug.h and add the flag at the last lines of the file: const size_t SEMAPHORE = Ansi_Yellow | OUTPUT_ENABLED;. As already mentioned, the or-ing of OUTPUT_ENABLED causes a debug flag to be actually printed.

In the following, you are given an overview of the available color codes (default background and text color according to your shell (probably black and a light grey):

Value Meaning
30 Foreground: black
31 Foreground: red
32 Foreground: green
33 Foreground: yellow
34 Foreground: blue
35 Foreground: magenta
36 Foreground: cyan
37 Foreground: white
39 Foreground: white
40 Background: black
41 Background: red
42 Background: green
43 Background: yellow
44 Background: blue
45 Background: magenta
46 Background: cyan
47 Background: white
49 Background: black

In case you want to disable colorful messages at all, add the following definition to the top of debug.h: #define NOCOLOR.

Finally, we want to encourage you to limit the amount of debug messages to a reasonable size. If you print absolutely everything you are running into the risk of overseeing messages that are really useful for your problem diagnosis. To avoid that, deactivate all flags which are not needed to find the bug you are searching for.