以下为本人学习笔记,如有转载请注明出处,谢谢

1. service用法 oneshot

DEFINE_MUTEX(buzzer_mutex);

 

mutex_lock(&buzzer_mutex);

mutex_unlock(&buzzer_mutex);

 

static void WriteNumber(const char *fileName, int number)

{                                                

         FILE *fp;                                                                                    

         fp = fopen(fileName, "w");                   

         if (fp == NULL) {                            

             LCD_DEBUG("open %s error, errno %d\n", fileName, errno);

             return; 

         }     

        fprintf(fp, "%d", number);

        fclose(fp);

 }

 

service aa /usr/bin/aa
class core
user root
group root
critical
onrestart restart aa
onrestart restart tt

#service bbd /usr/bin/bb
service bb /usr/bin/app_bbl_read
user root
group root
oneshot

 

2.【Shell脚本】怎样表示一个for循环

作者:gnuhpc 
出处:http://www.cnblogs.com/gnuhpc/

在此说一下我常用的两个结构: 
1. 
for i in $(seq 1 100); do 
        echo $i 
done 
2. 
for (( i = 1 ; $i <= 100; i++ )) ;do 
         echo $i; 
done

作者:gnuhpc 
出处:http://www.cnblogs.com/gnuhpc/

 

3.对于关闭抢占的内核接口

看内核接口是同步还是异步,比方说msleep就是异步接口,会休眠让出cpu

mdelay是忙等待,不会让出cpu

如果是同步接口的话,对于单核cpu,就不会出现多进程同时调用同一个接口出错的情况

如果是异步接口的话,就得具体问题具体分析,如果是仅仅避免多个进程调度同一个接口的情况的话,加信号量或者普通的锁就可以,不一定要加互斥锁,

如果是又有中间层调度接口,又有用户态上层调度接口,那么就另当别论了

 

spin_lock这种锁是用来多核cpu共同调用同个接口的问题,

mutex_lock这种是会休眠,同个cpu的互斥锁,如果是进程上下文就可以用,如果是中断上下文就不能用这种锁,因为这种锁会休眠,中断中是不允许休眠的

 

local_irq_save(flags);

local_irq_restore(flags);

 

spin_lock_irqsave(&iproc_bbl->lock, flags);

spin_unlock_irqrestore(&iproc_bbl->lock, flags);

 

4.网上大家经常碰到的不能连接问题:

请在运行-cmd输入netstat -n 查看5222端口是否在建立状态 ESTABLISHED,这个方法来源自网络,部落没有遇到过.另外,还有一个,就是清空本地本地DNS缓存,具体步骤如下:

在windows下运行运行菜单,开始->运行,输入"CMD"进行命令行窗口,然后输入 ipconfig/flushdns 按回车键

 

5.休眠唤醒调试

kernel/power/main.c中可以通过打印链表的信息,在休眠唤醒的时候将每个唤醒的源消耗的时间打印出来

 dpm_run_callback() ----》drivers/base/power

 

6.中断中不可以加入打印

中断处理函数应该避免调用不可重入函数, 因为新的中断可能发生并打断正在执行任务中,如果当前任务调用了一些不可重入的函数,将会产生错误。
一些常用库函数如printf,malloc,free等都是不可重入函数,因为在函数中引用了全局变量, 这个道理因该很容易明白了吧?
例如, printf会引用全局变量stdout,malloc,free会引用全局的内存分配表。

 

arch/arm/kernel/debug.S:157: Error: too many positional arguments

 

7.sys文件节点要注意返回值

static inline ssize_t show_counter_sysfs(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct asiu_pwm_chip *asiu_pwm = dev_get_drvdata(dev);
int i, n;
long value;

n=0;
if(asiu_pwm) {
for (i=0; i<6; i++) {
value = __onepulse_pwm_counter_extend(i);
n += sprintf(buf + n, "pwm_id(%d)----0x%x \n", i, value);
}
return n;
}
else
return -1;
}

static struct device_attribute sysfs_misc_list[] = {
__ATTR(onepluse_counter,S_IRUGO , show_counter_sysfs, NULL),
};

 

/* tp info show include tp sw version, fw version, cfg csum, hw version */
static ssize_t tp_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
struct synaptics_rmi4_data *rmi4_data = exp_data.rmi4_data;
int len = 0;
int retval;
unsigned char config_id[4];

if (NULL == rmi4_data || !f34_ctrl_base_addr)
return -1;
/* Get device config ID */
retval = synaptics_rmi4_reg_read(rmi4_data,
f34_ctrl_base_addr,
config_id, sizeof(config_id));
if (retval < 0) {
dev_err(rmi4_data->pdev,
"%s: Failed to read device config ID\n", __func__);
return -1;
}
len =
scnprintf(buf + len, PAGE_SIZE, "SW:%s\n",
SYNAPTICS_DSX_DRV_VERSION);

/* combine FW and CFG CSUM */
#define SPLIT_TAG "_CFGID"

len +=
scnprintf(buf + len, PAGE_SIZE, "FW:%u%s%02x%02x%02x%02x\n", rmi4_data->firmware_id,
SPLIT_TAG,
config_id[0],
config_id[1],
config_id[2],
config_id[3]);
/*
len +=
scnprintf(buf + len, PAGE_SIZE,
"CFG ID:0x%02x 0x%02x 0x%02x 0x%02x\n", config_id[0],
config_id[1], config_id[2], config_id[3]);
*/
/* add for chipset name */
len += scnprintf(buf + len, PAGE_SIZE, "CHIP NAME:%s\n", "synaptics");

