| 1 |
from ctypes import sizeof |
| 2 |
from ptrace.ctypes_tools import formatUintHex16, formatUintHex32, formatWordHex |
| 3 |
from datetime import datetime, timedelta |
| 4 |
from os import getenv, access, X_OK, pathsep, getcwd |
| 5 |
from os.path import join as path_join, isabs, dirname, normpath |
| 6 |
|
| 7 |
|
| 8 |
def dumpRegs(log, regs): |
| 9 |
""" |
| 10 |
Dump all registers using log callback (write one line). |
| 11 |
""" |
| 12 |
width = max(len(name) for name, type in regs._fields_) |
| 13 |
name_format = "%% %us" % width |
| 14 |
for name, type in regs._fields_: |
| 15 |
value = getattr(regs, name) |
| 16 |
name = name_format % name |
| 17 |
if sizeof(type) == 32: |
| 18 |
value = formatUintHex32(value) |
| 19 |
elif sizeof(type) == 16: |
| 20 |
value = formatUintHex16(value) |
| 21 |
else: |
| 22 |
value = formatWordHex(value) |
| 23 |
log("%s = %s" % (name, value)) |
| 24 |
|
| 25 |
|
| 26 |
def readBits(value, bitmasks): |
| 27 |
""" |
| 28 |
Extract bits from the integer value using a list of bit masks. |
| 29 |
bitmasks is a list of tuple (mask, text). |
| 30 |
|
| 31 |
>>> bitmask = ( |
| 32 |
... (1, "exec"), |
| 33 |
... (2, "write"), |
| 34 |
... (4, "read")) |
| 35 |
... |
| 36 |
>>> readBits(5, bitmask) |
| 37 |
['exec', 'read'] |
| 38 |
>>> readBits(12, bitmask) |
| 39 |
['read', '8'] |
| 40 |
""" |
| 41 |
bitset = [] |
| 42 |
for mask, item in bitmasks: |
| 43 |
if not value & mask: |
| 44 |
continue |
| 45 |
bitset.append(item) |
| 46 |
value = value & ~mask |
| 47 |
if value: |
| 48 |
bitset.append(str(value)) |
| 49 |
return bitset |
| 50 |
|
| 51 |
|
| 52 |
def formatBits(value, bitmasks, empty_text=None, format_value=str): |
| 53 |
""" |
| 54 |
Format a value using a bitmask: see readBits() functions. |
| 55 |
|
| 56 |
>>> bitmask = ( |
| 57 |
... (1, "exec"), |
| 58 |
... (2, "write"), |
| 59 |
... (4, "read")) |
| 60 |
... |
| 61 |
>>> formatBits(5, bitmask, empty_text="no permission") |
| 62 |
'<exec|read> (5)' |
| 63 |
>>> formatBits(0, bitmask, empty_text="no permission") |
| 64 |
'no permission' |
| 65 |
""" |
| 66 |
|
| 67 |
orig_value = value |
| 68 |
text = readBits(value, bitmasks) |
| 69 |
if text: |
| 70 |
text = "%s" % ("|".join(text)) |
| 71 |
if value: |
| 72 |
text = "<%s> (%s)" % (text, format_value(orig_value)) |
| 73 |
return text |
| 74 |
else: |
| 75 |
if empty_text: |
| 76 |
return empty_text |
| 77 |
else: |
| 78 |
return str(value) |
| 79 |
|
| 80 |
|
| 81 |
LOCAL_TIMEZONE_OFFSET = datetime.fromtimestamp( |
| 82 |
0) - datetime.utcfromtimestamp(0) |
| 83 |
|
| 84 |
# Start of UNIX timestamp (Epoch): 1st January 1970 at 00:00 |
| 85 |
UNIX_TIMESTAMP_T0 = datetime(1970, 1, 1) |
| 86 |
|
| 87 |
|
| 88 |
def timestampUNIX(value, is_local): |
| 89 |
""" |
| 90 |
Convert an UNIX (32-bit) timestamp to datetime object. Timestamp value |
| 91 |
is the number of seconds since the 1st January 1970 at 00:00. Maximum |
| 92 |
value is 2147483647: 19 january 2038 at 03:14:07. |
| 93 |
|
| 94 |
May raise ValueError for invalid value: value have to be in 0..2147483647. |
| 95 |
|
| 96 |
>>> timestampUNIX(0, False) |
| 97 |
datetime.datetime(1970, 1, 1, 0, 0) |
| 98 |
>>> timestampUNIX(1154175644.37, False) |
| 99 |
datetime.datetime(2006, 7, 29, 12, 20, 44, 370000) |
| 100 |
""" |
| 101 |
timestamp = UNIX_TIMESTAMP_T0 + timedelta(seconds=value) |
| 102 |
if is_local: |
| 103 |
timestamp += LOCAL_TIMEZONE_OFFSET |
| 104 |
return timestamp |
| 105 |
|
| 106 |
|
| 107 |
def locateProgram(program): |
| 108 |
""" |
| 109 |
Locate a program using the PATH environment variable. Return the |
| 110 |
unchanged program value if it's not possible to get the full |
| 111 |
program path. |
| 112 |
""" |
| 113 |
if isabs(program): |
| 114 |
return program |
| 115 |
if dirname(program): |
| 116 |
# ./test => $PWD/./test |
| 117 |
# ../python => $PWD/../python |
| 118 |
program = path_join(getcwd(), program) |
| 119 |
program = normpath(program) |
| 120 |
return program |
| 121 |
paths = getenv('PATH') |
| 122 |
if not paths: |
| 123 |
return program |
| 124 |
for path in paths.split(pathsep): |
| 125 |
filename = path_join(path, program) |
| 126 |
if access(filename, X_OK): |
| 127 |
return filename |
| 128 |
return program |
| 129 |
|
| 130 |
|
| 131 |
def minmax(min_value, value, max_value): |
| 132 |
""" |
| 133 |
Restrict value to [min_value; max_value] |
| 134 |
|
| 135 |
>>> minmax(-2, -3, 10) |
| 136 |
-2 |
| 137 |
>>> minmax(-2, 27, 10) |
| 138 |
10 |
| 139 |
>>> minmax(-2, 0, 10) |
| 140 |
0 |
| 141 |
""" |
| 142 |
return min(max(min_value, value), max_value) |
| 143 |
|
| 144 |
|
| 145 |
def inverseDict(data): |
| 146 |
""" |
| 147 |
Inverse a dictionary. |
| 148 |
|
| 149 |
>>> inverseDict({"0x10": 16, "0x20": 32}) == {32: '0x20', 16: '0x10'} |
| 150 |
True |
| 151 |
""" |
| 152 |
result = {} |
| 153 |
for key, value in data.items(): |
| 154 |
result[value] = key |
| 155 |
return result |
| 156 |
|
| 157 |
|
| 158 |
def signal_to_exitcode(signum): |
| 159 |
""" |
| 160 |
Converts a signal number to an exit code. |
| 161 |
UNIX: https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html |
| 162 |
|
| 163 |
>>> import signal |
| 164 |
>>> signal_to_exitcode(signal.SIGQUIT) |
| 165 |
131 |
| 166 |
""" |
| 167 |
return 128 + signum |