strace实时查看进程日志输出
来源:互联网 发布:mysql union 编辑:程序博客网 时间:2024/06/07 22:08
在linux下有的时候程序的运行出现问题了,但是这时如果没有将日志打印到文件,想查看一下日志的输出都看不了,停了程序重新跑又一时不能浮现问题,是一件很纠结的事情。
这个时候就可以利用strace 来变通的看一下日志,strace是一个用来跟踪进程执行时的系统调用和所接收的信号的程序会将程序的系统调用情况打印出来。而我们的日志输出,比如printf到了系统调用这一层,使用的是write,于是我们可以利用这一点动态的查看printf的输出。
比如如下命令来查看进程pid为1648的printf输出
strace -e trace=write -s 200 -f -p 1648
-e trace=write 的意思是设置一个表达式,用来控制如何跟踪,这里表示只跟踪write的系统调用情况
-s 200 的意思是字符串输出显示时的最大长度,默认是32个字节,对于printf的打印可能不够,这里我们设置为200
-f 的意思是需要跟踪子进程forks,这里对线程同样适用,不加这个参数只能输出main所在的线程的信息。
-p 1648 指定要跟踪的进程号为1648
然后,输出大概是这样的
27754 write(2, "[jt_close_video_stream]enter\n", 29) = 29
27754 write(2, "[get_device_by_stream]find device record, stm 0xf6300de8\n", 57) = 57
27754 write(2, "[hb_close_video_stream]\n", 24) = 24
27754 write(2, "[hb_close_video_stream]ip 192.168.40.10, port 8154\n", 51) = 51
27754 write(2, "[hb_close_video_stream]stm->playhandle==0, chn 0, stm 0, aready closed\n", 71) = 71
27754 write(2, "[LeaveSection]index 1973, type 4, time cost 1\n", 46) = 46
27754 write(2, "[static_task_with_parameter] leave, pend_cnt 0, total_cnt 83893\n", 64) = 64
27754 +++ exited with 0 +++
27755 write(2, "[static_task_with_parameter] enter\n", 35) = 35
27755 write(2, "[EnterSection]index 1974, type 3, time 143780434\n", 49) = 49
27755 write(2, "[Process]request->type 3\n", 25) = 25
27755 write(2, "[Process]OPENSTREAM VEDIO, userid=4130344424, 192.168.40.10, Channel=0, Codec=0, c3346a77\n", 90) = 90
27755 write(2, "[hb_open_video_stream]ip 192.168.40.10, port 8154\n", 50) = 50
27755 write(2, "[hb_open_video_stream]hbdev->DEVICECFG.dwDevType 0\n", 51) = 51
27755 write(2, "[hb_open_video_stream]before HB_NET_RealPlay -164611536, channel 0, nStream 0\n", 78) = 78
27755 write(2, "[JtAsynReceiver::Start]sem_wait before\n", 39) = 39
27755 write(2, "[JtAsynReceiver::Start]sem_wait after, res 0\n", 45) = 45
这里"[JtAsynReceiver::Start]sem_wait after, res 0\n"就是printf输出的内容
虽然可能有其他的write干扰,但这样变相的实现了查看printf的输出
如需要保存到文件则使用-o 参数
strace -o out.strace -e trace=write -s 200 -f -p 1648
弊端:CPU占用率较高,这里虽然只显示了write的调用,但实际上strace还是在探测所有的系统调用,只是过滤显示了write
- strace实时查看进程日志输出
- 查看init进程的日志输出
- iOS安全–实时查看模拟器的日志输出
- iOS安全–实时查看模拟器的日志输出
- 实时查看日志
- tomcat查看实时日志
- 使用strace查看后台程序stdout输出
- 使用strace命令查看php进程信息
- adb logcat查看某个进程的输出日志
- linux 实时查看tomcat日志
- 查看实时刷新刷新日志
- docker logs 查看实时日志
- linux查看tomcat实时日志
- docker logs 查看实时日志
- 实时查看docker容器日志
- 输出日志查看数据
- openwrt日志查看、输出
- Logcat查看/输出日志信息
- EJB
- 如何解决Word中粘贴图片时只显示一部分的问题?
- NuPlayer介绍
- 【android】:android实现一个加法计算器
- typedef 与 define 小结
- strace实时查看进程日志输出
- 自定义下拉弹窗ppw
- java文件读写的例子
- 编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)
- java Map及Map.Entry详解
- 面向对象设计原则
- Oracle数据库创建第一张表
- 数据结构基础知识(二)
- 欢迎使用CSDN-markdown编辑器