Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2159561
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2013-03-12 14:10:08

. 添加各种打印
1.Kernel中的打印
a.添加自己的打印函数
  1. 在include/linux/kernel.hprintk之后:
  2. #define DEBUG 1
  3. #if DEBUG
  4. #define dbmsg(fmt, args ...) printk(KERN_NOTICE "%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
  5. #else
  6. #define dbmsg(fmt, args ...)
  7. #endif
b. 打印16进制数
  1. (unsigned int)(base/1024/1024)括起来
  2. dbmsg("base=%dM,size=%dM",(unsigned int)(base/1024/1024),(unsigned int)(size/1024/1024));
c.16进制打印结构体
  1. a.实现
  2. int dump_raw_char(char* p, int len)
  3. {
  4.     int i;
  5.     printk("usb_raw: ");
  6.     for(i=0; i<len; i++)
  7.         printk("0x%02x ", (unsigned char)p[i]);
  8.     printk("\n");
  9.     return 0;
  10. }
  11. EXPORT_SYMBOL_GPL(dump_raw_char);
  12. b. 在头文件中添加定义
  13. extern int dump_raw_char(char* p, int len);

  14. c. 使用
  15. dump_raw_char((char*)dr,sizeof(struct usb_ctrlrequest));
d.
利用内核自带的函数, 打印堆栈信
  1. dump_stack();
e. 打印错误字符串
dbmsg("%s",strerror(errno));
2. Android下C/C++中加打印
a.程序文件中添加打印
在文件system/core/include/cutils/log.h的末尾添加
或者/system/core/include/log/log.h的末尾添加
  1. #define TAG_CONG "cong"
  2. #define LOG_CONG(...) __android_log_print(ANDROID_LOG_INFO, TAG_CONG, __VA_ARGS__)
  3. #define dbmsg(x, ...) LOG_CONG("%s:%s(%d), "x,__FILE__, __FUNCTION__,__LINE__, ##__VA_ARGS__)
  4. 其中打印级别是:
  5. ANDROID_LOG_ERROR > ANDROID_LOG_WARN > ANDROID_LOG_INFO > ANDROID_LOG_DEBUG

  6. 包含的头文件: #include
    Android.mk:  LOCAL_LDLIBS +=  -llog
b.在Android.mk中添加打印
  1. $(warning "the value of LOCAL_PATH is$(LOCAL_PATH)")
c.在Android 中的c/c++中打印堆栈
  1. #include <utils/CallStack.h//在system/core/include/utils/CallStack.h中
  2.     android::CallStack stack;
  3.     stack.update(1, 100);
  4.     stack.log("cong");        //注意:这儿是log,而不是传说中的dump
  5. 编译时在Android.mk中添加库
  6. LOCAL_SHARED_LIBRARIES += libutils
3.在shell中添加打印
  1. echo "exec line=$LINENO"
4. perl中打印出行号与文件名
  1. print "tmd:",__FILE__,"[",__LINE__,"]","\n";
  2. print ("tmd:",__FILE__,"[",__LINE__,"]","\n");
5.Makefile的脚本中的打印
  1. 在规则内部用的是echo
  2. @echo "tmd: Codegen.mak[613]: nvram_auto_gen"
  3. 在规则外部打印变量用的是warning $(warning "tmd:build.mak L2505")
6. java中打印行号

  1. public String getLineInfo()
  2. {
  3.     StackTraceElement ste = new Throwable().getStackTrace()[1];
  4.     return ste.getMethodName()+ "[" + ste.getLineNumber()+"]: ";
  5.  }