return len;
}

sys 文件节点写函数的时候,sh: write error: Bad address,这种错误一般是return没有返回正确的值,

什么值是正确的呢?那就是count,长度这些才可以,那么系统是怎么识别的呢?要取决于文件系统和echo的实现

static ssize_t test_sysfs_get_report_store(struct device *dev,
        struct device_attribute *attr, const char *buf, size_t count)
{
    int retval;
    unsigned char command;
    unsigned long setting;
    struct synaptics_rmi4_data *rmi4_data = f54->rmi4_data;

    retval = sstrtoul(buf, 10, &setting);
    if (retval)
        return retval;

    printk("zbh 111 \r\n");
    
    if (setting != 1) {
        printk("zbh %s(): ---> %d\r\n", __func__, __LINE__);
        return -EINVAL;
    }

    mutex_lock(&f54->status_mutex);

    retval = test_check_for_idle_status();
    if (retval < 0) {
        printk("zbh %s(): ---> %d\r\n", __func__, __LINE__);

        goto exit;
    }

    if (!test_report_type_valid(f54->report_type)) {
        dev_err(rmi4_data->pdev,
                "%s: Invalid report type\n",
                __func__);
        retval = -EINVAL;
        printk("zbh %s(): ---> %d\r\n", __func__, __LINE__);
        goto exit;
    }

    test_set_interrupt(true);

    command = (unsigned char)COMMAND_GET_REPORT;

    retval = synaptics_rmi4_reg_write(rmi4_data,
            f54->command_base_addr,
            &command,
            sizeof(command));
    if (retval < 0) {
        dev_err(rmi4_data->pdev,
                "%s: Failed to write get report command\n",
                __func__);
        goto exit;
    }

/* tddi f54 test reporting + */
#ifdef F54_POLLING_GET_REPORT

    retval = test_sysfs_get_report_polling();
    if (retval < 0) {
        dev_err(rmi4_data->pdev,
                "%s: Failed to get report image\n",
                __func__);
        printk("zbh 222 \r\n");
        goto exit;
    }

#else
/* tddi f54 test reporting - */

    f54->status = STATUS_BUSY;
    f54->report_size = 0;
    f54->data_pos = 0;

    hrtimer_start(&f54->watchdog,
            ktime_set(GET_REPORT_TIMEOUT_S, 0),
            HRTIMER_MODE_REL);

    retval = count;

#endif
    retval = count; // ===》如果去掉这一行的话,那么echo  1 > get_report 的话就会出现错误 sh: write error: Bad address

exit:
    mutex_unlock(&f54->status_mutex);

    printk("zbh %s(): retval=%d---> %d\r\n", __func__, retval, __LINE__);

    return retval;
}

 

因为bbl会影响打印机,1s=1000000us

Bbl的时钟是32.768kHz,

所以1000000/32768=30us左右

也就是这个时钟的精度是30us,而bbl寄存器读status状态是延时10us,太短了,至少要在一个clock完成后再读,所以改成延时35us,

 

8.shell脚本循环

#!/bin/sh

while true
do
let "i++"
echo "is $i"
done

 

9.查看这些宏CONFIG_SMP在linux内核中是否有定义

#ifndef CONFIG_SMP

要去linux3.6.5目录下.config文件

 

10.书写代码框架注意

goto 和return

 

static int asiu_pwmc_config(struct pwm_chip *chip, struct pwm_device *pwm,

                            int duty_ns, int period_ns)

{

       struct asiu_pwm_chip *asiu_pwm = to_asiu_pwm_chip(chip);

       struct asiu_pwm_cfg *pwm_cfg;

       unsigned long period_counts, dutyhi_counts;

       unsigned long prescale = 0;

       unsigned long long ticks;

 

       if (pwm_cfg->enabled) {

              /* Change the PWM output with the new config */

        asiu_pwmc_disable(chip, pwm);

              //asiu_pwmc_enable(chip, pwm);

       }

 

 

#ifdef PWMC_DEBUG

       dev_info(chip->dev, "%s : [pwm-%d] duty_ns = %d, period_ns = %d\n",

                            __FUNCTION__, pwm->hwpwm, duty_ns, period_ns);

#endif

 

       if (duty_ns == 0 && period_ns ==0) {

              /* PWM stay low, duty cycle = 0% */

              prescale = 0;

              period_counts = 0;

              dutyhi_counts = 0;

       }

       else if (duty_ns >= period_ns) {

              /* PWM stay high, duty cycle = 100% */

              prescale = 0;

              period_counts = ASIU_PWM_PERIOD_MASK;

              dutyhi_counts = ASIU_PWM_DUTYHI_MASK;

       }

       else {

              ticks = (unsigned long long)period_ns * asiu_pwm->tick_hz;

              do_div(ticks, NSEC_PER_SEC);

              period_counts = ticks;

              prescale = period_counts >> ASIU_PWM_PEROID_WIDTH;

              if(prescale & ~ASIU_PWM_PRESCALE_MASK) {

                     dev_warn(chip->dev, "%s(%d) : period_counts = %d, prescale = 0x%x\n",

                                   __FUNCTION__, __LINE__, period_counts, prescale);

                     return -EINVAL;

              }

              ticks = (unsigned long long)duty_ns * asiu_pwm->tick_hz;

              do_div(ticks, NSEC_PER_SEC);

              dutyhi_counts = ticks;

       }

 

       pwm_cfg = pwm_get_chip_data(pwm);

       if (!pwm_cfg) {

              dev_warn(chip->dev, "fail to get pwm config data\n");

              return -ENOMEM;

       }

 

       pwm_cfg->prescale = prescale;

       pwm_cfg->period_cnt = period_counts;

       pwm_cfg->dutyhi_cnt = dutyhi_counts;

   

       if (pwm_cfg->enabled) {

              /* Change the PWM output with the new config */

             //asiu_pwmc_disable(chip, pwm);

              asiu_pwmc_enable(chip, pwm);

       }

 

#ifdef PWMC_DEBUG

       dev_info(chip->dev, "%s : pwm_cfg->prescale = %d, period_cnt = %d, dutyhi_cnt = %d\n",

                     __FUNCTION__, pwm_cfg->prescale, pwm_cfg->period_cnt, pwm_cfg->dutyhi_cnt);

#endif

       return 0;

}

 

