#!/usr/bin/python # strace-decode v1.0 - decode hex output from 'strace -e (read|write)=all' # Copyright (c) 2008, John Morrissey # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Decodes output from Linux 'strace -e read=all' or 'strace -e write=all'. # Useful for obtaining raw data from a network connection or file I/O for, # say, manual replay during troubleshooting. # # | 00000 23 21 2f 75 73 72 2f 62 69 6e 2f 70 79 74 68 6f #!/usr/b in/pytho | # | 00010 6e 0a 0a 0a 0a 69 6d 70 6f 72 74 20 72 65 0a 69 n....imp ort re.i | # | 00020 6d 70 6f 72 74 20 73 79 73 0a 0a 66 6f 72 20 6c mport sy s..for l | # | 00030 69 6e 65 20 69 6e 20 73 79 73 2e 73 74 64 69 6e ine in s ys.stdin | # | 00040 3a 0a 09 6c 69 6e 65 20 3d 20 72 65 2e 73 75 62 :..line = re.sub | # | 00050 28 72 27 5e 5c 73 2b 5c 7c 5c 73 2b 5b 30 2d 39 (r'^\s+\ |\s+[0-9 | # | 00060 61 2d 66 5d 2b 5c 73 2b 27 2c 20 27 27 2c 20 6c a-f]+\s+ ', '', l | # | 00070 69 6e 65 29 0a 09 6c 69 6e 65 20 3d 20 72 65 2e ine)..li ne = re. | # | 00080 73 75 62 28 72 27 5c 73 2b 2e 7b 38 7d 20 2e 7b sub(r'\s +.{8} .{ | # | 00090 38 7d 20 5c 7c 5c 73 2a 24 27 2c 20 27 27 2c 20 8} \|\s* $', '', | # | 000a0 6c 69 6e 65 29 0a 0a 09 66 6f 72 20 63 68 61 72 line)... for char | # | 000b0 20 69 6e 20 72 65 2e 73 70 6c 69 74 28 72 27 5c in re.s plit(r'\ | # | 000c0 73 2b 27 2c 20 6c 69 6e 65 29 3a 0a 09 09 73 79 s+', lin e):...sy | # | 000d0 73 2e 73 74 64 6f 75 74 2e 77 72 69 74 65 28 63 s.stdout .write(c | # | 000e0 68 72 28 69 6e 74 28 63 68 61 72 2c 20 31 36 29 hr(int(c har, 16) | # | 000f0 29 29 0a )). | import re import sys for line in sys.stdin: line = re.sub(r'^\s+\|\s+[0-9a-f]+\s+', '', line) line = re.sub(r'\s+.{8} .{8} \|\s*$', '', line) for char in re.split(r'\s+', line): sys.stdout.write(chr(int(char, 16)))