Skip to content

Commit 2542aaf

Browse files
author
git apple-llvm automerger
committed
Merge commit '2fbeabf59fc3' from apple/stable/20210107 into swift/main
2 parents b3707bf + 2fbeabf commit 2542aaf

File tree

21 files changed

+541
-296
lines changed

21 files changed

+541
-296
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
#include "lldb/Utility/StringList.h"
2525
#include "lldb/lldb-forward.h"
2626
#include "lldb/lldb-private.h"
27-
2827
#include <mutex>
29-
#include <stack>
3028

3129
namespace lldb_private {
3230
class CommandInterpreter;
@@ -247,7 +245,7 @@ class CommandInterpreter : public Broadcaster,
247245

248246
CommandInterpreter(Debugger &debugger, bool synchronous_execution);
249247

250-
~CommandInterpreter() override = default;
248+
~CommandInterpreter() override;
251249

252250
// These two functions fill out the Broadcaster interface:
253251

@@ -302,11 +300,10 @@ class CommandInterpreter : public Broadcaster,
302300
CommandReturnObject &result);
303301

304302
bool HandleCommand(const char *command_line, LazyBool add_to_history,
305-
const ExecutionContext &override_context,
306-
CommandReturnObject &result);
307-
308-
bool HandleCommand(const char *command_line, LazyBool add_to_history,
309-
CommandReturnObject &result);
303+
CommandReturnObject &result,
304+
ExecutionContext *override_context = nullptr,
305+
bool repeat_on_empty_command = true,
306+
bool no_context_switching = false);
310307

311308
bool WasInterrupted() const;
312309

@@ -315,7 +312,9 @@ class CommandInterpreter : public Broadcaster,
315312
/// \param[in] commands
316313
/// The list of commands to execute.
317314
/// \param[in,out] context
318-
/// The execution context in which to run the commands.
315+
/// The execution context in which to run the commands. Can be nullptr in
316+
/// which case the default
317+
/// context will be used.
319318
/// \param[in] options
320319
/// This object holds the options used to control when to stop, whether to
321320
/// execute commands,
@@ -325,21 +324,18 @@ class CommandInterpreter : public Broadcaster,
325324
/// safely,
326325
/// and failed with some explanation if we aborted executing the commands
327326
/// at some point.
328-
void HandleCommands(const StringList &commands,
329-
const ExecutionContext &context,
330-
const CommandInterpreterRunOptions &options,
331-
CommandReturnObject &result);
332-
333-
void HandleCommands(const StringList &commands,
334-
const CommandInterpreterRunOptions &options,
327+
void HandleCommands(const StringList &commands, ExecutionContext *context,
328+
CommandInterpreterRunOptions &options,
335329
CommandReturnObject &result);
336330

337331
/// Execute a list of commands from a file.
338332
///
339333
/// \param[in] file
340334
/// The file from which to read in commands.
341335
/// \param[in,out] context
342-
/// The execution context in which to run the commands.
336+
/// The execution context in which to run the commands. Can be nullptr in
337+
/// which case the default
338+
/// context will be used.
343339
/// \param[in] options
344340
/// This object holds the options used to control when to stop, whether to
345341
/// execute commands,
@@ -349,12 +345,8 @@ class CommandInterpreter : public Broadcaster,
349345
/// safely,
350346
/// and failed with some explanation if we aborted executing the commands
351347
/// at some point.
352-
void HandleCommandsFromFile(FileSpec &file, const ExecutionContext &context,
353-
const CommandInterpreterRunOptions &options,
354-
CommandReturnObject &result);
355-
356-
void HandleCommandsFromFile(FileSpec &file,
357-
const CommandInterpreterRunOptions &options,
348+
void HandleCommandsFromFile(FileSpec &file, ExecutionContext *context,
349+
CommandInterpreterRunOptions &options,
358350
CommandReturnObject &result);
359351

360352
CommandObject *GetCommandObjectForCommand(llvm::StringRef &command_line);
@@ -399,7 +391,12 @@ class CommandInterpreter : public Broadcaster,
399391

400392
Debugger &GetDebugger() { return m_debugger; }
401393

402-
ExecutionContext GetExecutionContext() const;
394+
ExecutionContext GetExecutionContext() {
395+
const bool thread_and_frame_only_if_stopped = true;
396+
return m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped);
397+
}
398+
399+
void UpdateExecutionContext(ExecutionContext *override_context);
403400

404401
lldb::PlatformSP GetPlatform(bool prefer_target_platform);
405402

@@ -586,10 +583,6 @@ class CommandInterpreter : public Broadcaster,
586583
StringList *descriptions = nullptr) const;
587584

