1 00:00:00,790 --> 00:00:05,840 In this video we will implement assert function which is especially important in the system. 2 00:00:06,390 --> 00:00:12,000 The assertion is used in all the modules of the kernel. When we write different modules in the kernel, 3 00:00:12,150 --> 00:00:16,870 we assume that everything in it should run as we expect. 4 00:00:16,890 --> 00:00:18,590 A small error will bring the whole system down. 5 00:00:19,500 --> 00:00:25,950 As you see, this is the structure of our system. The user programs are in the untrusted zone. 6 00:00:26,890 --> 00:00:33,970 When they send requests to the kernel by system call, the barricade will check those requests and data 7 00:00:34,360 --> 00:00:35,600 to see if they are valid. 8 00:00:35,830 --> 00:00:39,180 If not, we will simply return error message to the programs. 9 00:00:39,640 --> 00:00:43,690 If the check pass, then the kernel will process these requests. 10 00:00:44,350 --> 00:00:46,440 And now we are in the trusted area 11 00:00:46,600 --> 00:00:50,190 where we will not perform the same checks as we did in the barricade. 12 00:00:50,770 --> 00:00:54,460 Instead we will use assertions to find errors within the kernel. 13 00:00:55,330 --> 00:01:01,430 If the assumption we made in the assertions failed, the assertion will stop the system 14 00:01:01,440 --> 00:01:08,190 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 15 00:01:08,200 --> 00:01:08,920 . 16 00:01:10,170 --> 00:01:14,570 The assert function in our system is really simple. So let's do it now. 17 00:01:16,170 --> 00:01:19,590 First off, we create a header file debug.h. 18 00:01:23,980 --> 00:01:31,030 As you see, the assert is actually a macro. We define assert and the expression we want to evaluate. 19 00:01:31,030 --> 00:01:34,330 If it evaluates to false, then we will call error check function. 20 00:01:34,520 --> 00:01:37,330 The function takes two parameters 21 00:01:37,330 --> 00:01:38,290 the file name and the line number 22 00:01:38,290 --> 00:01:38,740 . 23 00:01:39,940 --> 00:01:47,260 We pass the predefine macro file and line to it. The file will expand to the current file path 24 00:01:47,260 --> 00:01:49,150 and line expands to the current line number. 25 00:01:50,100 --> 00:01:56,580 Also we use do while loop to wrap up the macro, otherwise it may cause problems when used in if statements. 26 00:01:57,720 --> 00:01:59,370 The value is 0 in this case 27 00:01:59,370 --> 00:02:02,300 so that if statement executes only once. 28 00:02:03,250 --> 00:02:07,030 Ok let’s see the error check function in the file debug.c. 29 00:02:10,880 --> 00:02:17,740 Since we print error message on the screen using printk function, we include print header file. 30 00:02:17,780 --> 00:02:22,320 In the function, there are only two things we will do. Print message and jump to the infinite loop. 31 00:02:22,910 --> 00:02:26,960 In this example, we print error check on the screen, 32 00:02:26,960 --> 00:02:29,030 here we add lines to make it more readable. 33 00:02:30,240 --> 00:02:34,740 Then we print assertion failed, the name of the file and the line number. 34 00:02:35,790 --> 00:02:40,770 After we print the file name and line number, we jump to while loop to stop the system. 35 00:02:41,360 --> 00:02:45,030 Ok right now we can test the assert function in the main file. 36 00:02:49,510 --> 00:02:56,680 we include debug.h file and in this test, we simply assert 0. 37 00:02:56,680 --> 00:02:59,520 So the assertion will fail when we execute it. 38 00:02:59,620 --> 00:03:04,170 In the build script, we add the file debug.c and add debug.o in the linker command. 39 00:03:04,870 --> 00:03:07,230 So let's build the project and check it out. 40 00:03:16,080 --> 00:03:25,200 Ok you see the assertion failed at main.c file. The line number is 14. If we check main.c file, 41 00:03:25,200 --> 00:03:27,690 the assert is at this line. 42 00:03:28,650 --> 00:03:30,660 So the assert is actually working. 43 00:03:31,920 --> 00:03:36,750 what we are going to do next is we are going to talk about the function in print.c file. 44 00:03:39,790 --> 00:03:43,050 If we use assert in the functions of the print file, 45 00:03:43,470 --> 00:03:44,790 we could have problems. 46 00:03:46,350 --> 00:03:52,950 For example, if we added the simple check at the begnning of the function write screen, 47 00:03:52,950 --> 00:03:59,780 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 48 00:03:59,790 --> 00:04:00,510 . 49 00:04:01,500 --> 00:04:07,620 And also the data in the structure screen buffer is not correct if the check fails at this point. 50 00:04:07,620 --> 00:04:12,300 So we should write another function and print message by copying the characters on the screen buffer. 51 00:04:13,460 --> 00:04:17,600 For simplicity, we don't implement the simple check in this function.