实验12.实现断言

简介

实验.assert 断言函数

断言

    1.判断表达式,如果不成立那么打印文件名、行号、函数名和表达式
    注意:文件名、行号、函数名由编译器自动完成

主要代码

引导

省略

内核

main.c

// 文件: main.c
// 时间: 2024-07-19
// 来自: ccj
// 描述: 内核从此处开始

#include "print.h"
#include "init.h"
#include "debug.h"

int main(void) {
    put_str("I am kernel\n");

    init_all();

    ASSERT(1 == 2);

    while (1);
    return 0;
}

init.c


interrupt.c

// 文件: interrupt.c
// 时间: 2024-07-22
// 来自: ccj
// 描述: 中断
//          硬件初始化
//          初始化中断门描述符表,绑定中断门描述符到kernel.s的中断服务函数
//          创建一个中断函数处理数组,让kernel.s的中断服务函数调用
//          开启或关闭中断机制
...
/// @brief 关闭中断机制
/// @return 调用之前的状态
enum intr_status intr_disable() {
    enum intr_status old_status;
    if (INTR_ON == intr_get_status()) {
        old_status = INTR_ON;
        asm volatile("cli" : : : "memory");  // 关中断,cli指令将IF位置0
        return old_status;
    } else {
        old_status = INTR_OFF;
        return old_status;
    }
}

debug.h

#ifndef __KERNEL_DEBUG_H
#define __KERNEL_DEBUG_H

// 打印文件名、行号、函数名和条件表达式
void panic_spin(char* filename, int line, const char* func, const char* condition);

// 传入文件名、行号、函数名
#define PANIC(...) panic_spin(__FILE__, __LINE__, __func__, __VA_ARGS__)

...
#define ASSERT(CONDITION)
    if (CONDITION) {       \
    } else {               \
        PANIC(#CONDITION); \
    }
...

#endif /*__KERNEL_DEBUG_H*/

debug.c

// 文件: debug.c
// 时间: 2024-07-23
// 来自: ccj
// 描述: 打印文件名、行号、函数名和条件表达式

#include "debug.h"
#include "print.h"
#include "interrupt.h"

void panic_spin(char* filename, int line, const char* func, const char* condition) {
    // 关闭中断
    intr_disable();

    put_str("\n!!!!! error !!!!!\n");

    put_str("filename:");
    put_str(filename);
    put_str("\n");

    put_str("line:0x");
    put_int(line);
    put_str("\n");

    put_str("function:");
    put_str((char*)func);
    put_str("\n");

    put_str("condition:");
    put_str((char*)condition);
    put_str("\n");

    while (1);  // 阻塞
}

编译

省略

运行

start.sh

#/bin/bash
# 文件: start.sh
# 描述: 启动bochs
# 时间: 2024-07-19
# 来自: ccj


set -x

bin/bochs -f bochsrc.disk

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值