ptrace SIGTRAP未处理

时间:2017-03-11 13:43:45

标签: c linux ptrace

对于test1.c,我得到:

5
133
1
0

这意味着子进程首先得到SIGTRAP(5),原因是execl。 最后三行表示子进程因SIGSTRAP而死亡 来自父母的信号。

// test1.c
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/reg.h>
#include <stdio.h>
#include <pthread.h>

int main() {
  pid_t child;
  int status = 0;
  child = fork();
  if(child == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    execl("/bin/ls", "ls", NULL);
  } else {
    wait(&status);
    printf("%d\n", status >> 8);
    ptrace(PTRACE_CONT, child, NULL, SIGTRAP);

    wait(&status);
    printf("%d\n", status);
    printf("%d\n", WIFSIGNALED(status));
    printf("%d\n", WIFEXITED(status));
  }
  return 0;
}

对于test2.c,我得到:

19
1029
0
0
1

19是SIGSTOP1029(SIGTRAP | (PTRACE_EVENT_EXEC<<8)),但是SIGTRAP 最后三分线超出了我。为什么子进程正常退出?来自父母的// test2.c #include <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <sys/reg.h> #include <stdio.h> #include <pthread.h> int main() { pid_t child; int status = 0; child = fork(); if(child == 0) { ptrace(PTRACE_TRACEME, 0, NULL, NULL); raise(SIGSTOP); execl("/bin/ls", "ls", NULL); } else { wait(&status); ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACEEXEC); printf("%d\n", status >> 8); ptrace(PTRACE_CONT, child, NULL, 0); wait(&status); printf("%d\n", status >> 8); ptrace(PTRACE_CONT, child, NULL, SIGTRAP); wait(&status); printf("%d\n", status); printf("%d\n", WIFSIGNALED(status)); printf("%d\n", WIFEXITED(status)); } return 0; } 信号会发生什么?

module testcounter
(
    input wire clk,
    input wire resetn,
    input wire [31:0] num_to_count,
    output reg [7:0] output_signal
);

reg [31:0] counter;

always@(posedge clk or negedge resetn) begin
    if (!resetn) begin
        counter <= 0;
    end else begin
        if (counter == num_to_count) begin
            counter <= 0;
        end else begin
            counter <= counter + 1;
        end
    end
end
assign output_signal = (counter == num_to_count) ? 8'hff : 8'h00;

endmodule

1 个答案:

答案 0 :(得分:1)

使用跟踪器

分派SIGNAL时
ptrace(PTRACE_CONT, child, NULL, SIGXXXX);

跟踪器没有收到该信号的通知 - 这是有道理的。否则,你怎么会向tracee发出信号?你会永远抓住它。

  

PTRACE_CONT                 重新启动已停止的tracee进程。如果数据非零,则为                 解释为要传递给的信号的数量                 tracee;否则,没有信号传递。 因此,例如,                 跟踪器可以控制发送到tracee的信号是否为                 是否已送达

如果您想让孩子远离父母,您应该使用

kill(child_pid, SIGTRAP)

所以,我想当你在第一个例子中发送SIGTRAP时 - 你只是向进程发送一个未处理的信号,这可能会杀死他。

另一方面 - PTRACE_TRACEME不会导致子进程停止,因此,在调度SIGTRAP时,它也可能已经完成了它的执行。