Fix issues with multiple inferiors
[deliverable/binutils-gdb.git] / gdb / rocm-tdep.c
index af9d16bedb4202b02bb99ad778ab613b5bd01297..55b4c79d4500cc59162460431e89a4278a27116d 100644 (file)
@@ -1,7 +1,7 @@
 /* Target-dependent code for ROCm.
 
-   Copyright (C) 2019 Free Software Foundation, Inc.
-   Copyright (C) 2019 Advanced Micro Devices, Inc. All rights reserved.
+   Copyright (C) 2019-2020 Free Software Foundation, Inc.
+   Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
 
    This file is part of GDB.
 
@@ -76,10 +76,10 @@ struct rocm_notify_shared_library_info
 struct rocm_inferior_info
 {
   /* The amd_dbgapi_process_id for this inferior.  */
-  amd_dbgapi_process_id_t process_id;
+  amd_dbgapi_process_id_t process_id{ AMD_DBGAPI_PROCESS_NONE };
 
   /* The amd_dbgapi_notifier_t for this inferior.  */
-  amd_dbgapi_notifier_t notifier;
+  amd_dbgapi_notifier_t notifier{ -1 };
 
   /* True if commit_resume should all-start the GPU queues.  */
   bool commit_resume_all_start;
@@ -201,7 +201,7 @@ static int rocm_event_pipe[2] = { -1, -1 };
 
 /* Flush the event pipe.  */
 
-void
+static void
 async_file_flush (void)
 {
   int ret;
@@ -262,6 +262,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)
 {
@@ -350,13 +355,9 @@ rocm_target_ops::thread_alive (ptid_t ptid)
 
   /* Check that the wave_id is valid.  */
 
-  inferior *inf = find_inferior_ptid (ptid);
-  if (!inf)
-    return false;
-
   amd_dbgapi_wave_state_t state;
   return amd_dbgapi_wave_get_info (
-             get_amd_dbgapi_process_id (inf), get_amd_dbgapi_wave_id (ptid),
+             get_amd_dbgapi_process_id (), get_amd_dbgapi_wave_id (ptid),
              AMD_DBGAPI_WAVE_INFO_STATE, sizeof (state), &state)
          == AMD_DBGAPI_STATUS_SUCCESS;
 }
@@ -367,7 +368,7 @@ 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_process_id_t process_id = get_amd_dbgapi_process_id ();
   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;
@@ -429,9 +430,9 @@ rocm_target_ops::pid_to_str (ptid_t ptid)
              AMD_DBGAPI_WAVE_INFO_WAVE_NUMBER_IN_WORK_GROUP,
              sizeof (wave_in_group), &wave_in_group)
              != AMD_DBGAPI_STATUS_SUCCESS)
-    return std::string ("ROCm Thread");
+    return std::string ("AMDGPU Thread");
   else
-    return string_printf ("ROCm Thread %ld.%ld (%d,%d,%d)/%d",
+    return string_printf ("AMDGPU Thread %ld.%ld (%d,%d,%d)/%d",
                           dispatch_id.handle, wave_id.handle, group_ids[2],
                           group_ids[1], group_ids[0], wave_in_group);
 }
@@ -577,7 +578,7 @@ rocm_target_stop_one_wave (ptid_t ptid)
   if (status == AMD_DBGAPI_STATUS_ERROR_INVALID_WAVE_ID)
     {
       /* the wave must have exited, set the thread status to reflect that.  */
-      auto *tp = find_thread_ptid (ptid);
+      auto *tp = find_thread_ptid (current_inferior (), ptid);
       gdb_assert (tp);
 
       tp->state = THREAD_EXITED;
@@ -828,18 +829,19 @@ rocm_target_ops::wait (ptid_t ptid, struct target_waitstatus *ws,
   std::tie (event_wave_id, stop_reason) = info->wave_stop_events.front ();
   info->wave_stop_events.pop_front ();
 
-  ptid_t event_ptid (current_inferior ()->pid, 1, event_wave_id.handle);
+  struct inferior *inf = current_inferior ();
+  ptid_t event_ptid (inf->pid, 1, event_wave_id.handle);
 
-  if (!find_thread_ptid (event_ptid))
+  if (!find_thread_ptid (inf->process_target (), event_ptid))
     {
-      add_thread_silent (event_ptid);
-      set_running (event_ptid, 1);
-      set_executing (event_ptid, 1);
+      add_thread_silent (inf->process_target (), event_ptid);
+      set_running (inf->process_target (), event_ptid, 1);
+      set_executing (inf->process_target (), event_ptid, 1);
     }
 
   /* Since we are manipulating the register cache for the event thread,
      make sure it is the current thread.  */
-  switch_to_thread (event_ptid);
+  switch_to_thread (inf->process_target (), event_ptid);
 
   /* By caching the PC now, we avoid having to suspend/resume the queue
      later when we need to access it.  */
@@ -849,7 +851,7 @@ rocm_target_ops::wait (ptid_t ptid, struct target_waitstatus *ws,
                                 &stop_pc)
       == AMD_DBGAPI_STATUS_SUCCESS)
     {
-      struct regcache *regcache = get_thread_regcache (event_ptid);
+      struct regcache *regcache = get_thread_regcache_for_ptid (event_ptid);
       regcache->raw_supply (gdbarch_pc_regnum (regcache->arch ()), &stop_pc);
     }
   ws->kind = TARGET_WAITKIND_STOPPED;
@@ -889,7 +891,7 @@ rocm_target_ops::stopped_by_sw_breakpoint ()
 
   /* FIXME: we should check that the wave is not single-stepping.  */
 
-  struct regcache *regcache = get_thread_regcache (inferior_ptid);
+  struct regcache *regcache = get_thread_regcache_for_ptid (inferior_ptid);
 
   CORE_ADDR bkpt_pc = regcache_read_pc (regcache)
                       - gdbarch_decr_pc_after_break (regcache->arch ());
@@ -921,8 +923,7 @@ rocm_target_ops::fetch_registers (struct regcache *regcache, int regno)
       return;
     }
 
