1 //===-- JSONUtils.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 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_TOOLS_LLDB_VSCODE_JSONUTILS_H
10 #define LLDB_TOOLS_LLDB_VSCODE_JSONUTILS_H
12 #include "VSCodeForward.h"
13 #include "lldb/API/SBModule.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/JSON.h"
18 namespace lldb_vscode
{
20 /// Emplace a StringRef in a json::Object after enusring that the
21 /// string is valid UTF8. If not, first call llvm::json::fixUTF8
25 /// A JSON object that we will attempt to emplace the value in
28 /// The key to use when emplacing the value
31 /// The string to emplace
32 void EmplaceSafeString(llvm::json::Object
&obj
, llvm::StringRef key
,
35 /// Extract simple values as a string.
38 /// A JSON value to extract the string from.
41 /// A llvm::StringRef that contains the string value, or an empty
42 /// string if \a value isn't a string.
43 llvm::StringRef
GetAsString(const llvm::json::Value
&value
);
45 /// Extract the string value for the specified key from the
49 /// A JSON object that we will attempt to extract the value from
52 /// The key to use when extracting the value
55 /// A llvm::StringRef that contains the string value for the
56 /// specified \a key, or an empty string if there is no key that
57 /// matches or if the value is not a string.
58 llvm::StringRef
GetString(const llvm::json::Object
&obj
, llvm::StringRef key
);
59 llvm::StringRef
GetString(const llvm::json::Object
*obj
, llvm::StringRef key
);
61 /// Extract the unsigned integer value for the specified key from
62 /// the specified object.
65 /// A JSON object that we will attempt to extract the value from
68 /// The key to use when extracting the value
71 /// The unsigned integer value for the specified \a key, or
72 /// \a fail_value if there is no key that matches or if the
73 /// value is not an integer.
74 uint64_t GetUnsigned(const llvm::json::Object
&obj
, llvm::StringRef key
,
76 uint64_t GetUnsigned(const llvm::json::Object
*obj
, llvm::StringRef key
,
79 /// Extract the boolean value for the specified key from the
83 /// A JSON object that we will attempt to extract the value from
86 /// The key to use when extracting the value
89 /// The boolean value for the specified \a key, or \a fail_value
90 /// if there is no key that matches or if the value is not a
91 /// boolean value of an integer.
92 bool GetBoolean(const llvm::json::Object
&obj
, llvm::StringRef key
,
94 bool GetBoolean(const llvm::json::Object
*obj
, llvm::StringRef key
,
97 /// Extract the signed integer for the specified key from the
101 /// A JSON object that we will attempt to extract the value from
104 /// The key to use when extracting the value
107 /// The signed integer value for the specified \a key, or
108 /// \a fail_value if there is no key that matches or if the
109 /// value is not an integer.
110 int64_t GetSigned(const llvm::json::Object
&obj
, llvm::StringRef key
,
112 int64_t GetSigned(const llvm::json::Object
*obj
, llvm::StringRef key
,
115 /// Check if the specified key exists in the specified object.
118 /// A JSON object that we will attempt to extract the value from
121 /// The key to check for
124 /// \b True if the key exists in the \a obj, \b False otherwise.
125 bool ObjectContainsKey(const llvm::json::Object
&obj
, llvm::StringRef key
);
127 /// Extract an array of strings for the specified key from an object.
129 /// String values in the array will be extracted without any quotes
130 /// around them. Numbers and Booleans will be converted into
131 /// strings. Any NULL, array or objects values in the array will be
135 /// A JSON object that we will attempt to extract the array from
138 /// The key to use when extracting the value
141 /// An array of string values for the specified \a key, or
142 /// \a fail_value if there is no key that matches or if the
143 /// value is not an array or all items in the array are not
144 /// strings, numbers or booleans.
145 std::vector
<std::string
> GetStrings(const llvm::json::Object
*obj
,
146 llvm::StringRef key
);
148 /// Fill a response object given the request object.
150 /// The \a response object will get its "type" set to "response",
151 /// the "seq" set to zero, "response_seq" set to the "seq" value from
152 /// \a request, "command" set to the "command" from \a request,
153 /// and "success" set to true.
155 /// \param[in] request
156 /// The request object received from a call to VSCode::ReadJSON().
158 /// \param[in,out] response
159 /// An empty llvm::json::Object object that will be filled
160 /// in as noted in description.
161 void FillResponse(const llvm::json::Object
&request
,
162 llvm::json::Object
&response
);
164 /// Emplace the string value from an SBValue into the supplied object
165 /// using \a key as the key that will contain the value.
167 /// The value is what we will display in VS Code. Some SBValue objects
168 /// can have a value and/or a summary. If a value has both, we
169 /// combine the value and the summary into one string. If we only have a
170 /// value or summary, then that is considered the value. If there is
171 /// no value and no summary then the value is the type name followed by
172 /// the address of the type if it has an address.
176 /// A lldb::SBValue object to extract the string value from
179 /// \param[in] object
180 /// The object to place the value object into
184 /// The key name to use when inserting the value object we create
185 void SetValueForKey(lldb::SBValue
&v
, llvm::json::Object
&object
,
186 llvm::StringRef key
);
188 /// Converts \a bp to a JSON value and appends the first valid location to the
189 /// \a breakpoints array.
192 /// A LLDB breakpoint object which will get the first valid location
193 /// extracted and converted into a JSON object in the \a breakpoints array
195 /// \param[in] breakpoints
196 /// A JSON array that will get a llvm::json::Value for \a bp
199 /// \param[in] request_path
200 /// An optional source path to use when creating the "Source" object of this
201 /// breakpoint. If not specified, the "Source" object is created from the
202 /// breakpoint's address' LineEntry. It is useful to ensure the same source
203 /// paths provided by the setBreakpoints request are returned to the IDE.
205 /// \param[in] request_line
206 /// An optional line to use when creating the "Breakpoint" object to append.
207 /// It is used if the breakpoint has no valid locations.
208 /// It is useful to ensure the same line
209 /// provided by the setBreakpoints request are returned to the IDE as a
211 void AppendBreakpoint(lldb::SBBreakpoint
&bp
, llvm::json::Array
&breakpoints
,
212 llvm::Optional
<llvm::StringRef
> request_path
= llvm::None
,
213 llvm::Optional
<uint32_t> request_line
= llvm::None
);
215 /// Converts breakpoint location to a Visual Studio Code "Breakpoint"
218 /// A LLDB breakpoint object to convert into a JSON value
220 /// \param[in] request_path
221 /// An optional source path to use when creating the "Source" object of this
222 /// breakpoint. If not specified, the "Source" object is created from the
223 /// breakpoint's address' LineEntry. It is useful to ensure the same source
224 /// paths provided by the setBreakpoints request are returned to the IDE.
226 /// \param[in] request_line
227 /// An optional line to use when creating the resulting "Breakpoint" object.
228 /// It is used if the breakpoint has no valid locations.
229 /// It is useful to ensure the same line
230 /// provided by the setBreakpoints request are returned to the IDE as a
234 /// A "Breakpoint" JSON object with that follows the formal JSON
235 /// definition outlined by Microsoft.
237 CreateBreakpoint(lldb::SBBreakpoint
&bp
,
238 llvm::Optional
<llvm::StringRef
> request_path
= llvm::None
,
239 llvm::Optional
<uint32_t> request_line
= llvm::None
);
241 /// Converts a LLDB module to a VS Code DAP module for use in "modules" events.
243 /// \param[in] module
244 /// A LLDB module object to convert into a JSON value
247 /// A "Module" JSON object with that follows the formal JSON
248 /// definition outlined by Microsoft.
249 llvm::json::Value
CreateModule(lldb::SBModule
&module
);
251 /// Create a "Event" JSON object using \a event_name as the event name
253 /// \param[in] event_name
254 /// The string value to use for the "event" key in the JSON object.
257 /// A "Event" JSON object with that follows the formal JSON
258 /// definition outlined by Microsoft.
259 llvm::json::Object
CreateEventObject(const llvm::StringRef event_name
);
261 /// Create a "ExceptionBreakpointsFilter" JSON object as described in
262 /// the Visual Studio Code debug adaptor definition.
265 /// The exception breakpoint object to use
268 /// A "ExceptionBreakpointsFilter" JSON object with that follows
269 /// the formal JSON definition outlined by Microsoft.
271 CreateExceptionBreakpointFilter(const ExceptionBreakpoint
&bp
);
273 /// Create a "Scope" JSON object as described in the Visual Studio Code
274 /// debug adaptor definition.
277 /// The value to place into the "name" key
279 /// \param[in] variablesReference
280 /// The value to place into the "variablesReference" key
282 /// \param[in] namedVariables
283 /// The value to place into the "namedVariables" key
285 /// \param[in] expensive
286 /// The value to place into the "expensive" key
289 /// A "Scope" JSON object with that follows the formal JSON
290 /// definition outlined by Microsoft.
291 llvm::json::Value
CreateScope(const llvm::StringRef name
,
292 int64_t variablesReference
,
293 int64_t namedVariables
, bool expensive
);
295 /// Create a "Source" JSON object as described in the Visual Studio Code
296 /// debug adaptor definition.
298 /// \param[in] line_entry
299 /// The LLDB line table to use when populating out the "Source"
303 /// A "Source" JSON object with that follows the formal JSON
304 /// definition outlined by Microsoft.
305 llvm::json::Value
CreateSource(lldb::SBLineEntry
&line_entry
);
307 /// Create a "Source" object for a given source path.
309 /// \param[in] source_path
310 /// The path to the source to use when creating the "Source" object.
313 /// A "Source" JSON object that follows the formal JSON
314 /// definition outlined by Microsoft.
315 llvm::json::Value
CreateSource(llvm::StringRef source_path
);
317 /// Create a "Source" object for a given frame.
319 /// When there is no source file information for a stack frame, we will
320 /// create disassembly for a function and store a permanent
321 /// "sourceReference" that contains the textual disassembly for a
322 /// function along with address to line information. The "Source" object
323 /// that is created will contain a "sourceReference" that the VSCode
324 /// protocol can later fetch as text in order to display disassembly.
325 /// The PC will be extracted from the frame and the disassembly line
326 /// within the source referred to by "sourceReference" will be filled
330 /// The LLDB stack frame to use when populating out the "Source"
333 /// \param[out] disasm_line
334 /// The line within the "sourceReference" file that the PC from
335 /// \a frame matches.
338 /// A "Source" JSON object with that follows the formal JSON
339 /// definition outlined by Microsoft.
340 llvm::json::Value
CreateSource(lldb::SBFrame
&frame
, int64_t &disasm_line
);
342 /// Create a "StackFrame" object for a LLDB frame object.
344 /// This function will fill in the following keys in the returned
346 /// "id" - the stack frame ID as an integer
347 /// "name" - the function name as a string
348 /// "source" - source file information as a "Source" VSCode object
349 /// "line" - the source file line number as an integer
350 /// "column" - the source file column number as an integer
353 /// The LLDB stack frame to use when populating out the "StackFrame"
357 /// A "StackFrame" JSON object with that follows the formal JSON
358 /// definition outlined by Microsoft.
359 llvm::json::Value
CreateStackFrame(lldb::SBFrame
&frame
);
361 /// Create a "Thread" object for a LLDB thread object.
363 /// This function will fill in the following keys in the returned
365 /// "id" - the thread ID as an integer
366 /// "name" - the thread name as a string which combines the LLDB
367 /// thread index ID along with the string name of the thread
368 /// from the OS if it has a name.
370 /// \param[in] thread
371 /// The LLDB thread to use when populating out the "Thread"
375 /// A "Thread" JSON object with that follows the formal JSON
376 /// definition outlined by Microsoft.
377 llvm::json::Value
CreateThread(lldb::SBThread
&thread
);
379 /// Create a "StoppedEvent" object for a LLDB thread object.
381 /// This function will fill in the following keys in the returned
382 /// object's "body" object:
383 /// "reason" - With a valid stop reason enumeration string value
384 /// that Microsoft specifies
385 /// "threadId" - The thread ID as an integer
386 /// "description" - a stop description (like "breakpoint 12.3") as a
388 /// "preserveFocusHint" - a boolean value that states if this thread
389 /// should keep the focus in the GUI.
390 /// "allThreadsStopped" - set to True to indicate that all threads
391 /// stop when any thread stops.
393 /// \param[in] thread
394 /// The LLDB thread to use when populating out the "StoppedEvent"
398 /// A "StoppedEvent" JSON object with that follows the formal JSON
399 /// definition outlined by Microsoft.
400 llvm::json::Value
CreateThreadStopped(lldb::SBThread
&thread
, uint32_t stop_id
);
403 /// The variable name of \a value or a default placeholder.
404 const char *GetNonNullVariableName(lldb::SBValue value
);
406 /// VSCode can't display two variables with the same name, so we need to
407 /// distinguish them by using a suffix.
409 /// If the source and line information is present, we use it as the suffix.
410 /// Otherwise, we fallback to the variable address or register location.
411 std::string
CreateUniqueVariableNameForDisplay(lldb::SBValue v
,
412 bool is_name_duplicated
);
414 /// Create a "Variable" object for a LLDB thread object.
416 /// This function will fill in the following keys in the returned
418 /// "name" - the name of the variable
419 /// "value" - the value of the variable as a string
420 /// "type" - the typename of the variable as a string
421 /// "id" - a unique identifier for a value in case there are multiple
422 /// variables with the same name. Other parts of the VSCode
423 /// protocol refer to values by name so this can help
424 /// disambiguate such cases if a IDE passes this "id" value
426 /// "variablesReference" - Zero if the variable has no children,
427 /// non-zero integer otherwise which can be used to expand
429 /// "evaluateName" - The name of the variable to use in expressions
433 /// The LLDB value to use when populating out the "Variable"
436 /// \param[in] variablesReference
437 /// The variable reference. Zero if this value isn't structured
438 /// and has no children, non-zero if it does have children and
439 /// might be asked to expand itself.
442 /// A unique variable identifier to help in properly identifying
443 /// variables with the same name. This is an extension to the
446 /// \param[in] format_hex
447 /// It set to true the variable will be formatted as hex in
448 /// the "value" key value pair for the value of the variable.
450 /// \param[in] is_name_duplicated
451 /// Whether the same variable name appears multiple times within the same
452 /// context (e.g. locals). This can happen due to shadowed variables in
455 /// As VSCode doesn't render two of more variables with the same name, we
456 /// apply a suffix to distinguish duplicated variables.
459 /// A "Variable" JSON object with that follows the formal JSON
460 /// definition outlined by Microsoft.
461 llvm::json::Value
CreateVariable(lldb::SBValue v
, int64_t variablesReference
,
462 int64_t varID
, bool format_hex
,
463 bool is_name_duplicated
= false);
465 llvm::json::Value
CreateCompileUnit(lldb::SBCompileUnit unit
);
467 /// Create a runInTerminal reverse request object
469 /// \param[in] launch_request
470 /// The original launch_request object whose fields are used to construct
471 /// the reverse request object.
473 /// \param[in] debug_adaptor_path
474 /// Path to the current debug adaptor. It will be used to delegate the
475 /// launch of the target.
477 /// \param[in] comm_file
478 /// The fifo file used to communicate the with the target launcher.
481 /// A "runInTerminal" JSON object that follows the specification outlined by
484 CreateRunInTerminalReverseRequest(const llvm::json::Object
&launch_request
,
485 llvm::StringRef debug_adaptor_path
,
486 llvm::StringRef comm_file
);
488 /// Convert a given JSON object to a string.
489 std::string
JSONToString(const llvm::json::Value
&json
);
491 } // namespace lldb_vscode