[Lldb-commits] [lldb] [lldb] add stop-at-main option to process launch (PR #67019)

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 25 15:01:10 PDT 2023


=?utf-8?q?José?= L. Junior <jljuniorpb at gmail.com>,
=?utf-8?q?José?= L. Junior <jljuniorpb at gmail.com>,
=?utf-8?q?José?= L. Junior <jljuniorpb at gmail.com>
Message-ID:
In-Reply-To: <llvm/llvm-project/pull/67019/lldb at github.com>


================
@@ -38,7 +38,18 @@ Status CommandOptionsProcessLaunch::SetOptionValue(
   case 's': // Stop at program entry point
     launch_info.GetFlags().Set(eLaunchFlagStopAtEntry);
     break;
-
+  case 'm': // Stop at main function
+  {
+    TargetSP target_sp =
+        execution_context ? execution_context->GetTargetSP() : TargetSP();
+    BreakpointSP bp_sp = target_sp->CreateBreakpoint(
+        nullptr, nullptr, "main", eFunctionNameTypeAuto, eLanguageTypeUnknown,
----------------
clayborg wrote:

The other options would be to ask the Platform.h layer to do this. We have platforms classes that abstract things different platforms. Example include PlatformLinux, PlatformDarwin (for macOS, watchOS, tvOS, and iOS), PlatformAndroid, etc. We could add a default implementation into the Platform.h/Platform.cpp file that would set a breakpoint at main. The idea would be we would add a virtual function to Platform.h like:
```
class Platform {
  /// Create a beakpoint at the user level starting function for a program.
  virtual BreakpointSP CreateStartBreakpoint(lldb_private::Target &target);
...
```
Then this could can grab the platform from the target and call this:
```
BreakpointSP bp_sp  = target->GetPlatform()->CreateStartBreakpoint();
```
That way new platforms can do the right thing and customize this. The default implementation in the Platform.cpp can just do what you did above:
```
BreakpointSP Platform::CreateStartBreakpoint(lldb_private::Target &target) {
  return target_sp->CreateBreakpoint(
        nullptr, nullptr, "main", eFunctionNameTypeAuto, eLanguageTypeUnknown,
        0, eLazyBoolNo, false, false);
```
and we could override this for Darwin (base class for all Apple platforms) to use the use my previously suggested code:
```
BreakpointSP PlatformDarwin::CreateStartBreakpoint(lldb_private::Target &target) {
  // On Darwin systems the object file's entry point address is the user function entry
  // point like "main". Using this kind of breakpoint is more efficient than looking up a
  // function by name as this avoids parsing the debug info or symbol tables.
  ModuleSP exe_module_sp = target.GetExecutableModule();
  lldb_private::Address entry_addr;
  if (exe_module_sp ) {
    ObjectFile *objfile = exe_module_sp ->GetObjectFile();
    if (objfile)
        entry_addr = objfile->GetEntryPointAddress();
  }
  if (entry_addr.IsValid())
    return target.CreateBreakpoint(entry_addr, /*internal=*/false, /*hardware=*/false);
  return BreakpointSP();
}
```

https://github.com/llvm/llvm-project/pull/67019


More information about the lldb-commits mailing list