模块中printk信息打印到指定文件 终端 网络
2015-07-10 10:55
696 查看
有的时候调试内核程序,经常要将信息打印到其他地方如指定文件或终端还有网络, 网络的话dreanice版主写过个netconsole我这里就不说了...
1、打印到文件:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/syscalls.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>
#define MY_FILE "/root/LogFile"
char buf[128];
struct file *file = NULL;
static int __init init(void)
{
mm_segment_t old_fs;
printk("Hello, I'm the module that intends to write messages to file.\n");
if(file == NULL)
file = filp_open(MY_FILE, O_RDWR | O_APPEND | O_CREAT, 0644);
if (IS_ERR(file)) {
printk("error occured while opening file %s, exiting...\n", MY_FILE);
return 0;
}
sprintf(buf,"%s", "The Messages.");
old_fs = get_fs();
set_fs(KERNEL_DS);
file->f_op->write(file, (char *)buf, sizeof(buf), &file->f_pos);
set_fs(old_fs);
return 0;
}
static void __exit fini(void)
{
if(file != NULL)
filp_close(file, NULL);
}
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
2、打印到终端:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/tty.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("mq110");
static void print_string(char *str)
{
struct tty_struct *my_tty;
my_tty = current->signal->tty;
if (my_tty != NULL)
{
my_tty->driver->write(my_tty,str,strlen(str));
my_tty->driver->write(my_tty,"\015\013",2);
}
}
static int __init print_string_init(void)
{
print_string("Hello world!");
return 0;
}
static void __exit print_string_exit(void)
{
print_string("Goodbye world!");
}
module_init(print_string_init);
module_exit(print_string_exit);
3.修改一下/etc/syslog.conf 文件
#kern.* /dev/console
你打印的东西可能是某个级别的信息。比如说debug,这用printk 可以控制 。
那么就写程
kern.debug /var/log/kern_debug.log
-------------------------
printk(KERN_ALERT "Hello, world\n");
对应
/etc/syslog.conf 中的
kern.alert /kernel.txt
实验成功,修改后要执行
server syslogd restart 重启日志服务。
此方法等于用日志服务帮你做这个事情。该信息用
dmesg 命令也可以看到。
代码在Centos5.3 2.6.18上测试过了
1、打印到文件:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/syscalls.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>
#define MY_FILE "/root/LogFile"
char buf[128];
struct file *file = NULL;
static int __init init(void)
{
mm_segment_t old_fs;
printk("Hello, I'm the module that intends to write messages to file.\n");
if(file == NULL)
file = filp_open(MY_FILE, O_RDWR | O_APPEND | O_CREAT, 0644);
if (IS_ERR(file)) {
printk("error occured while opening file %s, exiting...\n", MY_FILE);
return 0;
}
sprintf(buf,"%s", "The Messages.");
old_fs = get_fs();
set_fs(KERNEL_DS);
file->f_op->write(file, (char *)buf, sizeof(buf), &file->f_pos);
set_fs(old_fs);
return 0;
}
static void __exit fini(void)
{
if(file != NULL)
filp_close(file, NULL);
}
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
2、打印到终端:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/tty.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("mq110");
static void print_string(char *str)
{
struct tty_struct *my_tty;
my_tty = current->signal->tty;
if (my_tty != NULL)
{
my_tty->driver->write(my_tty,str,strlen(str));
my_tty->driver->write(my_tty,"\015\013",2);
}
}
static int __init print_string_init(void)
{
print_string("Hello world!");
return 0;
}
static void __exit print_string_exit(void)
{
print_string("Goodbye world!");
}
module_init(print_string_init);
module_exit(print_string_exit);
3.修改一下/etc/syslog.conf 文件
#kern.* /dev/console
你打印的东西可能是某个级别的信息。比如说debug,这用printk 可以控制 。
那么就写程
kern.debug /var/log/kern_debug.log
-------------------------
printk(KERN_ALERT "Hello, world\n");
对应
/etc/syslog.conf 中的
kern.alert /kernel.txt
实验成功,修改后要执行
server syslogd restart 重启日志服务。
此方法等于用日志服务帮你做这个事情。该信息用
dmesg 命令也可以看到。
代码在Centos5.3 2.6.18上测试过了
相关文章推荐
- AFN 网络操作与AFNetworking
- win8设置无线网络共享
- Cydia添加源http://apt.Feng.com,完成packages卡住 解决方法
- Nginx 负载均衡模块 ngx_http_upstream_module 详述
- 有所述网络流的上限和下限
- ios同步与异步网络请求
- ImageNet Classification with Deep Convolutional Neural Networks笔记(摘要版)
- 在tableviewcell 加载图片,解决第三方SDWebImage网络不是很通畅的状态下往下拉不能加载图片
- windows2008NPS(网络连接策略)设置radius 实现telent登陆交换机路由器权限分配
- SCVMM 2012 R2运维管理十之:添加外围网络计算机
- 网络通信 数据压缩后发送
- 上传图片(http post,webservice与http区别)
- 上传图片(http post,webservice与http区别)
- 来自站点http://thecodist.com/article/all-i-need-to-know-to-be-a-better-programmer-i-learned-in-kindergar
- HTTP 笔记与总结(2 )HTTP 协议的(请求行的)请求方法 及 (响应行的)状态码
- USB相关的网络资料
- Samba服务器配置
- 面试WH mystcpy mystrnpy 链表
- Kickstart+PXE+DHCP+TFTP+HTTP自动安装Centos 6.6
- tcp封包才,解包