-  inferior *inf = find_inferior_ptid (regcache->ptid ());
-  amd_dbgapi_process_id_t process_id = get_amd_dbgapi_process_id (inf);
+  amd_dbgapi_process_id_t process_id = get_amd_dbgapi_process_id ();
   amd_dbgapi_wave_id_t wave_id = get_amd_dbgapi_wave_id (regcache->ptid ());
 
   gdb_byte raw[AMDGCN_MAX_REGISTER_SIZE];
@@ -955,8 +956,7 @@ rocm_target_ops::store_registers (struct regcache *regcache, int regno)
       return;
     }
 
-  inferior *inf = find_inferior_ptid (regcache->ptid ());
-  amd_dbgapi_process_id_t process_id = get_amd_dbgapi_process_id (inf);
+  amd_dbgapi_process_id_t process_id = get_amd_dbgapi_process_id ();
   amd_dbgapi_wave_id_t wave_id = get_amd_dbgapi_wave_id (regcache->ptid ());
 
   regcache->raw_collect (regno, &raw);
@@ -1038,16 +1038,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,17 +1074,18 @@ 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);
-          set_executing (wave_ptid, 1);
+          add_thread_silent (inf->process_target (), wave_ptid);
+          set_running (inf->process_target (), wave_ptid, 1);
+          set_executing (inf->process_target (), wave_ptid, 1);
         }
     }
 
@@ -1210,7 +1218,8 @@ rocm_target_inferior_exit (struct inferior *inf)
 
   amd_dbgapi_deactivated.notify ();
 
-  delete_file_handler (info->notifier);
+  if (info->notifier != -1)
+    delete_file_handler (info->notifier);
 
   amd_dbgapi_process_detach (info->process_id);
 
@@ -1352,6 +1361,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);
@@ -1557,15 +1567,14 @@ get_debug_amd_dbgapi_log_level ()
 
 static void
 set_debug_amd_dbgapi_log_level (const char *args, int from_tty,
-                                 struct cmd_list_element *c)
+                                struct cmd_list_element *c)
 {
   amd_dbgapi_set_log_level (get_debug_amd_dbgapi_log_level ());
 }
 
 static void
 show_debug_amd_dbgapi_log_level (struct ui_file *file, int from_tty,
-                                  struct cmd_list_element *c,
-                                  const char *value)
+                                 struct cmd_list_element *c, const char *value)
 {
   fprintf_filtered (file, _ ("The amd-dbgapi log level is %s.\n"), value);
 }
@@ -1588,8 +1597,7 @@ info_agents_command (const char *args, int from_tty)
 
   if (!count && !uiout->is_mi_like_p ())
     {
-      uiout->field_string (NULL,
-                           _ ("No agents are currently active.\n"));
+      uiout->field_string (NULL, _ ("No agents are currently active.\n"));
       return;
     }
 
@@ -1717,16 +1725,15 @@ _initialize_rocm_tdep (void)
 
   create_internalvar_type_lazy ("_wave_id", &rocm_wave_id_funcs, NULL);
 
-  add_prefix_cmd (
-      "amd-dbgapi", no_class, set_debug_amd_dbgapi,
-      _ ("Generic command for setting amd-dbgapi debugging flags"),
-      &set_debug_amd_dbgapi_list, "set debug amd-dbgapi ", 0, &setdebuglist);
+  add_prefix_cmd ("amd-dbgapi", no_class, set_debug_amd_dbgapi,
+                  _ ("Generic command for setting amd-dbgapi debugging flags"),
+                  &set_debug_amd_dbgapi_list, "set debug amd-dbgapi ", 0,
+                  &setdebuglist);
 
-  add_prefix_cmd (
-      "amd-dbgapi", no_class, show_debug_amd_dbgapi,
-      _ ("Generic command for showing amd-dbgapi debugging flags"),
-      &show_debug_amd_dbgapi_list, "show debug amd-dbgapi ", 0,
-      &showdebuglist);
+  add_prefix_cmd ("amd-dbgapi", no_class, show_debug_amd_dbgapi,
+                  _ ("Generic command for showing amd-dbgapi debugging flags"),
+                  &show_debug_amd_dbgapi_list, "show debug amd-dbgapi ", 0,
+                  &showdebuglist);
 
   add_setshow_enum_cmd (
       "log-level", class_maintenance, debug_amd_dbgapi_log_level_enums,
This page took 0.027235 seconds and 4 git commands to generate.