使用时: Log.i(TAG,getLineInfo() + "hello");
7.c中宏只打印文件名
c中用__FILE__, 会打印现文件的完整路径,像mtk平台这个路径会很长,如果想只打印文件名怎么办呢?
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define _STRRCHR_IMPL_COMMON(str, ch, offset) (str)[sizeof((str)) - 1 - (offset)] == (ch)? (str) + sizeof((str)) - (offset): sizeof((str)) <= (offset) + 1? (str)
  5.  
  6. #define _STRRCHR_IMPL_31(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 31): (str))
  7. #define _STRRCHR_IMPL_30(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 30): _STRRCHR_IMPL_31(str, ch))
  8. #define _STRRCHR_IMPL_29(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 29): _STRRCHR_IMPL_30(str, ch))
  9. #define _STRRCHR_IMPL_28(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 28): _STRRCHR_IMPL_29(str, ch))
  10. #define _STRRCHR_IMPL_27(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 27): _STRRCHR_IMPL_28(str, ch))
  11. #define _STRRCHR_IMPL_26(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 26): _STRRCHR_IMPL_27(str, ch))
  12. #define _STRRCHR_IMPL_25(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 25): _STRRCHR_IMPL_26(str, ch))
  13. #define _STRRCHR_IMPL_24(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 24): _STRRCHR_IMPL_25(str, ch))
  14. #define _STRRCHR_IMPL_23(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 23): _STRRCHR_IMPL_24(str, ch))
  15. #define _STRRCHR_IMPL_22(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 22): _STRRCHR_IMPL_23(str, ch))
  16. #define _STRRCHR_IMPL_21(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 21): _STRRCHR_IMPL_22(str, ch))
  17. #define _STRRCHR_IMPL_20(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 20): _STRRCHR_IMPL_21(str, ch))
  18. #define _STRRCHR_IMPL_19(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 19): _STRRCHR_IMPL_20(str, ch))
  19. #define _STRRCHR_IMPL_18(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 18): _STRRCHR_IMPL_19(str, ch))
  20. #define _STRRCHR_IMPL_17(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 17): _STRRCHR_IMPL_18(str, ch))
  21. #define _STRRCHR_IMPL_16(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 16): _STRRCHR_IMPL_17(str, ch))
  22. #define _STRRCHR_IMPL_15(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 15): _STRRCHR_IMPL_16(str, ch))
  23. #define _STRRCHR_IMPL_14(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 14): _STRRCHR_IMPL_15(str, ch))
  24. #define _STRRCHR_IMPL_13(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 13): _STRRCHR_IMPL_14(str, ch))
  25. #define _STRRCHR_IMPL_12(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 12): _STRRCHR_IMPL_13(str, ch))
  26. #define _STRRCHR_IMPL_11(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 11): _STRRCHR_IMPL_12(str, ch))
  27. #define _STRRCHR_IMPL_10(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 10): _STRRCHR_IMPL_11(str, ch))
  28. #define _STRRCHR_IMPL_9(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 9): _STRRCHR_IMPL_10(str, ch))
  29. #define _STRRCHR_IMPL_8(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 8): _STRRCHR_IMPL_9(str, ch))
  30. #define _STRRCHR_IMPL_7(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 7): _STRRCHR_IMPL_8(str, ch))
  31. #define _STRRCHR_IMPL_6(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 6): _STRRCHR_IMPL_7(str, ch))
  32. #define _STRRCHR_IMPL_5(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 5): _STRRCHR_IMPL_6(str, ch))
  33. #define _STRRCHR_IMPL_4(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 4): _STRRCHR_IMPL_5(str, ch))
  34. #define _STRRCHR_IMPL_3(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 3): _STRRCHR_IMPL_4(str, ch))
  35. #define _STRRCHR_IMPL_2(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 2): _STRRCHR_IMPL_3(str, ch))
  36. #define _STRRCHR_IMPL_1(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 1): _STRRCHR_IMPL_2(str, ch))
  37. #define _STRRCHR_IMPL_0(str, ch) (_STRRCHR_IMPL_COMMON(str, ch, 0): _STRRCHR_IMPL_1(str, ch))
  38.  
  39. // a macro version of strrchr().
  40. // it has several limitations:
  41. // - str must be a literal string, e.g. "a string".
  42. // - if ch cannot be found in last 32 characters, it will return the whole string.
  43. // this limit can be increased by adding more _STRRCHR_IMPL_* macros.
  44. #define _STRRCHR(str, ch) (sizeof((str)) <= 1? (str): _STRRCHR_IMPL_0(str, ch))
  45.  
  46. #define __FILENAME__ _STRRCHR(__FILE__, '/')
  47.  
  48. int main() {
  49.     cout << "full file path: \t" << __FILE__ << endl;
  50.     cout << "short filename: \t" << __FILENAME__ << endl;
  51.     cout << endl;
  52.  
  53.     cout << "case 1: \n expected: \tefgh.c\n actual: \t" << _STRRCHR("0123/2345/4567/6789/8901/0123/2345/4567/6789/abcd/cdef/efgh.c", '/') << endl;
  54.     cout << "case 2: \n expected: \tabcdcdefefgh.c\n actual: \t" << _STRRCHR("abcdcdefefgh.c", '/') << endl;
  55.     cout << "case 3: \n expected: \tabcd/cdef/0123456789012345678901234567890.c\n actual: \t" << _STRRCHR("abcd/cdef/0123456789012345678901234567890.c", '/') << endl;
  56.     return 0;
  57. }
