5

I've seen a lot of examples using dialog tools into scripts with lines as follows:

trap "rm $datafile" 0 1 2 5 15

What I don't understand is the meaning of SIGNAL 5, ok I know is SIGTRAP. But shame on me, still I don't understand its real significance...

Thanks in advance!!

1
  • It's not significant now - seems to have been reused from very old scripts which worked around some buggy shell implementation on Solaris. A better choice would be signal 3. Oct 29, 2015 at 22:47

2 Answers 2

7

There's a manpage signal(7):

Signal         Value   Action   Comment
SIGTRAP        5        Core    Trace/breakpoint trap

This signal (as the manpage suggests) is primarily used by debugging tools like gdb or strace, respectively the ptrace(2) system call. If one process is tracing another process's activity through ptrace(2), SIGTRAP will be heavily used to »interrupt« the child, even though the child in most cases won't effectively know this happened (a process monitored through ptrace(2) does not necessarily notice which signal had been sent to it, because of the tracer being able to intercept and filter delivered signals and SIGTRAP primarily serves to inform the tracer that something noticable happened).

Looking at the ptrace(2) manpage is very enlightening in this case.

2

I don't understand its real significance

Let's see how it's caused and used.

The major cause of SIGTRAP is the int3 instruction.

We can check that with:

int main() {
    asm("int3");
    return 0;
}

which when run outputs:

Trace/breakpoint trap (core dumped)

and has exit status 133 = 128 + 5, thus signal 5, SIGTRAP.

GDB inserts int3 instruction in the text segment, sets up ptrace, and lets the program run. When it hits int3, ptrace wakes up the parent which can the monitor the child's state.

Note that there are also hardware breakpoints, which have a different mechanism: What is the difference between hardware and software breakpoints? | Stack Overflow

More about int3

int3 has two encodings:

  • the regular int prefix + 3 which takes up 2 bytes, int 3 in NASM
  • a special 1 byte long encoding, int3 in NASM

The one byte long encoding is fundamental for GDB. If the instruction were larger than 1 byte, it could overwrite multiple instructions, which would be messy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .