如果我加载内核模块并使用 lsmod 列出加载的模块,我可以得到模块的“使用计数”(引用该模块的其他模块的数量)。但是,有没有办法弄清楚使用模块的内容?
问题是我正在开发的模块坚持其使用计数为 1,因此我不能使用 rmmod卸载它,但它的“by”列是空的。这意味着每次我想重新编译和重新加载模块时,我都必须重新启动机器(或者,至少,我想不出任何其他方法来卸载它)。
最佳答案
实际上,似乎有一种方法可以列出声明模块/驱动程序的进程——但是,我还没有看到它的广告(在 Linux 内核文档之外),所以我会在这里记下我的笔记:
首先,非常感谢 @haggai_e的回答;指向函数的指针 try_module_get和 try_module_put因为那些负责管理使用计数 (refcount) 的人是让我能够追踪程序的关键。
在网上进一步寻找这个,我不知何故偶然发现了帖子Linux-Kernel Archive: [PATCH 1/2] tracing: Reduce overhead of module tracepoints ;它最终指向内核中存在的一个工具,称为(我猜)“跟踪”;相关文档在目录 Documentation/trace - Linux kernel source tree 中.特别是,两个文件解释了跟踪工具,events.txt和 ftrace.txt .
但是,在 /sys/kernel/debug/tracing/README 中还有一个关于正在运行的 Linux 系统的简短“跟踪迷你 HOWTO”。 (另见 I'm really really tired of people saying that there's no documentation…);注意在内核源码树中,这个文件实际上是由文件kernel/trace/trace.c生成的。 .我已经在 Ubuntu natty 上测试过了,并注意,因为 /sys归 root 所有,你必须使用 sudo读取此文件,如 sudo cat或
sudo less /sys/kernel/debug/tracing/README
/sys 下的所有其他操作这将在此处描述。/proc/testmod-sample文件节点,它返回字符串“This is testmod”。正在阅读时;这是 testmod.c :/*
https://github.com/spotify/linux/blob/master/samples/tracepoints/tracepoint-sample.c
https://www.linux.com/learn/linux-training/37985-the-kernel-newbie-corner-kernel-debugging-using-proc-qsequenceq-files-part-1
*/
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h> // for sequence files
struct proc_dir_entry *pentry_sample;
char *defaultOutput = "This is testmod.";
static int my_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s\n", defaultOutput);
return 0;
}
static int my_open(struct inode *inode, struct file *file)
{
return single_open(file, my_show, NULL);
}
static const struct file_operations mark_ops = {
.owner = THIS_MODULE,
.open = my_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init sample_init(void)
{
printk(KERN_ALERT "sample init\n");
pentry_sample = proc_create(
"testmod-sample", 0444, NULL, &mark_ops);
if (!pentry_sample)
return -EPERM;
return 0;
}
static void __exit sample_exit(void)
{
printk(KERN_ALERT "sample exit\n");
remove_proc_entry("testmod-sample", NULL);
}
module_init(sample_init);
module_exit(sample_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mathieu Desnoyers et al.");
MODULE_DESCRIPTION("based on Tracepoint sample");
Makefile (只需将它放在与 testmod.c 相同的目录中,然后在同一目录中运行 make ):CONFIG_MODULE_FORCE_UNLOAD=y
# for oprofile
DEBUG_INFO=y
EXTRA_CFLAGS=-g -O0
obj-m += testmod.o
# mind the tab characters needed at start here:
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
testmod.ko .try_module_get相关的事件追踪。和 try_module_put ;那些在 /sys/kernel/debug/tracing/events/module :$ sudo ls /sys/kernel/debug/tracing/events/module
enable filter module_free module_get module_load module_put module_request
$ sudo cat /sys/kernel/debug/tracing/tracing_enabled
1
$ sudo cat /sys/kernel/debug/tracing/events/module/enable
0
module_get 使用react。 , module_put等事件,但仅适用于 testmod模块。为此,我们应该首先检查事件的格式:$ sudo cat /sys/kernel/debug/tracing/events/module/module_put/format
name: module_put
ID: 312
format:
...
field:__data_loc char[] name; offset:20; size:4; signed:1;
print fmt: "%s call_site=%pf refcnt=%d", __get_str(name), (void *)REC->ip, REC->refcnt
name ,它包含驱动程序名称,我们可以对其进行过滤。要创建过滤器,我们只需 echo将过滤器字符串放入相应的文件中:sudo bash -c "echo name == testmod > /sys/kernel/debug/tracing/events/module/filter"
sudo ,我们必须包装整个echo重定向作为 sudo 的参数命令-ed bash .其次,请注意,由于我们写信给“父”module/filter ,而不是特定事件(即 module/module_put/filter 等),此过滤器将应用于列为 module 的“子级”的所有事件。目录。sudo bash -c "echo 1 > /sys/kernel/debug/tracing/events/module/enable"
sudo cat /sys/kernel/debug/tracing/trace_pipe | tee tracelog.txt
trace_pipe 正在读取的不同终端中):$ sudo insmod ./testmod.ko
$ cat /proc/testmod-sample
This is testmod.
$ sudo rmmod testmod
trace_pipe正在阅读,我们应该看到如下内容:# tracer: nop
#
# TASK-PID CPU# TIMESTAMP FUNCTION
# | | | | |
insmod-21137 [001] 28038.101509: module_load: testmod
insmod-21137 [001] 28038.103904: module_put: testmod call_site=sys_init_module refcnt=2
rmmod-21354 [000] 28080.244448: module_free: testmod
testmod 所获得的全部内容。驱动程序 - 引用计数仅在驱动程序加载( insmod )或卸载( rmmod )时发生变化,而不是在我们读取 cat 时发生变化.所以我们可以简单地中断对 trace_pipe 的读取在该终端中使用 CTRL+C;并完全停止跟踪:sudo bash -c "echo 0 > /sys/kernel/debug/tracing/tracing_enabled"
/sys/kernel/debug/tracing/trace而不是 trace_pipe如这里。然而,一个问题是这个文件并不意味着“管道”(所以你不应该在这个 tail -f 文件上运行 trace);但是你应该重新阅读 trace每次操作后。后第一insmod ,我们将从 cat 获得相同的输出-ing 两个 trace和 trace_pipe ;然而,在rmmod之后,阅读 trace文件会给出: <...>-21137 [001] 28038.101509: module_load: testmod
<...>-21137 [001] 28038.103904: module_put: testmod call_site=sys_init_module refcnt=2
rmmod-21354 [000] 28080.244448: module_free: testmod
insmod已经退出很长时间,因此它不再存在于进程列表中 - 因此无法通过当时记录的进程 ID (PID) 找到 - 因此我们得到一个空白 <...>作为进程名称。因此,最好记录(通过 tee )来自 trace_pipe 的运行输出在这种情况下。另外,请注意,为了清除/重置/删除 trace文件,一个简单的写一个 0 到它:sudo bash -c "echo 0 > /sys/kernel/debug/tracing/trace"
trace是一个特殊文件,并且始终报告文件大小为零:$ sudo ls -la /sys/kernel/debug/tracing/trace
-rw-r--r-- 1 root root 0 2013-03-19 06:39 /sys/kernel/debug/tracing/trace
grep。等等,因为那些使用 binfmt_misc模块:...
tr-6232 [001] 25149.815373: module_put: binfmt_misc call_site=search_binary_handler refcnt=133194
..
grep-6231 [001] 25149.816923: module_put: binfmt_misc call_site=search_binary_handler refcnt=133196
..
cut-6233 [000] 25149.817842: module_put: binfmt_misc call_site=search_binary_handler refcnt=129669
..
sudo-6234 [001] 25150.289519: module_put: binfmt_misc call_site=search_binary_handler refcnt=133198
..
tail-6235 [000] 25150.316002: module_put: binfmt_misc call_site=search_binary_handler refcnt=129671
trace-cmd 还有一个“前端阅读器”GUI叫 KernelShark ;这两个也在 Debian/Ubuntu 存储库中,通过 sudo apt-get install trace-cmd kernelshark .这些工具可以替代上述过程。testmod示例并未真正显示在多个声明的上下文中的使用,我使用相同的跟踪程序发现我正在编码的 USB 模块被 pulseaudio 反复声明。一旦插入 USB 设备 - 因此该过程似乎适用于此类用例。
关于linux - 有没有办法找出使用 Linux 内核模块的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/448999/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po