参考:
8.实现一个简单的printf
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>

  4. static void itoa(char **buf, int i, int base)
  5. {
  6.     char *s;
  7. #define LEN    20
  8.     int rem;
  9.     static char rev[LEN+1];

  10.     rev[LEN] = 0;
  11.     if (i == 0)
  12.         {
  13.         (*buf)[0] = '0';
  14.         ++(*buf);
  15.         return;
  16.         }
  17.     s = &rev[LEN];
  18.     while (i)
  19.         {
  20.         rem = i % base;
  21.         if (rem < 10)
  22.             *--s = rem + '0';
  23.         else if (base == 16)
  24.             *--s = "abcdef"[rem - 10];
  25.         i /= base;
  26.         }
  27.     while (*s)
  28.         {
  29.         (*buf)[0] = *s++;
  30.         ++(*buf);
  31.         }
  32. }
  33. char print_buf[256];
  34. void dbg_print(char *fmt,...)
  35. {
  36.    va_list ap;
  37.    double dval;
  38.    int ival;
  39.    char *p, *sval;
  40.    char *bp, cval;
  41.    int fract;
  42.    unsigned short len;
  43.     bp= print_buf;
  44.     *bp= 0;
  45.     
  46.     va_start (ap, fmt);
  47.     for (p= fmt; *p; p++)
  48.     {
  49.         if (*p != '%')
  50.         {
  51.             *bp++= *p;
  52.             continue;
  53.         }
  54.         switch (*++p) {
  55.         case 'd':
  56.             ival= va_arg(ap, int);
  57.             if (ival < 0){
  58.                 *bp++= '-';
  59.              ival= -ival;
  60.             }
  61.             itoa (&bp, ival, 10);
  62.             break;
  63.             
  64.             case 'o':
  65.             ival= va_arg(ap, int);
  66.             if (ival < 0){
  67.                 *bp++= '-';
  68.              ival= -ival;
  69.             }
  70.             *bp++= '0';
  71.             itoa (&bp, ival, 8);
  72.             break;
  73.             
  74.         case 'x':
  75.             ival= va_arg(ap, int);
  76.             if (ival < 0){
  77.              *bp++= '-';
  78.              ival= -ival;
  79.             }
  80.             *bp++= '0';
  81.             *bp++= 'x';
  82.             itoa (&bp, ival, 16);
  83.             break;
  84.             
  85.         case 'c':
  86.             cval= va_arg(ap, int);
  87.             *bp++= cval;
  88.             break;
  89.             
  90.         case 's':
  91.             for (sval = va_arg(ap, char *) ; *sval ; sval++ )
  92.              *bp++= *sval;
  93.             break;
  94.         }
  95.     }
  96.     *bp= 0;
  97.     printf("%s",print_buf);    //在pc机上测试用的是printf,真正用时把这个函数替换即可
  98.     va_end (ap);
  99. }

  100. int main ( int argc, char *argv[] )
  101. {
  102.     dbg_print("cong:%s%s%d\n",__FILE__,__FUNCTION__,__LINE__);
  103.     return EXIT_SUCCESS;
  104. }
