7

I'm looking for a way to attach to a process using lldb without stopping it. The program I'm debugging has race conditions and I'm worried that the pause is inducing more entropy.

Similar question but for gdb: gdb attach to a process without stop.

Version used:

lldb -v
  lldb-900.3.72
Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47

1 Answers1

9

That is not really possible on x86_64 or ARMv8-A (I'm making an assumption here but it stands for most modern OS designs/architectures).

In general any time the process (forget about realtime stuff for now) makes a system call or is interrupted (which can happen in many ways, in a preemptive operating system) it will yield to the OS at which point there is an indeterminate period of time it may spend doing other things, depending on the system state and what the scheduler decides on (based on things like timing and priorities) before execution returns to that point.

You should just attach and resume execution straight away. There is no way to do what you actually want without introducing some form of jitter (even launching LLDB itself may happen on the same CPU the process you're after was last "running" on).

This is why race conditions are kind of a pain to debug, because they're a pain to reproduce consistently. There are ways to reduce jitter to a minimum when debugging but they involve fairly complicated, usually OS specific tools.

I would also suggest looking into ThreadSanitizer to help find potential race conditions.

Kristina
  • 15,859
  • 29
  • 111
  • 181
  • My current target is ARM but I'm guessing that it will be the same answer :) – Mr_Pouet Feb 24 '18 at 02:12
  • 1
    Yes pretty much, iPhoneOS is still a preemptive OS based on XNU. – Kristina Feb 24 '18 at 02:16
  • 1
    Just to make concrete Kristina's excellent explanation. The ptrace call which is the affordance for the debugger to attach to a process stops the process being attached to. So it is currently not possible for a debugger to attach to a process without stopping it at least once on any ptrace based system - which all Darwin systems are. – Jim Ingham Feb 26 '18 at 18:23
  • That being said, the Mach task APIs may be useful in the future as they can query program state without stopping the process. And given the state of `ptrace` on Darwin today, I would be surprised if LLDB even used it anymore on that platform… – saagarjha Jan 20 '20 at 15:10