1 //===-- MICmnLLDBDebugSessionInfo.h -----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // Third party headers:
12 #include "lldb/API/SBDebugger.h"
13 #include "lldb/API/SBListener.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBTarget.h"
20 #include "MICmnBase.h"
21 #include "MICmnLLDBDebugSessionInfoVarObj.h"
22 #include "MICmnMIValueTuple.h"
23 #include "MIUtilMapIdToVariant.h"
24 #include "MIUtilSingletonBase.h"
25 #include "MIUtilThreadBaseStd.h"
28 class CMICmnLLDBDebugger;
30 class CMICmnMIValueTuple;
31 class CMICmnMIValueList;
34 //============================================================================
35 // Details: MI debug session object that holds debugging information between
36 // instances of MI commands executing their work and producing MI
37 // result records. Information/data is set by one or many commands then
38 // retrieved by the same or other subsequent commands.
39 // It primarily holds LLDB type objects.
42 class CMICmnLLDBDebugSessionInfo
44 public MI::ISingleton<CMICmnLLDBDebugSessionInfo> {
45 friend class MI::ISingleton<CMICmnLLDBDebugSessionInfo>;
50 //============================================================================
51 // Details: Break point information object. Used to easily pass information
53 // a break around and record break point information to be recalled
55 // other commands or LLDB event handling functions.
59 : m_id(0), m_bDisp(false), m_bEnabled(false), m_pc(0), m_nLine(0),
60 m_bHaveArgOptionThreadGrp(false), m_nTimes(0), m_bPending(false),
61 m_nIgnore(0), m_bCondition(false), m_bBrkPtThreadId(false),
62 m_nBrkPtThreadId(0) {}
64 MIuint m_id; // LLDB break point ID.
65 CMIUtilString m_strType; // Break point type.
66 bool m_bDisp; // True = "del", false = "keep".
67 bool m_bEnabled; // True = enabled, false = disabled break point.
68 lldb::addr_t m_pc; // Address number.
69 CMIUtilString m_fnName; // Function name.
70 CMIUtilString m_fileName; // File name text.
71 CMIUtilString m_path; // Full file name and path text.
72 MIuint m_nLine; // File line number.
73 bool m_bHaveArgOptionThreadGrp; // True = include MI field, false = do not
74 // include "thread-groups".
75 CMIUtilString m_strOptThrdGrp; // Thread group number.
76 MIuint m_nTimes; // The count of the breakpoint existence.
77 CMIUtilString m_strOrigLoc; // The name of the break point.
78 bool m_bPending; // True = the breakpoint has not been established yet,
79 // false = location found
80 MIuint m_nIgnore; // The number of time the breakpoint is run over before it
81 // is stopped on a hit
82 bool m_bCondition; // True = break point is conditional, use condition
83 // expression, false = no condition
84 CMIUtilString m_strCondition; // Break point condition expression
85 bool m_bBrkPtThreadId; // True = break point is specified to work with a
86 // specific thread, false = no specified thread given
88 m_nBrkPtThreadId; // Restrict the breakpoint to the specified thread-id
93 //++ ===================================================================
94 // Details: The type of variable used by MIResponseFormVariableInfo family
98 eVariableType_InScope = (1u << 0), // In scope only.
99 eVariableType_Statics = (1u << 1), // Statics.
100 eVariableType_Locals = (1u << 2), // Locals.
101 eVariableType_Arguments = (1u << 3) // Arguments.
104 //++ ===================================================================
105 // Details: Determine the information that should be shown by using
106 // MIResponseFormVariableInfo family functions.
108 enum VariableInfoFormat_e {
109 eVariableInfoFormat_NoValues = 0,
110 eVariableInfoFormat_AllValues = 1,
111 eVariableInfoFormat_SimpleValues = 2
114 //++ ===================================================================
115 // Details: Determine the information that should be shown by using
116 // MIResponseFormThreadInfo family functions.
118 enum ThreadInfoFormat_e {
119 eThreadInfoFormat_NoFrames,
120 eThreadInfoFormat_AllFrames
123 //++ ===================================================================
124 // Details: Determine the information that should be shown by using
125 // MIResponseFormFrameInfo family functions.
127 enum FrameInfoFormat_e {
128 eFrameInfoFormat_NoArguments,
129 eFrameInfoFormat_AllArguments,
130 eFrameInfoFormat_AllArgumentsInSimpleForm
135 typedef std::vector<uint32_t> VecActiveThreadId_t;
139 bool Initialize() override;
140 bool Shutdown() override;
142 // Variant type data which can be assigned and retrieved across all command
144 template <typename T>
145 bool SharedDataAdd(const CMIUtilString &vKey, const T &vData);
146 template <typename T>
147 bool SharedDataRetrieve(const CMIUtilString &vKey, T &vwData);
148 void SharedDataDestroy();
150 // Common command required functionality
151 bool AccessPath(const CMIUtilString &vPath, bool &vwbYesAccessible);
152 bool ResolvePath(const SMICmdData &vCmdData, const CMIUtilString &vPath,
153 CMIUtilString &vwrResolvedPath);
154 bool ResolvePath(const CMIUtilString &vstrUnknown,
155 CMIUtilString &vwrResolvedPath);
156 bool MIResponseFormFrameInfo(const lldb::SBThread &vrThread,
157 const MIuint vnLevel,
158 const FrameInfoFormat_e veFrameInfoFormat,
159 CMICmnMIValueTuple &vwrMiValueTuple);
160 bool MIResponseFormThreadInfo(const SMICmdData &vCmdData,
161 const lldb::SBThread &vrThread,
162 const ThreadInfoFormat_e veThreadInfoFormat,
163 CMICmnMIValueTuple &vwrMIValueTuple);
164 bool MIResponseFormVariableInfo(const lldb::SBFrame &vrFrame,
165 const MIuint vMaskVarTypes,
166 const VariableInfoFormat_e veVarInfoFormat,
167 CMICmnMIValueList &vwrMiValueList,
168 const MIuint vnMaxDepth = 10,
169 const bool vbMarkArgs = false);
170 void MIResponseFormBrkPtFrameInfo(const SBrkPtInfo &vrBrkPtInfo,
171 CMICmnMIValueTuple &vwrMiValueTuple);
172 bool MIResponseFormBrkPtInfo(const SBrkPtInfo &vrBrkPtInfo,
173 CMICmnMIValueTuple &vwrMiValueTuple);
174 bool GetBrkPtInfo(const lldb::SBBreakpoint &vBrkPt,
175 SBrkPtInfo &vrwBrkPtInfo) const;
176 bool RecordBrkPtInfo(const MIuint vnBrkPtId, const SBrkPtInfo &vrBrkPtInfo);
177 bool RecordBrkPtInfoGet(const MIuint vnBrkPtId,
178 SBrkPtInfo &vrwBrkPtInfo) const;
179 bool RecordBrkPtInfoDelete(const MIuint vnBrkPtId);
180 CMIUtilThreadMutex &GetSessionMutex() { return m_sessionMutex; }
181 lldb::SBDebugger &GetDebugger() const;
182 lldb::SBListener &GetListener() const;
183 lldb::SBTarget GetTarget() const;
184 lldb::SBProcess GetProcess() const;
188 // The following are available to all command instances
189 const MIuint m_nBrkPointCntMax;
190 VecActiveThreadId_t m_vecActiveThreadId;
191 lldb::tid_t m_currentSelectedThread;
193 // These are keys that can be used to access the shared data map
194 // Note: This list is expected to grow and will be moved and abstracted in the
196 const CMIUtilString m_constStrSharedDataKeyWkDir;
197 const CMIUtilString m_constStrSharedDataSolibPath;
198 const CMIUtilString m_constStrPrintCharArrayAsString;
199 const CMIUtilString m_constStrPrintExpandAggregates;
200 const CMIUtilString m_constStrPrintAggregateFieldNames;
204 typedef std::vector<CMICmnLLDBDebugSessionInfoVarObj> VecVarObj_t;
205 typedef std::map<MIuint, SBrkPtInfo> MapBrkPtIdToBrkPtInfo_t;
206 typedef std::pair<MIuint, SBrkPtInfo> MapPairBrkPtIdToBrkPtInfo_t;
210 /* ctor */ CMICmnLLDBDebugSessionInfo();
211 /* ctor */ CMICmnLLDBDebugSessionInfo(const CMICmnLLDBDebugSessionInfo &);
212 void operator=(const CMICmnLLDBDebugSessionInfo &);
214 bool GetVariableInfo(const lldb::SBValue &vrValue, const bool vbInSimpleForm,
215 CMIUtilString &vwrStrValue);
216 bool GetFrameInfo(const lldb::SBFrame &vrFrame, lldb::addr_t &vwPc,
217 CMIUtilString &vwFnName, CMIUtilString &vwFileName,
218 CMIUtilString &vwPath, MIuint &vwnLine);
219 bool GetThreadFrames(const SMICmdData &vCmdData, const MIuint vThreadIdx,
220 const FrameInfoFormat_e veFrameInfoFormat,
221 CMIUtilString &vwrThreadFrames);
223 MIResponseForVariableInfoInternal(const VariableInfoFormat_e veVarInfoFormat,
224 CMICmnMIValueList &vwrMiValueList,
225 const lldb::SBValueList &vwrSBValueList,
226 const MIuint vnMaxDepth,
227 const bool vbIsArgs, const bool vbMarkArgs);
232 /* dtor */ ~CMICmnLLDBDebugSessionInfo() override;
236 CMIUtilMapIdToVariant m_mapIdToSessionData; // Hold and retrieve key to value
237 // data available across all
239 VecVarObj_t m_vecVarObj; // Vector of session variable objects
240 MapBrkPtIdToBrkPtInfo_t m_mapBrkPtIdToBrkPtInfo;
241 CMIUtilThreadMutex m_sessionMutex;
245 // Details: Command instances can create and share data between other instances
247 // This function adds new data to the shared data. Using the same ID
249 // once replaces any previous matching data keys.
250 // Type: Template method.
251 // Args: T - The type of the object to be stored.
252 // vKey - (R) A non empty unique data key to retrieve the data by.
253 // vData - (R) Data to be added to the share.
254 // Return: MIstatus::success - Functional succeeded.
255 // MIstatus::failure - Functional failed.
258 template <typename T>
259 bool CMICmnLLDBDebugSessionInfo::SharedDataAdd(const CMIUtilString &vKey,
261 if (!m_mapIdToSessionData.Add<T>(vKey, vData)) {
262 SetErrorDescription(m_mapIdToSessionData.GetErrorDescription());
263 return MIstatus::failure;
266 return MIstatus::success;
270 // Details: Command instances can create and share data between other instances
272 // This function retrieves data from the shared data container.
274 // Args: T - The type of the object being retrieved.
275 // vKey - (R) A non empty unique data key to retrieve the data by.
276 // vData - (W) The data.
277 // Return: bool - True = data found, false = data not found or an error
278 // occurred trying to fetch.
281 template <typename T>
282 bool CMICmnLLDBDebugSessionInfo::SharedDataRetrieve(const CMIUtilString &vKey,
284 bool bDataFound = false;
286 if (!m_mapIdToSessionData.Get<T>(vKey, vwData, bDataFound)) {
287 SetErrorDescription(m_mapIdToSessionData.GetErrorDescription());
288 return MIstatus::failure;