对于如上框架:

进入config函数时先pwm_disable, 然后中间计算,最后enable

但是中间有很多return,我们做接口的目的有个宗旨

函数是为了改变原来的状态的,如果此函数运行失败返回,那么就要还原原来的场景,不要改变原来的场景

如上例子如果用中间用return,一开始执行了disable,那么中间如果执行失败return了,后面就不会执行enable,那么pwm状态就会变了,那么对于这种情况我们要使用goto到后面,然后再进行enable,就算失败了,也要还原之前的场景去enable,所以需要把上一次的值保存起来。

 

11.vim快捷键

vi  -d   a.c   b.c,可以对比

对比过程中,如果拷贝的话,可以用快捷键dp

 

12.对触摸屏中断的理解

触摸屏与cpu是由一个gpio中断口线相连,所有的中断都是由tp那端抛给cpu的,

Tp通过设定各种参数,刷新频率,扫描频率,电容值(即基准点),tp通过计算触摸屏上的点来决定是否发送中断给cpu,如果计算到没点,就不发送中断给cpu,如果计算到有点,那么就会发送中断给cpu,(注意:tp只要上电启动了那么就会一直进行运算),每次触摸屏上只要有按下,不管几个手指都会产生一个中断,抬起又产生一个中断,最小单位产生两个中断,如果是电平中断,一直按着手指,会不停的产生中断,产生中断的时间和次数由tp的运算速度决定,很难去量化,按下的手指越多,tp运算时间越长,至于有时候发现log有10个finger,和10个status状态,那是驱动代码在一个中断产生时遍历了report动作10次,这些finger和status是通过tp寄存器读取到再由驱动上报内核空间,最终通过input子系统发送给用户空间

 

 ===========================================================================================

13.怎么样让编译器对某些函数不再抱怨warning: unused parameter ‘xxx’?

转自:https://segmentfault.com/q/1010000002395334

#define UNUSED_PARAMETER(x) (void)x 

int main(int argc, char **argv)

{

 

// 这两行是为了避免编译报警告

  UNUSED_PARAMETER(argc);
  UNUSED_PARAMETER(argv);

}

 

如果可能,请务必用正常方法消除 warning,真的多余就去掉吧

方法一:

void foo(int a) {
    (void)a;
    // ...
}

方法二:

#ifdef __GNUC__
#  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
#  define UNUSED(x) UNUSED_ ## x
#endif

void foo(int UNUSED(a)) {
    // ...
}

参考:http://stackoverflow.com/a/12891181

 ===========================================================================================

 

字符串比较时,strncmp   strcmp 需要注意的地方,要注意比较长度 

if ((strlen(ts_keyword) != strlen(fw_ugd->ts_keyid)) ||
strcasecmp(ts_keyword, fw_ugd->ts_keyid)) {
printf("%s: check ts_keyword[%s],fw_ugd->ts_keyid[%s]\n",
__FUNCTION__, ts_keyword, fw_ugd->ts_keyid);
retval = TP_FW_ERR_TS_KEYID_COMPARE;
goto error;
}

 

中断 IRQF_ONESHOT 与 IRQF_SHARED 不能同时使用
当多个设备共享中断时,由于IRQF_ONESHOT会关闭中断线程的中断,而线程一般执行时间会比较长,所以是不允许的
当hardirq函数为NULL时,必须声明IRQF_ONESHOT, 表示threadirq线程中关闭该中断,在某些情况下,这个标志会非常有用
例如:设备是低电平产生中断,而硬中断函数为NULL,如果不使用IRQF_ONESHOT,就会一直产生中断执行NULL函数,中断线程
得不到执行,声明IRQF_ONESHOT后,会执行完线程才使能该中断  


14.devm_request_irq---- 这种架构会自己释放资源,不需要自己释放,交由devr内核链表管理

http://lxr.free-electrons.com/ident?i=devm_request_irq

15.这个网址可以查询内核源码

 

16.rm命令

删除除了 tt.c的所有文件,只保留tt.c不被删除
rm -rf !(tt.c)

 

17.查看当前项目有哪些远程仓库

$ git remote 

bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git remote origin

查看远程仓库