有了myprintf这个函数就可以定义dbmsg了,实现自动打印文件名函数名+行号
#define dbmsg(fmt, args ...) myprintf("%s:%s[%d]: "fmt"\n", __FNAME__,__FUNCTION__, __LINE__,##args)

. 单独编译与清除某个模块

1. 内核如果想单独编译touchscreen模块
  1. sun@ubuntu:/work/ok_linux-3.0.1$ make CONFIG_TOUCHSCREEN_S3C=m -C./ M=./drivers/input/touchscreen/ modules
  2. sun@ubuntu:/work/ok_linux-3.0.1$ make -C./ M=./drivers/video/  clean
清除时 只需要把-M后面的路径改一下就可以了
2. android 平台
  1. cong@ubuntu:/work/an6410/an2.3.4$ export TARGET_PRODUCT=OK6410
  2. cong@ubuntu:/work/an6410/an2.3.4$ mmm ./hardware/libhardware/modules/gralloc/

三. 内核的一些小问题

1. rmmod 出错
  1. [误]: rmmod: chdir(/lib/modules): No such file or directory
  2. [决]: 建目录 /lib/modules/
  3. [又错]: rmmod: chdir(3.0.1): No such file or directory
  4. [决]: 建目录 /lib/modules/3.0.1
2. irq释放时
  1. request_irq(IRQ_EINT(0), button_interrupt, IRQF_TRIGGER_FALLING, "KEY1", NULL);    //注册irq
  2. free_irq(BUTTON_IRQ, button_interrupt);                                            //释放irq
  3. 出现以下问题:
  4. WARNING: at kernel/irq/manage.c:1147 __free_irq+0x9c/0x180()
  5. Trying to free already-free IRQ 101
  6. [解]:
  7. request_irq(IRQ_EINT(0), button_interrupt, IRQF_TRIGGER_FALLING, "KEY1", NULL);    //注册irq
  8. free_irq(BUTTON_IRQ, NULL);                                            //释放irq
  9. 注册与释放时最后一个参数必须相同
3. 把zImage镜像copy出来
在arch/arm/boot/Makefile中添加
  1.  56 $(obj)/zImage: $(obj)/compressed/vmlinux FORCE
  2.  57 $(call if_changed,objcopy)
  3.  58 cp -f arch/arm/boot/zImage ./
  4.  59 @echo ' Kernel: $@ is ready
4. 删除内核中没有参与编译的文件
    a. 在当前目录中查找.c文件
    b. 将查找到的.c转为.o,若不存在,说明这个.c没有编译,删除
    c. 将没有编译的.c文件的头文件去掉
  1. #!/bin/sh
  2. for cf in `find . -maxdepth 1 -name "*.c"`
  3. do
  4.     of=`echo "$cf" | sed 's/\.c/\.o/'`
  5.     if [ ! -f "$of" ]; then
  6.         rm $cf
  7.         echo "rmcf $cf"
  8.         hf=`echo "$cf" | sed 's/\.c/\.h/'`
  9.         if [ -f "$hf" ]; then
  10.             rm $hf
  11.             echo "rmhf $hf"
  12.         fi
  13.     fi
  14. done
注: 只对当前目录有效,删除还是需要谨慎的.若不想谨慎去掉 find中的maxdepth

四. android端的技巧:
1.设置dns
  setprop net.dns1 8.8.8.8
2. 设置打开飞行模式时wifi不关闭
  1. root@78P01:/ # settings get global airplane_mode_radios
  2. cell,bluetooth,wifi,nfc,wimax
  3. root@78P01:/ # settings put global airplane_mode_radios "cell,bluetooth,nfc"
  4. root@78P01:/ # settings get global airplane_mode_radios
  5. cell,bluetooth,nfc


阅读(2193) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~