[Lldb-commits] [PATCH] D106226: [lldb] Improve error message when "lldb attach" fails

APOORV SACHAN via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Sat Sep 4 15:06:00 PDT 2021


apoos-maximus updated this revision to Diff 370769.
apoos-maximus added a comment.

moved the error message building logic to Target::Attach()
where the ptrace policy related mesasge gets appended only
if the debugee process is running on LinuxOS


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106226/new/

https://reviews.llvm.org/D106226

Files:
  lldb/source/Commands/CommandObjectProcess.cpp
  lldb/source/Target/Target.cpp


Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -3118,6 +3118,16 @@
   return CreateTrace();
 }
 
+const char *fetchPtracePolicyIfApplicable(lldb::PlatformSP platform_sp) {
+  Platform *platform = platform_sp.get();
+
+  // read value of /proc/sys/kernel/yama/ptrace_scope
+  return "Could not attach to process. If your uid matches the uid of the "
+         "target process, \n"
+         "check the setting of /proc/sys/kernel/yama/ptrace_scope, \n"
+         "or try again as the root user. \n";
+}
+
 Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
   auto state = eStateInvalid;
   auto process_sp = GetProcessSP();
@@ -3188,9 +3198,14 @@
 
       if (state != eStateStopped) {
         const char *exit_desc = process_sp->GetExitDescription();
-        if (exit_desc)
-          error.SetErrorStringWithFormat("%s", exit_desc);
-        else
+        const char *ptrace_reason = "";
+        if (exit_desc) {
+          // If we are on a linux flavoured system
+          if (platform_sp->GetSystemArchitecture().GetTriple().isOSLinux()) {
+            ptrace_reason = fetchPtracePolicyIfApplicable(platform_sp);
+          }
+          error.SetErrorStringWithFormat("%s\n%s", exit_desc, ptrace_reason);
+        } else
           error.SetErrorString(
               "process did not stop (no such process or permission problem?)");
         process_sp->Destroy(false);
Index: lldb/source/Commands/CommandObjectProcess.cpp
===================================================================
--- lldb/source/Commands/CommandObjectProcess.cpp
+++ lldb/source/Commands/CommandObjectProcess.cpp
@@ -410,7 +410,7 @@
             "no error returned from Target::Attach, and target has no process");
       }
     } else {
-      result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
+      result.AppendErrorWithFormat("attach failed: %s", error.AsCString());
     }
 
     if (!result.Succeeded())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106226.370769.patch
Type: text/x-patch
Size: 2072 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210904/a3ba8188/attachment-0001.bin>


More information about the lldb-commits mailing list