@@ -445,11 +445,16 @@ class PGLogger(bdb.Bdb):
445445 # for each module rather than all stdout going to a single stream
446446 def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalizer_func,
447447 disable_security_checks=False, crazy_mode=False,
448- custom_modules=None, separate_stdout_by_module=False):
448+ custom_modules=None, separate_stdout_by_module=False, probe_exprs=None ):
449449 bdb.Bdb.__init__(self)
450450 self.mainpyfile = ''
451451 self._wait_for_mainpyfile = 0
452452
453+ if probe_exprs:
454+ self.probe_exprs = probe_exprs
455+ else:
456+ self.probe_exprs = None
457+
453458 self.separate_stdout_by_module = separate_stdout_by_module
454459 self.stdout_by_module = {} # Key: module name, Value: StringIO faux-stdout
455460
@@ -1036,6 +1041,7 @@ def create_encoded_stack_entry(cur_frame):
10361041
10371042
10381043 # climb up until you find '<module>', which is (hopefully) the global scope
1044+ top_frame = None
10391045 while True:
10401046 cur_frame = self.stack[i][0]
10411047 cur_name = cur_frame.f_code.co_name
@@ -1061,6 +1067,8 @@ def create_encoded_stack_entry(cur_frame):
10611067 # stack to render should only be [foo, baz].
10621068 if cur_frame in self.frame_ordered_ids:
10631069 encoded_stack_locals.append(create_encoded_stack_entry(cur_frame))
1070+ if not top_frame:
1071+ top_frame = cur_frame
10641072 i -= 1
10651073
10661074 zombie_encoded_stack_locals = [create_encoded_stack_entry(e) for e in zombie_frames_to_render]
@@ -1069,7 +1077,8 @@ def create_encoded_stack_entry(cur_frame):
10691077 # encode in a JSON-friendly format now, in order to prevent ill
10701078 # effects of aliasing later down the line ...
10711079 encoded_globals = {}
1072- for (k, v) in get_user_globals(tos[0], at_global_scope=(self.curindex <= 1)).items():
1080+ cur_globals_dict = get_user_globals(tos[0], at_global_scope=(self.curindex <= 1))
1081+ for (k, v) in cur_globals_dict.items():
10731082 encoded_val = self.encoder.encode(v, self.get_parent_of_function)
10741083 encoded_globals[k] = encoded_val
10751084
@@ -1143,6 +1152,21 @@ def create_encoded_stack_entry(cur_frame):
11431152 e['unique_hash'] = hash_str
11441153
11451154
1155+ # handle probe_exprs *before* encoding the heap with self.encoder.get_heap
1156+ encoded_probe_vals = {}
1157+ if self.probe_exprs:
1158+ if top_frame: # are we in a function call?
1159+ top_frame_locals = get_user_locals(top_frame)
1160+ else:
1161+ top_frame_locals = {}
1162+ for e in self.probe_exprs:
1163+ try:
1164+ # evaluate it with globals + locals of the top frame ...
1165+ probe_val = eval(e, cur_globals_dict, top_frame_locals)
1166+ encoded_probe_vals[e] = self.encoder.encode(probe_val, self.get_parent_of_function)
1167+ except:
1168+ pass # don't encode the value if there's been an error
1169+
11461170 if self.show_only_outputs:
11471171 trace_entry = dict(line=lineno,
11481172 event=event_type,
@@ -1161,6 +1185,8 @@ def create_encoded_stack_entry(cur_frame):
11611185 stack_to_render=stack_to_render,
11621186 heap=self.encoder.get_heap(),
11631187 stdout=self.get_user_stdout())
1188+ if encoded_probe_vals:
1189+ trace_entry['probe_exprs'] = encoded_probe_vals
11641190
11651191 # optional column numbers for greater precision
11661192 # (only relevant in Py2crazy, a hacked CPython that supports column numbers)
@@ -1524,9 +1550,12 @@ def exec_script_str(script_str, raw_input_lst_json, options_json, finalizer_func
15241550# disables security check and returns the result of finalizer_func
15251551# WARNING: ONLY RUN THIS LOCALLY and never over the web, since
15261552# security checks are disabled
1527- def exec_script_str_local(script_str, raw_input_lst_json, cumulative_mode, heap_primitives, finalizer_func):
1553+ #
1554+ # [optional] probe_exprs is a list of strings representing
1555+ # expressions whose values to probe at each step (advanced)
1556+ def exec_script_str_local(script_str, raw_input_lst_json, cumulative_mode, heap_primitives, finalizer_func, probe_exprs=None):
15281557 # TODO: add py_crazy_mode option here too ...
1529- logger = PGLogger(cumulative_mode, heap_primitives, False, finalizer_func, disable_security_checks=True)
1558+ logger = PGLogger(cumulative_mode, heap_primitives, False, finalizer_func, disable_security_checks=True, probe_exprs=probe_exprs )
15301559
15311560 # TODO: refactor these NOT to be globals
15321561 global input_string_queue
0 commit comments