$ git remote -v 
bixiaopeng@bixiaopengtekiMacBook-Pro wirelessqa$ git remote -v
origin     git@gitlab.***.com:xiaopeng.bxp/wirelessqa.git (fetch)
origin     git@gitlab.***.com:xiaopeng.bxp/wirelessqa.git (push)


0x1000 16*16*16=4096bit = 4KB


18.堆栈的限制

  堆栈空间的最大值是由setrlimit系统调用确定的,也可以通过bash内建的ulimit命令来设定和查看.

  例如:

  查看当前可使用的最大堆栈(以KB为单位)

  ulimit -s

  8192 //栈的大小默认是8M

  设定为最大的使用堆栈为15KB

  ulimit -s 15

  此时执行ls将会得到一个段错误.

  ls -l /etc/

  total 1040

  Segmentation fault

  通过用strace跟踪ls命令,将发现有如下的系统调用

  getrlimit(RLIMIT_STACK, {rlim_cur=15*1024, rlim_max=15*1024}) = 0

  说明当前可用的堆栈空间,已经不足以运行strace命令了.

 

 

19.Linux驱动调试中的Debugfs的使用简介

http://blog.csdn.net/wealoong/article/details/7992071

 

Linux DebugFS 子目录也是用debugfs_create_dir来实现

http://blog.csdn.net/superkris/article/details/8626517

mount来调试文件节点, sysfs节点调试方法

make menuconfig ----

Global build settings ------

Compile the kernel with Debug FileSystem enabled

 

Make kernel_menuconfig  --------------

Kernel hacking --------

Debug Filesystem

mount -t debugfs none /sys/kernel/debug

 

20.grep 命令

grep -run "\< abc \>"


21.检测内存泄露的方法

Make  kernel_menuconfig

Kernel hacking =======>

[*] Kernel memory leak detector  

(40000) Maximum kmemleak early log entries

 

[*] Compile the kernel with debug info  

 

make menuconfig

[*] Compile the kernel with Debug FileSystem enabled

[*] Compile the kernel with debug information

 

 

开机后

mount -t debugfs none /sys/kernel/debug

cd  /sys/kernel/debug

cat kmemleak

 

make  kernel_menuconfig

这个选项是说明

General setup ================>

Choose SLAB allocator (SLUB (Unqueued Allocator))

(X) SLUB (Unqueued Allocator)

[*] Enable SLUB debugging support

 

22.这里使用slub分配内存比slab更高效

 

我们在调试内核时,如果出现系统响应非常慢的情况

先看有没有死锁,用lockdep来检测

检测出来后再用ftrace来跟踪函数

如果出现kernel内存泄露,可以 用kmemleak来查看,查看前要确认内核使用哪个分配器,一般是slab或者slub

 cat /proc/meminfo     查看内存泄露

 

23.Linux设备驱动之semaphore机制

static noinline void __down(struct semaphore *sem);
static noinline int __down_interruptible(struct semaphore *sem);
static noinline int __down_killable(struct semaphore *sem);
static noinline int __down_timeout(struct semaphore *sem, long jiffies);
static noinline void __up(struct semaphore *sem);

#define DEFINE_SEMAPHORE(name) \
struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)

static inline void sema_init(struct semaphore *sem, int val)
{
static struct lock_class_key __key;
*sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
}

 

一个核,执行了两个死循环进程,那么这两个进程是通过内核的调度算法去选择要执行哪个进程的,最终会执行arm的指令,这两个进程在单和上是永远不可能同时执行的,MMU里面会有进程id寄存器

 

24.获得内核函数地址的四种方法 

 本文以获取内核函数 sys_open()的地址为例。
   1)从System.map文件中直接得到地址
      $ grep sys_open /usr/src/linux/System.map
   
   2)使用 nm 命令
      $ nm vmlinuz | grep sys_open
   
   3)从 /proc/kallsyms 文件获得地址
      $ cat /proc/kallsyms | grep sys_open
   
   4)使用 kallsyms_lookup_name() 函数
      是在kernel/kallsyms.c文件中定义的,要使用它必须启用CONFIG_KALLSYMS编译内核。
      kallsyms_lookup_name()接受一个字符串格式内核函数名,返回那个内核函数的地址。
        kallsyms_lookup_name("sys_open");

 

方法一、

通过打印函数地址,可以查看函数在哪里调用

例如:

Core.c   drivers\pwm

int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)

{

         if (!pwm || period_ns == 0 || duty_ns > period_ns)

                   return -EINVAL;

 

    printk("%s  drivers\pwm Core.c----(%d)\r\n", __func__, __LINE__);

    printk("pwm->chip->ops->config=%p----(%d)\r\n", pwm->chip->ops->config, __LINE__);

         return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);

}

 

终端显示如下:

[   42.550000] pwm->chip->ops->config=c001b0c0----(378)

 

然后可以在

如下目录

Z:\linux-3.6.5

 

中的System.map中找到

c001b0c0  t  asiu_pwmc_config

就调用的是这个函数asiu_pwmc_config

 

方法二、

dump_stack()函数

 

25.打印语句的使用,带颜色的打印

#define RESETCOLOR "\033[0m"
#define GREEN "\033[0;32m"
#define RED "\033[0;31m"
#define LIGHT_RED "\033[1;31m"
#define YELLOW "\033[1;33m"
#define BLUE "\033[0;34m"
#define LIGHT_BLUE "\033[1;34m"
#define CYAN "\033[0;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define WHITE "\033[1;37m"
#define LIGHT_GRAY "\033[0;37m"
#define DARY_GRAY "\033[1;30m"

