diff options
-rw-r--r-- | lldb/include/lldb/Core/Debugger.h | 5 | ||||
-rw-r--r-- | lldb/include/lldb/Core/Log.h | 6 | ||||
-rw-r--r-- | lldb/include/lldb/Interpreter/Args.h | 9 | ||||
-rw-r--r-- | lldb/source/API/SBDebugger.cpp | 13 | ||||
-rw-r--r-- | lldb/source/Commands/CommandObjectLog.cpp | 6 | ||||
-rw-r--r-- | lldb/source/Core/Debugger.cpp | 7 | ||||
-rw-r--r-- | lldb/source/Core/Log.cpp | 36 | ||||
-rw-r--r-- | lldb/tools/lldb-server/LLDBServerUtilities.cpp | 4 | ||||
-rw-r--r-- | lldb/unittests/Core/LogTest.cpp | 26 | ||||
-rw-r--r-- | lldb/unittests/Interpreter/TestArgs.cpp | 8 |
10 files changed, 75 insertions, 45 deletions
diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h index 262ea4a13e5..93f71250586 100644 --- a/lldb/include/lldb/Core/Debugger.h +++ b/lldb/include/lldb/Core/Debugger.h @@ -192,8 +192,9 @@ public: void SetCloseInputOnEOF(bool b); - bool EnableLog(const char *channel, const char **categories, - const char *log_file, uint32_t log_options, + bool EnableLog(llvm::StringRef channel, + llvm::ArrayRef<const char *> categories, + llvm::StringRef log_file, uint32_t log_options, Stream &error_stream); void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton); diff --git a/lldb/include/lldb/Core/Log.h b/lldb/include/lldb/Core/Log.h index 2def0241a04..fb30bb5f3d1 100644 --- a/lldb/include/lldb/Core/Log.h +++ b/lldb/include/lldb/Core/Log.h @@ -100,10 +100,12 @@ public: static bool EnableLogChannel(const std::shared_ptr<llvm::raw_ostream> &log_stream_sp, uint32_t log_options, llvm::StringRef channel, - const char **categories, Stream &error_stream); + llvm::ArrayRef<const char *> categories, + Stream &error_stream); static bool DisableLogChannel(llvm::StringRef channel, - const char **categories, Stream &error_stream); + llvm::ArrayRef<const char *> categories, + Stream &error_stream); static bool ListChannelCategories(llvm::StringRef channel, Stream &stream); diff --git a/lldb/include/lldb/Interpreter/Args.h b/lldb/include/lldb/Interpreter/Args.h index 3dcb4dbdd35..3e56198f74a 100644 --- a/lldb/include/lldb/Interpreter/Args.h +++ b/lldb/include/lldb/Interpreter/Args.h @@ -193,6 +193,15 @@ public: const char **GetConstArgumentVector() const; //------------------------------------------------------------------ + /// Gets the argument as an ArrayRef. Note that the return value does *not* + /// have a nullptr const char * at the end, as the size of the list is + /// embedded in the ArrayRef object. + //------------------------------------------------------------------ + llvm::ArrayRef<const char *> GetArgumentArrayRef() const { + return llvm::makeArrayRef(m_argv).drop_back(); + } + + //------------------------------------------------------------------ /// Appends a new argument to the end of the list argument list. /// /// @param[in] arg_cstr diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp index 97e6f7b518a..d0a16042d8e 100644 --- a/lldb/source/API/SBDebugger.cpp +++ b/lldb/source/API/SBDebugger.cpp @@ -1120,13 +1120,22 @@ SBTypeSynthetic SBDebugger::GetSyntheticForType(SBTypeNameSpecifier type_name) { } #endif // LLDB_DISABLE_PYTHON +static llvm::ArrayRef<const char *> GetCategoryArray(const char **categories) { + if (categories == nullptr) + return {}; + size_t len = 0; + while (categories[len] != nullptr) + ++len; + return llvm::makeArrayRef(categories, len); +} + bool SBDebugger::EnableLog(const char *channel, const char **categories) { if (m_opaque_sp) { uint32_t log_options = LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; StreamString errors; - return m_opaque_sp->EnableLog(channel, categories, nullptr, log_options, - errors); + return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "", + log_options, errors); } else return false; } diff --git a/lldb/source/Commands/CommandObjectLog.cpp b/lldb/source/Commands/CommandObjectLog.cpp index c10f868dea4..976096387c0 100644 --- a/lldb/source/Commands/CommandObjectLog.cpp +++ b/lldb/source/Commands/CommandObjectLog.cpp @@ -172,8 +172,8 @@ protected: else log_file[0] = '\0'; bool success = m_interpreter.GetDebugger().EnableLog( - channel.c_str(), args.GetConstArgumentVector(), log_file, - m_options.log_options, result.GetErrorStream()); + channel, args.GetArgumentArrayRef(), log_file, m_options.log_options, + result.GetErrorStream()); if (success) result.SetStatus(eReturnStatusSuccessFinishNoResult); else @@ -233,7 +233,7 @@ protected: Log::DisableAllLogChannels(&result.GetErrorStream()); result.SetStatus(eReturnStatusSuccessFinishNoResult); } else { - if (Log::DisableLogChannel(channel, args.GetConstArgumentVector(), + if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(), result.GetErrorStream())) result.SetStatus(eReturnStatusSuccessFinishNoResult); } diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp index 451c2e5513f..f24888eaf03 100644 --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -1239,8 +1239,9 @@ void Debugger::SetLoggingCallback(lldb::LogOutputCallback log_callback, m_log_callback_stream_sp.reset(new StreamCallback(log_callback, baton)); } -bool Debugger::EnableLog(const char *channel, const char **categories, - const char *log_file, uint32_t log_options, +bool Debugger::EnableLog(llvm::StringRef channel, + llvm::ArrayRef<const char *> categories, + llvm::StringRef log_file, uint32_t log_options, Stream &error_stream) { const bool should_close = true; const bool unbuffered = true; @@ -1251,7 +1252,7 @@ bool Debugger::EnableLog(const char *channel, const char **categories, // For now when using the callback mode you always get thread & timestamp. log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME; - } else if (log_file == nullptr || *log_file == '\0') { + } else if (log_file.empty()) { log_stream_sp = std::make_shared<llvm::raw_fd_ostream>( GetOutputFile()->GetFile().GetDescriptor(), !should_close, unbuffered); } else { diff --git a/lldb/source/Core/Log.cpp b/lldb/source/Core/Log.cpp index a69ae9b0ffe..ad4c3689663 100644 --- a/lldb/source/Core/Log.cpp +++ b/lldb/source/Core/Log.cpp @@ -59,27 +59,26 @@ static void ListCategories(Stream &stream, const ChannelMap::value_type &entry) } static uint32_t GetFlags(Stream &stream, const ChannelMap::value_type &entry, - const char **categories) { + llvm::ArrayRef<const char *> categories) { bool list_categories = false; uint32_t flags = 0; - for (size_t i = 0; categories[i]; ++i) { - if (llvm::StringRef("all").equals_lower(categories[i])) { + for (const char *category : categories) { + if (llvm::StringRef("all").equals_lower(category)) { flags |= UINT32_MAX; continue; } - if (llvm::StringRef("default").equals_lower(categories[i])) { + if (llvm::StringRef("default").equals_lower(category)) { flags |= entry.second.channel.default_flags; continue; } - auto cat = llvm::find_if(entry.second.channel.categories, - [&](const Log::Category &c) { - return c.name.equals_lower(categories[i]); - }); + auto cat = llvm::find_if( + entry.second.channel.categories, + [&](const Log::Category &c) { return c.name.equals_lower(category); }); if (cat != entry.second.channel.categories.end()) { flags |= cat->flag; continue; } - stream.Format("error: unrecognized log category '{0}'\n", categories[i]); + stream.Format("error: unrecognized log category '{0}'\n", category); list_categories = true; } if (list_categories) @@ -237,31 +236,32 @@ void Log::Unregister(llvm::StringRef name) { bool Log::EnableLogChannel( const std::shared_ptr<llvm::raw_ostream> &log_stream_sp, - uint32_t log_options, llvm::StringRef channel, const char **categories, - Stream &error_stream) { + uint32_t log_options, llvm::StringRef channel, + llvm::ArrayRef<const char *> categories, Stream &error_stream) { auto iter = g_channel_map->find(channel); if (iter == g_channel_map->end()) { error_stream.Format("Invalid log channel '{0}'.\n", channel); return false; } - uint32_t flags = categories && categories[0] - ? GetFlags(error_stream, *iter, categories) - : iter->second.channel.default_flags; + uint32_t flags = categories.empty() + ? iter->second.channel.default_flags + : GetFlags(error_stream, *iter, categories); iter->second.channel.Enable(iter->second.log, log_stream_sp, log_options, flags); return true; } -bool Log::DisableLogChannel(llvm::StringRef channel, const char **categories, +bool Log::DisableLogChannel(llvm::StringRef channel, + llvm::ArrayRef<const char *> categories, Stream &error_stream) { auto iter = g_channel_map->find(channel); if (iter == g_channel_map->end()) { error_stream.Format("Invalid log channel '{0}'.\n", channel); return false; } - uint32_t flags = categories && categories[0] - ? GetFlags(error_stream, *iter, categories) - : UINT32_MAX; + uint32_t flags = categories.empty() + ? UINT32_MAX + : GetFlags(error_stream, *iter, categories); iter->second.channel.Disable(flags); return true; } diff --git a/lldb/tools/lldb-server/LLDBServerUtilities.cpp b/lldb/tools/lldb-server/LLDBServerUtilities.cpp index e07087e00b2..bf7b555c35c 100644 --- a/lldb/tools/lldb-server/LLDBServerUtilities.cpp +++ b/lldb/tools/lldb-server/LLDBServerUtilities.cpp @@ -52,8 +52,8 @@ bool LLDBServerUtilities::SetupLogging(const std::string &log_file, channel_then_categories.Shift(); // Shift off the channel bool success = Log::EnableLogChannel( - log_stream_sp, log_options, channel.c_str(), - channel_then_categories.GetConstArgumentVector(), error_stream); + log_stream_sp, log_options, channel, + channel_then_categories.GetArgumentArrayRef(), error_stream); if (!success) { fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n", log_file.c_str(), channel_with_categories.str().c_str()); diff --git a/lldb/unittests/Core/LogTest.cpp b/lldb/unittests/Core/LogTest.cpp index 16670aafce4..cf4d9e00ec2 100644 --- a/lldb/unittests/Core/LogTest.cpp +++ b/lldb/unittests/Core/LogTest.cpp @@ -93,7 +93,7 @@ TEST(LogTest, Unregister) { llvm::llvm_shutdown_obj obj; Log::Register("chan", test_channel); EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO)); - const char *cat1[] = {"foo", nullptr}; + const char *cat1[] = {"foo"}; std::string message; std::shared_ptr<llvm::raw_string_ostream> stream_sp( new llvm::raw_string_ostream(message)); @@ -110,20 +110,20 @@ TEST_F(LogChannelTest, Enable) { std::shared_ptr<llvm::raw_string_ostream> stream_sp( new llvm::raw_string_ostream(message)); StreamString err; - EXPECT_FALSE(Log::EnableLogChannel(stream_sp, 0, "chanchan", nullptr, err)); + EXPECT_FALSE(Log::EnableLogChannel(stream_sp, 0, "chanchan", {}, err)); EXPECT_EQ("Invalid log channel 'chanchan'.\n", err.GetString()); err.Clear(); - EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", nullptr, err)); - EXPECT_EQ("", err.GetString()); + EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", {}, err)); + EXPECT_EQ("", err.GetString()) << "err: " << err.GetString().str(); EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO)); EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR)); - const char *cat2[] = {"bar", nullptr}; + const char *cat2[] = {"bar"}; EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", cat2, err)); EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR)); - const char *cat3[] = {"baz", nullptr}; + const char *cat3[] = {"baz"}; EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", cat3, err)); EXPECT_TRUE(err.GetString().contains("unrecognized log category 'baz'")) << "err: " << err.GetString().str(); @@ -137,7 +137,7 @@ TEST_F(LogChannelTest, EnableOptions) { new llvm::raw_string_ostream(message)); StreamString err; EXPECT_TRUE(Log::EnableLogChannel(stream_sp, LLDB_LOG_OPTION_VERBOSE, "chan", - nullptr, err)); + {}, err)); Log *log = test_channel.GetLogIfAll(FOO); ASSERT_NE(nullptr, log); @@ -146,7 +146,7 @@ TEST_F(LogChannelTest, EnableOptions) { TEST_F(LogChannelTest, Disable) { EXPECT_EQ(nullptr, test_channel.GetLogIfAll(FOO)); - const char *cat12[] = {"foo", "bar", nullptr}; + const char *cat12[] = {"foo", "bar"}; std::string message; std::shared_ptr<llvm::raw_string_ostream> stream_sp( new llvm::raw_string_ostream(message)); @@ -154,12 +154,12 @@ TEST_F(LogChannelTest, Disable) { EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", cat12, err)); EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR)); - const char *cat2[] = {"bar", nullptr}; + const char *cat2[] = {"bar"}; EXPECT_TRUE(Log::DisableLogChannel("chan", cat2, err)); EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO)); EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR)); - const char *cat3[] = {"baz", nullptr}; + const char *cat3[] = {"baz"}; EXPECT_TRUE(Log::DisableLogChannel("chan", cat3, err)); EXPECT_TRUE(err.GetString().contains("unrecognized log category 'baz'")) << "err: " << err.GetString().str(); @@ -167,7 +167,7 @@ TEST_F(LogChannelTest, Disable) { EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR)); err.Clear(); - EXPECT_TRUE(Log::DisableLogChannel("chan", nullptr, err)); + EXPECT_TRUE(Log::DisableLogChannel("chan", {}, err)); EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO | BAR)); } @@ -195,13 +195,13 @@ TEST_F(LogChannelTest, LogThread) { std::shared_ptr<llvm::raw_string_ostream> stream_sp( new llvm::raw_string_ostream(message)); StreamString err; - EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", nullptr, err)); + EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", {}, err)); Log *log = test_channel.GetLogIfAll(FOO); // Start logging on one thread. Concurrently, try disabling the log channel. std::thread log_thread([log] { LLDB_LOG(log, "Hello World"); }); - EXPECT_TRUE(Log::DisableLogChannel("chan", nullptr, err)); + EXPECT_TRUE(Log::DisableLogChannel("chan", {}, err)); log_thread.join(); // The log thread either managed to write to the log in time, or it didn't. In diff --git a/lldb/unittests/Interpreter/TestArgs.cpp b/lldb/unittests/Interpreter/TestArgs.cpp index 9dcf09d54ae..2aeed0f542b 100644 --- a/lldb/unittests/Interpreter/TestArgs.cpp +++ b/lldb/unittests/Interpreter/TestArgs.cpp @@ -169,6 +169,14 @@ TEST(ArgsTest, AppendArguments) { EXPECT_STREQ("4", args.GetArgumentAtIndex(3)); } +TEST(ArgsTest, GetArgumentArrayRef) { + Args args("foo bar"); + auto ref = args.GetArgumentArrayRef(); + ASSERT_EQ(2u, ref.size()); + EXPECT_STREQ("foo", ref[0]); + EXPECT_STREQ("bar", ref[1]); +} + TEST(ArgsTest, StringToBoolean) { bool success = false; EXPECT_TRUE(Args::StringToBoolean(llvm::StringRef("true"), false, nullptr)); |