Unregister the the rocm event handler
[deliverable/binutils-gdb.git] / gdb / rocm-tdep.c
index 8816df38fd8385cc37876f5ee096eac3eb44f5d9..4cd14a6b15dd1f7e7ea9c5dd54f263cfc3ea0922 100644 (file)
@@ -84,6 +84,9 @@ struct rocm_inferior_info
   /* True if commit_resume should all-start the GPU queues.  */
   bool commit_resume_all_start;
 
+  /* True is the inferior has exited.  */
+  bool has_exited{ false };
+
   std::unordered_map<decltype (amd_dbgapi_breakpoint_id_t::handle),
                      struct breakpoint *>
       breakpoint_map;
@@ -126,6 +129,7 @@ struct rocm_target_ops final : public target_ops
     return arch_stratum;
   }
 
+  void close () override;
   void mourn_inferior () override;
 
   void async (int enable) override;
@@ -201,7 +205,7 @@ static int rocm_event_pipe[2] = { -1, -1 };
 
 /* Flush the event pipe.  */
 
-void
+static void
 async_file_flush (void)
 {
   int ret;
@@ -262,6 +266,11 @@ get_amd_dbgapi_process_id (struct inferior *inferior)
   return get_rocm_inferior_info (inferior)->process_id;
 }
 
+static void
+rocm_breakpoint_re_set (struct breakpoint *b)
+{
+}
+
 static void
 rocm_breakpoint_check_status (struct bpstats *bs)
 {
@@ -367,40 +376,39 @@ rocm_target_ops::thread_name (thread_info *tp)
   if (!ptid_is_gpu (tp->ptid))
     return beneath ()->thread_name (tp);
 
-  amd_dbgapi_process_id_t process_id = get_amd_dbgapi_process_id (tp->inf);
-  amd_dbgapi_wave_id_t wave_id = get_amd_dbgapi_wave_id (tp->ptid);
-  amd_dbgapi_dispatch_id_t dispatch_id;
-  amd_dbgapi_global_address_t kernel_addr;
+  /* Return the process's comm value—that is, the command name associated with
+     the process.  */
 
-  if (amd_dbgapi_wave_get_info (process_id, wave_id,
-                                AMD_DBGAPI_WAVE_INFO_DISPATCH,
-                                sizeof (dispatch_id), &dispatch_id)
-          != AMD_DBGAPI_STATUS_SUCCESS
-      || amd_dbgapi_dispatch_get_info (
-             process_id, dispatch_id,
-             AMD_DBGAPI_DISPATCH_INFO_KERNEL_ENTRY_ADDRESS,
-             sizeof (kernel_addr), &kernel_addr)
-             != AMD_DBGAPI_STATUS_SUCCESS)
-    return NULL;
+  char comm_path[128];
+  xsnprintf (comm_path, sizeof (comm_path), "/proc/%ld/comm",
+             (long)tp->ptid.pid ());
 
-  struct bound_minimal_symbol msymbol
-      = lookup_minimal_symbol_by_pc_section (kernel_addr, nullptr);
+  gdb_file_up comm_file = gdb_fopen_cloexec (comm_path, "r");
+  if (!comm_file)
+    return nullptr;
 
-  if (msymbol.minsym != NULL)
-    {
-      static char buf[256];
-      char *endp;
+#if !defined(TASK_COMM_LEN)
+#define TASK_COMM_LEN 16 /* As defined in the kernel's sched.h.  */
+#endif
 
-      xsnprintf (buf, sizeof (buf), "%s", msymbol.minsym->print_name ());
+  static char comm_buf[TASK_COMM_LEN];
+  const char *comm_value;
 
-      /* Strip the arguments from the demangled function name.  */
-      if ((endp = strchr (buf, '(')))
-        *endp = '\0';
+  comm_value = fgets (comm_buf, sizeof (comm_buf), comm_file.get ());
+  comm_buf[sizeof (comm_buf) - 1] = '\0';
 
-      return buf;
+  /* Make sure there is no newline at the end.  */
+  if (comm_value)
+    {
+      for (int i = 0; i < sizeof (comm_buf); i++)
+        if (comm_buf[i] == '\n')
+          {
+            comm_buf[i] = '\0';
+            break;
+          }
     }
 
-  return NULL;
+  return comm_value;
 }
 
 std::string
@@ -475,7 +483,7 @@ rocm_target_ops::xfer_partial (enum target_object object, const char *annex,
                  AMD_DBGAPI_ARCHITECTURE_INFO_DEFAULT_GLOBAL_ADDRESS_SPACE,
                  sizeof (address_space_id), &address_space_id)
                  != AMD_DBGAPI_STATUS_SUCCESS)
-        error (_ ("Couldn't get default global address space"));
+        return TARGET_XFER_EOF;
 
       size_t len = requested_len;
       amd_dbgapi_status_t status;
@@ -488,12 +496,7 @@ rocm_target_ops::xfer_partial (enum target_object object, const char *annex,
             process_id, wave_id, 0, address_space_id, offset, &len, writebuf);
 
       if (status != AMD_DBGAPI_STATUS_SUCCESS)
-        {
-          if (status != AMD_DBGAPI_STATUS_ERROR_MEMORY_ACCESS)
-            error (_ ("amd_dbgapi_%s_memory failed (rc=%d"),
-                   readbuf ? "read" : "write", status);
-          return TARGET_XFER_EOF;
-        }
+        return TARGET_XFER_EOF;
 
       *xfered_len = len;
       return TARGET_XFER_OK;
