+ -
当前位置:首页 → 问答吧 → 如何把一个字符串输出到终端?(在内核里实现)

如何把一个字符串输出到终端?(在内核里实现)

时间:2005-12-01

来源:互联网

假设内核源代码中有一个系统调用函数src_fun(),
如果在其内部加入printk函数的话,好像它把信息输出
到(开机时的)控制台(如果系统启动时用到此系统调用)。
当编程用到此系统调用时,此printk打印的信息并没输出
到用户终端,我就试着用write系统调用来代替printk,
并且加入了头文件:#include <unistd.h>
但是编译内核还是提示错误:找不到write的引用
我怀疑是不是头文件加的不对呀?还是其它的原因?

我见其它源程序中也有对write的调用,但它并没包含
write所在的头文件

如何把一个字符串输出到终端呢(通过改写源代码)
不管什么方法,只要能让编程者看到输出的信息就行

作者: dean13   发布时间: 2005-12-01

If you just want to see the messages, at least 3 possible ways:
BTW: Here I assum you login as a normal user and already set sudo for the user.

1. in text console, run
代码:
sudo sh -c "echo 8 > /proc/sys/kernel/printk"
then insmod

2. dmesg

3. /var/log/messages

作者: zhllg   发布时间: 2005-12-04

kernel里面不能用一般的系统调用的,她必须自给自足……
应该是printk就可以的。

作者: pank7.yardbird   发布时间: 2005-12-04

引用:
作者: pank7.yardbird
kernel里面不能用一般的系统调用的,她必须自给自足……
应该是printk就可以的。
可以通过int $0x80
特别是一些简单的,没有side effect的,是绝对没有问题的
比如getpid
它当然自给自足

作者: zhllg   发布时间: 2005-12-04

引用:
作者: zhllg
If you just want to see the messages, at least 3 possible ways:
BTW: Here I assum you login as a normal user and already set sudo for the user.
我的意思是说假设当编程时用到某个内核系统调用(如sys_func())
在此系统调用函数加入自己的代码如printk("In sys_func()");
然后在你自己写的程序中用到系统调用sys_func(),那么运行你的程序
时应该输出In sys_func(). 用printk好像不可以把信息用户的终端,
如果在linux启动时也用到sys_func(),此printk将会把信息输出到屏幕,
也就可以通过你所说的3种方法查看
我试着用write(1,buf,sizeof(buf)); //buf己初始化
但编译时提示有错,我看源代码中有的程序也用到write但其头文件中也
没见有对它的声明,也很纳闷
怎么通过编程在内核中实现呢,用汇编吧,那个AT&T不熟悉,能不能用C或者利用内核中
己有的函数实现呢

作者: dean13   发布时间: 2005-12-05

you can LOOK his problem specifically at the bottom

作者: dean13   发布时间: 2005-12-05

引用:
作者: dean13
如果在linux启动时也用到sys_func(),此printk将会把信息输出到屏幕,
也就可以通过你所说的3种方法查看
启动时不用任何方法也会在屏幕上看到

你不就是想看到那句话打印出来么
dmesg的运行结果
/var/log/messages里
都有

作者: zhllg   发布时间: 2005-12-06

引用:
作者: dean13
到用户终端,我就试着用write系统调用来代替printk,
并且加入了头文件:#include <unistd.h>
但是编译内核还是提示错误:找不到write的引用
内核编程和user space编程是不一样的

#include<unistd.h>
write()
这是写用户空间程序的方法
not applicable to kernel code

作者: zhllg   发布时间: 2005-12-06

引用:
作者: dean13
you can LOOK his problem specifically at the bottom
his problem? whose?
specifically? what's generically?

I don't quite understand what you said.

BTW, LOOK is a non-transitive verb
If you want to put a noun after LOOK, you have to use LOOK AT
However, unfortunately, "LOOK AT" here is still incorrect
You'd better use SEE

作者: zhllg   发布时间: 2005-12-06

如果用户终端是tty的,可以用tty_io里的读写函数调用实现。
实际上kernel也可以访问终端设备来输出信息,不过在init里先加载终端设备驱动是必要的。

作者: yetop   发布时间: 2005-12-06

/*************---printk.c-----**********/
/*
**desp:将提示信息输出到当前用户的终端tty,使用telnet or ssh连接的用户将会看到
*在当前终端输出的提示信息
*/
#include <linux/kernel.h>
#include <linux/module.h>
#ifdef CONFIG_MODULEVERSIONS
#define MODULEVERSIONS
#include <linux/moduleversions>
#endif
MODULE_LICENSE("GPL");
MODULE_AUTHOR("duanjigang");

#include <linux/sched.h>/*For current*/
#include <linux/tty.h> /*For tty declarations*/

void print_string(char *str)
{
struct tty_struct * my_tty;
/*获得当前进程的tty,写到标准输出*/
my_tty = current->tty;
if(my_tty != NULL)
{
(*(my_tty->driver).write)(my_tty, 0 ,str, strlen(str));
(*(my_tty->driver).write)(my_tty, 0 ,"\015\012", 2);
}
}

int init_module()
{
print_string("module inserted!");
return 0;
}
void cleanup_module()
{
print_string("module cleaned!");
}

Makefile
#Author: duanjigang <[email protected]> <[email protected]>
#Date: 2006-3-19
CC=cc
CFLAG := -Wall -DMODULE -D__KERNEL__ -DLINUX
printk.orintk.c /usr/include/linux/version.h
$(CC) $(CFLAG) -c printk.c -I /usr/src/linux-2.4.20-8/include
install:
insmod printk.o
clean:
rm -f *.o

作者: pangumax   发布时间: 2006-04-18