Skip to content

Commit 51c2750

Browse files
authored
[lldb] Update examples in docs/use/python-reference.rst to work with Python 3 (llvm#134204)
The examples on this page were using the Python 2-style print. I ran the updated code examples under Python 3 to confirm they are still up-to-date.
1 parent 50fe5b9 commit 51c2750

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

lldb/docs/use/python-reference.rst

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,16 @@ pass them to the Python print function:
153153

154154
(lldb) script
155155
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
156-
>>> print lldb.debugger
156+
>>> print(lldb.debugger)
157157
Debugger (instance: "debugger_1", id: 1)
158-
>>> print lldb.target
158+
>>> print(lldb.target)
159159
a.out
160-
>>> print lldb.process
161-
SBProcess: pid = 59289, state = stopped, threads = 1, executable = a.out
162-
>>> print lldb.thread
163-
SBThread: tid = 0x1f03
164-
>>> print lldb.frame
165-
frame #0: 0x0000000100000bb6 a.out main + 54 at main.c:16
160+
>>> print(lldb.process)
161+
SBProcess: pid = 58842, state = stopped, threads = 1, executable = a.out
162+
>>> print(lldb.thread)
163+
thread #1: tid = 0x2265ce3, 0x0000000100000334 a.out`main at t.c:2:3, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
164+
>>> print(lldb.frame)
165+
frame #0: 0x0000000100000334 a.out`main at t.c:2:3
166166

167167

168168
Running a python script when a breakpoint gets hit
@@ -252,7 +252,7 @@ Here is the code:
252252
> # Get the name of the function
253253
> name = frame.GetFunctionName()
254254
> # Print the order and the function name
255-
> print '[%i] %s' % (counter, name)
255+
> print('[%i] %s' % (counter, name))
256256
> # Disable the current breakpoint location so it doesn't get hit again
257257
> bp_loc.SetEnabled(False)
258258
> # No need to stop here
@@ -588,7 +588,7 @@ say
588588
589589
.. code-block:: python
590590
591-
print >>result, "my command does lots of cool stuff"
591+
print("my command does lots of cool stuff", file=result)
592592
593593
SBCommandReturnObject and SBStream both support this file-like behavior by
594594
providing write() and flush() calls at the Python layer.
@@ -712,7 +712,7 @@ your lldb.ParsedCommand subclass should implement:
712712
"""
713713
714714
And to handle the completion of arguments:
715-
715+
716716
.. code-block:: python
717717
718718
def handle_argument_completion(self, args, arg_pos, cursor_pos):
@@ -826,7 +826,7 @@ a function that can be used by LLDB's python command code:
826826
# And the initialization code to add your commands
827827
def __lldb_init_module(debugger, internal_dict):
828828
debugger.HandleCommand('command script add -f ls.ls ls')
829-
print 'The "ls" python command has been installed and is ready for use.'
829+
print('The "ls" python command has been installed and is ready for use.')
830830
831831
Now we can load the module into LLDB and use it
832832

@@ -964,16 +964,18 @@ script that will launch a program from the current working directory called
964964
"a.out", set a breakpoint at "main", and then run and hit the breakpoint, and
965965
print the process, thread and frame objects if the process stopped:
966966

967-
::
967+
.. code-block:: python
968968
969-
#!/usr/bin/env python
969+
#!/usr/bin/env python3
970970
971971
import lldb
972972
import os
973973
974+
974975
def disassemble_instructions(insts):
975976
for i in insts:
976-
print i
977+
print(i)
978+
977979
978980
# Set the path to the executable to debug
979981
exe = "./a.out"
@@ -983,54 +985,56 @@ print the process, thread and frame objects if the process stopped:
983985
984986
# When we step or continue, don't return from the function until the process
985987
# stops. Otherwise we would have to handle the process events ourselves which, while doable is
986-
#a little tricky. We do this by setting the async mode to false.
987-
debugger.SetAsync (False)
988+
# a little tricky. We do this by setting the async mode to false.
989+
debugger.SetAsync(False)
988990
989991
# Create a target from a file and arch
990-
print "Creating a target for '%s'" % exe
992+
print("Creating a target for '%s'" % exe)
991993
992-
target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
994+
target = debugger.CreateTargetWithFileAndArch(exe, lldb.LLDB_ARCH_DEFAULT)
993995
994996
if target:
995997
# If the target is valid set a breakpoint at main
996-
main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
998+
main_bp = target.BreakpointCreateByName(
999+
"main", target.GetExecutable().GetFilename()
1000+
)
9971001
998-
print main_bp
1002+
print(main_bp)
9991003
10001004
# Launch the process. Since we specified synchronous mode, we won't return
10011005
# from this function until we hit the breakpoint at main
1002-
process = target.LaunchSimple (None, None, os.getcwd())
1006+
process = target.LaunchSimple(None, None, os.getcwd())
10031007
10041008
# Make sure the launch went ok
10051009
if process:
10061010
# Print some simple process info
1007-
state = process.GetState ()
1008-
print process
1011+
state = process.GetState()
1012+
print(process)
10091013
if state == lldb.eStateStopped:
10101014
# Get the first thread
1011-
thread = process.GetThreadAtIndex (0)
1015+
thread = process.GetThreadAtIndex(0)
10121016
if thread:
10131017
# Print some simple thread info
1014-
print thread
1018+
print(thread)
10151019
# Get the first frame
1016-
frame = thread.GetFrameAtIndex (0)
1020+
frame = thread.GetFrameAtIndex(0)
10171021
if frame:
10181022
# Print some simple frame info
1019-
print frame
1023+
print(frame)
10201024
function = frame.GetFunction()
10211025
# See if we have debug info (a function)
10221026
if function:
10231027
# We do have a function, print some info for the function
1024-
print function
1028+
print(function)
10251029
# Now get all instructions for this function and print them
10261030
insts = function.GetInstructions(target)
1027-
disassemble_instructions (insts)
1031+
disassemble_instructions(insts)
10281032
else:
10291033
# See if we have a symbol in the symbol table for where we stopped
1030-
symbol = frame.GetSymbol();
1034+
symbol = frame.GetSymbol()
10311035
if symbol:
10321036
# We do have a symbol, print some info for the symbol
1033-
print symbol
1037+
print(symbol)
10341038
10351039
Writing lldb frame recognizers in Python
10361040
----------------------------------------

0 commit comments

Comments
 (0)