588585
private:
589-
void OverrideExecutionContext(const ExecutionContext &override_context);
590-
591-
void RestoreExecutionContext();
592-
593586
Status PreprocessCommand(std::string &command);
594587

595588
void SourceInitFile(FileSpec file, CommandReturnObject &result);
@@ -628,9 +621,8 @@ class CommandInterpreter : public Broadcaster,
628621

629622
Debugger &m_debugger; // The debugger session that this interpreter is
630623
// associated with
631-
// Execution contexts that were temporarily set by some of HandleCommand*
632-
// overloads.
633-
std::stack<ExecutionContext> m_overriden_exe_contexts;
624+
ExecutionContextRef m_exe_ctx_ref; // The current execution context to use
625+
// when handling commands
634626
bool m_synchronous_execution;
635627
bool m_skip_lldbinit_files;
636628
bool m_skip_app_init_files;

lldb/source/API/SBCommandInterpreter.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,27 @@ lldb::ReturnStatus SBCommandInterpreter::HandleCommand(
171171
lldb::SBCommandReturnObject &, bool),
172172
command_line, override_context, result, add_to_history);
173173

174+
175+
ExecutionContext ctx, *ctx_ptr;
176+
if (override_context.get()) {
177+
ctx = override_context.get()->Lock(true);
178+
ctx_ptr = &ctx;
179+
} else
180+
ctx_ptr = nullptr;
181+
174182
result.Clear();
175183
if (command_line && IsValid()) {
176184
result.ref().SetInteractive(false);
177-
auto do_add_to_history = add_to_history ? eLazyBoolYes : eLazyBoolNo;
178-
if (override_context.get())
179-
m_opaque_ptr->HandleCommand(command_line, do_add_to_history,
180-
override_context.get()->Lock(true),
181-
result.ref());
182-
else
183-
m_opaque_ptr->HandleCommand(command_line, do_add_to_history,
184-
result.ref());
185+
m_opaque_ptr->HandleCommand(command_line,
186+
add_to_history ? eLazyBoolYes : eLazyBoolNo,
187+
result.ref(), ctx_ptr);
185188
} else {
186189
result->AppendError(
187190
"SBCommandInterpreter or the command line is not valid");
188191
result->SetStatus(eReturnStatusFailed);
189192
}
190193

194+
191195
return result.GetStatus();
192196
}
193197

@@ -215,14 +219,15 @@ void SBCommandInterpreter::HandleCommandsFromFile(
215219
}
216220

217221
FileSpec tmp_spec = file.ref();
218-
if (override_context.get())
219-
m_opaque_ptr->HandleCommandsFromFile(tmp_spec,
220-
override_context.get()->Lock(true),
221-
options.ref(),
222-
result.ref());
223-
224-
else
225-
m_opaque_ptr->HandleCommandsFromFile(tmp_spec, options.ref(), result.ref());
222+
ExecutionContext ctx, *ctx_ptr;
223+
if (override_context.get()) {
224+
ctx = override_context.get()->Lock(true);
225+
ctx_ptr = &ctx;
226+
} else
227+
ctx_ptr = nullptr;
228+
229+
m_opaque_ptr->HandleCommandsFromFile(tmp_spec, ctx_ptr, options.ref(),
230+
result.ref());
226231
}
227232

228233
int SBCommandInterpreter::HandleCompletion(

lldb/source/Breakpoint/BreakpointOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ bool BreakpointOptions::BreakpointOptionsCallbackFunction(
647647
options.SetPrintErrors(true);
648648
options.SetAddToHistory(false);
649649

650-
debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx,
650+
debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
651651
options, result);
652652
result.GetImmediateOutputStream()->Flush();
653653
result.GetImmediateErrorStream()->Flush();

lldb/source/Commands/CommandObjectCommands.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@ class CommandObjectCommandsSource : public CommandObjectParsed {
134134

135135
FileSpec cmd_file(command[0].ref());
136136
FileSystem::Instance().Resolve(cmd_file);
137+
ExecutionContext *exe_ctx = nullptr; // Just use the default context.
137138

138-
CommandInterpreterRunOptions options;
139139
// If any options were set, then use them
140140
if (m_options.m_stop_on_error.OptionWasSet() ||
141141
m_options.m_silent_run.OptionWasSet() ||
142142
m_options.m_stop_on_continue.OptionWasSet()) {
143+
// Use user set settings
144+
CommandInterpreterRunOptions options;
145+
143146
if (m_options.m_stop_on_continue.OptionWasSet())
144147
options.SetStopOnContinue(
145148
m_options.m_stop_on_continue.GetCurrentValue());
@@ -156,9 +159,14 @@ class CommandObjectCommandsSource : public CommandObjectParsed {
156159
options.SetEchoCommands(m_interpreter.GetEchoCommands());
157160
options.SetEchoCommentCommands(m_interpreter.GetEchoCommentCommands());
158161
}
159-
}
160162

161-
m_interpreter.HandleCommandsFromFile(cmd_file, options, result);
163+
m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
164+
} else {
165+
// No options were set, inherit any settings from nested "command source"
166+
// commands, or set to sane default settings...
167+
CommandInterpreterRunOptions options;
168+
m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
169+
}
162170
return result.Succeeded();
163171
}
164172

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,18 @@ void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
298298
options.SetAutoApplyFixIts(false);
299299
options.SetGenerateDebugInfo(false);
300300

