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
11
12def in_source_file(source_files, step_info):
13    if not step_info.current_frame:
14        return False
15    if not step_info.current_location.path:
16        return False
17    if not os.path.exists(step_info.current_location.path):
18        return False
19    return any(
20        os.path.samefile(step_info.current_location.path, f) for f in source_files
21    )
22
23
24def have_hit_line(watch, loc):
25    if hasattr(watch, "on_line"):
26        return watch.on_line == loc.lineno
27    elif hasattr(watch, "_from_line"):
28        return watch._from_line <= loc.lineno and watch._to_line >= loc.lineno
29    elif watch.lineno == loc.lineno:
30        return True
31    return False
32
33
34def update_step_watches(step_info, watches, commands):
35    watch_cmds = ["DexUnreachable", "DexExpectStepOrder"]
36    towatch = chain.from_iterable(commands[x] for x in watch_cmds if x in commands)
37    try:
38        # Iterate over all watches of the types named in watch_cmds
39        for watch in towatch:
40            loc = step_info.current_location
41            if (
42                loc.path is not None
43                and os.path.exists(loc.path)
44                and os.path.samefile(watch.path, loc.path)
45                and have_hit_line(watch, loc)
46            ):
47                result = watch.eval(step_info)
48                step_info.watches.update(result)
49                break
50    except KeyError:
51        pass
52