| 1 |
from ptrace.tools import readBits, formatBits |
| 2 |
from ptrace.signames import signalName |
| 3 |
from ptrace.ctypes_tools import uint2int |
| 4 |
|
| 5 |
# From /usr/include/bits/mman.h (Ubuntu Feisty, i386) |
| 6 |
MMAP_PROT_BITMASK = ( |
| 7 |
(1, "PROT_READ"), |
| 8 |
(2, "PROT_WRITE"), |
| 9 |
(4, "PROT_EXEC"), |
| 10 |
(0x01000000, "PROT_GROWSDOWN"), |
| 11 |
(0x02000000, "PROT_GROWSUP"), |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
def formatMmapProt(argument): |
| 16 |
return formatBits(argument.value, MMAP_PROT_BITMASK, "PROT_NONE") |
| 17 |
|
| 18 |
|
| 19 |
# From /usr/include/bits/mman.h (Ubuntu Feisty, i386) |
| 20 |
ACCESS_MODE_BITMASK = ( |
| 21 |
(1, "X_OK"), |
| 22 |
(2, "W_OK"), |
| 23 |
(4, "R_OK"), |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def formatAccessMode(argument): |
| 28 |
return formatBits(argument.value, ACCESS_MODE_BITMASK, "F_OK") |
| 29 |
|
| 30 |
|
| 31 |
# From /usr/include/bits/fcntl.h (Ubuntu Feisty, i386) |
| 32 |
OPEN_FLAGS_BITMASK = [ |
| 33 |
(0o1, "O_WRONLY"), |
| 34 |
(0o2, "O_RDWR"), |
| 35 |
(0o100, "O_CREAT"), |
| 36 |
(0o200, "O_EXCL"), |
| 37 |
(0o400, "O_NOCTTY"), |
| 38 |
(0o1000, "O_TRUNC"), |
| 39 |
(0o2000, "O_APPEND"), |
| 40 |
(0o4000, "O_NONBLOCK"), |
| 41 |
(0o10000, "O_SYNC"), |
| 42 |
(0o20000, "O_ASYNC"), |
| 43 |
(0o40000, "O_DIRECT"), |
| 44 |
(0o100000, "O_LARGEFILE"), |
| 45 |
(0o200000, "O_DIRECTORY"), |
| 46 |
(0o400000, "O_NOFOLLOW"), |
| 47 |
(0o1000000, "O_NOATIME"), |
| 48 |
(0o2000000, "O_CLOEXEC"), |
| 49 |
(0o10000000, "O_PATH"), # Linux 2.6.39 |
| 50 |
(0o20200000, "O_TMPFILE"), # Linux 3.11 |
| 51 |
] |
| 52 |
|
| 53 |
|
| 54 |
def formatOpenFlags(argument): |
| 55 |
value = argument.value |
| 56 |
flags = readBits(int(value), OPEN_FLAGS_BITMASK) |
| 57 |
|
| 58 |
# Add default access mode if neither of the others are present. |
| 59 |
if not flags or flags[0] not in ("O_WRONLY", "O_RDWR"): |
| 60 |
flags.insert(0, "O_RDONLY") |
| 61 |
|
| 62 |
text = "|".join(flags) |
| 63 |
if value: |
| 64 |
text = "%s (%s)" % (text, oct(argument.value)) |
| 65 |
return text |
| 66 |
|
| 67 |
|
| 68 |
OPEN_MODE_BITMASK = [ |
| 69 |
(0o0400, "S_IRUSR"), |
| 70 |
(0o0200, "S_IWUSR"), |
| 71 |
(0o0100, "S_IXUSR"), |
| 72 |
(0o0040, "S_IRGRP"), |
| 73 |
(0o0020, "S_IWGRP"), |
| 74 |
(0o0010, "S_IXGRP"), |
| 75 |
(0o0004, "S_IROTH"), |
| 76 |
(0o0002, "S_IWOTH"), |
| 77 |
(0o0001, "S_IXOTH"), |
| 78 |
] |
| 79 |
|
| 80 |
|
| 81 |
def formatOpenMode(argument): |
| 82 |
value = argument.value |
| 83 |
flags = readBits(int(value), OPEN_MODE_BITMASK) |
| 84 |
|
| 85 |
text = "|".join(flags) |
| 86 |
|
| 87 |
# Overwrite text, add it depending on verbosity later |
| 88 |
if value: |
| 89 |
text = "%s" % oct(argument.value) |
| 90 |
return text |
| 91 |
|
| 92 |
|
| 93 |
CLONE_FLAGS_BITMASK = ( |
| 94 |
(0x00000100, "CLONE_VM"), |
| 95 |
(0x00000200, "CLONE_FS"), |
| 96 |
(0x00000400, "CLONE_FILES"), |
| 97 |
(0x00000800, "CLONE_SIGHAND"), |
| 98 |
(0x00002000, "CLONE_PTRACE"), |
| 99 |
(0x00004000, "CLONE_VFORK"), |
| 100 |
(0x00008000, "CLONE_PARENT"), |
| 101 |
(0x00010000, "CLONE_THREAD"), |
| 102 |
(0x00020000, "CLONE_NEWNS"), |
| 103 |
(0x00040000, "CLONE_SYSVSEM"), |
| 104 |
(0x00080000, "CLONE_SETTLS"), |
| 105 |
(0x00100000, "CLONE_PARENT_SETTID"), |
| 106 |
(0x00200000, "CLONE_CHILD_CLEARTID"), |
| 107 |
(0x00400000, "CLONE_DETACHED"), |
| 108 |
(0x00800000, "CLONE_UNTRACED"), |
| 109 |
(0x01000000, "CLONE_CHILD_SETTID"), |
| 110 |
(0x02000000, "CLONE_STOPPED"), |
| 111 |
(0x04000000, "CLONE_NEWUTS"), |
| 112 |
(0x08000000, "CLONE_NEWIPC"), |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
def formatCloneFlags(argument): |
| 117 |
flags = argument.value |
| 118 |
bits = readBits(flags, CLONE_FLAGS_BITMASK) |
| 119 |
signum = flags & 0xFF |
| 120 |
if signum: |
| 121 |
bits.insert(0, signalName(signum)) |
| 122 |
if bits: |
| 123 |
bits = "%s" % ("|".join(bits)) |
| 124 |
return "<%s> (%s)" % (bits, str(flags)) |
| 125 |
else: |
| 126 |
return str(flags) |
| 127 |
|
| 128 |
|
| 129 |
AT_FDCWD = -100 |
| 130 |
|
| 131 |
|
| 132 |
def formatDirFd(argument): |
| 133 |
value = uint2int(argument.value) |
| 134 |
return "AT_FDCWD" if value == AT_FDCWD else str(value) |