[Lldb-commits] [lldb] r143252 - in /lldb/trunk: examples/customization/bin-utils/.lldbinit examples/customization/bin-utils/README examples/customization/bin-utils/binutils.py source/Commands/CommandObjectHelp.cpp
Johnny Chen
johnny.chen at apple.com
Fri Oct 28 16:30:28 PDT 2011
Author: johnny
Date: Fri Oct 28 18:30:28 2011
New Revision: 143252
URL: http://llvm.org/viewvc/llvm-project?rev=143252&view=rev
Log:
Add an example customization directory which uses a binutils.py module to provide
commands to print the binary representaion of an integer.
Added:
lldb/trunk/examples/customization/bin-utils/.lldbinit
lldb/trunk/examples/customization/bin-utils/README
Modified:
lldb/trunk/examples/customization/bin-utils/binutils.py
lldb/trunk/source/Commands/CommandObjectHelp.cpp
Added: lldb/trunk/examples/customization/bin-utils/.lldbinit
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/customization/bin-utils/.lldbinit?rev=143252&view=auto
==============================================================================
--- lldb/trunk/examples/customization/bin-utils/.lldbinit (added)
+++ lldb/trunk/examples/customization/bin-utils/.lldbinit Fri Oct 28 18:30:28 2011
@@ -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
Added: lldb/trunk/examples/customization/bin-utils/README
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/customization/bin-utils/README?rev=143252&view=auto
==============================================================================
--- lldb/trunk/examples/customization/bin-utils/README (added)
+++ lldb/trunk/examples/customization/bin-utils/README Fri Oct 28 18:30:28 2011
@@ -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)
Modified: lldb/trunk/examples/customization/bin-utils/binutils.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/customization/bin-utils/binutils.py?rev=143252&r1=143251&r2=143252&view=diff
==============================================================================
--- lldb/trunk/examples/customization/bin-utils/binutils.py (original)
+++ lldb/trunk/examples/customization/bin-utils/binutils.py Fri Oct 28 18:30:28 2011
@@ -1,5 +1,7 @@
"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.
@@ -51,3 +53,70 @@
# 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)
+
Modified: lldb/trunk/source/Commands/CommandObjectHelp.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectHelp.cpp?rev=143252&r1=143251&r2=143252&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectHelp.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectHelp.cpp Fri Oct 28 18:30:28 2011
@@ -182,7 +182,7 @@
const char *long_help = sub_cmd_obj->GetHelpLong();
if ((long_help != NULL)
&& (strlen (long_help) > 0))
- output_strm.Printf ("\n%s", long_help);
+ output_strm.Printf ("%s", long_help);
else if (sub_cmd_obj->WantsRawCommandString())
{
std::string help_text (sub_cmd_obj->GetHelp());
More information about the lldb-commits
mailing list