• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_TOOLING_INSPECTOR_THREAD_STATE_H
17 #define PANDA_TOOLING_INSPECTOR_THREAD_STATE_H
18 
19 #include <memory>
20 #include <unordered_set>
21 #include <vector>
22 
23 #include "breakpoint.h"
24 #include "breakpoint_storage.h"
25 #include "common.h"
26 #include "debug_info_cache.h"
27 #include "runtime/tooling/debugger.h"
28 
29 #include "include/tooling/debug_interface.h"
30 #include "types/numeric_id.h"
31 #include "types/pause_on_exceptions_state.h"
32 
33 namespace ark::tooling::inspector {
34 
35 class DebuggableThread;
36 
37 class ThreadState final {
38 public:
ThreadState(EvaluationEngine & engine,BreakpointStorage & bpStorage)39     explicit ThreadState(EvaluationEngine &engine, BreakpointStorage &bpStorage)
40         : engine_(engine), bpStorage_(bpStorage)
41     {
42     }
43     ~ThreadState() = default;
44 
45     NO_COPY_SEMANTIC(ThreadState);
46     NO_MOVE_SEMANTIC(ThreadState);
47 
IsPaused()48     bool IsPaused() const
49     {
50         return paused_;
51     }
52 
53     std::vector<BreakpointId> GetBreakpointsByLocation(const PtLocation &location) const;
54 
55     void Reset();
56     void BreakOnStart();
57     void Continue();
58     void ContinueTo(std::unordered_set<PtLocation, HashLocation> locations);
59     void StepInto(std::unordered_set<PtLocation, HashLocation> locations);
60     void StepOver(std::unordered_set<PtLocation, HashLocation> locations);
61     void StepOut();
62     void Pause();
63     void SetSkipAllPauses(bool skip);
64     void SetMixedDebugEnabled(bool mixedDebugEnabled);
65     void SetPauseOnExceptions(PauseOnExceptionsState state);
66 
67     void OnException(bool uncaught);
68     void OnFramePop();
69     bool OnMethodEntry();
70     void OnSingleStep(const PtLocation &location, const char *sourceFile);
71 
GetPauseReason()72     PauseReason GetPauseReason() const
73     {
74         return pauseReason_;
75     }
76 
77 private:
78     enum class StepKind {
79         // Just continue execution
80         NONE,
81 
82         // Stop on the next step event. It is not reset to NONE during resetting the state on a new connection
83         BREAK_ON_START,
84 
85         // Continue execution until one of the specified locations is reached
86         CONTINUE_TO,
87 
88         // Stop on the next step event
89         PAUSE,
90 
91         // Continue execution until one of the locations, other than the current line, is reached
92         STEP_INTO,
93 
94         // Continue execution until the current frame is popped
95         STEP_OUT,
96 
97         // Continue execution until both:
98         // - The call frame is not a child to the starting position (e.g. we have not entered some method);
99         // - One of the locations, other than the current line, is reached.
100         STEP_OVER
101     };
102 
103 private:
104     bool ShouldStopAtBreakpoint(const PtLocation &location);
105 
106 private:
107     StepKind stepKind_ {StepKind::NONE};
108 
109     // The set of locations has different semantics for various kinds of stepping:
110     // - for CONTINUE_TO it contains the set of locations we should reach to end the step;
111     // - for STEP_INTO and STEP_OVER it contains the set of locations corresponding to the current line
112     //   (then, the locations we should leave to end the step).
113     std::unordered_set<PtLocation, HashLocation> stepLocations_;
114 
115     bool methodEntered_ {false};
116 
117     EvaluationEngine &engine_;
118     BreakpointStorage &bpStorage_;
119 
120     PauseOnExceptionsState pauseOnExceptionsState_ {PauseOnExceptionsState::NONE};
121 
122     bool paused_ {false};
123     bool skipAllPauses_ {false};
124     bool mixedDebugEnabled_ {false};
125     bool breakOnStart_ {false};
126     std::set<std::string_view> sourceFiles_;
127     PauseReason pauseReason_ {PauseReason::OTHER};
128 };
129 }  // namespace ark::tooling::inspector
130 
131 #endif  // PANDA_TOOLING_INSPECTOR_THREAD_STATE_H
132