Skip to content

Commit d21cea4

Browse files
committed
Non-working checkpoint commit -- initial Python/ctypes scaffolding around DLLs. TracerConfig heap glue.
1 parent 31ffe22 commit d21cea4

File tree

15 files changed

+901
-31
lines changed

15 files changed

+901
-31
lines changed

PythonApp/lib/tracer/commands.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,91 @@
2424
InvariantAwareCommand,
2525
)
2626

27+
#===============================================================================
28+
# Imports
29+
#===============================================================================
30+
class TracerCommand(InvariantAwareCommand):
31+
32+
def prompt_for_directory(self, activity_name,
33+
success_filename,
34+
base_directory=None,
35+
directory_option_name=None):
36+
37+
if self.has_parent:
38+
raise CommandError(
39+
"--directory argument needs to be provided when "
40+
"--parent-pid is specified"
41+
)
42+
43+
from .path import join, isdir, isfile, basename
44+
from .util import is_win32, file_timestamps, yes_no_quit
45+
from os import listdir
46+
47+
out = self._out
48+
conf = self.conf
49+
verbose = self._verbose
50+
51+
success = success_filename
52+
base = base_directory
53+
if not base:
54+
base = conf.base_output_dir
55+
56+
optname = directory_option_name
57+
if not optname:
58+
optname = '--directory'
59+
60+
paths = [ join(base, p) for p in listdir(base) ]
61+
dirs = [ d for d in paths if isdir(d) ]
62+
dirs = [ d for d in dirs if not isfile(join(d, success)) ]
63+
latest_dirs = file_timestamps(dirs)
64+
if not latest_dirs:
65+
fmt = (
66+
"No suitable directories found within %s. Try the %s "
67+
"option to use a directory outside of the default area."
68+
)
69+
msg = fmt % (base, optname)
70+
return
71+
72+
verbose("Found %d output directories." % len(latest_dirs))
73+
74+
ostream = self.ostream
75+
estream = self.estream
76+
istream = self.istream
77+
fmt = "%s %s? [y/n/q] " % (activity_name, '%s')
78+
errmsg = "\nSorry, I didn't get that.\n"
79+
80+
found = None
81+
for latest_dir in latest_dirs:
82+
path = latest_dir.path
83+
name = basename(path)
84+
prompt = fmt % name
85+
while True:
86+
ostream.write(prompt)
87+
response = yes_no_quit(istream)
88+
if response:
89+
break
90+
estream.write(errmsg)
91+
92+
if response == 'y':
93+
found = path
94+
break
95+
elif response == 'q':
96+
out("Quitting.")
97+
self.returncode = 1
98+
return
99+
100+
if not found:
101+
fmt = (
102+
"Sorry, no more directories left. Try the %s option to use "
103+
"a directory outside of %s."
104+
)
105+
msg = fmt % (optname, base)
106+
out(msg)
107+
return
108+
109+
return path
110+
111+
27112
#===============================================================================
28113
# Commands
29114
#===============================================================================
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#===============================================================================
2+
# Imports
3+
#===============================================================================
4+
5+
from ctypes import (
6+
Union,
7+
Structure,
8+
9+
POINTER,
10+
CFUNCTYPE,
11+
)
12+
13+
from ..wintypes import (
14+
PVOID,
15+
SIZE_T,
16+
HANDLE,
17+
)
18+
19+
from .util import NullObject
20+
21+
#===============================================================================
22+
# Function Prototypes
23+
#===============================================================================
24+
25+
MALLOC = CFUNCTYPE(PVOID, [ PVOID, SIZE_T ])
26+
PMALLOC = POINTER(MALLOC)
27+
28+
CALLOC = CFUNCTYPE(PVOID, [ PVOID, SIZE_T, SIZE_T ])
29+
PCALLOC = POINTER(CALLOC)
30+
31+
REALLOC = CFUNCTYPE(PVOID, [ PVOID, PVOID, SIZE_T ])
32+
PREALLOC = POINTER(REALLOC)
33+
34+
FREE = CFUNCTYPE(None, [ PVOID, PVOID ])
35+
PFREE = POINTER(FREE)
36+
37+
FREE_POINTER = CFUNCTYPE(None, [ PVOID, PVOID ])
38+
PFREE_POINTER = POINTER(FREE_POINTER)
39+
40+
class ALLOCATOR(Structure):
41+
pass
42+
PALLOCATOR = POINTER(ALLOCATOR)
43+
PPALLOCATOR = POINTER(PALLOCATOR)
44+
45+
INITIALIZE_ALLOCATOR = CFUNCTYPE(BOOL, [ PALLOCATOR ])
46+
PINITIALIZE_ALLOCATOR = POINTER(INITIALIZE_ALLOCATOR)
47+
48+
DESTROY_ALLOCATOR = CFUNCTYPE(None, [ PALLOCATOR ])
49+
PDESTROY_ALLOCATOR = POINTER(DESTROY_ALLOCATOR)
50+
51+
class ALLOCATOR_FLAGS(Structure):
52+
_fields_ = [
53+
('IsTlsAware', ULONG, 1),
54+
('IsTlsRedirectionEnabled', ULONG, 1),
55+
]
56+
PALLOCATOR_FLAGS = POINTER(ALLOCATOR_FLAGS)
57+
58+
class _ALLOCATOR_CONTEXT1(Union):
59+
_fields_ = [
60+
('Context', PVOID),
61+
('Allocator', PALLOCATOR),
62+
]
63+
64+
class _ALLOCATOR_CONTEXT2(Union):
65+
_fields_ = [
66+
('Context2', PVOID),
67+
('HeapHandle', HANDLE),
68+
]
69+
70+
ALLOCATOR._fields_ = [
71+
('Context', _ALLOCATOR_CONTEXT),
72+
('Malloc', PMALLOC),
73+
('Calloc', PCALLOC),
74+
('Realloc', PREALLOC),
75+
('Free', PFREE),
76+
('FreePointer', PFREE_POINTER),
77+
('Initialize', PINITIALIZE_ALLOCATOR),
78+
('Destroy', PDESTROY_ALLOCATOR),
79+
('Context2', _ALLOCATOR_CONTEXT2),
80+
('Parent', PALLOCATOR),
81+
('ThreadId', ULONG),
82+
('TlsIndex', ULONG),
83+
('Flags', ALLOCATOR_FLAGS),
84+
('NumberOfThreads', ULONG),
85+
]
86+
87+
# vim:set ts=8 sw=4 sts=4 tw=80 ai et :

0 commit comments

Comments
 (0)