diff options
Diffstat (limited to 'examples/customization')
-rw-r--r-- | examples/customization/bin-utils/.lldbinit | 5 | ||||
-rw-r--r-- | examples/customization/bin-utils/README | 36 | ||||
-rw-r--r-- | examples/customization/bin-utils/binutils.py | 122 | ||||
-rw-r--r-- | examples/customization/import-python/README | 40 | ||||
-rw-r--r-- | examples/customization/import-python/importcmd.py | 31 | ||||
-rw-r--r-- | examples/customization/pwd-cd-and-system/.lldbinit | 7 | ||||
-rw-r--r-- | examples/customization/pwd-cd-and-system/README | 41 | ||||
-rw-r--r-- | examples/customization/pwd-cd-and-system/utils.py | 49 |
8 files changed, 331 insertions, 0 deletions
diff --git a/examples/customization/bin-utils/.lldbinit b/examples/customization/bin-utils/.lldbinit new file mode 100644 index 000000000000..5a2f6feb94df --- /dev/null +++ b/examples/customization/bin-utils/.lldbinit @@ -0,0 +1,5 @@ +# So that ~/binutils.py takes precedence. +script sys.path[:0] = [os.path.expanduser('~')] +script import binutils +command script add -f binutils.itob itob +command script add -f binutils.utob utob diff --git a/examples/customization/bin-utils/README b/examples/customization/bin-utils/README new file mode 100644 index 000000000000..1352d93b2787 --- /dev/null +++ b/examples/customization/bin-utils/README @@ -0,0 +1,36 @@ +Files in this directory: + +o .lldbinit: + +An example lldb init file that imports the binutils.py module and adds the +following commands: 'itob' and 'utob'. + +o binutils.py: + +Python module which provides implementation for the 'itob' and 'utob' commands. + +o README: + +The file you are reading now. + +================================================================================ +The following terminal output shows an interaction with lldb using the .lldbinit +and the binutils.py files which are located in my HOME directory. The lldb init +file imports the utils Python module and adds the 'itob' and 'utob' commands. + +$ /Volumes/data/lldb/svn/trunk/build/Debug/lldb +(lldb) help itob +Convert the integer to print its two's complement representation. + args[0] (mandatory) is the integer to be converted + args[1] (mandatory) is the bit width of the two's complement representation + args[2] (optional) if specified, turns on verbose printing +Syntax: itob +(lldb) itob -5 4 + [1, 0, 1, 1] +(lldb) itob -5 32 v + 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1] +(lldb) utob 0xABCD 32 v + 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1] +(lldb) diff --git a/examples/customization/bin-utils/binutils.py b/examples/customization/bin-utils/binutils.py new file mode 100644 index 000000000000..313a354ec3a5 --- /dev/null +++ b/examples/customization/bin-utils/binutils.py @@ -0,0 +1,122 @@ +"Collection of tools for displaying bit representation of numbers.""" + +import StringIO + +def binary(n, width=None): + """ + Return a list of (0|1)'s for the binary representation of n where n >= 0. + If you specify a width, it must be > 0, otherwise it is ignored. The list + could be padded with 0 bits if width is specified. + """ + l = [] + if width and width <= 0: + width = None + while n > 0: + l.append(1 if n&1 else 0) + n = n >> 1 + + if width: + for i in range(width - len(l)): + l.append(0) + + l.reverse() + return l + +def twos_complement(n, width): + """ + Return a list of (0|1)'s for the binary representation of a width-bit two's + complement numeral system of an integer n which may be negative. + """ + val = 2**(width-1) + if n >= 0: + if n > (val-1): + return None + # It is safe to represent n with width-bits. + return binary(n, width) + + if n < 0: + if abs(n) > val: + return None + # It is safe to represent n (a negative int) with width-bits. + return binary(val*2 - abs(n)) + +# print binary(0xABCD) +# [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1] +# print binary(0x1F, 8) +# [0, 0, 0, 1, 1, 1, 1, 1] +# print twos_complement(-5, 4) +# [1, 0, 1, 1] +# print twos_complement(7, 4) +# [0, 1, 1, 1] +# print binary(7) +# [1, 1, 1] +# print twos_complement(-5, 64) +# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1] + +def positions(width): + """Helper function returning a list describing the bit positions. + Bit positions greater than 99 are truncated to 2 digits, for example, + 100 -> 00 and 127 -> 27.""" + return ['{0:2}'.format(i)[-2:] for i in reversed(range(width))] + + +def utob(debugger, command_line, result, dict): + """Convert the unsigned integer to print its binary representation. + args[0] (mandatory) is the unsigned integer to be converted + args[1] (optional) is the bit width of the binary representation + args[2] (optional) if specified, turns on verbose printing""" + args = command_line.split() + try: + n = int(args[0], 0) + width = None + if len(args) > 1: + width = int(args[1], 0) + if width < 0: + width = 0 + except: + print utob.__doc__ + return + + if len(args) > 2: + verbose = True + else: + verbose = False + + bits = binary(n, width) + if not bits: + print "insufficient width value: %d" % width + return + if verbose and width > 0: + pos = positions(width) + print ' '+' '.join(pos) + print ' %s' % str(bits) + +def itob(debugger, command_line, result, dict): + """Convert the integer to print its two's complement representation. + args[0] (mandatory) is the integer to be converted + args[1] (mandatory) is the bit width of the two's complement representation + args[2] (optional) if specified, turns on verbose printing""" + args = command_line.split() + try: + n = int(args[0], 0) + width = int(args[1], 0) + if width < 0: + width = 0 + except: + print itob.__doc__ + return + + if len(args) > 2: + verbose = True + else: + verbose = False + + bits = twos_complement(n, width) + if not bits: + print "insufficient width value: %d" % width + return + if verbose and width > 0: + pos = positions(width) + print ' '+' '.join(pos) + print ' %s' % str(bits) + diff --git a/examples/customization/import-python/README b/examples/customization/import-python/README new file mode 100644 index 000000000000..9122f8f46dcf --- /dev/null +++ b/examples/customization/import-python/README @@ -0,0 +1,40 @@ +Files in this directory: + +o importcmd.py: + +Python module which provides implementation for the 'import' command. + +o README: + +The file you are reading now. + +================================================================================ +The import command defined by importcmd.py can be used in LLDB to load a Python +module given its full pathname. +The command works by extending Python's sys.path lookup to include the path to +the module to be imported when required, and then going through the language +ordinary 'import' mechanism. In this respect, modules imported from LLDB command +line should not be distinguishable from those imported using the script interpreter. +The following terminal output shows an interaction with lldb using this new command. + +Enrico-Granatas-MacBook-Pro:Debug enricogranata$ ./lldb +(lldb) script import importcmd +(lldb) command script add import -f importcmd.pyimport_cmd +(lldb) import ../demo.py +(lldb) script demo.test_function('hello world') +I am a Python function that says hello world +(lldb) quit +Enrico-Granatas-MacBook-Pro:Debug enricogranata$ + +Of course, the commands to import the importcmd.py module and to define the import +command, can be included in the .lldbinit file to make this feature available at +debugger startup + +WARNING: The import command defined by importcmd.py is now obsolete +In TOT LLDB, you can say: +(lldb) command script import ../demo.py +(lldb) script demo.test_function('hello world') +I am a Python function that says hello world +(lldb) quit + +using the native "command script import" command, which offers a superset of what the import command provided by importcmd.py does diff --git a/examples/customization/import-python/importcmd.py b/examples/customization/import-python/importcmd.py new file mode 100644 index 000000000000..576a642d5a01 --- /dev/null +++ b/examples/customization/import-python/importcmd.py @@ -0,0 +1,31 @@ +import sys,os,lldb +def check_has_dir_in_path(dirname): + return sys.path.__contains__(dirname); + +def ensure_has_dir_in_path(dirname): + dirname = os.path.abspath(dirname) + if not (check_has_dir_in_path(dirname)): + sys.path.append(dirname); + +def do_import(debugger,modname): + if (len(modname) > 4 and modname[-4:] == '.pyc'): + modname = modname[:-4] + if (len(modname) > 3 and modname[-3:] == '.py'): + modname = modname[:-3] + debugger.HandleCommand("script import " + modname) + +def pyimport_cmd(debugger, args, result, dict): + """Import a Python module given its full path""" + print 'WARNING: obsolete feature - use native command "command script import"' + if args == "": + return "no module path given"; + if not (os.sep in args): + modname = args + ensure_has_dir_in_path('.') + else: + endofdir = args.rfind(os.sep) + modname = args[endofdir+1:] + args = args[0:endofdir] + ensure_has_dir_in_path(args) + do_import(debugger,modname) + return None diff --git a/examples/customization/pwd-cd-and-system/.lldbinit b/examples/customization/pwd-cd-and-system/.lldbinit new file mode 100644 index 000000000000..f477b5797bec --- /dev/null +++ b/examples/customization/pwd-cd-and-system/.lldbinit @@ -0,0 +1,7 @@ +script import os, sys +# So that ~/utils.py takes precedence. +script sys.path[:0] = [os.path.expanduser('~')] +script import utils +command alias pwd script print os.getcwd() +command script add -f utils.chdir cd +command script add -f utils.system system diff --git a/examples/customization/pwd-cd-and-system/README b/examples/customization/pwd-cd-and-system/README new file mode 100644 index 000000000000..1b67d0b09c01 --- /dev/null +++ b/examples/customization/pwd-cd-and-system/README @@ -0,0 +1,41 @@ +Files in this directory: + +o .lldbinit: + +An example lldb init file that imports the utils.py module and adds the +following commands: 'pwd', 'cd', and 'system'. + +o utils.py: + +Python module which provides implementation for the 'cd' and 'system' commands. + +o README: + +The file you are reading now. + +================================================================================ +The following terminal output shows an interaction with lldb using the .lldbinit +and the utils.py files which are located in my HOME directory. The lldb init +file imports the utils Python module and adds the 'pwd', 'cd', and 'system' +commands. + +Johnnys-MacBook-Pro:multiple_threads johnny$ pwd +/Volumes/data/lldb/svn/trunk/test/functionalities/watchpoint/multiple_threads +Johnnys-MacBook-Pro:multiple_threads johnny$ lldb +(lldb) pwd +/Volumes/data/lldb/svn/trunk/test/functionalities/watchpoint/multiple_threads +(lldb) cd .. +Current working directory: /Volumes/data/lldb/svn/trunk/test/functionalities/watchpoint +(lldb) help system + +Execute the command (a string) in a subshell. +Syntax: system +(lldb) system ls -l +total 0 +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 hello_watchlocation +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 hello_watchpoint +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 multiple_threads +drwxr-xr-x 7 johnny admin 238 Oct 11 17:24 watchpoint_commands + +retcode: 0 +(lldb) diff --git a/examples/customization/pwd-cd-and-system/utils.py b/examples/customization/pwd-cd-and-system/utils.py new file mode 100644 index 000000000000..e975e8869773 --- /dev/null +++ b/examples/customization/pwd-cd-and-system/utils.py @@ -0,0 +1,49 @@ +"""Utility for changing directories and execution of commands in a subshell.""" + +import os, shlex, subprocess + +# Store the previous working directory for the 'cd -' command. +class Holder: + """Holds the _prev_dir_ class attribute for chdir() function.""" + _prev_dir_ = None + + @classmethod + def prev_dir(cls): + return cls._prev_dir_ + + @classmethod + def swap(cls, dir): + cls._prev_dir_ = dir + +def chdir(debugger, args, result, dict): + """Change the working directory, or cd to ${HOME}. + You can also issue 'cd -' to change to the previous working directory.""" + new_dir = args.strip() + if not new_dir: + new_dir = os.path.expanduser('~') + elif new_dir == '-': + if not Holder.prev_dir(): + # Bad directory, not changing. + print "bad directory, not changing" + return + else: + new_dir = Holder.prev_dir() + + Holder.swap(os.getcwd()) + os.chdir(new_dir) + print "Current working directory: %s" % os.getcwd() + +def system(debugger, command_line, result, dict): + """Execute the command (a string) in a subshell.""" + args = shlex.split(command_line) + process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, error = process.communicate() + retcode = process.poll() + if output and error: + print "stdout=>\n", output + print "stderr=>\n", error + elif output: + print output + elif error: + print error + print "retcode:", retcode |