[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
Mon Sep 6 09:02:38 PDT 2021


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

added logic to report ptrace related error only if ptrace policy 
was the actual reason for failure. i.e. by reading the value of
/proc/sys/kernel/yama/ptrace_scope file if it exists, and setting the
ptrace error only if the value is not 0.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D106226

Files:
  lldb/source/Target/Target.cpp


Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -3118,6 +3118,46 @@
   return CreateTrace();
 }
 
+const char *fetchPtracePolicyIfApplicable(lldb::PlatformSP platform_sp) {
+  FileSpec filespec =
+      FileSpec(llvm::StringRef("/proc/sys/kernel/yama/ptrace_scope"));
+  char ptrace_scope = '\0';
+  char *status_message = NULL;
+
+  // check if /proc/sys/kernel/yama/ptrace_scope file exists
+  if (platform_sp->GetFileExists(filespec)) {
+    Status fileOperationStatus = Status();
+    mode_t perms = lldb::eFilePermissionsUserRW |
+                   lldb::eFilePermissionsGroupRead |
+                   lldb::eFilePermissionsWorldRead;
+    lldb::user_id_t fd = platform_sp->OpenFile(
+        filespec, File::eOpenOptionReadOnly, perms, fileOperationStatus);
+
+    platform_sp->ReadFile(fd, 0, &ptrace_scope, sizeof(char),
+                          fileOperationStatus);
+    platform_sp->CloseFile(fd, fileOperationStatus);
+
+    // return ptrace policy message only if ptrace_scope value is not zero
+    // i.e. ptrace_scope is the reason behind attach fail
+    if (ptrace_scope != '0') {
+      status_message =
+          "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. ";
+    } else {
+      status_message = "Try again as the root user";
+    }
+  }
+
+  // simply return try as Root user if file doesn't exist
+  else {
+    status_message = "Try again as the root user";
+  }
+
+  return status_message;
+}
+
 Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
   auto state = eStateInvalid;
   auto process_sp = GetProcessSP();
@@ -3188,9 +3228,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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106226.370935.patch
Type: text/x-patch
Size: 2623 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210906/6c917c8a/attachment-0001.bin>


More information about the lldb-commits mailing list