1 /* Target-dependent code for ROCm.
3 Copyright (C) 2019-2020 Free Software Foundation, Inc.
4 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "amdgcn-rocm-tdep.h"
24 #include "arch-utils.h"
25 #include "cli/cli-style.h"
27 #include "event-loop.h"
28 #include "filenames.h"
31 #include "gdbsupport/filestuff.h"
32 #include "gdbsupport/scoped_fd.h"
33 #include "gdbthread.h"
39 #include "observable.h"
40 #include "progspace-and-thread.h"
42 #include "rocm-tdep.h"
52 #include <unordered_map>
54 #include <amd-dbgapi.h>
56 /* Big enough to hold the size of the largest register in bytes. */
57 #define AMDGCN_MAX_REGISTER_SIZE 256
59 #define DEFINE_OBSERVABLE(name) decltype (name) name (#name)
61 DEFINE_OBSERVABLE (amd_dbgapi_activated
);
62 DEFINE_OBSERVABLE (amd_dbgapi_deactivated
);
63 DEFINE_OBSERVABLE (amd_dbgapi_code_object_list_updated
);
65 #undef DEFINE_OBSERVABLE
67 struct rocm_notify_shared_library_info
69 std::string compare
; /* Compare loaded library names with this string. */
70 struct so_list
*solib
;
74 /* ROCm-specific inferior data. */
76 struct rocm_inferior_info
78 /* The amd_dbgapi_process_id for this inferior. */
79 amd_dbgapi_process_id_t process_id
{ AMD_DBGAPI_PROCESS_NONE
};
81 /* The amd_dbgapi_notifier_t for this inferior. */
82 amd_dbgapi_notifier_t notifier
{ -1 };
84 /* True if commit_resume should all-start the GPU queues. */
85 bool commit_resume_all_start
;
87 /* True is the inferior has exited. */
88 bool has_exited
{ false };
90 std::unordered_map
<decltype (amd_dbgapi_breakpoint_id_t::handle
),
94 /* List of pending events the rocm target retrieved from the dbgapi. */
95 std::list
<std::pair
<amd_dbgapi_wave_id_t
, amd_dbgapi_wave_stop_reason_t
>>
98 /* Map of rocm_notify_shared_library_info's for libraries that have been
99 registered to receive notifications when loading/unloading. */
100 std::unordered_map
<decltype (amd_dbgapi_shared_library_id_t::handle
),
101 struct rocm_notify_shared_library_info
>
105 static amd_dbgapi_event_id_t
106 rocm_process_event_queue (amd_dbgapi_event_kind_t until_event_kind
107 = AMD_DBGAPI_EVENT_KIND_NONE
);
109 /* Return the inferior's rocm_inferior_info struct. */
110 static struct rocm_inferior_info
*
111 get_rocm_inferior_info (struct inferior
*inferior
= nullptr);
113 static const target_info rocm_ops_info
114 = { "rocm", N_ ("ROCm GPU debugging support"),
115 N_ ("ROCm GPU debugging support") };
117 static amd_dbgapi_log_level_t
get_debug_amd_dbgapi_log_level ();
119 struct rocm_target_ops final
: public target_ops
122 info () const override
124 return rocm_ops_info
;
127 stratum () const override
132 void mourn_inferior () override
;
134 void async (int enable
) override
;
136 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
137 void resume (ptid_t
, int, enum gdb_signal
) override
;
138 void commit_resume () override
;
139 void stop (ptid_t ptid
) override
;
141 void fetch_registers (struct regcache
*, int) override
;
142 void store_registers (struct regcache
*, int) override
;
144 void update_thread_list () override
;
146 struct gdbarch
*thread_architecture (ptid_t
) override
;
148 std::string
pid_to_str (ptid_t ptid
) override
;
150 const char *thread_name (thread_info
*tp
) override
;
152 const char *extra_thread_info (thread_info
*tp
) override
;
154 bool thread_alive (ptid_t ptid
) override
;
156 enum target_xfer_status
xfer_partial (enum target_object object
,
157 const char *annex
, gdb_byte
*readbuf
,
158 const gdb_byte
*writebuf
,
159 ULONGEST offset
, ULONGEST len
,
160 ULONGEST
*xfered_len
) override
;
163 stopped_by_watchpoint () override
165 return !ptid_is_gpu (inferior_ptid
)
166 && beneath ()->stopped_by_watchpoint ();
170 stopped_data_address (CORE_ADDR
*addr_p
) override
172 return !ptid_is_gpu (inferior_ptid
)
173 && beneath ()->stopped_data_address (addr_p
);
177 supports_stopped_by_sw_breakpoint () override
182 bool stopped_by_sw_breakpoint () override
;
185 stopped_by_hw_breakpoint () override
187 return !ptid_is_gpu (inferior_ptid
)
188 && beneath ()->stopped_by_hw_breakpoint ();
192 /* ROCm's target vector. */
193 static struct rocm_target_ops rocm_ops
;
195 /* ROCm breakpoint ops. */
196 static struct breakpoint_ops rocm_breakpoint_ops
;
198 /* Per-inferior data key. */
199 static const struct inferior_key
<rocm_inferior_info
> rocm_inferior_data
;
201 /* The read/write ends of the pipe registered as waitable file in the
203 static int rocm_event_pipe
[2] = { -1, -1 };
205 /* Flush the event pipe. */
208 async_file_flush (void)
215 ret
= read (rocm_event_pipe
[0], &buf
, 1);
217 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
220 /* Put something (anything, doesn't matter what, or how much) in event
221 pipe, so that the select/poll in the event-loop realizes we have
222 something to process. */
225 async_file_mark (void)
229 /* It doesn't really matter what the pipe contains, as long we end
230 up with something in it. Might as well flush the previous
236 ret
= write (rocm_event_pipe
[1], "+", 1);
238 while (ret
== -1 && errno
== EINTR
);
240 /* Ignore EAGAIN. If the pipe is full, the event loop will already
241 be awakened anyway. */
244 /* Fetch the rocm_inferior_info data for the given inferior. */
246 static struct rocm_inferior_info
*
247 get_rocm_inferior_info (struct inferior
*inferior
)
250 inferior
= current_inferior ();
252 struct rocm_inferior_info
*info
= rocm_inferior_data
.get (inferior
);
255 info
= rocm_inferior_data
.emplace (inferior
);
260 /* Fetch the amd_dbgapi_process_id for the given inferior. */
262 amd_dbgapi_process_id_t
263 get_amd_dbgapi_process_id (struct inferior
*inferior
)
265 return get_rocm_inferior_info (inferior
)->process_id
;
269 rocm_breakpoint_re_set (struct breakpoint
*b
)
274 rocm_breakpoint_check_status (struct bpstats
*bs
)
276 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
277 amd_dbgapi_process_id_t process_id
= info
->process_id
;
278 amd_dbgapi_status_t status
;
281 bs
->print_it
= print_it_noop
;
283 /* Find the address the breakpoint is set at. */
284 auto it
= std::find_if (
285 info
->breakpoint_map
.begin (), info
->breakpoint_map
.end (),
286 [=] (const decltype (info
->breakpoint_map
)::value_type
&value
) {
287 return value
.second
== bs
->breakpoint_at
;
290 if (it
== info
->breakpoint_map
.end ())
291 error (_ ("Could not find breakpoint_id for breakpoint at %#lx"),
292 bs
->bp_location_at
->address
);
294 amd_dbgapi_breakpoint_id_t breakpoint_id
{ it
->first
};
295 amd_dbgapi_breakpoint_action_t action
;
297 status
= amd_dbgapi_report_breakpoint_hit (process_id
, breakpoint_id
,
298 inferior_thread (), &action
);
300 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
301 error (_ ("amd_dbgapi_report_breakpoint_hit failed: breakpoint_%ld "
303 breakpoint_id
.handle
, bs
->bp_location_at
->address
, status
);
305 if (action
== AMD_DBGAPI_BREAKPOINT_ACTION_RESUME
)
308 /* If the action is AMD_DBGAPI_BREAKPOINT_ACTION_HALT, we need to wait until
309 a breakpoint resume event for this breakpoint_id is seen. */
311 amd_dbgapi_event_id_t resume_event_id
312 = rocm_process_event_queue (AMD_DBGAPI_EVENT_KIND_BREAKPOINT_RESUME
);
314 /* We should always get a breakpoint_resume event after processing all
315 events generated by reporting the breakpoint was hit. */
316 gdb_assert (resume_event_id
.handle
!= AMD_DBGAPI_EVENT_NONE
.handle
);
318 amd_dbgapi_breakpoint_id_t resume_breakpoint_id
;
319 status
= amd_dbgapi_event_get_info (
320 process_id
, resume_event_id
, AMD_DBGAPI_EVENT_INFO_BREAKPOINT
,
321 sizeof (resume_breakpoint_id
), &resume_breakpoint_id
);
323 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
324 error (_ ("amd_dbgapi_event_get_info failed (rc=%d)"), status
);
326 /* The debugger API guarantees that [breakpoint_hit...resume_breakpoint]
327 sequences cannot interleave, so this breakpoint resume event must be
328 for our breakpoint_id. */
329 if (resume_breakpoint_id
.handle
!= breakpoint_id
.handle
)
330 error (_ ("breakpoint resume event is not for this breakpoint. "
331 "Expected breakpoint_%ld, got breakpoint_%ld"),
332 breakpoint_id
.handle
, resume_breakpoint_id
.handle
);
334 amd_dbgapi_event_processed (process_id
, resume_event_id
);
338 rocm_target_dbgapi_activated ()
340 /* FIXME: only push on the first activation. */
341 /* Engage the ROCm target_ops and so_ops. */
342 push_target (&rocm_ops
);
346 rocm_target_dbgapi_deactivated ()
348 /* FIXME: only unpush on the last activation. */
349 /* Disengage the ROCm target_ops. */
350 unpush_target (&rocm_ops
);
354 rocm_target_ops::thread_alive (ptid_t ptid
)
356 if (!ptid_is_gpu (ptid
))
357 return beneath ()->thread_alive (ptid
);
359 /* Check that the wave_id is valid. */
361 inferior
*inf
= find_inferior_ptid (ptid
);
365 amd_dbgapi_wave_state_t state
;
366 return amd_dbgapi_wave_get_info (
367 get_amd_dbgapi_process_id (inf
), get_amd_dbgapi_wave_id (ptid
),
368 AMD_DBGAPI_WAVE_INFO_STATE
, sizeof (state
), &state
)
369 == AMD_DBGAPI_STATUS_SUCCESS
;
373 rocm_target_ops::thread_name (thread_info
*tp
)
375 if (!ptid_is_gpu (tp
->ptid
))
376 return beneath ()->thread_name (tp
);
378 /* Return the process's comm value—that is, the command name associated with
382 xsnprintf (comm_path
, sizeof (comm_path
), "/proc/%ld/comm",
383 (long)tp
->ptid
.pid ());
385 gdb_file_up comm_file
= gdb_fopen_cloexec (comm_path
, "r");
389 #if !defined(TASK_COMM_LEN)
390 #define TASK_COMM_LEN 16 /* As defined in the kernel's sched.h. */
393 static char comm_buf
[TASK_COMM_LEN
];
394 const char *comm_value
;
396 comm_value
= fgets (comm_buf
, sizeof (comm_buf
), comm_file
.get ());
397 comm_buf
[sizeof (comm_buf
) - 1] = '\0';
399 /* Make sure there is no newline at the end. */
402 for (int i
= 0; i
< sizeof (comm_buf
); i
++)
403 if (comm_buf
[i
] == '\n')
414 rocm_target_ops::pid_to_str (ptid_t ptid
)
416 if (!ptid_is_gpu (ptid
))
418 return beneath ()->pid_to_str (ptid
);
421 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
422 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (ptid
);
423 amd_dbgapi_dispatch_id_t dispatch_id
;
424 uint32_t group_ids
[3], wave_in_group
;
426 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
427 AMD_DBGAPI_WAVE_INFO_DISPATCH
,
428 sizeof (dispatch_id
), &dispatch_id
)
429 != AMD_DBGAPI_STATUS_SUCCESS
430 || amd_dbgapi_wave_get_info (process_id
, wave_id
,
431 AMD_DBGAPI_WAVE_INFO_WORK_GROUP_COORD
,
432 sizeof (group_ids
), &group_ids
)
433 != AMD_DBGAPI_STATUS_SUCCESS
434 || amd_dbgapi_wave_get_info (
436 AMD_DBGAPI_WAVE_INFO_WAVE_NUMBER_IN_WORK_GROUP
,
437 sizeof (wave_in_group
), &wave_in_group
)
438 != AMD_DBGAPI_STATUS_SUCCESS
)
439 return std::string ("AMDGPU Thread");
441 return string_printf ("AMDGPU Thread %ld.%ld (%d,%d,%d)/%d",
442 dispatch_id
.handle
, wave_id
.handle
, group_ids
[2],
443 group_ids
[1], group_ids
[0], wave_in_group
);
447 rocm_target_ops::extra_thread_info (thread_info
*tp
)
449 if (!ptid_is_gpu (tp
->ptid
))
450 beneath ()->extra_thread_info (tp
);
455 enum target_xfer_status
456 rocm_target_ops::xfer_partial (enum target_object object
, const char *annex
,
457 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
458 ULONGEST offset
, ULONGEST requested_len
,
459 ULONGEST
*xfered_len
)
461 gdb::optional
<scoped_restore_current_thread
> maybe_restore_thread
;
463 if (ptid_is_gpu (inferior_ptid
))
465 gdb_assert (requested_len
&& xfered_len
&& "checking invariants");
467 if (object
!= TARGET_OBJECT_MEMORY
)
468 return TARGET_XFER_E_IO
;
470 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
471 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (inferior_ptid
);
473 amd_dbgapi_architecture_id_t architecture_id
;
474 amd_dbgapi_address_space_id_t address_space_id
;
476 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
477 AMD_DBGAPI_WAVE_INFO_ARCHITECTURE
,
478 sizeof (architecture_id
), &architecture_id
)
479 != AMD_DBGAPI_STATUS_SUCCESS
480 || amd_dbgapi_architecture_get_info (
482 AMD_DBGAPI_ARCHITECTURE_INFO_DEFAULT_GLOBAL_ADDRESS_SPACE
,
483 sizeof (address_space_id
), &address_space_id
)
484 != AMD_DBGAPI_STATUS_SUCCESS
)
485 return TARGET_XFER_EOF
;
487 size_t len
= requested_len
;
488 amd_dbgapi_status_t status
;
491 status
= amd_dbgapi_read_memory (
492 process_id
, wave_id
, 0, address_space_id
, offset
, &len
, readbuf
);
494 status
= amd_dbgapi_write_memory (
495 process_id
, wave_id
, 0, address_space_id
, offset
, &len
, writebuf
);
497 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
498 return TARGET_XFER_EOF
;
501 return TARGET_XFER_OK
;
504 return beneath ()->xfer_partial (object
, annex
, readbuf
, writebuf
, offset
,
505 requested_len
, xfered_len
);
509 rocm_target_ops::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
511 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
516 "\e[1;34minfrun: rocm_target_ops::resume ([%d,%ld,%ld])\e[0m\n",
517 ptid
.pid (), ptid
.lwp (), ptid
.tid ());
519 /* Check if the thread focus is on the GPU device. */
520 if (ptid
== minus_one_ptid
|| !ptid_is_gpu (ptid
))
522 beneath ()->resume (ptid
, step
, signo
);
523 if (ptid
!= minus_one_ptid
)
527 /* A specific PTID means `step only this process id'. */
528 bool resume_one
= ptid
!= minus_one_ptid
&& !ptid
.is_pid ();
529 gdb_assert (resume_one
|| !step
);
532 error (_ ("internal error - unimplemented "));
534 amd_dbgapi_process_set_progress (info
->process_id
,
535 AMD_DBGAPI_PROGRESS_NO_FORWARD
);
537 amd_dbgapi_status_t status
= amd_dbgapi_wave_resume (
538 info
->process_id
, get_amd_dbgapi_wave_id (ptid
),
539 step
? AMD_DBGAPI_RESUME_MODE_SINGLE_STEP
540 : AMD_DBGAPI_RESUME_MODE_NORMAL
);
541 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
542 warning (_ ("Could not resume %s (rc=%d)"),
543 target_pid_to_str (ptid
).c_str (), status
);
545 info
->commit_resume_all_start
= true;
549 rocm_target_ops::commit_resume ()
551 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
556 "\e[1;34minfrun: rocm_target_ops::commit_resume ()\e[0m\n");
558 beneath ()->commit_resume ();
560 if (info
->commit_resume_all_start
)
562 amd_dbgapi_process_set_progress (info
->process_id
,
563 AMD_DBGAPI_PROGRESS_NORMAL
);
564 info
->commit_resume_all_start
= false;
567 if (target_can_async_p ())
572 rocm_target_stop_one_wave (ptid_t ptid
)
574 amd_dbgapi_status_t status
;
576 status
= amd_dbgapi_wave_stop (get_amd_dbgapi_process_id (),
577 get_amd_dbgapi_wave_id (ptid
));
579 if (status
== AMD_DBGAPI_STATUS_ERROR_INVALID_WAVE_ID
)
581 /* the wave must have exited, set the thread status to reflect that. */
582 auto *tp
= find_thread_ptid (ptid
);
585 tp
->state
= THREAD_EXITED
;
587 else if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
588 warning (_ ("Could not stop %s (rc=%d)"),
589 target_pid_to_str (ptid
).c_str (), status
);
593 rocm_target_ops::stop (ptid_t ptid
)
598 "\e[1;34minfrun: rocm_target_ops::stop ([%d,%ld,%ld])\e[0m\n",
599 ptid
.pid (), ptid
.lwp (), ptid
.tid ());
601 if (ptid
== minus_one_ptid
|| !ptid_is_gpu (ptid
))
603 beneath ()->stop (ptid
);
604 if (ptid
!= minus_one_ptid
)
608 if (ptid
== minus_one_ptid
)
609 error (_ ("internal error - unimplemented "));
611 rocm_target_stop_one_wave (ptid
);
615 handle_target_event (int error
, gdb_client_data client_data
)
617 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
618 amd_dbgapi_process_id_t process_id
= info
->process_id
;
620 amd_dbgapi_process_set_progress (process_id
, AMD_DBGAPI_PROGRESS_NO_FORWARD
);
622 /* Flush the async file first. */
623 if (target_is_async_p ())
626 rocm_process_event_queue ();
628 /* In all-stop mode, unless the event queue is empty (spurious wake-up),
629 we can keep the process in progress_no_forward mode. The infrun loop
630 will enable forward progress when a thread is resumed. */
631 if (non_stop
|| info
->wave_stop_events
.empty ())
632 amd_dbgapi_process_set_progress (process_id
, AMD_DBGAPI_PROGRESS_NORMAL
);
634 if (!info
->wave_stop_events
.empty ())
635 inferior_event_handler (INF_REG_EVENT
, nullptr);
639 rocm_target_ops::async (int enable
)
641 beneath ()->async (enable
);
645 if (rocm_event_pipe
[0] != -1)
648 if (gdb_pipe_cloexec (rocm_event_pipe
) == -1)
649 internal_error (__FILE__
, __LINE__
, "creating event pipe failed.");
651 ::fcntl (rocm_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
652 ::fcntl (rocm_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
654 add_file_handler (rocm_event_pipe
[0], handle_target_event
, nullptr);
656 /* There may be pending events to handle. Tell the event loop
662 delete_file_handler (rocm_event_pipe
[0]);
664 if (rocm_event_pipe
[0] == -1)
667 ::close (rocm_event_pipe
[0]);
668 ::close (rocm_event_pipe
[1]);
669 rocm_event_pipe
[0] = -1;
670 rocm_event_pipe
[1] = -1;
675 rocm_process_one_event (amd_dbgapi_event_id_t event_id
,
676 amd_dbgapi_event_kind_t event_kind
)
678 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
679 amd_dbgapi_process_id_t process_id
= info
->process_id
;
680 amd_dbgapi_status_t status
;
684 case AMD_DBGAPI_EVENT_KIND_WAVE_STOP
:
686 amd_dbgapi_wave_id_t wave_id
;
687 if ((status
= amd_dbgapi_event_get_info (process_id
, event_id
,
688 AMD_DBGAPI_EVENT_INFO_WAVE
,
689 sizeof (wave_id
), &wave_id
))
690 != AMD_DBGAPI_STATUS_SUCCESS
)
691 error (_ ("event_get_info for event_%ld failed (rc=%d)"),
692 event_id
.handle
, status
);
694 amd_dbgapi_wave_stop_reason_t stop_reason
;
695 status
= amd_dbgapi_wave_get_info (process_id
, wave_id
,
696 AMD_DBGAPI_WAVE_INFO_STOP_REASON
,
697 sizeof (stop_reason
), &stop_reason
);
699 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
700 && status
!= AMD_DBGAPI_STATUS_ERROR_INVALID_WAVE_ID
)
701 error (_ ("wave_get_info for wave_%ld failed (rc=%d)"),
702 wave_id
.handle
, status
);
704 /* The wave may have exited, or the queue went into an error
705 state. In such cases, we will see another wave command
706 terminated event, and handle the wave termination then. */
708 if (status
== AMD_DBGAPI_STATUS_SUCCESS
)
709 info
->wave_stop_events
.emplace_back (
710 std::make_pair (wave_id
, stop_reason
));
714 case AMD_DBGAPI_EVENT_KIND_CODE_OBJECT_LIST_UPDATED
:
715 amd_dbgapi_code_object_list_updated
.notify ();
718 case AMD_DBGAPI_EVENT_KIND_BREAKPOINT_RESUME
:
719 /* Breakpoint resume events should be handled by the breakpoint
720 action, and this code should not reach this. */
721 gdb_assert_not_reached (_ ("unhandled event kind"));
724 case AMD_DBGAPI_EVENT_KIND_RUNTIME
:
726 amd_dbgapi_runtime_state_t runtime_state
;
728 if ((status
= amd_dbgapi_event_get_info (
729 process_id
, event_id
, AMD_DBGAPI_EVENT_INFO_RUNTIME_STATE
,
730 sizeof (runtime_state
), &runtime_state
))
731 != AMD_DBGAPI_STATUS_SUCCESS
)
732 error (_ ("event_get_info for event_%ld failed (rc=%d)"),
733 event_id
.handle
, status
);
735 switch (runtime_state
)
737 case AMD_DBGAPI_RUNTIME_STATE_LOADED_SUPPORTED
:
738 amd_dbgapi_activated
.notify ();
740 case AMD_DBGAPI_RUNTIME_STATE_LOADED_UNSUPPORTED
:
741 warning (_ ("ROCm-GDB: low-level runtime version not supported"));
744 case AMD_DBGAPI_RUNTIME_STATE_UNLOADED
:
745 amd_dbgapi_deactivated
.notify ();
752 error (_ ("event kind (%d) not supported"), event_kind
);
755 amd_dbgapi_event_processed (process_id
, event_id
);
758 /* Drain the amd_dbgapi event queue until an event of the given type is seen.
759 If no particular event kind is specified (AMD_DBGAPI_EVENT_KIND_NONE), the
760 event queue is completely drained. Wave stop events that are not returned
761 are re-queued into the current's process pending wave events. */
762 static amd_dbgapi_event_id_t
763 rocm_process_event_queue (amd_dbgapi_event_kind_t until_event_kind
)
765 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
769 amd_dbgapi_event_id_t event_id
;
770 amd_dbgapi_event_kind_t event_kind
;
772 amd_dbgapi_status_t status
= amd_dbgapi_next_pending_event (
773 info
->process_id
, &event_id
, &event_kind
);
775 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
776 error (_ ("next_pending_event failed (rc=%d)"), status
);
778 if (event_id
.handle
== AMD_DBGAPI_EVENT_NONE
.handle
779 || event_kind
== until_event_kind
)
782 rocm_process_one_event (event_id
, event_kind
);
787 rocm_target_ops::wait (ptid_t ptid
, struct target_waitstatus
*ws
,
791 fprintf_unfiltered (gdb_stdlog
,
792 "\e[1;34minfrun: rocm_target_ops::wait\e[0m\n");
794 if (!ptid_is_gpu (ptid
))
796 ptid_t event_ptid
= beneath ()->wait (ptid
, ws
, target_options
);
797 if (event_ptid
!= minus_one_ptid
)
801 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
802 amd_dbgapi_process_id_t process_id
= info
->process_id
;
804 /* Drain all the events from the amd_dbgapi, and preserve the ordering. */
805 if (info
->wave_stop_events
.empty ())
807 amd_dbgapi_process_set_progress (process_id
,
808 AMD_DBGAPI_PROGRESS_NO_FORWARD
);
810 /* Flush the async file first. */
811 if (target_is_async_p ())
814 rocm_process_event_queue ();
816 /* In all-stop mode, unless the event queue is empty (spurious wake-up),
817 we can keep the process in progress_no_forward mode. The infrun loop
818 will enable forward progress when a thread is resumed. */
819 if (non_stop
|| info
->wave_stop_events
.empty ())
820 amd_dbgapi_process_set_progress (process_id
,
821 AMD_DBGAPI_PROGRESS_NORMAL
);
824 if (info
->wave_stop_events
.empty ())
825 return minus_one_ptid
;
827 amd_dbgapi_wave_id_t event_wave_id
;
828 amd_dbgapi_wave_stop_reason_t stop_reason
;
830 std::tie (event_wave_id
, stop_reason
) = info
->wave_stop_events
.front ();
831 info
->wave_stop_events
.pop_front ();
833 ptid_t
event_ptid (current_inferior ()->pid
, 1, event_wave_id
.handle
);
835 if (!find_thread_ptid (event_ptid
))
837 add_thread_silent (event_ptid
);
838 set_running (event_ptid
, 1);
839 set_executing (event_ptid
, 1);
842 /* Since we are manipulating the register cache for the event thread,
843 make sure it is the current thread. */
844 switch_to_thread (event_ptid
);
846 /* By caching the PC now, we avoid having to suspend/resume the queue
847 later when we need to access it. */
848 amd_dbgapi_global_address_t stop_pc
;
849 if (amd_dbgapi_wave_get_info (process_id
, event_wave_id
,
850 AMD_DBGAPI_WAVE_INFO_PC
, sizeof (stop_pc
),
852 == AMD_DBGAPI_STATUS_SUCCESS
)
854 struct regcache
*regcache
= get_thread_regcache (event_ptid
);
855 regcache
->raw_supply (gdbarch_pc_regnum (regcache
->arch ()), &stop_pc
);
857 ws
->kind
= TARGET_WAITKIND_STOPPED
;
860 & (AMD_DBGAPI_WAVE_STOP_REASON_BREAKPOINT
861 | AMD_DBGAPI_WAVE_STOP_REASON_SINGLE_STEP
))
862 ws
->value
.sig
= GDB_SIGNAL_TRAP
;
863 else if (stop_reason
& AMD_DBGAPI_WAVE_STOP_REASON_MEMORY_VIOLATION
)
864 ws
->value
.sig
= GDB_SIGNAL_SEGV
;
866 & (AMD_DBGAPI_WAVE_STOP_REASON_FP_INPUT_DENORMAL
867 | AMD_DBGAPI_WAVE_STOP_REASON_FP_DIVIDE_BY_0
868 | AMD_DBGAPI_WAVE_STOP_REASON_FP_OVERFLOW
869 | AMD_DBGAPI_WAVE_STOP_REASON_FP_UNDERFLOW
870 | AMD_DBGAPI_WAVE_STOP_REASON_FP_INEXACT
871 | AMD_DBGAPI_WAVE_STOP_REASON_FP_INVALID_OPERATION
872 | AMD_DBGAPI_WAVE_STOP_REASON_INT_DIVIDE_BY_0
))
873 ws
->value
.sig
= GDB_SIGNAL_FPE
;
875 ws
->value
.sig
= GDB_SIGNAL_0
;
877 /* If there are more events in the list, mark the async file so that
878 rocm_target_ops::wait gets called again. */
879 if (target_is_async_p () && !info
->wave_stop_events
.empty ())
886 rocm_target_ops::stopped_by_sw_breakpoint ()
888 if (!ptid_is_gpu (inferior_ptid
))
889 return beneath ()->supports_stopped_by_sw_breakpoint ()
890 && beneath ()->stopped_by_sw_breakpoint ();
892 /* FIXME: we should check that the wave is not single-stepping. */
894 struct regcache
*regcache
= get_thread_regcache (inferior_ptid
);
896 CORE_ADDR bkpt_pc
= regcache_read_pc (regcache
)
897 - gdbarch_decr_pc_after_break (regcache
->arch ());
899 return software_breakpoint_inserted_here_p (regcache
->aspace (), bkpt_pc
);
903 rocm_target_ops::mourn_inferior ()
905 /* FIXME: only unpush on the last activation. */
906 /* Disengage the ROCm target_ops. */
907 unpush_target (&rocm_ops
);
909 beneath ()->mourn_inferior ();
913 rocm_target_ops::fetch_registers (struct regcache
*regcache
, int regno
)
915 struct gdbarch
*gdbarch
= regcache
->arch ();
916 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
918 /* delegate to the host routines when not on the device */
920 if (!rocm_is_amdgcn_gdbarch (gdbarch
))
922 beneath ()->fetch_registers (regcache
, regno
);
926 inferior
*inf
= find_inferior_ptid (regcache
->ptid ());
927 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id (inf
);
928 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (regcache
->ptid ());
930 gdb_byte raw
[AMDGCN_MAX_REGISTER_SIZE
];
932 amd_dbgapi_status_t status
= amd_dbgapi_read_register (
933 process_id
, wave_id
, tdep
->register_ids
[regno
], 0,
934 TYPE_LENGTH (register_type (gdbarch
, regno
)), raw
);
936 if (status
== AMD_DBGAPI_STATUS_SUCCESS
)
938 regcache
->raw_supply (regno
, raw
);
940 else if (status
!= AMD_DBGAPI_STATUS_ERROR_INVALID_REGISTER_ID
)
942 warning (_ ("Couldn't read register %s (#%d)."),
943 gdbarch_register_name (gdbarch
, regno
), regno
);
948 rocm_target_ops::store_registers (struct regcache
*regcache
, int regno
)
950 struct gdbarch
*gdbarch
= regcache
->arch ();
951 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
952 gdb_byte raw
[AMDGCN_MAX_REGISTER_SIZE
];
954 if (!rocm_is_amdgcn_gdbarch (gdbarch
))
956 beneath ()->store_registers (regcache
, regno
);
960 inferior
*inf
= find_inferior_ptid (regcache
->ptid ());
961 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id (inf
);
962 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (regcache
->ptid ());
964 regcache
->raw_collect (regno
, &raw
);
966 amd_dbgapi_status_t status
= amd_dbgapi_write_register (
967 process_id
, wave_id
, tdep
->register_ids
[regno
], 0,
968 TYPE_LENGTH (register_type (gdbarch
, regno
)), raw
);
970 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
972 warning (_ ("Couldn't write register %s (#%d)."),
973 gdbarch_register_name (gdbarch
, regno
), regno
);
977 /* Fix breakpoints created with an address location while the
978 architecture was set to the host (could be fixed in core GDB). */
981 rocm_target_breakpoint_fixup (struct breakpoint
*b
)
983 if (b
->location
.get ()
984 && event_location_type (b
->location
.get ()) == ADDRESS_LOCATION
985 && gdbarch_bfd_arch_info (b
->loc
->gdbarch
)->arch
== bfd_arch_amdgcn
986 && gdbarch_bfd_arch_info (b
->gdbarch
)->arch
!= bfd_arch_amdgcn
)
988 b
->gdbarch
= b
->loc
->gdbarch
;
993 rocm_target_ops::thread_architecture (ptid_t ptid
)
995 static std::result_of
<decltype (&ptid_t::tid
) (ptid_t
)>::type last_tid
= 0;
996 static struct gdbarch
*cached_arch
= nullptr;
998 if (!ptid_is_gpu (ptid
))
999 return beneath ()->thread_architecture (ptid
);
1001 /* We can cache the gdbarch for a given wave_id (ptid::tid) because
1002 wave IDs are unique, and aren't reused. */
1003 if (ptid
.tid () == last_tid
)
1006 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
1007 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (ptid
);
1008 amd_dbgapi_architecture_id_t architecture_id
;
1010 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
1011 AMD_DBGAPI_WAVE_INFO_ARCHITECTURE
,
1012 sizeof (architecture_id
), &architecture_id
)
1013 != AMD_DBGAPI_STATUS_SUCCESS
)
1014 error (_ ("Couldn't get architecture for wave_%ld"), ptid
.tid ());
1016 uint32_t elf_amdgpu_machine
;
1017 if (amd_dbgapi_architecture_get_info (
1018 architecture_id
, AMD_DBGAPI_ARCHITECTURE_INFO_ELF_AMDGPU_MACHINE
,
1019 sizeof (elf_amdgpu_machine
), &elf_amdgpu_machine
)
1020 != AMD_DBGAPI_STATUS_SUCCESS
)
1021 error (_ ("Couldn't get elf_amdgpu_machine for architecture_%ld"),
1022 architecture_id
.handle
);
1024 struct gdbarch_info info
;
1025 gdbarch_info_init (&info
);
1027 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_amdgcn
, elf_amdgpu_machine
);
1028 info
.byte_order
= BFD_ENDIAN_LITTLE
;
1029 info
.osabi
= GDB_OSABI_AMDGPU_HSA
;
1031 last_tid
= ptid
.tid ();
1032 if (!(cached_arch
= gdbarch_find_by_info (info
)))
1033 error (_ ("Couldn't get elf_amdgpu_machine (%#x)"), elf_amdgpu_machine
);
1039 rocm_target_ops::update_thread_list ()
1041 for (inferior
*inf
: all_inferiors ())
1043 amd_dbgapi_process_id_t process_id
;
1044 amd_dbgapi_wave_id_t
*wave_list
;
1047 process_id
= get_amd_dbgapi_process_id (inf
);
1048 if (process_id
.handle
== AMD_DBGAPI_PROCESS_NONE
.handle
)
1050 /* The inferior may not be attached yet. */
1054 amd_dbgapi_changed_t changed
;
1055 amd_dbgapi_status_t status
;
1057 = amd_dbgapi_wave_list (process_id
, &count
, &wave_list
, &changed
))
1058 != AMD_DBGAPI_STATUS_SUCCESS
)
1059 error (_ ("amd_dbgapi_wave_list failed (rc=%d)"), status
);
1061 if (changed
== AMD_DBGAPI_CHANGED_NO
)
1064 /* Create a set and free the wave list. */
1065 std::set
<std::result_of
<decltype (&ptid_t::tid
) (ptid_t
)>::type
> threads
;
1066 for (size_t i
= 0; i
< count
; ++i
)
1067 threads
.emplace (wave_list
[i
].handle
);
1070 /* Then prune the wave_ids that already have a thread_info. */
1071 for (thread_info
*tp
: inf
->non_exited_threads ())
1072 if (ptid_is_gpu (tp
->ptid
))
1073 threads
.erase (tp
->ptid
.tid ());
1075 /* The wave_ids that are left require a new thread_info. */
1076 for (auto &&tid
: threads
)
1078 ptid_t
wave_ptid (inf
->pid
, 1, tid
);
1079 /* FIXME: is this really needed?
1080 amd_dbgapi_wave_state_t state;
1082 if (amd_dbgapi_wave_get_info (
1083 process_id, get_amd_dbgapi_wave_id (wave_ptid),
1084 AMD_DBGAPI_WAVE_INFO_STATE, sizeof (state), &state)
1085 != AMD_DBGAPI_STATUS_SUCCESS)
1088 add_thread_silent (wave_ptid
);
1089 set_running (wave_ptid
, 1);
1090 set_executing (wave_ptid
, 1);
1094 /* Give the beneath target a chance to do extra processing. */
1095 this->beneath ()->update_thread_list ();
1099 rocm_target_solib_loaded (struct so_list
*solib
)
1101 /* Notify the amd_dbgapi that a shared library has been loaded. */
1102 for (auto &&value
: get_rocm_inferior_info ()->notify_solib_map
)
1103 /* TODO: If we want to support file name wildcards, change this code. */
1104 if (::strstr (solib
->so_original_name
, value
.second
.compare
.c_str ())
1105 && !value
.second
.is_loaded
)
1107 value
.second
.solib
= solib
;
1108 value
.second
.is_loaded
= true;
1110 amd_dbgapi_report_shared_library (
1111 get_amd_dbgapi_process_id (),
1112 amd_dbgapi_shared_library_id_t
{ value
.first
},
1113 AMD_DBGAPI_SHARED_LIBRARY_STATE_LOADED
);
1118 rocm_target_solib_unloaded (struct so_list
*solib
)
1120 /* Notify the amd_dbgapi that a shared library will unload. */
1121 for (auto &&value
: get_rocm_inferior_info ()->notify_solib_map
)
1122 /* TODO: If we want to support file name wildcards, change this code. */
1123 if (::strstr (solib
->so_original_name
, value
.second
.compare
.c_str ())
1124 && value
.second
.is_loaded
)
1126 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
1128 amd_dbgapi_report_shared_library (
1129 info
->process_id
, amd_dbgapi_shared_library_id_t
{ value
.first
},
1130 AMD_DBGAPI_SHARED_LIBRARY_STATE_UNLOADED
);
1132 /* Delete breakpoints that were left inserted in this shared library.
1134 for (auto it
= info
->breakpoint_map
.begin ();
1135 it
!= info
->breakpoint_map
.end ();)
1136 if (solib_contains_address_p (solib
, it
->second
->loc
->address
))
1138 warning (_ ("breakpoint_%ld is still inserted after "
1139 "shared_library_%ld was unloaded"),
1140 it
->first
, value
.first
);
1141 delete_breakpoint (it
->second
);
1142 it
= info
->breakpoint_map
.erase (it
);
1147 value
.second
.solib
= nullptr;
1148 value
.second
.is_loaded
= false;
1153 rocm_target_inferior_created (struct target_ops
*target
, int from_tty
)
1155 struct inferior
*inf
= current_inferior ();
1156 auto *info
= get_rocm_inferior_info (inf
);
1157 amd_dbgapi_status_t status
;
1159 if (!target_can_async_p ())
1162 _ ("ROCm-GDB requires target-async, GPU debugging is disabled"));
1166 gdb_assert (info
->wave_stop_events
.empty ());
1168 status
= amd_dbgapi_process_attach (inf
, &info
->process_id
);
1170 if (status
== AMD_DBGAPI_STATUS_ERROR_VERSION_MISMATCH
)
1171 warning (_ ("The version of the kernel driver does not match the version "
1172 "required by the ROCm debugger library"));
1174 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
1176 warning (_ ("Could not attach to process %d"), inf
->pid
);
1180 if (amd_dbgapi_process_get_info (info
->process_id
,
1181 AMD_DBGAPI_PROCESS_INFO_NOTIFIER
,
1182 sizeof (info
->notifier
), &info
->notifier
)
1183 != AMD_DBGAPI_STATUS_SUCCESS
)
1185 warning (_ ("Could not retrieve process %d's notifier"), inf
->pid
);
1186 amd_dbgapi_process_detach (info
->process_id
);
1190 /* We add a file handler for events returned by the debugger api. We'll use
1191 this handler to signal our async handler that events are available. */
1194 [] (int error
, gdb_client_data client_data
) {
1195 auto info_
= static_cast<struct rocm_inferior_info
*> (client_data
);
1198 /* Drain the notifier pipe. */
1202 ret
= read (info_
->notifier
, &buf
, 1);
1204 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
1206 /* Signal our async handler. */
1211 /* Attaching to the inferior may have generated runtime events, process
1213 rocm_process_event_queue ();
1217 rocm_target_inferior_exit (struct inferior
*inf
)
1219 auto *info
= get_rocm_inferior_info (inf
);
1220 info
->has_exited
= true;
1222 amd_dbgapi_deactivated
.notify ();
1224 if (info
->notifier
!= -1)
1225 delete_file_handler (info
->notifier
);
1227 amd_dbgapi_process_detach (info
->process_id
);
1229 /* Delete the breakpoints that are still active. */
1230 for (auto &&value
: info
->breakpoint_map
)
1231 delete_breakpoint (value
.second
);
1233 rocm_inferior_data
.clear (inf
);
1236 static cli_style_option
warning_style ("rocm_warning", ui_file_style::RED
);
1237 static cli_style_option
info_style ("rocm_info", ui_file_style::GREEN
);
1238 static cli_style_option
verbose_style ("rocm_verbose", ui_file_style::BLUE
);
1240 static amd_dbgapi_callbacks_t dbgapi_callbacks
= {
1241 /* allocate_memory. */
1242 .allocate_memory
= malloc
,
1244 /* deallocate_memory. */
1245 .deallocate_memory
= free
,
1248 .get_os_pid
= [] (amd_dbgapi_client_process_id_t client_process_id
,
1249 pid_t
*pid
) -> amd_dbgapi_status_t
{
1250 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1251 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1253 if (info
->has_exited
)
1254 return AMD_DBGAPI_STATUS_ERROR_PROCESS_EXITED
;
1257 return AMD_DBGAPI_STATUS_SUCCESS
;
1260 /* enable_notify_shared_library callback. */
1261 .enable_notify_shared_library
1262 = [] (amd_dbgapi_client_process_id_t client_process_id
,
1263 const char *library_name
, amd_dbgapi_shared_library_id_t library_id
,
1264 amd_dbgapi_shared_library_state_t
*library_state
)
1265 -> amd_dbgapi_status_t
{
1266 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1267 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1269 if (!library_name
|| !library_state
)
1270 return AMD_DBGAPI_STATUS_ERROR_INVALID_ARGUMENT
;
1272 if (info
->notify_solib_map
.find (library_id
.handle
)
1273 != info
->notify_solib_map
.end ())
1275 /* This library id is already registered. */
1276 return AMD_DBGAPI_STATUS_ERROR
;
1279 /* Check whether the library is already loaded. */
1280 bool is_loaded
= false;
1281 struct so_list
*solib
;
1282 for (solib
= inf
->pspace
->so_list
; solib
; solib
= solib
->next
)
1283 if (::strstr (solib
->so_original_name
, library_name
))
1289 /* Check that the library_name is valid. If must not be empty, and
1290 should not have wildcard characters. */
1291 if (*library_name
== '\0'
1292 || std::string (library_name
).find_first_of ("*?[]")
1293 != std::string::npos
)
1294 return AMD_DBGAPI_STATUS_ERROR_INVALID_ARGUMENT
;
1296 /* Add a new entry in the notify_solib_map. */
1297 if (!info
->notify_solib_map
1298 .emplace (std::piecewise_construct
,
1299 std::forward_as_tuple (library_id
.handle
),
1300 std::forward_as_tuple (rocm_notify_shared_library_info
{
1301 library_name
, solib
, is_loaded
}))
1303 return AMD_DBGAPI_STATUS_ERROR
;
1305 *library_state
= is_loaded
? AMD_DBGAPI_SHARED_LIBRARY_STATE_LOADED
1306 : AMD_DBGAPI_SHARED_LIBRARY_STATE_UNLOADED
;
1308 return AMD_DBGAPI_STATUS_SUCCESS
;
1311 /* disable_notify_shared_library callback. */
1312 .disable_notify_shared_library
1313 = [] (amd_dbgapi_client_process_id_t client_process_id
,
1314 amd_dbgapi_shared_library_id_t library_id
) -> amd_dbgapi_status_t
{
1315 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1316 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1318 auto it
= info
->notify_solib_map
.find (library_id
.handle
);
1319 if (it
== info
->notify_solib_map
.end ())
1320 return AMD_DBGAPI_STATUS_ERROR_INVALID_SHARED_LIBRARY_ID
;
1322 info
->notify_solib_map
.erase (it
);
1323 return AMD_DBGAPI_STATUS_SUCCESS
;
1326 /* get_symbol_address callback. */
1327 .get_symbol_address
=
1328 [] (amd_dbgapi_client_process_id_t client_process_id
,
1329 amd_dbgapi_shared_library_id_t library_id
, const char *symbol_name
,
1330 amd_dbgapi_global_address_t
*address
) {
1331 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1332 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1334 auto it
= info
->notify_solib_map
.find (library_id
.handle
);
1335 if (it
== info
->notify_solib_map
.end ())
1336 return AMD_DBGAPI_STATUS_ERROR_INVALID_SHARED_LIBRARY_ID
;
1338 struct so_list
*solib
= it
->second
.solib
;
1340 return AMD_DBGAPI_STATUS_ERROR_LIBRARY_NOT_LOADED
;
1342 solib_read_symbols (solib
, 0);
1343 gdb_assert (solib
->objfile
);
1345 struct bound_minimal_symbol msymbol
1346 = lookup_minimal_symbol (symbol_name
, NULL
, solib
->objfile
);
1348 if (!msymbol
.minsym
|| BMSYMBOL_VALUE_ADDRESS (msymbol
) == 0)
1349 return AMD_DBGAPI_STATUS_ERROR_SYMBOL_NOT_FOUND
;
1351 *address
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1352 return AMD_DBGAPI_STATUS_SUCCESS
;
1355 /* set_breakpoint callback. */
1357 [] (amd_dbgapi_client_process_id_t client_process_id
,
1358 amd_dbgapi_shared_library_id_t shared_library_id
,
1359 amd_dbgapi_global_address_t address
,
1360 amd_dbgapi_breakpoint_id_t breakpoint_id
) {
1361 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1362 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1364 /* Initialize the breakpoint ops lazily since we depend on
1365 bkpt_breakpoint_ops and we can't control the order in which
1366 initializers are called. */
1367 if (rocm_breakpoint_ops
.check_status
== NULL
)
1369 rocm_breakpoint_ops
= bkpt_breakpoint_ops
;
1370 rocm_breakpoint_ops
.check_status
= rocm_breakpoint_check_status
;
1371 rocm_breakpoint_ops
.re_set
= rocm_breakpoint_re_set
;
1374 auto it
= info
->breakpoint_map
.find (breakpoint_id
.handle
);
1375 if (it
!= info
->breakpoint_map
.end ())
1376 return AMD_DBGAPI_STATUS_ERROR_INVALID_BREAKPOINT_ID
;
1378 /* Create a new breakpoint. */
1379 struct obj_section
*section
= find_pc_section (address
);
1380 if (!section
|| !section
->objfile
)
1381 return AMD_DBGAPI_STATUS_ERROR
;
1383 event_location_up location
1384 = new_address_location (address
, nullptr, 0);
1385 if (!create_breakpoint (
1386 get_objfile_arch (section
->objfile
), location
.get (),
1387 /*cond_string*/ NULL
, /*thread*/ -1, /*extra_sring*/ NULL
,
1388 /*parse_extra*/ 0, /*tempflag*/ 0, /*bptype*/ bp_breakpoint
,
1389 /*ignore_count*/ 0, /*pending_break*/ AUTO_BOOLEAN_FALSE
,
1390 /*ops*/ &rocm_breakpoint_ops
, /*from_tty*/ 0,
1391 /*enabled*/ 1, /*internal*/ 1, /*flags*/ 0))
1392 return AMD_DBGAPI_STATUS_ERROR
;
1394 /* Find our breakpoint in the breakpoint list. */
1395 auto bp_loc
= std::make_pair (inf
->aspace
, address
);
1396 auto bp
= breakpoint_find_if (
1397 [] (struct breakpoint
*b
, void *data
) {
1398 auto *arg
= static_cast<decltype (&bp_loc
)> (data
);
1399 if (b
->ops
== &rocm_breakpoint_ops
&& b
->loc
1400 && b
->loc
->pspace
->aspace
== arg
->first
1401 && b
->loc
->address
== arg
->second
)
1405 reinterpret_cast<void *> (&bp_loc
));
1408 error (_ ("Could not find breakpoint"));
1410 info
->breakpoint_map
.emplace (breakpoint_id
.handle
, bp
);
1411 return AMD_DBGAPI_STATUS_SUCCESS
;
1414 /* remove_breakpoint callback. */
1415 .remove_breakpoint
=
1416 [] (amd_dbgapi_client_process_id_t client_process_id
,
1417 amd_dbgapi_breakpoint_id_t breakpoint_id
) {
1418 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1419 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1421 auto it
= info
->breakpoint_map
.find (breakpoint_id
.handle
);
1422 if (it
== info
->breakpoint_map
.end ())
1423 return AMD_DBGAPI_STATUS_ERROR_INVALID_BREAKPOINT_ID
;
1425 delete_breakpoint (it
->second
);
1426 info
->breakpoint_map
.erase (it
);
1428 return AMD_DBGAPI_STATUS_SUCCESS
;
1431 /* set_breakpoint_state callback. */
1432 .set_breakpoint_state
=
1433 [] (amd_dbgapi_client_process_id_t client_process_id
,
1434 amd_dbgapi_breakpoint_id_t breakpoint_id
,
1435 amd_dbgapi_breakpoint_state_t breakpoint_state
) {
1436 inferior
*inf
= static_cast<inferior
*> (client_process_id
);
1437 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1439 auto it
= info
->breakpoint_map
.find (breakpoint_id
.handle
);
1440 if (it
== info
->breakpoint_map
.end ())
1441 return AMD_DBGAPI_STATUS_ERROR_INVALID_BREAKPOINT_ID
;
1443 if (breakpoint_state
== AMD_DBGAPI_BREAKPOINT_STATE_ENABLE
)
1444 it
->second
->enable_state
= bp_enabled
;
1445 else if (breakpoint_state
== AMD_DBGAPI_BREAKPOINT_STATE_DISABLE
)
1446 it
->second
->enable_state
= bp_disabled
;
1448 return AMD_DBGAPI_STATUS_ERROR_INVALID_ARGUMENT
;
1450 return AMD_DBGAPI_STATUS_SUCCESS
;
1454 = [] (amd_dbgapi_log_level_t level
, const char *message
) -> void {
1455 gdb::optional
<target_terminal::scoped_restore_terminal_state
> tstate
;
1457 if (level
> get_debug_amd_dbgapi_log_level ())
1460 if (target_supports_terminal_ours ())
1463 target_terminal::ours_for_output ();
1466 if (filtered_printing_initialized ())
1469 struct ui_file
*out_file
1470 = (level
>= AMD_DBGAPI_LOG_LEVEL_INFO
) ? gdb_stdlog
: gdb_stderr
;
1474 case AMD_DBGAPI_LOG_LEVEL_FATAL_ERROR
:
1475 fputs_unfiltered ("[amd-dbgapi]: ", out_file
);
1477 case AMD_DBGAPI_LOG_LEVEL_WARNING
:
1478 fputs_styled ("[amd-dbgapi]: ", warning_style
.style (), out_file
);
1480 case AMD_DBGAPI_LOG_LEVEL_INFO
:
1481 fputs_styled ("[amd-dbgapi]: ", info_style
.style (), out_file
);
1483 case AMD_DBGAPI_LOG_LEVEL_VERBOSE
:
1484 fputs_styled ("[amd-dbgapi]: ", verbose_style
.style (), out_file
);
1488 fputs_unfiltered (message
, out_file
);
1489 fputs_unfiltered ("\n", out_file
);
1493 /* Implementation of `_wave_id' variable. */
1495 static struct value
*
1496 rocm_wave_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1499 if (ptid_is_gpu (inferior_ptid
))
1501 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
1502 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (inferior_ptid
);
1503 uint32_t group_ids
[3], wave_in_group
;
1505 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
1506 AMD_DBGAPI_WAVE_INFO_WORK_GROUP_COORD
,
1507 sizeof (group_ids
), &group_ids
)
1508 == AMD_DBGAPI_STATUS_SUCCESS
1509 && amd_dbgapi_wave_get_info (
1510 process_id
, wave_id
,
1511 AMD_DBGAPI_WAVE_INFO_WAVE_NUMBER_IN_WORK_GROUP
,
1512 sizeof (wave_in_group
), &wave_in_group
)
1513 == AMD_DBGAPI_STATUS_SUCCESS
)
1515 std::string wave_id_str
1516 = string_printf ("(%d,%d,%d)/%d", group_ids
[2], group_ids
[1],
1517 group_ids
[0], wave_in_group
);
1519 return value_cstring (wave_id_str
.data (), wave_id_str
.length () + 1,
1520 builtin_type (gdbarch
)->builtin_char
);
1524 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
1527 static const struct internalvar_funcs rocm_wave_id_funcs
1528 = { rocm_wave_id_make_value
, NULL
, NULL
};
1530 /* List of set/show debug amd_dbgapi commands. */
1531 struct cmd_list_element
*set_debug_amd_dbgapi_list
;
1532 struct cmd_list_element
*show_debug_amd_dbgapi_list
;
1535 set_debug_amd_dbgapi (const char *arg
, int from_tty
)
1537 help_list (set_debug_amd_dbgapi_list
, "set debug amd-dbgapi ",
1538 (enum command_class
) - 1, gdb_stdout
);
1542 show_debug_amd_dbgapi (const char *args
, int from_tty
)
1544 cmd_show_list (show_debug_amd_dbgapi_list
, from_tty
, "");
1547 constexpr char amd_dbgapi_log_level_off
[] = "off";
1548 constexpr char amd_dbgapi_log_level_error
[] = "error";
1549 constexpr char amd_dbgapi_log_level_warning
[] = "warning";
1550 constexpr char amd_dbgapi_log_level_info
[] = "info";
1551 constexpr char amd_dbgapi_log_level_verbose
[] = "verbose";
1553 constexpr const char *debug_amd_dbgapi_log_level_enums
[]
1554 = { [AMD_DBGAPI_LOG_LEVEL_NONE
] = amd_dbgapi_log_level_off
,
1555 [AMD_DBGAPI_LOG_LEVEL_FATAL_ERROR
] = amd_dbgapi_log_level_error
,
1556 [AMD_DBGAPI_LOG_LEVEL_WARNING
] = amd_dbgapi_log_level_warning
,
1557 [AMD_DBGAPI_LOG_LEVEL_INFO
] = amd_dbgapi_log_level_info
,
1558 [AMD_DBGAPI_LOG_LEVEL_VERBOSE
] = amd_dbgapi_log_level_verbose
,
1561 static const char *debug_amd_dbgapi_log_level
= amd_dbgapi_log_level_error
;
1563 static amd_dbgapi_log_level_t
1564 get_debug_amd_dbgapi_log_level ()
1567 for (pos
= 0; debug_amd_dbgapi_log_level_enums
[pos
]; ++pos
)
1568 if (debug_amd_dbgapi_log_level
== debug_amd_dbgapi_log_level_enums
[pos
])
1571 gdb_assert (debug_amd_dbgapi_log_level_enums
[pos
]);
1572 return static_cast<amd_dbgapi_log_level_t
> (pos
);
1576 set_debug_amd_dbgapi_log_level (const char *args
, int from_tty
,
1577 struct cmd_list_element
*c
)
1579 amd_dbgapi_set_log_level (get_debug_amd_dbgapi_log_level ());
1583 show_debug_amd_dbgapi_log_level (struct ui_file
*file
, int from_tty
,
1584 struct cmd_list_element
*c
,
1587 fprintf_filtered (file
, _ ("The amd-dbgapi log level is %s.\n"), value
);
1591 info_agents_command (const char *args
, int from_tty
)
1593 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
1594 struct ui_out
*uiout
= current_uiout
;
1595 amd_dbgapi_status_t status
;
1597 amd_dbgapi_agent_id_t
*agent_list
;
1600 if (process_id
.handle
!= AMD_DBGAPI_PROCESS_NONE
.handle
1602 = amd_dbgapi_agent_list (process_id
, &count
, &agent_list
, nullptr))
1603 != AMD_DBGAPI_STATUS_SUCCESS
)
1604 error (_ ("amd_dbgapi_agent_list failed (rc=%d)"), status
);
1606 if (!count
&& !uiout
->is_mi_like_p ())
1608 uiout
->field_string (NULL
,
1609 _ ("No agents are currently active.\n"));
1613 /* Calculate the maximum size needed to print the agents names. */
1614 std::vector
<std::string
> agent_names (count
);
1616 size_t max_name_len
= 0;
1617 for (size_t i
= 0; i
< count
; ++i
)
1621 if ((status
= amd_dbgapi_agent_get_info (
1622 process_id
, agent_list
[i
], AMD_DBGAPI_AGENT_INFO_NAME
,
1623 sizeof (agent_name
), &agent_name
))
1624 != AMD_DBGAPI_STATUS_SUCCESS
)
1626 if (status
== AMD_DBGAPI_STATUS_ERROR_INVALID_AGENT_ID
)
1627 agent_names
[i
] = "N/A";
1629 error (_ ("amd_dbgapi_agent_get_info failed (rc=%d"), status
);
1633 agent_names
[i
] = agent_name
;
1637 max_name_len
= std::max (max_name_len
, agent_names
[i
].size ());
1641 ui_out_emit_table
table_emmitter (uiout
, 7, count
, "InfoRocmDevicesTable");
1643 uiout
->table_header (2, ui_left
, "agent_id", "Id");
1644 uiout
->table_header (8, ui_left
, "location_id", "PCI Slot");
1645 uiout
->table_header (std::max (11ul, max_name_len
), ui_left
, "name",
1647 uiout
->table_header (14, ui_left
, "num_se", "Shader Engines");
1648 uiout
->table_header (13, ui_left
, "num_cu", "Compute Units");
1649 uiout
->table_header (7, ui_left
, "simd", "SIMD/CU");
1650 uiout
->table_header (15, ui_left
, "waves", "Wavefronts/SIMD");
1651 uiout
->table_body ();
1654 for (size_t i
= 0; i
< count
; ++i
)
1656 ui_out_emit_tuple
tuple_emitter (uiout
, "InfoRocmDevicesRow");
1659 uiout
->field_signed ("agent_id", agent_list
[i
].handle
);
1662 uint32_t location_id
;
1663 if ((status
= amd_dbgapi_agent_get_info (
1664 process_id
, agent_list
[i
], AMD_DBGAPI_AGENT_INFO_PCIE_SLOT
,
1665 sizeof (location_id
), &location_id
))
1666 != AMD_DBGAPI_STATUS_SUCCESS
)
1668 if (status
== AMD_DBGAPI_STATUS_ERROR_INVALID_AGENT_ID
)
1669 uiout
->field_string ("location_id", "N/A");
1671 error (_ ("amd_dbgapi_agent_get_info failed (rc=%d"), status
);
1674 uiout
->field_string (
1676 string_printf ("%02x:%02x.%d", (location_id
>> 8) & 0xFF,
1677 (location_id
>> 3) & 0x1F, location_id
& 0x7));
1680 uiout
->field_string ("name", agent_names
[i
]);
1682 /* num_se, num_cu, simd, waves */
1684 #define UIOUT_FIELD_INT(name, query) \
1686 if ((status = amd_dbgapi_agent_get_info (process_id, agent_list[i], query, \
1687 sizeof (name), &name)) \
1688 != AMD_DBGAPI_STATUS_SUCCESS) \
1690 if (status == AMD_DBGAPI_STATUS_ERROR_INVALID_AGENT_ID) \
1691 uiout->field_string (#name, "N/A"); \
1693 error (_ ("amd_dbgapi_agent_get_info failed (rc=%d"), status); \
1696 uiout->field_signed (#name, name);
1698 UIOUT_FIELD_INT (num_se
, AMD_DBGAPI_AGENT_INFO_SHADER_ENGINE_COUNT
);
1699 UIOUT_FIELD_INT (num_cu
, AMD_DBGAPI_AGENT_INFO_COMPUTE_UNIT_COUNT
);
1700 UIOUT_FIELD_INT (simd
, AMD_DBGAPI_AGENT_INFO_NUM_SIMD_PER_COMPUTE_UNIT
);
1701 UIOUT_FIELD_INT (waves
, AMD_DBGAPI_AGENT_INFO_MAX_WAVES_PER_SIMD
);
1703 #undef UIOUT_FIELD_INT
1709 gdb_flush (gdb_stdout
);
1712 /* -Wmissing-prototypes */
1713 extern initialize_file_ftype _initialize_rocm_tdep
;
1716 _initialize_rocm_tdep (void)
1718 /* Make sure the loaded debugger library version is greater than or equal to
1719 the one used to build ROCgdb. */
1720 uint32_t major
, minor
, patch
;
1721 amd_dbgapi_get_version (&major
, &minor
, &patch
);
1722 if (major
!= AMD_DBGAPI_VERSION_MAJOR
|| minor
< AMD_DBGAPI_VERSION_MINOR
)
1724 _ ("amd-dbgapi library version mismatch, got %d.%d.%d, need %d.%d+"),
1725 major
, minor
, patch
, AMD_DBGAPI_VERSION_MAJOR
,
1726 AMD_DBGAPI_VERSION_MINOR
);
1728 /* Initialize the ROCm Debug API. */
1729 amd_dbgapi_status_t status
= amd_dbgapi_initialize (&dbgapi_callbacks
);
1730 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
1731 error (_ ("amd-dbgapi failed to initialize (rc=%d)"), status
);
1733 /* Set the initial log level. */
1734 amd_dbgapi_set_log_level (get_debug_amd_dbgapi_log_level ());
1736 /* Install observers. */
1737 gdb::observers::breakpoint_created
.attach (rocm_target_breakpoint_fixup
);
1738 gdb::observers::solib_loaded
.attach (rocm_target_solib_loaded
);
1739 gdb::observers::solib_unloaded
.attach (rocm_target_solib_unloaded
);
1740 gdb::observers::inferior_created
.attach (rocm_target_inferior_created
);
1741 gdb::observers::inferior_exit
.attach (rocm_target_inferior_exit
);
1743 amd_dbgapi_activated
.attach (rocm_target_dbgapi_activated
);
1744 amd_dbgapi_deactivated
.attach (rocm_target_dbgapi_deactivated
);
1746 create_internalvar_type_lazy ("_wave_id", &rocm_wave_id_funcs
, NULL
);
1749 "amd-dbgapi", no_class
, set_debug_amd_dbgapi
,
1750 _ ("Generic command for setting amd-dbgapi debugging flags"),
1751 &set_debug_amd_dbgapi_list
, "set debug amd-dbgapi ", 0, &setdebuglist
);
1754 "amd-dbgapi", no_class
, show_debug_amd_dbgapi
,
1755 _ ("Generic command for showing amd-dbgapi debugging flags"),
1756 &show_debug_amd_dbgapi_list
, "show debug amd-dbgapi ", 0,
1759 add_setshow_enum_cmd (
1760 "log-level", class_maintenance
, debug_amd_dbgapi_log_level_enums
,
1761 &debug_amd_dbgapi_log_level
, _ ("Set the amd-dbgapi log level."),
1762 _ ("Show the amd-dbgapi log level."),
1763 _ ("off == no logging is enabled\n"
1764 "error == fatal errors are reported\n"
1765 "warning == fatal errors and warnings are reported\n"
1766 "info == fatal errors, warnings, and info messages are reported\n"
1767 "verbose == all messages are reported"),
1768 set_debug_amd_dbgapi_log_level
, show_debug_amd_dbgapi_log_level
,
1769 &set_debug_amd_dbgapi_list
, &show_debug_amd_dbgapi_list
);
1771 add_cmd ("agents", class_info
, info_agents_command
,
1772 _ ("Info about currently active agents."), &infolist
);