[lldb-dev] issues with python scripting in LLDB

Jim Ingham jingham at apple.com
Fri Nov 4 10:18:26 PDT 2011


Michael,

The script interpreter runs in "asynchronous" mode by default.  So if you want to step the program you are debugging, and the do something after that step returns, you have to wait on the process for state changed events.  Otherwise you're queueing up a bunch of steps, executing your prints before the step returns, and then exiting your command with a bunch of stuff still queued up.  It looks like doing that may get lldb confused, if what Enrico describes is true.  We'll have to figure out why that it true, though that seems secondary. 

Anyway, rather than figure out how the event system works, however, for your purposes it's easier to just put the debugger in synchronous mode.. So for instance with Enrico's version of your script, rewritten as:

gdbrulez:lldb-clean/build/Debug > cat ~/Desktop/script.py
#########################
import lldb
import sys
import os
import time

def StepOver(debugger, args, result, dict):
	arg_split = args.split(" ")
	print type(arg_split)
	count = int(arg_split[0])
        debugger.SetAsync(False)
	for i in range(0,count):
		lldb.thread.StepOver(lldb.eOnlyThisThread)
		debugger.HandleCommand("fr var")
		print "step<%d>"%i

        debugger.SetAsync (True)

def __lldb_init_module(debugger, session_dict):
	debugger.HandleCommand("command script add -f script.StepOver mysto")
	return None

##########################

I get (with the current TOT lldb):

 > ./lldb ~/Desktop/programma
Current executable set to '/Volumes/ThePlayground/Users/jingham/Desktop/programma' (x86_64).
(lldb) br s -n main
Breakpoint created: 1: name = 'main', locations = 1
(lldb) command script import ~/Desktop/script.py
(lldb) run
Process 5116 launched: '/Volumes/ThePlayground/Users/jingham/Desktop/programma' (x86_64)
Process 5116 stopped
* thread #1: tid = 0x2003, 0x0000000100000eef programma`main + 15 at programma.cpp:8, stop reason = breakpoint 1.1
    frame #0: 0x0000000100000eef programma`main + 15 at programma.cpp:8
   5   	}
   6   	int main()
   7   	{
-> 8   		int j = 0;
   9   		while (j < 1000)
   10  			foo(j);
   11  		return 1;
(lldb) n
Process 5116 stopped
* thread #1: tid = 0x2003, 0x0000000100000ef6 programma`main + 22 at programma.cpp:9, stop reason = step over
    frame #0: 0x0000000100000ef6 programma`main + 22 at programma.cpp:9
   6   	int main()
   7   	{
   8   		int j = 0;
-> 9   		while (j < 1000)
   10  			foo(j);
   11  		return 1;
   12  	}
(lldb) n
Process 5116 stopped
* thread #1: tid = 0x2003, 0x0000000100000f07 programma`main + 39 at programma.cpp:10, stop reason = step over
    frame #0: 0x0000000100000f07 programma`main + 39 at programma.cpp:10
   7   	{
   8   		int j = 0;
   9   		while (j < 1000)
-> 10  			foo(j);
   11  		return 1;
   12  	}
(lldb) mysto 10
<type 'list'>
(int) j = 1
step<0>
(int) j = 1
step<1>
(int) j = 2
step<2>
(int) j = 2
step<3>
(int) j = 3
step<4>
(int) j = 3
step<5>
(int) j = 4
step<6>
(int) j = 4
step<7>
(int) j = 5
step<8>
(int) j = 5
step<9>
(lldb)

Be sure to set the debugger back to Async mode in the script when you're done.  My guess is that the most useful way to run lldb "script" commands is to set them to synchronous mode by default.  Or maybe this should be a flag on the "command script add"?

Hope this helps,

Jim


On Nov 3, 2011, at 11:58 PM, michael wrote:

> I have a script, something like:
> 
> #########################
> import lldb
> import sys
> import os
> import time
> 
> def StepOver(debugger, args, result, dict):
>   arg_split = args.split(" ")
>   print type(arg_split)
>   count = int(arg_split[0])
>   for i in range(0,count):
>     lldb.thread.StepOver(lldb.eOnlyThisThread)
>     print "step<%d>"%i
>     time.sleep(.75)
> ##########################
> 
> I also have an command shortcut assigned to call it.
> 
> I have 2 issues:
> 1. I have to sleep in the loop (for some period over some threshold),
>     or the script seems to lock up.
>     Is this due to some race condition ?
> 2. how can I force a console flush inside the loop, currently the console
>     is deferred until the script returns. Is there some sort of yield
>     or console flush command?
> 
> Thanks,
> 
> mtm
> 
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev




More information about the lldb-dev mailing list