printf(YELLOW"**   10. set asiu_pwm while                               **"RESETCOLOR"\r\n");

printf(YELLOW"**"LIGHT_RED" 4. onepluse pwm disable "YELLOW"**"RESETCOLOR"\r\n");

 

RC文件延时方法

wait  /dev/zhangb   随便一个不存在的节点都可以,固定为延时5s 

 

如何增加打印信息---灵活使用宏定义:

#include <stdio.h>
#include <stdlib.h>

#define qWiFiDebug(format, ...) printf("[WiFi] "format" File:%s, Line:%d, Function:%s \r\n", ##__VA_ARGS__, __FILE__, __LINE__ , __FUNCTION__);


int main(void)
-{
| qWiFiDebug("aaaaaa -----");
|
| return 0;
|}

打印输出:

  ./a.out 

[WiFi] aaaaaa ----- File:a.c, Line:9, Function:main

 

#define RESETCOLOR    "\033[0m"
#define GREEN         "\033[0;32m"
#define RED           "\033[0;31m"
#define LIGHT_RED     "\033[1;31m"
#define YELLOW        "\033[1;33m"
#define BLUE          "\033[0;34m"
#define LIGHT_BLUE    "\033[1;34m"
#define CYAN          "\033[0;36m"
#define PURPLE        "\033[0;35m"
#define LIGHT_PURPLE  "\033[1;35m"
#define BROWN         "\033[0;33m"
#define WHITE         "\033[1;37m"
#define LIGHT_GRAY    "\033[0;37m"
#define DARY_GRAY     "\033[1;30m"

#define ZBH_TRACE_DEBUG            (1 << 0)
#define ZBH_TRACE_INIT                (1 << 1)
#define ZBH_TRACE_INT              (1 << 2)
#define ZBH_TRACE_X_Y_COORDINATE   (1 << 3)
#define ZBH_TRACE_FINGER_UP        (1 << 4)


#ifdef __BASE_FILE_NAME__
#define printk_get_basename(x)   __BASE_FILE_NAME__
#else
static char *printk_get_basename(char *path)
{
    char *p1 = path, *p2 = p1;
    while (*p1 != '\0') {
        if (*p1 == '/')
            p2 = p1 + 1;
        p1++;
    }
    return (p2);
}
#endif

//printk(KERN_INFO "[%s]"format"File:%s, Line:%d, Function:%s \r\n", \
        //      ##__VA_ARGS__, printk_get_basename(__FILE__), __LINE__, __FUNCTION__); \
//printk(KERN_INFO "[%s%4d@%18s] "format, GT1X_PREFIX, __LINE__, printk_get_basename(__FILE__), ##arg); \
//printk(KERN_INFO "[%s]"format"[File:%s, Line:%d, Function:%s] \r\n", ZBH_PREFIX, ##arg, printk_get_basename(__FILE__), __LINE__, __FUNCTION__); \

#define ZBH_PREFIX   "ZBH: "
#define zbh_trace(flag, format, arg...) \
do { \
    if (zbh_trace_param & flag) \
        printk(KERN_INFO "[%s%4d@ %s] %s(): "format, ZBH_PREFIX, __LINE__, \
            printk_get_basename(__FILE__), __FUNCTION__, ##arg); \            
} while (0)

#define zbh_printk(format, arg...) \
        printk(KERN_INFO "%s"format, ZBH_PREFIX, ##arg)

 

 

26.linux用户栈和内核栈的设置

http://stackoverflow.com/questions/2562602/how-does-fork-return-for-child-process

http://blog.csdn.net/u011279649/article/details/18795547

copy_thread(unsigned long clone_flags, unsigned long sp, unsigned long unused, struct task_struct * p, struct pt_regs * regs)

How to set the new process entry

int

copy_thread(unsigned long clone_flags, unsigned long stack_start,
        unsigned long stk_sz, struct task_struct *p, struct pt_regs *regs)
{
    struct thread_info *thread = task_thread_info(p);
    struct pt_regs *childregs = task_pt_regs(p);

    *childregs = *regs;
    childregs->ARM_r0 = 0;
    childregs->ARM_sp = stack_start;

    memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
    thread->cpu_context.sp = (unsigned long)childregs;
    thread->cpu_context.pc = (unsigned long)ret_from_fork;

 

27.makefile编写注意:

EXTRA_CFLAGS += -DDO_TEST_REPORTING

ifeq ($(CONFIG_ENABLE_PCI),y)

EXTRA_CPPFLAGS += -DCONFIG_ENABLE_PCI

endif

 

28.好的网址

http://web.mit.edu/gnu/www/index.html -------》麻省理工大学的校网,gnu  , automake , autoconf

www.yoctoproject.org --------》飞思卡尔搞的非常强大的linux开发环境

 

29.文件系统操作,查看内核一些信息

 cat /proc/meminfo 

/data/app # cat /proc/meminfo
MemTotal: 60488 kB
MemFree: 19200 kB
Buffers: 0 kB
Cached: 27028 kB
SwapCached: 0 kB
Active: 11276 kB
Inactive: 22708 kB
Active(anon): 11256 kB
Inactive(anon): 13432 kB
Active(file): 20 kB
Inactive(file): 9276 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 6976 kB
Mapped: 2832 kB
Shmem: 17732 kB
Slab: 3448 kB
SReclaimable: 668 kB
SUnreclaim: 2780 kB
KernelStack: 496 kB
PageTables: 312 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 30244 kB
Committed_AS: 46220 kB
VmallocTotal: 319488 kB
VmallocUsed: 21720 kB
VmallocChunk: 294908 kB

/data/app # cat /proc/mtd
dev: size erasesize name
mtd0: 000c0000 00020000 "boot"
mtd1: 00080000 00020000 "nvram_fac"
mtd2: 000c0000 00020000 "boot_res"
mtd3: 00400000 00020000 "kernel"
mtd4: 00600000 00020000 "ramdisk"
mtd5: 00600000 00020000 "base"
mtd6: 06e00000 00020000 "data"

 

30.find 命令和grep命令

 find . "*sdio*" -maxdepth 2

find . README -maxdepth 1 | xargs grep -nri "a"

find target/linux/brcm5830/files/arch/arm/mach-iproc/pm_iproc/ -name "*.c" -print | xargs grep "USB"

find ./package/kmod-prolin/bbl/ -maxdepth 1 -print | xargs grep -rni "PCI" 

find ./ -name "synaptics_dsx_core.h"  | xargs grep -rwi "reset_device"

 find ./package/kmod-brcm5830x/input/keypad_matrix/src/keypad_matrix.c -print0 |xargs grep -run -B8 -A2 "buzzer_level"

 find ./ -name "*.c" |xargs grep --colour -run printf

dev_dbg(rmi4_data->pdev,
"%s: rmi4_data->report_type = %d\n"
"rmi4_data->finger_limit = %d\n"
"fhandler->num_of_data_points = %d\n",
__func__, rmi4_data->report_type,
rmi4_data->finger_limit, fhandler->num_of_data_points);

 

这是个小技巧,grep的A(after,后)选项和B(before,前)选项可以同时输出其匹配行的前后几行。

比如包含有如下文本的message.txt:

Aug 5 02:43:12 zion kernel: [ 0.000000] Zone PFN ranges: Aug 5 02:43:12 zion kernel: [ 0.000000] DMA 0 -> 4096 Aug 5 02:43:12 zion kernel: [ 0.000000] Normal 4096 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] HighMem 130730 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] early_node_map[1] active PFN ranges Aug 5 02:43:12 zion kernel: [ 0.000000] 0: 0 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] DMI 2.3 present.

