24#include "llvm/Support/Error.h"
25#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/FormatVariadic.h"
27#include "llvm/Support/JSON.h"
28#include "llvm/Support/raw_ostream.h"
53 static std::optional<Diagnostics> g_diagnostics;
65 if (!diagnostics_dir) {
66 stream <<
"unable to create diagnostic dir: "
67 <<
toString(diagnostics_dir.takeError()) <<
'\n';
71 return Dump(stream, *diagnostics_dir);
75 stream <<
"LLDB diagnostics will be written to " << dir.
GetPath() <<
"\n";
76 stream <<
"Please include the directory content when filing a bug report\n";
87 SmallString<128> diagnostics_dir;
89 sys::fs::createUniqueDirectory(
"diagnostics", diagnostics_dir);
91 return errorCodeToError(ec);
92 return FileSpec(diagnostics_dir.str());
99 return Error::success();
105 llvm::raw_fd_ostream stream(log_file.
GetPath(), ec, llvm::sys::fs::OF_None);
107 return errorCodeToError(ec);
109 return Error::success();
121 llvm::StringRef content,
122 std::vector<std::string> &files) {
125 llvm::raw_fd_ostream os(file.
GetPath(), ec, llvm::sys::fs::OF_Text);
130 llvm::sys::fs::setPermissions(file.
GetPath(), llvm::sys::fs::owner_read |
131 llvm::sys::fs::owner_write);
132 files.push_back(name.str());
148struct TriageCommand {
149 llvm::StringRef command;
150 Requires requirement;
158 {
"target list", Requires::Always},
159 {
"image list", Requires::Target},
160 {
"process status", Requires::Process},
161 {
"thread list", Requires::Process},
162 {
"thread backtrace all", Requires::Process},
163 {
"image lookup -va $pc", Requires::Frame},
164 {
"register read", Requires::Frame},
165 {
"frame variable", Requires::Frame},
169 switch (requirement) {
170 case Requires::Always:
172 case Requires::Target:
174 case Requires::Process:
176 case Requires::Frame:
179 llvm_unreachable(
"unhandled Requires");
182llvm::Expected<Diagnostics::Report>
187 llvm::sys::fs::setPermissions(dir.
GetPath(), llvm::sys::fs::owner_read |
188 llvm::sys::fs::owner_write |
189 llvm::sys::fs::owner_exe);
204 std::vector<std::string> &files) {
207 consumeError(std::move(
error));
209 files.push_back(
"diagnostics.log");
213 files.push_back(std::move(name));
219 std::vector<std::string> &files) {
224 raw_string_ostream os(str);
225 os << formatv(
"{0:2}", stats);
232 std::vector<std::string> &files) {
233 std::string snapshot;
237 snapshot += formatv(
"=== {0} ===\n", tc.command).str();
245 std::string os = HostInfo::GetTargetTriple().str();
253 os += formatv(
" platform={0}", platform_sp->GetName()).str();
254 VersionTuple version = platform_sp->GetOSVersion();
255 if (!version.empty())
256 os +=
" os=" + version.getAsString();
257 if (std::optional<std::string> build = platform_sp->GetOSBuildString())
258 os +=
" build=" + *build;
270 std::string invocation;
286 obj[
"attachments"] = json::Object{
static llvm::raw_ostream & error(Stream &strm)
static constexpr size_t g_num_log_messages
static constexpr TriageCommand g_triage_commands[]
static bool Available(Requires requirement, const ExecutionContext &exe_ctx)
static std::string CaptureCommand(Debugger &debugger, llvm::StringRef command)
static void WriteArtifact(const FileSpec &dir, llvm::StringRef name, llvm::StringRef content, std::vector< std::string > &files)
A command line argument class.
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
bool HandleCommand(const char *command_line, LazyBool add_to_history, const ExecutionContext &override_context, CommandReturnObject &result)
std::string GetErrorString(bool with_diagnostics=true) const
Return the errors as a string.
llvm::StringRef GetOutputString() const
static llvm::json::Value ReportStatistics(Debugger &debugger, Target *target, const lldb_private::StatisticsOptions &options)
Get metrics associated with one or all targets in a debugger in JSON format.
A class to manage flag bits.
CommandInterpreter & GetCommandInterpreter()
std::vector< std::string > CopyLogFilesToDirectory(const FileSpec &dir)
Copy this debugger's file-backed log files into the given directory, for inclusion in a diagnostics b...
RotatingLogHandler m_log_handler
bool Dump(llvm::raw_ostream &stream)
Write the diagnostic log into a directory and print a message to the given output stream.
static std::string GetInvocation()
static std::string GetHostDescription(const ExecutionContext &exe_ctx)
Scalars carried in the report rather than written as files.
llvm::Error DumpDiangosticsLog(const FileSpec &dir) const
void CollectLogs(Debugger &debugger, const FileSpec &dir, std::vector< std::string > &files)
Collect the individual parts of the bundle into dir, appending the name of each file to files as it i...
llvm::Error Create(const FileSpec &dir)
Write the in-memory diagnostic log into the given directory.
void Record(llvm::StringRef message)
Record a diagnostic message into the always-on, in-memory log.
static llvm::Expected< FileSpec > CreateUniqueDirectory()
Create a unique diagnostic directory.
static std::optional< Diagnostics > & InstanceImpl()
llvm::Expected< Report > Collect(Debugger &debugger, const ExecutionContext &exe_ctx, const FileSpec &dir)
Collect a full diagnostics bundle into dir and return its report.
static void CollectCommands(Debugger &debugger, const ExecutionContext &exe_ctx, const FileSpec &dir, std::vector< std::string > &files)
static Diagnostics & Instance()
static void CollectStatistics(Debugger &debugger, const ExecutionContext &exe_ctx, const FileSpec &dir, std::vector< std::string > &files)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
static lldb::pid_t GetCurrentProcessID()
Get the process ID for the calling process.
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
A plug-in interface definition class for debugging a process.
lldb::PlatformSP GetPlatform()
A class that represents a running process on the host machine.
const char * GetVersion()
Retrieves a string representing the complete LLDB version, which includes the lldb version number,...
llvm::json::Value toJSON(const Diagnostics::Report &report)
Render a diagnostics report as JSON, for diagnostics dump's terminal output.
std::string toString(FormatterBytecode::OpCodes op)
std::shared_ptr< lldb_private::Platform > PlatformSP
std::vector< std::string > files
The state a triager needs to make sense of a bug report.