代码改变世界

linux申请strace ,lstrace, ptrace, dtrace

  youxin  阅读(2702)  评论(0编辑  收藏  举报

ltrace命令是用来跟踪进程调用库函数的情况。

 

ltrace -h
Usage: ltrace [option ...] [command [arg ...]]
Trace library calls of a given program.

-a, --align=COLUMN align return values in a secific column.
-A MAXELTS maximum number of array elements to print.
-b, --no-signals don't print signals.
-c count time and calls, and report a summary on exit.
-C, --demangle decode low-level symbol names into user-level names.
-D, --debug=MASK enable debugging (see -Dh or --debug=help).
-Dh, --debug=help show help on debugging.
-e FILTER modify which library calls to trace.
-f trace children (fork() and clone()).
-F, --config=FILE load alternate configuration file (may be repeated).
-h, --help display this help and exit.
-i print instruction pointer at time of library call.
-l, --library=LIBRARY_PATTERN only trace symbols implemented by this library.
-L do NOT display library calls.
-n, --indent=NR indent output by NR spaces for each call level nesting.
-o, --output=FILENAME write the trace output to file with given name.
-p PID attach to the process with the process ID pid.
-r print relative timestamps.
-s STRSIZE specify the maximum string size to print.
-S trace system calls as well as library calls.
-t, -tt, -ttt print absolute timestamps.
-T show the time spent inside each call.
-u USERNAME run command with the userid, groupid of username.
-V, --version output version information and exit.
-x FILTER modify which static functions to trace.

 

https://blog.csdn.net/macky0668/article/details/6839520

 

 

运行strace,出现问题:

ptrace: Operation not permitted.

 

早上发现一个进程没有相应, 试图gdb之.

(gdb) attach 30721
Attaching to program: /data0/s/bin/s, process 30721
ptrace: Operation not permitted.

晕倒, 俺已经是sudo权限了. 后来看到一篇博客说:当他已经在 gdb 某个进程时, 试图再利用 gcore 来手动产生一个 core 文件而不得(可以在gdb下用 generate-core-file 完成), 我有所启发, 莫非还有别人在gdb 这个进程.

ps 一下, 发现我的猜想是对的, 我进一步猜想, strace 也应该会失败.

0> strace -p 30721
attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted

果然如此, 两者都用到了 ptrace 这个系统调用.  man ptrace 可以查看详情:

The  ptrace  system  call provides a means by which a parent process may observe and control the execution of another pro-cess, and examine and change its core image and registers.  It is primarily used to  implement  breakpoint  debugging  and system call tracing.

看linux内核详解的话, 应该能给出更深层次的解释, 我现在还留在意会阶段.

 
 
 
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2014-04-15 Church encoding
2014-04-15 jquery 插件JTable使用
2013-04-15 python简介
2012-04-15 C++ 各种排序算法的区别
2012-04-15 c++计算程序运行时间
2012-04-15 c++ rbegin() 和rend()
2012-04-15 fibonacci数的矩阵乘法
点击右上角即可分享
微信分享提示