[Lldb-commits] [lldb] [lldb] add command start (PR #67019)
via lldb-commits
lldb-commits at lists.llvm.org
Thu Sep 21 08:17:33 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
<details>
<summary>Changes</summary>
## Description
This pull request adds a new `start` command to LLDB, allowing users to launch a process and pause execution at either the `main` function or the first instruction.
## Motivation
The `start` command provides a convenient way to begin debugging a program by launching it and breaking at the desired entry point.
## Changes Made
- Added `CommandObjectStart` class to implement the `start` command.
- The `start` command accepts an optional argument to specify whether to pause at the `main` function or the first instruction.
- Registered the command in the CMakeLists.txt file and in the CommandInterpreter.
## Usage
`start` runs the program and pauses execution at main function
`start first` runs the program and pauses execution at the first instruction.
---
Full diff: https://github.com/llvm/llvm-project/pull/67019.diff
4 Files Affected:
- (modified) lldb/source/Commands/CMakeLists.txt (+1)
- (added) lldb/source/Commands/CommandObjectStart.cpp (+102)
- (added) lldb/source/Commands/CommandObjectStart.h (+32)
- (modified) lldb/source/Interpreter/CommandInterpreter.cpp (+2)
``````````diff
diff --git a/lldb/source/Commands/CMakeLists.txt b/lldb/source/Commands/CMakeLists.txt
index 6a36c5376d5c574..54c62e0f5284beb 100644
--- a/lldb/source/Commands/CMakeLists.txt
+++ b/lldb/source/Commands/CMakeLists.txt
@@ -30,6 +30,7 @@ add_lldb_library(lldbCommands NO_PLUGIN_DEPENDENCIES
CommandObjectSession.cpp
CommandObjectSettings.cpp
CommandObjectSource.cpp
+ CommandObjectStart.cpp
CommandObjectStats.cpp
CommandObjectTarget.cpp
CommandObjectThread.cpp
diff --git a/lldb/source/Commands/CommandObjectStart.cpp b/lldb/source/Commands/CommandObjectStart.cpp
new file mode 100644
index 000000000000000..7406143c50fec1b
--- /dev/null
+++ b/lldb/source/Commands/CommandObjectStart.cpp
@@ -0,0 +1,102 @@
+//===-- CommandObjectStart.cpp -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CommandObjectStart.h"
+#include "lldb/Interpreter/CommandObject.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+// Constructor for CommandObjectStart
+CommandObjectStart::CommandObjectStart(CommandInterpreter &interpreter)
+ : CommandObjectParsed(
+ interpreter, "start",
+ "Launches the process and pauses execution at main function",
+ "start args [optional args]") {
+ // Define command arguments
+ CommandArgumentData pause_location{eArgTypeName, eArgRepeatPlain};
+ m_arguments.push_back({pause_location});
+}
+
+CommandObjectStart::~CommandObjectStart() = default;
+
+// Execute the 'start' command
+bool CommandObjectStart::DoExecute(Args &command, CommandReturnObject &result) {
+ // Check if the 'first' subcommand is specified
+ bool pause_at_first_instruction = false;
+
+ if (command.GetArgumentCount() == 1 &&
+ strcmp(command.GetArgumentAtIndex(0), "first") == 0) {
+ pause_at_first_instruction = true;
+ }
+
+ // Get the current selected target
+ TargetSP target_sp = GetDebugger().GetSelectedTarget();
+ if (!target_sp) {
+ result.AppendError("No target selected.\n");
+ return false;
+ }
+
+ // Create the breakpoint at main or the first instruction
+ BreakpointSP bp_sp;
+ if (pause_at_first_instruction) {
+ ModuleSP exe_module_sp = target_sp->GetExecutableModule();
+ ObjectFile *object = exe_module_sp->GetObjectFile();
+ Address address = object->GetEntryPointAddress();
+
+ if (!address.IsValid()) {
+ result.AppendError("Failed to get the entry point address");
+ result.SetStatus(eReturnStatusFailed);
+ return false;
+ }
+
+ bp_sp = target_sp->CreateBreakpoint(address, false, false);
+
+ } else {
+ // Create a breakpoint at the main function
+ bp_sp = target_sp->CreateBreakpoint(
+ nullptr, nullptr, "main", eFunctionNameTypeAuto, eLanguageTypeUnknown,
+ 0, eLazyBoolNo, false, false);
+ }
+
+ if (!bp_sp) {
+ result.AppendError("Breakpoint creation failed.\n");
+ result.SetStatus(eReturnStatusFailed);
+ return false;
+ } else {
+ result.GetOutputStream().Printf("Breakpoint created%s.\n",
+ pause_at_first_instruction
+ ? " at first instruction"
+ : " in main function");
+ result.SetStatus(eReturnStatusSuccessFinishResult);
+ }
+
+ // Construct the process launch info
+ ProcessLaunchInfo launch_info;
+ launch_info.SetShell(HostInfo::GetDefaultShell());
+ ModuleSP exe_module_sp = target_sp->GetExecutableModule();
+ if (!exe_module_sp) {
+ result.AppendError("No executable module found.\n");
+ return false;
+ }
+
+ launch_info.SetExecutableFile(exe_module_sp->GetPlatformFileSpec(), true);
+ // Launch the process
+ StreamString stream;
+ Status error = target_sp->Launch(launch_info, &stream);
+
+ if (error.Success()) {
+ result.SetStatus(eReturnStatusSuccessFinishResult);
+ result.GetOutputStream().Printf("Process launched successfully.\n");
+ } else {
+ result.AppendErrorWithFormat("Process launch failed: %s\n",
+ error.AsCString());
+ return false;
+ }
+ return result.Succeeded();
+}
diff --git a/lldb/source/Commands/CommandObjectStart.h b/lldb/source/Commands/CommandObjectStart.h
new file mode 100644
index 000000000000000..8475a17c347f7be
--- /dev/null
+++ b/lldb/source/Commands/CommandObjectStart.h
@@ -0,0 +1,32 @@
+//===-- CommandObjectStart.h ---------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SOURCE_COMMANDS_COMMANDOBJECTStart_H
+#define LLDB_SOURCE_COMMANDS_COMMANDOBJECTStart_H
+
+#include "lldb/Core/Module.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/ProcessLaunchInfo.h"
+#include "lldb/Interpreter/CommandInterpreter.h"
+#include "lldb/Interpreter/CommandObject.h"
+#include "lldb/Interpreter/CommandObjectMultiword.h"
+#include "lldb/Interpreter/CommandReturnObject.h"
+#include "lldb/Symbol/SymbolContext.h"
+
+namespace lldb_private {
+class CommandObjectStart : public CommandObjectParsed {
+public:
+ CommandObjectStart(CommandInterpreter &interpreter);
+ ~CommandObjectStart() override;
+
+protected:
+ bool DoExecute(Args &args, CommandReturnObject &result) override;
+};
+} // namespace lldb_private
+
+#endif // LLDB_SOURCE_COMMANDS_COMMANDOBJECTStart_H
diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
index dcff53ff843f328..5d6ad7f0dbb1e8e 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -36,6 +36,7 @@
#include "Commands/CommandObjectSession.h"
#include "Commands/CommandObjectSettings.h"
#include "Commands/CommandObjectSource.h"
+#include "Commands/CommandObjectStart.h"
#include "Commands/CommandObjectStats.h"
#include "Commands/CommandObjectTarget.h"
#include "Commands/CommandObjectThread.h"
@@ -559,6 +560,7 @@ void CommandInterpreter::LoadCommandDictionary() {
REGISTER_COMMAND_OBJECT("settings", CommandObjectMultiwordSettings);
REGISTER_COMMAND_OBJECT("session", CommandObjectSession);
REGISTER_COMMAND_OBJECT("source", CommandObjectMultiwordSource);
+ REGISTER_COMMAND_OBJECT("start", CommandObjectStart);
REGISTER_COMMAND_OBJECT("statistics", CommandObjectStats);
REGISTER_COMMAND_OBJECT("target", CommandObjectMultiwordTarget);
REGISTER_COMMAND_OBJECT("thread", CommandObjectMultiwordThread);
``````````
</details>
https://github.com/llvm/llvm-project/pull/67019
More information about the lldb-commits
mailing list