#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/uaccess.h>
typedef void* ( *PShowStack )( struct task_struct *task, unsigned long *esp );
PShowStack pShowStack;
static int __init init_my_module( void );
static void __exit exit_my_module( void );
static void *find_kernel_addr( const char *symbol );
int __init
init_my_module( void )
{
printk( KERN_INFO "begin to init module...\n" );
pShowStack = find_kernel_addr( "show_stack" );
printk( KERN_INFO "-----------------show stack------------\n" );
pShowStack( current, NULL );
printk( KERN_INFO "---------------------------------------\n" );
printk( KERN_INFO "-----------------dump stack------------\n" );
dump_stack();
printk( KERN_INFO "---------------------------------------\n" );
return 0;
}
void __exit
exit_my_module( void )
{
printk( KERN_INFO "begin to clear module...\n" );
}
void *
find_kernel_addr( const char *symbol )
{
struct file *fp;
char *buf;
char *ptr_last, *ptr_start, *ptr_end;
int iLen;
long address;
char sign;
char name[ 128 ];
loff_t offset = 0;
mm_segment_t old_fs;
if( ( fp = filp_open( "/proc/kallsyms", O_RDONLY, FMODE_READ ) ) == NULL )
{
printk( KERN_ERR "fail to open /proc/kallsyms\n" );
return NULL;
}
buf = kmalloc( sizeof( ( *buf ) ) * ( 1024 + 1 ), GFP_KERNEL );
if( buf == NULL )
{
printk( KERN_ERR "kmalloc fail\n" );
filp_close( fp, NULL );
return NULL;
}
old_fs = get_fs();
set_fs( KERNEL_DS );
while( ( iLen = vfs_read( fp, buf, 1024, &offset ) ) > 0 )
{
buf[ iLen ] = '\0';
ptr_start = buf;
ptr_last = buf + strlen( buf );
memset( name, 0x00, sizeof( name ) );
if( ( ptr_last = strrchr( ptr_start, '\n' ) ) != NULL )
{
ptr_last++;
*ptr_last = '\0';
while( ( ptr_end = strchr( ptr_start, '\n' ) ) != NULL )
{ /* a line */
*ptr_end = '\0';
if( strstr( ptr_start, symbol ) != NULL )
{ /* it's possible that I hoped */
printk( KERN_INFO "to handle %s\n", ptr_start );
sscanf( ptr_start, "%lx %c %s", &address, &sign, name );
printk( KERN_INFO "%lx %c %s\n", address, sign, name );
if( strcmp( name, symbol ) == 0 )
{ /* it is what I hoped */
break;
}
}
/* another line */
ptr_start = ptr_end + 1;
}
}
else
{ /* last line */
if( strstr( ptr_start, symbol ) != NULL )
{
sscanf( ptr_start, "%lx %c %s", &address, &sign, name );
printk( KERN_INFO "%lx %c %s\n", address, sign, name );
}
}
if( strcmp( name, symbol ) == 0 )
{ /* it's time to stop reading the file */
printk( KERN_INFO "find the address of %s: %lx\n", name, address );
break;
}
offset += ptr_last - buf;
}
set_fs( old_fs );
kfree( buf );
filp_close( fp, NULL );
printk( KERN_INFO "finish reading /proc/kallsyms, iLen=%d\n", iLen );
return strcmp( name, symbol ) == 0 ? ( void * )address : NULL;
}
module_init( init_my_module );
module_exit( exit_my_module );
MODULE_LICENSE( "GPL" );
MODULE_AUTHOR( "Jack Liu" );
没有合适的资源?快使用搜索试试~ 我知道了~
linux kernel module打印指定进程栈信息
共2个文件
makefile:1个
c:1个
需积分: 32 7 下载量 59 浏览量
2016-01-19
15:52:06
上传
评论
收藏 1KB RAR 举报
温馨提示
linux kernel module打印指定进程栈信息,简单说明: http://blog.csdn.net/qq123386926/article/details/50524901
资源推荐
资源详情
资源评论
收起资源包目录
show_stack.rar (2个子文件)
show_stack
find.c 3KB
Makefile 152B
共 2 条
- 1
资源评论
qq1223386926
- 粉丝: 9
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功