linux kernel将buf保存到文件做法 很好的调试方法
2014-09-05 21:46
232 查看
linux kernel将关键信息保存到文件做法 很好的调试方法
下面有2个示例:
1:保存机器从开机到结束的VBATT;
2:保存uart接收到的数据到文件;
意义不多说了。
以下是代码:
1:保存VBATT与SOC:
2:将串口接收到的字符转化为16进制保存到文件中:
下面有2个示例:
1:保存机器从开机到结束的VBATT;
2:保存uart接收到的数据到文件;
意义不多说了。
以下是代码:
#include <linux/fs.h> #include <asm/uaccess.h> #include <asm/unaligned.h> static struct file *fp =NULL; int write_to_file (char *buf, int size) { int ret = 0; struct file *fp; mm_segment_t old_fs; loff_t pos = 0; int nwrite = 0; static int offset = 0; static int first_flag=0; /* change to KERNEL_DS address limit */ old_fs = get_fs(); set_fs(KERNEL_DS); if( first_flag==0){ first_flag=1; /* open file to write */ fp = filp_open("/data/at_log1", O_WRONLY|O_CREAT, 0640); if (!fp) { printk("%s: open file error\n", __FUNCTION__); ret = -1; goto exit; } } pos=(unsigned long)offset; /* Write buf to file */ nwrite=vfs_write(fp, buf, size, &pos); offset +=nwrite; exit: return ret; } int xxxx_exit(void){ if (fp) filp_close(fp, NULL); }
1:保存VBATT与SOC:
static int adjust_soc(struct pm8921_bms_chip *chip, int soc, int batt_temp, int chargecycles, int rbatt, int fcc_uah, int uuc_uah, int cc_uah) { ------------ static char vbatt[100]; rc = pm8921_bms_get_simultaneous_battery_voltage_and_current( &ibat_ua, &vbat_uv); if (rc < 0) { pr_err("simultaneous vbat ibat failed err = %d\n", rc); goto out; } chip->vbat_mv= vbat_uv/1000; sprintf(vbatt,"%d %d\n",the_chip->vbat_mv,calculated_soc); write_to_file(vbatt,strlen(vbatt)); ---------- }
2:将串口接收到的字符转化为16进制保存到文件中:
static void msm_serial_hs_rx_tlet(unsigned long tlet_ptr) { --------------- static char temp[1024]; rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR); /* order the read of rx.buffer */ rmb(); if (0 != (uport->read_status_mask & CREAD)) { retval = tty_insert_flip_string(tty, msm_uport->rx.buffer, rx_count); if (retval != rx_count) { msm_uport->rx.buffer_pending |= CHARS_NORMAL | retval << 5 | (rx_count - retval) << 16; } if(rx_count<=512){ //memcpy(temp,msm_uport->rx.buffer,rx_count); for(i=0;i<rx_count;i++){ sprintf(temp+2*i,"%02x",msm_uport->rx.buffer[i]); write_to_file(temp,strlen(temp)); } } ---------------- }
相关文章推荐
- 保存文件等二进制大对象到oracle数据库中的解决方法
- 关于文件保存到数据库及其读取的方法
- MyEclipse中js文件包含中文时保存出错的解决方法
- Delphi下生成Map文件方法,免杀或调试用的
- vs2005中保存文件可以保存中文方法
- C#中保存GIF文件后透明背景问题的一个解决方法
- (转帖)使用OpenFileDialog对话框打开文本文件(写此代码的人应该是有一定功力的程序员,因为代码写的很规范,很容易阅读,很好理解,很好调试)
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- 让EXE文件不能直接启动的方法以防止直接调试的方法
- zend studio5不能以utf-8编码保存文件的解决方法
- 关于PhotoShop中保存CMYK格式TIFF文件在GDI+错误地显示颜色的问题解决方法
- mobile开发中用模拟器调试时不需重新部署数据库文件的方法
- C#中保存GIF文件后透明背景问题的一个解决方法
- 调试找不到文件的解决方法
- IE下载文件,没有提示保存的解决方法(zz)
- 把异常的堆栈跟踪输出保存到日志文件的方法
- ASP实现网页打开任何类型文件都提示保存的方法附代码
- struts资源文件的配置及程序调试的方法-视频(包含所有中文乱码的解决方案)
- [Linux]core文件调试方法
- Linux下core文件调试方法