@@ -657,11 +660,11 @@ rocm_target_ops::async (int enable)
     }
   else
     {
-      delete_file_handler (rocm_event_pipe[0]);
-
       if (rocm_event_pipe[0] == -1)
         return;
 
+      delete_file_handler (rocm_event_pipe[0]);
+
       ::close (rocm_event_pipe[0]);
       ::close (rocm_event_pipe[1]);
       rocm_event_pipe[0] = -1;
@@ -897,6 +900,14 @@ rocm_target_ops::stopped_by_sw_breakpoint ()
   return software_breakpoint_inserted_here_p (regcache->aspace (), bkpt_pc);
 }
 
+void
+rocm_target_ops::close ()
+{
+  /* Unregister from the event loop.  */
+  async (0);
+  beneath ()->close ();
+}
+
 void
 rocm_target_ops::mourn_inferior ()
 {
@@ -1038,16 +1049,23 @@ rocm_target_ops::update_thread_list ()
 {
   for (inferior *inf : all_inferiors ())
     {
-      amd_dbgapi_process_id_t process_id = get_amd_dbgapi_process_id (inf);
+      amd_dbgapi_process_id_t process_id;
       amd_dbgapi_wave_id_t *wave_list;
       size_t count;
 
+      process_id = get_amd_dbgapi_process_id (inf);
+      if (process_id.handle == AMD_DBGAPI_PROCESS_NONE.handle)
+        {
+          /* The inferior may not be attached yet.  */
+          continue;
+        }
+
       amd_dbgapi_changed_t changed;
       amd_dbgapi_status_t status;
       if ((status
            = amd_dbgapi_wave_list (process_id, &count, &wave_list, &changed))
           != AMD_DBGAPI_STATUS_SUCCESS)
-        error (_ ("amd_dbgapi_wave_list failed (rc=%d"), status);
+        error (_ ("amd_dbgapi_wave_list failed (rc=%d)"), status);
 
       if (changed == AMD_DBGAPI_CHANGED_NO)
         continue;
@@ -1067,13 +1085,14 @@ rocm_target_ops::update_thread_list ()
       for (auto &&tid : threads)
         {
           ptid_t wave_ptid (inf->pid, 1, tid);
+          /* FIXME: is this really needed?
           amd_dbgapi_wave_state_t state;
 
           if (amd_dbgapi_wave_get_info (
                   process_id, get_amd_dbgapi_wave_id (wave_ptid),
                   AMD_DBGAPI_WAVE_INFO_STATE, sizeof (state), &state)
               != AMD_DBGAPI_STATUS_SUCCESS)
-            continue;
+            continue;*/
 
           add_thread_silent (wave_ptid);
           set_running (wave_ptid, 1);
@@ -1207,6 +1226,7 @@ static void
 rocm_target_inferior_exit (struct inferior *inf)
 {
   auto *info = get_rocm_inferior_info (inf);
+  info->has_exited = true;
 
   amd_dbgapi_deactivated.notify ();
 
@@ -1228,15 +1248,19 @@ static cli_style_option verbose_style ("rocm_verbose", ui_file_style::BLUE);
 
 static amd_dbgapi_callbacks_t dbgapi_callbacks = {
   /* allocate_memory.  */
-  .allocate_memory = xmalloc,
+  .allocate_memory = malloc,
 
   /* deallocate_memory.  */
-  .deallocate_memory = xfree,
+  .deallocate_memory = free,
 
   /* get_os_pid.  */
   .get_os_pid = [] (amd_dbgapi_client_process_id_t client_process_id,
                     pid_t *pid) -> amd_dbgapi_status_t {
     inferior *inf = static_cast<inferior *> (client_process_id);
+    struct rocm_inferior_info *info = get_rocm_inferior_info (inf);
+
+    if (info->has_exited)
+      return AMD_DBGAPI_STATUS_ERROR_PROCESS_EXITED;
 
     *pid = inf->pid;
     return AMD_DBGAPI_STATUS_SUCCESS;
@@ -1353,6 +1377,7 @@ static amd_dbgapi_callbacks_t dbgapi_callbacks = {
           {
             rocm_breakpoint_ops = bkpt_breakpoint_ops;
             rocm_breakpoint_ops.check_status = rocm_breakpoint_check_status;
+            rocm_breakpoint_ops.re_set = rocm_breakpoint_re_set;
           }
 
         auto it = info->breakpoint_map.find (breakpoint_id.handle);
@@ -1699,9 +1724,20 @@ extern initialize_file_ftype _initialize_rocm_tdep;
 void
 _initialize_rocm_tdep (void)
 {
+  /* Make sure the loaded debugger library version is greater than or equal to
+     the one used to build ROCgdb.  */
+  uint32_t major, minor, patch;
+  amd_dbgapi_get_version (&major, &minor, &patch);
+  if (major != AMD_DBGAPI_VERSION_MAJOR || minor < AMD_DBGAPI_VERSION_MINOR)
+    error (
+        _ ("amd-dbgapi library version mismatch, got %d.%d.%d, need %d.%d+"),
+        major, minor, patch, AMD_DBGAPI_VERSION_MAJOR,
+        AMD_DBGAPI_VERSION_MINOR);
+
   /* Initialize the ROCm Debug API.  */
-  if (amd_dbgapi_initialize (&dbgapi_callbacks) != AMD_DBGAPI_STATUS_SUCCESS)
-    return;
+  amd_dbgapi_status_t status = amd_dbgapi_initialize (&dbgapi_callbacks);
+  if (status != AMD_DBGAPI_STATUS_SUCCESS)
+    error (_ ("amd-dbgapi failed to initialize (rc=%d)"), status);
 
   /* Set the initial log level.  */
   amd_dbgapi_set_log_level (get_debug_amd_dbgapi_log_level ());
This page took 0.026903 seconds and 4 git commands to generate.