summaryrefslogtreecommitdiffstats
path: root/lldb/examples/customization
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/examples/customization')
-rw-r--r--lldb/examples/customization/bin-utils/binutils.py19
-rw-r--r--lldb/examples/customization/import-python/importcmd.py57
-rw-r--r--lldb/examples/customization/pwd-cd-and-system/utils.py13
3 files changed, 54 insertions, 35 deletions
diff --git a/lldb/examples/customization/bin-utils/binutils.py b/lldb/examples/customization/bin-utils/binutils.py
index 313a354ec3a..576dcba02f7 100644
--- a/lldb/examples/customization/bin-utils/binutils.py
+++ b/lldb/examples/customization/bin-utils/binutils.py
@@ -2,6 +2,7 @@
import StringIO
+
def binary(n, width=None):
"""
Return a list of (0|1)'s for the binary representation of n where n >= 0.
@@ -12,7 +13,7 @@ def binary(n, width=None):
if width and width <= 0:
width = None
while n > 0:
- l.append(1 if n&1 else 0)
+ l.append(1 if n & 1 else 0)
n = n >> 1
if width:
@@ -22,14 +23,15 @@ def binary(n, width=None):
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)
+ val = 2**(width - 1)
if n >= 0:
- if n > (val-1):
+ if n > (val - 1):
return None
# It is safe to represent n with width-bits.
return binary(n, width)
@@ -38,7 +40,7 @@ def twos_complement(n, width):
if abs(n) > val:
return None
# It is safe to represent n (a negative int) with width-bits.
- return binary(val*2 - abs(n))
+ 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]
@@ -53,12 +55,13 @@ def twos_complement(n, width):
# 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.
@@ -88,9 +91,10 @@ def utob(debugger, command_line, result, dict):
return
if verbose and width > 0:
pos = positions(width)
- print ' '+' '.join(pos)
+ 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
@@ -117,6 +121,5 @@ def itob(debugger, command_line, result, dict):
return
if verbose and width > 0:
pos = positions(width)
- print ' '+' '.join(pos)
+ print ' ' + ' '.join(pos)
print ' %s' % str(bits)
-
diff --git a/lldb/examples/customization/import-python/importcmd.py b/lldb/examples/customization/import-python/importcmd.py
index 576a642d5a0..1d47ad2132f 100644
--- a/lldb/examples/customization/import-python/importcmd.py
+++ b/lldb/examples/customization/import-python/importcmd.py
@@ -1,31 +1,38 @@
-import sys,os,lldb
+import sys
+import os
+import lldb
+
+
def check_has_dir_in_path(dirname):
- return sys.path.__contains__(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);
+ 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 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
+ """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/lldb/examples/customization/pwd-cd-and-system/utils.py b/lldb/examples/customization/pwd-cd-and-system/utils.py
index e975e886977..6e3462e1c86 100644
--- a/lldb/examples/customization/pwd-cd-and-system/utils.py
+++ b/lldb/examples/customization/pwd-cd-and-system/utils.py
@@ -1,8 +1,12 @@
"""Utility for changing directories and execution of commands in a subshell."""
-import os, shlex, subprocess
+import os
+import shlex
+import subprocess
# Store the previous working directory for the 'cd -' command.
+
+
class Holder:
"""Holds the _prev_dir_ class attribute for chdir() function."""
_prev_dir_ = None
@@ -15,6 +19,7 @@ class Holder:
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."""
@@ -33,10 +38,14 @@ def chdir(debugger, args, result, dict):
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)
+ process = subprocess.Popen(
+ args,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
output, error = process.communicate()
retcode = process.poll()
if output and error:
OpenPOWER on IntegriCloud