用带-B1和-A2选项的grep匹配搜索"DMA"。

grep -B1 -A2 "DMA" message.txt

输出:

Aug 5 02:43:12 zion kernel: [ 0.000000] Zone PFN ranges: Aug 5 02:43:12 zion kernel: [ 0.000000] DMA 0 -> 4096 Aug 5 02:43:12 zion kernel: [ 0.000000] Normal 4096 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] HighMem 130730 -> 130730

grep匹配一个结果,输出多行的功能,在搜索日志文件时很有用。

 

 

Linux下find一次查找多个指定文件或者排除某类文件,在 GREP 中匹配多个关键字的方法
(1)Linux下find一次查找多个指定文件:
查找a.html和b.html
find . -name "a.html"  -name "b.html"  

find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'
find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'  
./a.txt  
./a.doc  
./a.mp3  

(2)排除某些文件类型:
排除目录下所有以html结尾的文件:
find . -type f ! -name "*.html"    

find . -type f ! -name "*.html"       
./ge.bak.02.09  
./ge.html.changed.by.jack  
./a.txt  
./a.doc  
./a.mp3  

(3)排除多种文件类型的示例:
find . -type f ! -name "*.html" -type  f ! -name "*.php" -type  f ! -name "*.svn-base"  -type  f ! -name "*.js"  -type  f ! -name "*.gif"  -type  f ! -name "*.png"  -type  f ! -name "*.cpp"  -type  f ! -name "*.h"  -type  f ! -name "*.o"  -type  f ! -name "*.jpg"  -type  f ! -name "*.so"  -type  f ! -name "*.bak"  -type  f ! -name "*.log"3)在 GREP 中匹配多个关键字的方法:
grep查找多个数字的文件:
-r 递归,-E:正则  -l:只显示文件名
root@116.255.139.240:~/a# grep -r -E '0341028|100081|10086|10001' *  
a.txt:100081  
b.txt:10086  
c/cc.txt:0341028  
c/cc.txt:100081  
c/cc.txt:10086  
c/cc.txt:10001  
c.txt:10001  
d.txt:0341028  

grep -r  -E -l '0341028|100081|10086|10001' *     
a.txt  
b.txt  
c/cc.txt  
c.txt  
d.txt  

多种类型文件示例:
view plainprint?
find . -name "*.html" -o -name "*.js"|xargs grep -r "BusiTree"   

 

 

31.printk 调试方法

内核中打印回调函数的名称

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/current.h>
#include <linux/utsname.h>
#include <asm/cacheflush.h>
#include <asm/processor.h>
#include <asm/thread_notify.h>
#include <asm/stacktrace.h>
#include <asm/mach/time.h>

// add by zbh
int get_func_name(void *ip)
{    

    printk("[<%p>] %pS\n", (void *) ip, (void *) ip);
    
    printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
        KERN_DEFAULT, raw_smp_processor_id(),
        current->pid, current->comm,
        print_tainted(), init_utsname()->release,
        (int)strcspn(init_utsname()->version, " "),
        init_utsname()->version);
    
    return 0;
}

