深圳百度竞价推广聊石家庄seo
交叉编译
为了支持strace对pid进行解析,因系统默认支持的strace版本较低,需要使用较新的版本对strace进行交叉编译,这里使用了github上的 https://github.com/strace/strace/releases 发布的strace v5.19版本,2022-08-12发布。log中说明包含了对prctl调用的解析。从log来看strace 5.14版本就已经引入了prctl解析的支持。
Improvements
...Enhanced prctl syscall decoding.
...
基于该版本的源码对strace进行了交叉编译。编译过程中遇到了与mpers相关的报错.
print_fields.h:246:3: note: in expansion of macro 'PRINT_VAL_U'PRINT_VAL_U((where_).field_); \^~~~~~~~~~~
printsiginfo.c:225:5: note: in expansion of macro 'PRINT_FIELD_U'PRINT_FIELD_U(*sip, si_pkey);^~~~~~~~~~~~~
Makefile:3709: recipe for target 'libmpers_m32_a-printsiginfo.o' failed
make[3]: *** [libmpers_m32_a-printsiginfo.o] Error 1
Makefile:2710: recipe for target 'all' failed
make[2]: *** [all] Error 2
configure时选择禁用mper。成功交叉编译并在设备上执行。
./configure --host=aarch64-openwrt-linux --enable-mpers=no
放到设备上执行,成功获得较新版本v5.19 的strace 的可执行文件。
# ./strace --version
strace -- version 5.19
Copyright (c) 1991-2022 The strace developers <https://strace.io>.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Optional features enabled: no-m32-mpers
监控流程
strace的基本使用场景是监控系统调用,但是默认的场景下,大量的系统调用,及其参数的打印,会占用大量的系统资源,在实际生产环境中的使用是不太适用的。如下使用方式,实现了监控进程向特定文件输出的内容,过滤无效内容,并基于输出内容再进行二次过滤,有效控制了strace跟踪对cpu的占用。
针对特定文件的写流程,我们也采取对路径进行过滤的方式,同时仅捕获write系统调用,同时通过-qqq选项和-e signal 去除无效的输出打印。通过如下方式启动strace对目标进程对特定文件的读写进行监控。
# ./strace -qqq -f -P /path/to/monitor -e signal=none -e trace=write -y -Y -p $pid
下面我们将该流程应用在一个实际的程序中,并观察其输出。
如下是我们监控的进程的源文件,其模拟启动一个线程,打开一个源文件,并在一段时间后对这个文件进行读写,先后进行4次读写,我们假定默认是进行18字节的读写,然后其中有一次进行了非18字节的读写,希望把这次读写过滤出来,认为是异常的读写。
void * worker_thread1(void * pArg){prctl(PR_SET_NAME, "thread1");FILE * fp = NULL;fp = fopen("/path/to/strace/test","w+");sleep(5);fwrite("test write 1 end \n",1,18,fp);fflush(fp);fwrite("test write 2 end \n",1,18,fp);fflush(fp);fwrite("test write 300 end \n",1,10,fp);fflush(fp);fwrite("test write 4 end \n",1,18,fp);fflush(fp);fclose(fp);return NULL;
}int start_thread(){pthread_t tid ;pthread_attr_t threadattr;pthread_attr_init(&threadattr);pthread_create(&tid,&threadattr,worker_thread1,NULL);
}int main(){start_thread();sleep(10);return 0 ;
}
如下是在执行这个进程后,对其输出结果基于grep进行过滤,并重定向到a.log文件中,展示a.log文件中的内容。
$ ./a.out &
[1] 12876$ strace -qqq -f -e signal=none -e trace=write -P /path/to/strace/test -e decode-pids=comm -p 12876 2>&1 |grep -v "18)" > a.log$ cat a.log
[pid 12877<thread1>] write(3, "test write", 10) = 10
我们再看下如果没有基于grep进行过滤的输出结果。
$ strace -qqq -f -e signal=none -e trace=write -P /path/to/strace/test -e decode-pids=comm -p 12865 2>&1
[pid 12866<thread1>] write(3, "test write 1 end \n", 18) = 18
[pid 12866<thread1>] write(3, "test write 2 end \n", 18) = 18
[pid 12866<thread1>] write(3, "test write", 10) = 10
[pid 12866<thread1>] write(3, "test write 4 end \n", 18) = 18
[1]+ Done ./a.out
补充说明
strace命令选项说明
这里对上述流程中使用的strace命令的具体含义做一个简单的说明。
-qqq, --quiet=allsuppress all suppressible messages.
//抑制所有可抑制的辅助打印信息
-f, --follow-forksfollow forks
//跟踪所有子进程
-P PATH, --trace-path=PATHtrace accesses to PATH
//跟踪访问特定的路径的系统调用
-y, --decode-fds[=path]print paths associated with file descriptor arguments
//解析文件描述符到路径进行展示
-Y, --decode-pids=commprint command names associated with PIDs
//解析进程id为comm,comm一般是通过prctl配置。
-p PID, --attach=PIDtrace process with process id PID, may be repeated
//跟踪特定的执行中的指定pid的进程。
-e // -e表示expression,后面可以对应多种类型的表达式。-e trace=SYSCALL
//跟踪trace指定的系统调用。可以是特定系统调用,也可以是某一类系统调用。-e signal=SET, --signal=SET
//设定 -e signal=none 就可以忽略所有的信号。
-o FILE, --output=FILEsend trace output to FILE instead of stderr
//指定输出到文件而不是stderr标准错误输出。在结尾对标准错误的输出进行管道重定向。
2>&1
//基于描述符的读写过滤表达式,需要注意的是,-e write=set选项能够输出的前提是对应的write系统调用有被捕获。
//所以其实这个输出结果是和write系统调用的输出结果相重叠的。
-e write=set (监控文件描述符的输出)
-e read=set (监控文件描述符)
--read=set 从特定文件描述符中读取出来的内容,输出。
参考文档
https://www.man7.org/linux/man-pages/man1/strace.1.html