1

I am debugging a program that is short-lived. That is, it won't stop and wait for the lldb/gdb debugger to attach. So how can I attach lldb debugger to this kind of process?

7
  • 2
    Get the debugger to start the process? Change the code to add a sleep at the start? Commented Dec 21, 2014 at 8:44
  • Have some way to test that program independently, and start it inside gdb Commented Dec 21, 2014 at 8:45
  • tromey.com/blog/?p=734 ? Commented Dec 21, 2014 at 8:46
  • @EdHeal I am using vim-lldb as a lldb frontend, so it is not possible to start it in the debugger. And I don't think hacking into the code is a good idea :) Commented Dec 21, 2014 at 8:52
  • One would imagine that you have access to the source code (hence doing the debugging). Just put after main a sleep for ten seconds . Give you time to attach the debugger Commented Dec 21, 2014 at 8:55

2 Answers 2

1

The traditional approach is to have a sleep loop, or if you have access to the source, to put a busyloop like

int wait_for_debugger = 1
while (wait_for_debugger)
    ;

Then you attach to the process and set wait_for_debugger to 0 (e.g. p wait_for_debugger = 0) and continue the process.

lldb has a --waitfor option to attach to a process. This has lldb repeatedly poll the process table looking for a new instance of that process name and attaches to it. The process will get to execute a little bit but the polling frequency is high enough that it usually catches things early enough. This is the process launch --waitfor --name procname command, or pr la -w -n processname for short.

Sign up to request clarification or add additional context in comments.

Comments

1

So how can I attach lldb debugger to this kind of process?

Attach the debugger to the parent process, and set follow-fork-mode child. According to the docs, "The new process is debugged after a fork. The parent process runs unimpeded". See 4.11 Debugging Forks in the GDB manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.