Skip to content

Commit 85393db

Browse files
authored
Merge pull request swiftlang#3412 from apple/lldb-Auto-enable-swift-health-logging-to-memory-buffer
[lldb] Add basic healthcheck command
2 parents 63a47a8 + 0379089 commit 85393db

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

lldb/include/lldb/Utility/Logging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
#include <cstdint>
1313

14+
#ifdef LLDB_ENABLE_SWIFT
15+
#include "llvm/ADT/StringRef.h"
16+
#endif
17+
1418
// Log Bits specific to logging in lldb
1519
#define LIBLLDB_LOG_PROCESS (1u << 1)
1620
#define LIBLLDB_LOG_THREAD (1u << 2)
@@ -64,6 +68,7 @@ Log *GetLogIfAnyCategoriesSet(uint32_t mask);
6468

6569
#ifdef LLDB_ENABLE_SWIFT
6670
Log *GetSwiftHealthLog();
71+
llvm::StringRef GetSwiftHealthLogData();
6772
#endif
6873

6974
void InitializeLldbChannel();

lldb/source/Commands/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_lldb_library(lldbCommands
1212
CommandObjectExpression.cpp
1313
CommandObjectFrame.cpp
1414
CommandObjectGUI.cpp
15+
CommandObjectHealthcheck.cpp
1516
CommandObjectHelp.cpp
1617
CommandObjectLanguage.cpp
1718
CommandObjectLog.cpp
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===-- CommandObjectHealthcheck.cpp --------------------------------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "CommandObjectHealthcheck.h"
10+
11+
#include "lldb/Host/HostInfo.h"
12+
#include "lldb/Interpreter/CommandReturnObject.h"
13+
#include "lldb/Utility/Log.h"
14+
#include "lldb/Host/FileSystem.h"
15+
#include "lldb/lldb-private.h"
16+
17+
using namespace lldb;
18+
using namespace lldb_private;
19+
20+
CommandObjectHealthcheck::CommandObjectHealthcheck(
21+
CommandInterpreter &interpreter)
22+
: CommandObjectParsed(interpreter, "healthcheck",
23+
"Show the LLDB debugger health check diagnostics.",
24+
"healthcheck") {}
25+
26+
bool CommandObjectHealthcheck::DoExecute(Args &args,
27+
CommandReturnObject &result) {
28+
#ifdef LLDB_ENABLE_SWIFT
29+
std::error_code err;
30+
llvm::SmallString<128> temp_path;
31+
int temp_fd = -1;
32+
if (FileSpec temp_file_spec = HostInfo::GetProcessTempDir()) {
33+
temp_file_spec.AppendPathComponent("lldb-healthcheck-%%%%%%.log");
34+
err = llvm::sys::fs::createUniqueFile(temp_file_spec.GetPath(), temp_fd,
35+
temp_path);
36+
} else {
37+
err = llvm::sys::fs::createTemporaryFile("lldb-healthcheck", "log", temp_fd,
38+
temp_path);
39+
}
40+
41+
if (temp_fd == -1) {
42+
result.AppendErrorWithFormat("could not write to temp file %s",
43+
err.message().c_str());
44+
return false;
45+
}
46+
47+
llvm::raw_fd_ostream temp_stream(temp_fd, true, true);
48+
llvm::StringRef data = GetSwiftHealthLogData();
49+
temp_stream << data;
50+
51+
result.AppendMessageWithFormat("Health check written to %s\n",
52+
temp_path.c_str());
53+
#endif
54+
return true;
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- CommandObjectHealthcheck.h ------------------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_SOURCE_COMMANDS_COMMANDOBJECTHEALTHCHECK_H
10+
#define LLDB_SOURCE_COMMANDS_COMMANDOBJECTHEALTHCHECK_H
11+
12+
#include "lldb/Interpreter/CommandObject.h"
13+
14+
namespace lldb_private {
15+
16+
class CommandObjectHealthcheck : public CommandObjectParsed {
17+
public:
18+
CommandObjectHealthcheck(CommandInterpreter &interpreter);
19+
20+
~CommandObjectHealthcheck() override = default;
21+
22+
protected:
23+
bool DoExecute(Args &args, CommandReturnObject &result) override;
24+
};
25+
26+
} // namespace lldb_private
27+
28+
#endif // LLDB_SOURCE_COMMANDS_COMMANDOBJECTHEALTHCHECK_H

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "Commands/CommandObjectFrame.h"
2121
#include "Commands/CommandObjectGUI.h"
2222
#include "Commands/CommandObjectHelp.h"
23+
#include "Commands/CommandObjectHealthcheck.h"
2324
#include "Commands/CommandObjectLanguage.h"
2425
#include "Commands/CommandObjectLog.h"
2526
#include "Commands/CommandObjectMemory.h"
@@ -511,6 +512,9 @@ void CommandInterpreter::LoadCommandDictionary() {
511512
REGISTER_COMMAND_OBJECT("frame", CommandObjectMultiwordFrame);
512513
REGISTER_COMMAND_OBJECT("gui", CommandObjectGUI);
513514
REGISTER_COMMAND_OBJECT("help", CommandObjectHelp);
515+
#ifdef LLDB_ENABLE_SWIFT
516+
REGISTER_COMMAND_OBJECT("healthcheck", CommandObjectHealthcheck);
517+
#endif
514518
REGISTER_COMMAND_OBJECT("log", CommandObjectLog);
515519
REGISTER_COMMAND_OBJECT("memory", CommandObjectMemory);
516520
REGISTER_COMMAND_OBJECT("platform", CommandObjectPlatform);

lldb/source/Utility/Logging.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@ static constexpr Log::Category g_swift_categories[] = {
5959

6060
static Log::Channel g_swift_log_channel(g_swift_categories, LIBLLDB_SWIFT_LOG_HEALTH);
6161

62+
static std::string g_swift_log_buffer;
63+
6264
#endif
6365

6466
void lldb_private::InitializeLldbChannel() {
6567
Log::Register("lldb", g_log_channel);
6668
#ifdef LLDB_ENABLE_SWIFT
6769
Log::Register("swift", g_swift_log_channel);
70+
71+
llvm::raw_null_ostream error_stream;
72+
Log::EnableLogChannel(
73+
std::make_shared<llvm::raw_string_ostream>(g_swift_log_buffer),
74+
LLDB_LOG_OPTION_THREADSAFE, "swift", {"health"}, error_stream);
6875
#endif
6976
}
7077

@@ -77,7 +84,13 @@ Log *lldb_private::GetLogIfAnyCategoriesSet(uint32_t mask) {
7784
}
7885

7986
#ifdef LLDB_ENABLE_SWIFT
87+
8088
Log *lldb_private::GetSwiftHealthLog() {
8189
return g_swift_log_channel.GetLogIfAny(LIBLLDB_SWIFT_LOG_HEALTH);
8290
}
91+
92+
llvm::StringRef lldb_private::GetSwiftHealthLogData() {
93+
return g_swift_log_buffer;
94+
}
95+
8396
#endif

0 commit comments

Comments
 (0)