In this video we will implement assert function which is especially important in the system.
The assertion is used in all the modules of the kernel. When we write different modules in the kernel,
we assume that everything in it should run as we expect.
A small error will bring the whole system down.
As you see, this is the structure of our system. The user programs are in the untrusted zone.
When they send requests to the kernel by system call, the barricade will check those requests and data
to see if they are valid.
If not, we will simply return error message to the programs.
If the check pass, then the kernel will process these requests.
And now we are in the trusted area
where we will not perform the same checks as we did in the barricade.
Instead we will use assertions to find errors within the kernel.
If the assumption we made in the assertions failed, the assertion will stop the system
and print the file name and line number which causes the error. So it could help us find some errors as we build up the project
.
The assert function in our system is really simple. So let's do it now.
First off, we create a header file debug.h.
As you see, the assert is actually a macro. We define assert and the expression we want to evaluate.
If it evaluates to false, then we will call error check function.
The function takes two parameters
the file name and the line number
.
We pass the predefine macro file and line to it. The file will expand to the current file path
and line expands to the current line number.
Also we use do while loop to wrap up the macro, otherwise it may cause problems when used in if statements.
The value is 0 in this case
so that if statement executes only once.
Ok let�s see the error check function in the file debug.c.
Since we print error message on the screen using printk function, we include print header file.
In the function, there are only two things we will do. Print message and jump to the infinite loop.
In this example, we print error check on the screen,
here we add lines to make it more readable.
Then we print assertion failed, the name of the file and the line number.
After we print the file name and line number, we jump to while loop to stop the system.
Ok right now we can test the assert function in the main file.
we include debug.h file and in this test, we simply assert 0.
So the assertion will fail when we execute it.
In the build script, we add the file debug.c and add debug.o in the linker command.
So let's build the project and check it out.
Ok you see the assertion failed at main.c file. The line number is 14. If we check main.c file,
the assert is at this line.
So the assert is actually working.
what we are going to do next is we are going to talk about the function in print.c file.
If we use assert in the functions of the print file,
we could have problems.
For example, if we added the simple check at the begnning of the function write screen,
we cannot simply using assert to do the check. Because the printk function is used in the assert and we will eventually enter this function again
.
And also the data in the structure screen buffer is not correct if the check fails at this point.
So we should write another function and print message by copying the characters on the screen buffer.
For simplicity, we don't implement the simple check in this function.