void *ip = NULL;
  ip = keydata->buzzer->enable; printk("[<%p>] %pS\n", (void *) ip, (void *) ip); printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n", KERN_DEFAULT, raw_smp_processor_id(), current->pid, current->comm, print_tainted(), init_utsname()->release, (int)strcspn(init_utsname()->version, " "), init_utsname()->version); keydata->buzzer->enable(keydata,freq,duty);


再在panic.c中 找到print_tainted
EXPORT_SYMBOL(print_tainted);
这样可以给其他驱动调用此函数

 

 

 

 

print_hex_dump ===>内核一个不错的打印方式

此网址是我记录的一个printk 打印函数地址的方法

 http://www.cnblogs.com/sky-heaven/p/7161373.html

    unsigned char input[0x28];
    unsigned char output[0x28];

        print_hex_dump(KERN_CRIT, "bbl_ram@ ", DUMP_PREFIX_OFFSET,
                       16, 1, output, sizeof(output), true);
        printk("\n"); 

 

 __builtin_return_address

http://www.cnblogs.com/sky-heaven/p/7161404.html

printk("\033[1;31m %p---%pS \033[0m\r\n", __builtin_return_address(0), __builtin_return_address(0));

 

    printk("\033[1;31m of_reserved_mem.c zbh call from %p---%pS \033[0m\r\n", 
        __builtin_return_address(0), __builtin_return_address(0));

 

sprint_symbol也可以通过函数地址打印函数名字

Linux内核API sprint_symbol【转】

 https://www.cnblogs.com/sky-heaven/p/14977059.html

#include <linux/kallsyms.h>
#include <linux/module.h>
    

printk("\033[1;33m of_reserved_mem.c zbh %s(%d):\033[0m\r\n", __func__, __LINE__); void *ip = NULL; ip = (unsigned long)rmem->ops->device_init; printk("\033[1;33m of_reserved_mem.c zbh rmem->ops->device_init: [<%p>] %pS \033[0m\r\n", (void *) ip, (void *) ip); printk("\033[1;33m of_reserved_mem.c zbh %s(%d):sprint_symbol start \033[0m\r\n", __func__, __LINE__); char my_buf[KSYM_SYMBOL_LEN]; unsigned long my_address; my_address = (unsigned long)rmem->ops->device_init; sprint_symbol(my_buf, my_address); printk("\033[1;33m of_reserved_mem.c zbh %s(%d):sprint_symbol my_buf=%s \033[0m\r\n", __func__, __LINE__, my_buf); printk("\033[1;33m of_reserved_mem.c zbh %s(%d):sprint_symbol my_address=0x%08x \033[0m\r\n", __func__, __LINE__, my_address); ret = rmem->ops->device_init(rmem, dev);

 


#include <linux/kallsyms.h>

#include <linux/module.h>

CONFIG_KALLSYMS 这个要定义



int
my_get_function_name(unsigned long *addr) { void *ip = NULL; char my_buf[KSYM_SYMBOL_LEN]; unsigned long my_address; ip = addr; printk("\033[1;33m zbh %s(%d) [<%p>] %pS \033[0m\r\n", __func__, __LINE__, (void *) ip, (void *) ip); my_address = addr; sprint_symbol(my_buf, my_address); printk("\033[1;33m zbh %s(%d):sprint_symbol my_buf=%s my_address=0x%08x\033[0m\r\n", __func__, __LINE__, my_buf, my_address); return 0; } my_get_function_name((unsigned long)rmem->ops->device_init); ret = rmem->ops->device_init(rmem, dev);

 

 

