C++ 如何在Linux上使用Ptrace打印C+的其他进程的调用堆栈+;
我正在开发一个应用程序,它需要Linux上所有执行进程的调用堆栈。 我正在尝试使用ptrace,但无法继续,因为我不清楚代码中需要遵循的步骤 我也尝试过回溯,但它的使用仅限于当前流程 有人能给我同样的指导吗 谢谢, Sandeep此代码取自ptrace的HP UX手册页。 希望这有用。 下面的示例演示了一些ptrace()的使用 通过跟踪进程进行的调用C++ 如何在Linux上使用Ptrace打印C+的其他进程的调用堆栈+;,c++,linux,ptrace,C++,Linux,Ptrace,我正在开发一个应用程序,它需要Linux上所有执行进程的调用堆栈。 我正在尝试使用ptrace,但无法继续,因为我不清楚代码中需要遵循的步骤 我也尝试过回溯,但它的使用仅限于当前流程 有人能给我同样的指导吗 谢谢, Sandeep此代码取自ptrace的HP UX手册页。 希望这有用。 下面的示例演示了一些ptrace()的使用 通过跟踪进程进行的调用 #包括 #包括 #包括 #包括 #定义BUFSIZ 1024 #定义MAXPATH 1024 pid_t npid、cpid、pid; int状
#包括
#包括
#包括
#包括
#定义BUFSIZ 1024
#定义MAXPATH 1024
pid_t npid、cpid、pid;
int状态,错误=0,路径长度;
ptrace_event_t*event_addr;
ptrace_state_t*state_addr;
char*buf_addr;
大小、事件、状态;
国际文件[2];
child()
{
int n,bar;
关闭(存档[1]);
/*等待父级写入管道*/
而((n=read(field[0],&bar,BUFSIZ))==0);
/*现在孩子可以执行了*/
执行期间如果(execlp(“ls”,“ls”,“char*)0)<0)/*错误*/
printf(“子:exec失败\n”);
出口(0);
}
父项()
{
关闭(存档[0]);
/*在child执行exec之前,附加它并设置其事件标志*/
if(ptrace(PT_ATTACH,pid))/*未能连接进程*/
printf(“父项:未能附加子项\n”);
如果(pid!=等待(&status))/*等待失败*/
printf(“父级:附加失败,等待状态错误\n”);
如果(!WIFSTOPPED(status)| |(WSTOPSIG(status)!=SIGTRAP))
printf(“父项:SIGTRAP未停止子项\n”);
/*
*子进程现在已停止。请设置其事件标志以指示
*它需要在PTRACE_EXEC事件中触发。
*/
事件地址->设置事件=PTRACE\u EXEC;
if(ptrace(PT设置事件掩码、pid、事件地址、事件长度))
printf(“父项:PT\u SET\u事件\u掩码ptrace请求失败\n”);
如果(pid!=等待(&status))/*等待失败*/
printf(“父:wait()失败,等待状态错误\n”);
/*
*向孩子发送一条消息,使其能够跳出while循环。
*让它运行,这样它就可以执行。
*/
写入(归档[1],“现在运行”,7);
如果(ptrace(PT_CONTIN,pid,1,0)!=0)
printf(“父进程:无法使子进程运行\n”);
/*
*在exec系统调用后,等待跟踪的子项停止
*对其ptrace_事件结构中的exec事件集的响应。
*/
如果(pid!=(npid=等待(&status))/*等待失败*/
printf(“父:等待()失败,状态错误\n”);
如果(!WIFSTOPPED(状态))
printf(“父项:无效的等待()完成\n”);
/*
*子进程已停止;获取其进程状态并检查状态
*信息。
*/
if(ptrace(PT_GET_PROCESS_STATE,pid,STATE_addr,STATE_len)<0)
printf(“父级:PT_GET_进程状态ptrace请求失败\n”);
如果(pid!=等待(&status))/*等待失败*/
printf(“父:wait()失败,等待状态错误\n”);
/*检查返回的pathlength值是否为非零*/
如果((路径长度=状态地址->路径长度)=0)
printf(“父:返回的零长度路径名\n”);
/*获取执行文件路径名并将其存储在缓冲区中*/
if(ptrace(PT_GET_PROCESS_PATHNAME,pid,buf_addr,(路径长度+1))
< 0){
printf(“父:无法获取exec路径名\n”);
}否则{
printf(“父:exec路径名是%s\n”,buf_addr);
如果(pid!=等待(&status))/*等待失败*/
printf(“父:等待()失败,状态错误\n”);
}
}
main()
{
事件长度=sizeof(ptrace事件长度);
state_len=sizeof(ptrace_state_t);
事件地址=calloc(事件名称,1);
state_addr=calloc(state_len,1);
buf_addr=calloc(MAXPATH,1);
管道(已归档);
开关(pid=fork()){
案例1:
出口(1);
案例0:
child();
打破
违约:
父项();
打破
}
}
查看pstack代码。pstack在ubuntu lucid上可用。谢谢你的回复,但我无法在Linux上编译代码,因为我没有看到ptrace\u事件的标题。
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#define BUFSIZ 1024
#define MAXPATH 1024
pid_t npid, cpid, pid;
int status, errors=0, pathlength;
ptrace_event_t *event_addr;
ptrace_state_t *state_addr;
char *buf_addr;
size_t event_len, state_len;
int filed[2];
child()
{
int n, bar;
close(filed[1]);
/* Wait for parent to write to pipe */
while ((n = read(filed[0], &bar, BUFSIZ)) == 0);
/* Now the child can exec. */
if (execlp("ls", "ls", (char *)0) < 0) /* error during exec */
printf("Child: exec failed\n");
exit(0);
}
parent()
{
close(filed[0]);
/* Before child does an exec, attach it and set its event flag. */
if (ptrace(PT_ATTACH,pid)) /* failed to attach process */
printf("Parent: Failed to attach child\n");
if (pid != wait(&status)) /* wait failed */
printf("Parent: attach failed with wrong wait status\n");
if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
printf("Parent: SIGTRAP didn't stop child\n");
/*
* The child process has now stopped. Set its event flag indicating
* that it needs to trigger on a PTRACE_EXEC event.
*/
event_addr->pe_set_event = PTRACE_EXEC;
if (ptrace(PT_SET_EVENT_MASK, pid, event_addr, event_len))
printf("Parent: PT_SET_EVENT_MASK ptrace request failed\n");
if (pid != wait(&status)) /* wait failed */
printf("Parent: wait() failed with wrong wait status\n");
/*
* Send the child a message so it can break out of the while loop.
* Get it running so it can exec.
*/
write(filed[1], "now run", 7);
if (ptrace(PT_CONTIN, pid, 1, 0) != 0)
printf("Parent: failed to get child process running\n");
/*
* Wait for the traced child to stop after the exec system call in
* response to an exec event set in its ptrace_event structure.
*/
if (pid != (npid = wait(&status))) /* wait failed */
printf("Parent: wait() failed with wrong status\n");
if (!WIFSTOPPED(status))
printf("Parent: invalid wait() completion\n");
/*
* Child has stopped; fetch its process state and examine state
* information.
*/
if (ptrace(PT_GET_PROCESS_STATE, pid, state_addr, state_len) < 0)
printf("Parent: PT_GET_PROCESS_STATE ptrace request failed\n");
if (pid != wait(&status)) /* wait failed */
printf("Parent: wait() failed with wrong wait status\n");
/* Check if the pathlength value returned is non-zero */
if ((pathlength = state_addr->pe_path_len) == 0)
printf("Parent: zero length pathname returned\n");
/* Fetch exec'd file pathname and store it in the buffer. */
if (ptrace(PT_GET_PROCESS_PATHNAME, pid, buf_addr, (pathlength+1))
< 0){
printf("Parent: Failed to get exec pathname\n");
} else {
printf("Parent: the exec pathname is %s\n", buf_addr);
if (pid != wait(&status)) /* wait failed */
printf("Parent: wait() failed with wrong status\n");
}
}
main()
{
event_len = sizeof(ptrace_event_t);
state_len = sizeof(ptrace_state_t);
event_addr = calloc(event_len, 1);
state_addr = calloc(state_len, 1);
buf_addr = calloc(MAXPATH, 1);
pipe(filed);
switch (pid = fork()) {
case -1:
exit(1);
case 0:
child();
break;
default:
parent();
break;
}
}