linux内核函数集锦

0. header

linux/delay延迟相关函数,长延时ssleep msleep(睡眠等待),短延时mdelay udelay(忙等待)

linux/sched.h进程相关的头文件, struct task_struct *进程控制块

linux/kernel.h
linux/module.h   //module_init
linux/fs.h       //file_operations
asm/uaccess.h    //copy_from_user
asm/io.h         // ioremap

linux/irqs.h     //中断号
linux/workqueue.h //工作队列
linux/timer.h     // 内核定时器

sudo apt-get install linux-headers-2.6.32-21-generic
sudo apt-get install linux-source-2.6.32

1. simple_strtoul

    用于将字符串转换为无符号长整数,第3个参数10意味着转换方式是10进制。

  ival = simple_strtoul(buffer, NULL, 10);

2.  BUG()和BUG_ON()

大部分体系结构把BUG()和BUG_ON()定义成某种非法操作,这样自然会产生需要的oops

#ifndef HAVE_ARCH_BUG
#define BUG() do { \
    printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
    panic("BUG!"); \
} while (0)
#endif

#ifndef HAVE_ARCH_BUG_ON
#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
#endif

定义在include/asm-generic/bug.h中,在条件不满足时输出oops。需要linux 内核开启General setup->Configure standard kernel features->BUG() support。

有些时候,你只是需要在终端上打印一下栈的回溯信息来帮助你测试。此时可以使用dump_stack()。它只在终端上打印寄存器上下文和函数的跟踪线索:

if (!debug_check) {
       printk(KERN_DEBUG "provide some information.../n");
       dump_stack();
}

 3. __must_check

include/linux/compiler-gcc4.h 
#define __must_check            __attribute__((warn_unused_result))

__must_check函数是指调用函数一定要处理该函数的返回值,否则编译器会给出警告。

定义一个必须检查返回值得函数:

int pop(_Type& data) __must_check {

....

}

int __must_check pop(_Type& data) {

....

}

 

 

posted @ 2017-03-12 19:40  yuxi_o  阅读(567)  评论(0编辑  收藏  举报