Can strace/ptrace cause a program to crash?
No, strace
should not cause a program crash -
Except in this somewhat unusual case:
If it has a bug that depends on timing of execution, or runtime memory locations.
It may trigger this kind of "heisenbug" - but extremely rarely, because this kind of bug is rare, and it needs to only trigger under strace or other instrumentation. And when you find a heisenbug, that's often a good thing.
Regarding ptrace()
- the syscall - that is just what strace
does inside I think, so it's similar. One can just do more than strace
can when using ptrace()
directly.
Your example would be just this kind of bug:
In the example, strace
would change the timing of the steps to create a network connection. If that causes a problem, it was a "problem waiting to happen" - the timing of execution changes constantly. With strace
, just a little more. But any other application could have changed the timing more, like starting a program.
what would happen if you straced a running process just as it was creating a network socket or something similar.
Stuff like that is done via system calls, meaning the process makes a request of the kernel, and the kernel fulfils it. The kernel also does the magic to make ptrace()
work. While this doesn't mean it would be impossible for it to trip over itself (which would be a bug), it seems unlikely as this is explicitly one of its purposes: to coordinate processes on a multitasking system.