1# DExTer : Debugging Experience Tester
2# ~~~~~~   ~         ~~         ~   ~~
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
8import os
9from itertools import chain
10
11def in_source_file(source_files, step_info):
12    if not step_info.current_frame:
13        return False
14    if not step_info.current_location.path:
15        return False
16    if not os.path.exists(step_info.current_location.path):
17        return False
18    return any(os.path.samefile(step_info.current_location.path, f) \
19               for f in source_files)
20
21def have_hit_line(watch, loc):
22  if hasattr(watch, '_on_line'):
23    return watch._on_line == loc.lineno
24  elif hasattr(watch, '_from_line'):
25    return watch._from_line <= loc.lineno and watch._to_line >= loc.lineno
26  elif watch.lineno == loc.lineno:
27    return True
28  return False
29
30def update_step_watches(step_info, watches, commands):
31    watch_cmds = ['DexUnreachable', 'DexExpectStepOrder']
32    towatch = chain.from_iterable(commands[x]
33                                  for x in watch_cmds
34                                  if x in commands)
35    try:
36        # Iterate over all watches of the types named in watch_cmds
37        for watch in towatch:
38            loc = step_info.current_location
39            if (loc.path != None
40                    and os.path.exists(loc.path)
41                    and os.path.samefile(watch.path, loc.path)
42                    and have_hit_line(watch, loc)):
43                result = watch.eval(step_info)
44                step_info.watches.update(result)
45                break
46    except KeyError:
47        pass
48