301-
ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
302-
303-
// Get out before we start doing things that expect a valid frame pointer.
304-
if (exe_ctx.GetFramePtr() == nullptr)
301+
// We need a valid execution context with a frame pointer for this
302+
// completion, so if we don't have one we should try to make a valid
303+
// execution context.
304+
if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr)
305+
m_interpreter.UpdateExecutionContext(nullptr);
306+
307+
// This didn't work, so let's get out before we start doing things that
308+
// expect a valid frame pointer.
309+
if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr)
305310
return;
306311

312+
ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
307313
Target *exe_target = exe_ctx.GetTargetPtr();
308314
Target &target = exe_target ? *exe_target : GetDummyTarget();
309315

lldb/source/Commands/CommandObjectProcess.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
380380
return false;
381381
}
382382

383+
m_interpreter.UpdateExecutionContext(nullptr);
383384
StreamString stream;
384385
const auto error = target->Attach(m_options.attach_info, &stream);
385386
if (error.Success()) {

lldb/source/Commands/CommandObjectRegexCommand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
5353
// Pass in true for "no context switching". The command that called us
5454
// should have set up the context appropriately, we shouldn't have to
5555
// redo that.
56-
return m_interpreter.HandleCommand(new_command.c_str(),
57-
eLazyBoolCalculate, result);
56+
return m_interpreter.HandleCommand(
57+
new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
5858
}
5959
}
6060
result.SetStatus(eReturnStatusFailed);

lldb/source/Commands/CommandObjectSettings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,14 @@ class CommandObjectSettingsRead : public CommandObjectParsed {
469469
bool DoExecute(Args &command, CommandReturnObject &result) override {
470470
FileSpec file(m_options.m_filename);
471471
FileSystem::Instance().Resolve(file);
472+
ExecutionContext clean_ctx;
472473
CommandInterpreterRunOptions options;
473474
options.SetAddToHistory(false);
474475
options.SetEchoCommands(false);
475476
options.SetPrintResults(true);
476477
options.SetPrintErrors(true);
477478
options.SetStopOnError(false);
478-
m_interpreter.HandleCommandsFromFile(file, options, result);
479+
m_interpreter.HandleCommandsFromFile(file, &clean_ctx, options, result);
479480
return result.Succeeded();
480481
}
481482

lldb/source/Commands/CommandObjectWatchpointCommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ are no syntax errors may indicate that a function was declared but never called.
301301
options.SetPrintErrors(true);
302302
options.SetAddToHistory(false);
303303

304-
debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx,
304+
debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
305305
options, result);
306306
result.GetImmediateOutputStream()->Flush();
307307
result.GetImmediateErrorStream()->Flush();

lldb/source/Core/IOHandlerCursesGUI.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,8 @@ class Application {
13901390
ConstString broadcaster_class(
13911391
broadcaster->GetBroadcasterClass());
13921392
if (broadcaster_class == broadcaster_class_process) {
1393+
debugger.GetCommandInterpreter().UpdateExecutionContext(
1394+
nullptr);
13931395
m_update_screen = true;
13941396
continue; // Don't get any key, just update our view
13951397
}
@@ -1401,6 +1403,7 @@ class Application {
14011403
HandleCharResult key_result = m_window_sp->HandleChar(ch);
14021404
switch (key_result) {
14031405
case eKeyHandled:
1406+
debugger.GetCommandInterpreter().UpdateExecutionContext(nullptr);
14041407
m_update_screen = true;
14051408
break;
14061409
case eKeyNotHandled:

0 commit comments

Comments
 (0)