#define BF3005_PREFIX   "bf3005: "
#define bf3005_trace(flag, format, arg...) \
do { \
    if (bf3005_trace_param & flag) \
        printk(KERN_ERR "[%s%4d@%18s] "format, BF3005_PREFIX, __LINE__, printk_get_basename(__FILE__), ##arg);  \
} while (0)

#define bf3005_printk(format, arg...) \
    printk(KERN_ERR "%s"format, BF3005_PREFIX, ##arg)

bf3005_trace(BF3005_TRACE_DESCR, "enter %s(initialized = %d)\n",
             __func__, bf3005->initialized);

 

 

方法1:

//#define MY_DEBUG
#ifdef MY_DEBUG
#define MY_DBG(x...) do{printk(x);}while(0)
#else
#define MY_DBG(x...)
#endif

 

方法2:

驱动可以如下写:

#define MY_LEVEL1 (1 << 0)
#define MY_LEVEL2 (1 << 1)

unsigned int my_trace_param=0;
module_param_named(trace, my_trace_param, uint, S_IRUGO|S_IWUSR);


#define MY_DBG(flag, msg...) \
do { \
if (my_trace_param & flag) \
printk(KERN_ERR "zbh-debug: " msg); \
} while (0)

 

MY_DBG(MY_LEVEL1, "Goodbye module exit1.\r\n");
MY_DBG(MY_LEVEL2, "Goodbye module exit2.\r\n");
MY_DBG(MY_LEVEL2, "Goodbye module exit3.\r\n");

 

测试:

insmod my_printk_driver.ko 

echo 2 > /sys/module/my_printk_driver/parameters/trace 

这样就可以选择到底打印哪一条语句,用来动态调试开关,默认关打印

linux内核编程笔记【原创】

 

 

 

code:

linux内核编程笔记【原创】
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include<linux/slab.h>        //kmalloc
#include<asm/io.h>            //ioremap
#include<linux/device.h>    //class_create/device_create
#include <asm/uaccess.h>
#include <linux/pwm.h>
#include <linux/cdev.h>
#include <pax/gpio_cfg.h>
#include <pax/bcm5830x_gpio_def.h>
#include <mach/iproc_regs.h>
#include <mach/memory.h>
#include <mach/iomux.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>

#define PWM_IOC_MAGIC         'n'
#define PWM_CONFIG            _IOW(PWM_IOC_MAGIC, 1, int)
#define PWM_DISABLE            _IOW(PWM_IOC_MAGIC, 2, int)
#define ONE_PWM_CONFIG      _IOW(PWM_IOC_MAGIC, 3, int)
#define ONE_PWM_DISABLE     _IOW(PWM_IOC_MAGIC, 4, int)
#define ONE_PWM_WHILE_10000 _IOW(PWM_IOC_MAGIC, 5, int)
#define SET_PWM_GPIO        _IOW(PWM_IOC_MAGIC, 6, int)
#define SET_PWM_POLATIRY    _IOW(PWM_IOC_MAGIC, 7, int)
#define READ_ONEPLUSE_COUNTER  _IOW(PWM_IOC_MAGIC, 8, int)
#define SET_PWM_FUNC           _IOW(PWM_IOC_MAGIC, 9, int)
#define SET_PWM_WHILE           _IOW(PWM_IOC_MAGIC, 10, int)
#define ONE_PWM_DOORBELL           _IOW(PWM_IOC_MAGIC, 11, int)


#define MY_LEVEL1                    (1 << 0)
#define MY_LEVEL2                    (1 << 1)

unsigned int my_trace_param = 0;
module_param_named(trace, my_trace_param, uint, S_IRUGO|S_IWUSR);


#define MY_DBG(flag, msg...) \
    do { \
        if (my_trace_param & flag) \
            printk(KERN_ERR "zbh-bbl: " msg); \
    } while (0)


static int ttime = 0;
static int pmode = 0; // pmode=0 ----> asiu_pwmc , pmode=1 ----> onepluse

module_param(ttime, int, 0);
module_param(pmode, int, 0);


//module_param(period, int, 0);

#define HELLO_MAJOR 230
int hello_major = HELLO_MAJOR;

module_param(hello_major, int, 0);

static struct cdev *hello_cdev = NULL;
static struct class *dev_class = NULL;
static struct device *dev_device = NULL;


static int hello_open(struct inode *inode, struct file *filp);
static int hello_release(struct inode *inode, struct file *filp);

static int hello_open(struct inode *inode, struct file *filp)
{
    printk("hello_open is OK\r\n");
    return 0;
}

static int hello_release(struct inode *inode, struct file *filp)
{
    printk("hello_release is OK\r\n");

    printk("pwm disable and free\r\n");
    return 0;
}

struct file_operations hello_ops = {
    .owner    = THIS_MODULE,
    .open    = hello_open,
    .release     = hello_release,
    //.unlocked_ioctl = hello_ioctl,
};

static int __init hello_init(void)
{
    dev_t devno;
    int ret;

    devno = MKDEV(hello_major, 0);
    ret = register_chrdev_region(devno, 1, "zbh_hello");
    if (!ret) {
        printk("register dev OK.\r\n");
    }
    else {
        printk("register dev failed.\r\n");
    }

    hello_cdev = cdev_alloc();
    cdev_init(hello_cdev, &hello_ops);
    cdev_add(hello_cdev, devno, 1);
    hello_cdev->owner = THIS_MODULE;
    hello_cdev->ops = &hello_ops;

    dev_class = class_create(THIS_MODULE, "dev_class");
    if (IS_ERR(dev_class)) {
        printk(KERN_ERR "class_create() failed for dev_class\n");
        ret = -EINVAL;
        goto out_err_1;
    }

    dev_device = device_create(dev_class, NULL, devno, NULL, "zbh_hello");
    if (IS_ERR(dev_device)) {
        printk(KERN_ERR "device_create failed.\r\n");
        ret = -ENODEV;
        goto out_err_2;
    }
    
    printk("Hello module init OK.\r\n");

    return 0;

out_err_2:
    class_destroy(dev_class);

out_err_1:
    unregister_chrdev_region(MKDEV(hello_major, 0), 1);
    cdev_del(hello_cdev);
    return ret;
}

static void __exit hello_exit(void)
{
    cdev_del(hello_cdev);

    device_destroy(dev_class, MKDEV(hello_major, 0));
    class_destroy(dev_class);

    unregister_chrdev_region(MKDEV(hello_major, 0), 1);

    MY_DBG(MY_LEVEL1, "Goodbye module exit1.\r\n");
    MY_DBG(MY_LEVEL2, "Goodbye module exit2.\r\n");
    MY_DBG(MY_LEVEL2, "Goodbye module exit3.\r\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("zhangbinghua");
MODULE_DESCRIPTION("zhangbh debug driver");
View Code

相关文章:

  • 2021-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
  • 2021-11-10
  • 2021-11-14
猜你喜欢
  • 2022-12-23
  • 2021-12-23
  • 2021-07-14
  • 2021-09-25
  • 2022-12-23
  • 2021-05-25
相关资源
相似解决方案