linux打出某一进程的堆栈,如何在Linux上使用Ptrace打印C ++其他进程的调用堆栈

#include

#include

#include

#include

#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 failedn");

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 childn");

if (pid != wait(&status)) /* wait failed */

printf("Parent: attach failed with wrong wait statusn");

if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))

printf("Parent: SIGTRAP didn't stop childn");

/*

* 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 failedn");

if (pid != wait(&status)) /* wait failed */

printf("Parent: wait() failed with wrong wait statusn");

/*

* 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 runningn");

/*

* 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 statusn");

if (!WIFSTOPPED(status))

printf("Parent: invalid wait() completionn");

/*

* 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 failedn");

if (pid != wait(&status)) /* wait failed */

printf("Parent: wait() failed with wrong wait statusn");

/* Check if the pathlength value returned is non-zero */

if ((pathlength = state_addr->pe_path_len) == 0)

printf("Parent: zero length pathname returnedn");

/* 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 pathnamen");

} else {

printf("Parent: the exec pathname is %sn", buf_addr);

if (pid != wait(&status)) /* wait failed */

printf("Parent: wait() failed with wrong statusn");

}

}

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值