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 /* True if the target is activated. */
79 bool activated
{ false };
81 /* The amd_dbgapi_process_id for this inferior. */
82 amd_dbgapi_process_id_t process_id
{ AMD_DBGAPI_PROCESS_NONE
};
84 /* The amd_dbgapi_notifier_t for this inferior. */
85 amd_dbgapi_notifier_t notifier
{ -1 };
87 /* True if commit_resume should all-start the GPU queues. */
88 bool commit_resume_all_start
;
90 /* True if the inferior has exited. */
91 bool has_exited
{ false };
93 std::unordered_map
<decltype (amd_dbgapi_breakpoint_id_t::handle
),
97 /* List of pending events the rocm target retrieved from the dbgapi. */
98 std::list
<std::pair
<amd_dbgapi_wave_id_t
, amd_dbgapi_wave_stop_reason_t
>>
101 /* Map of rocm_notify_shared_library_info's for libraries that have been
102 registered to receive notifications when loading/unloading. */
103 std::unordered_map
<decltype (amd_dbgapi_shared_library_id_t::handle
),
104 struct rocm_notify_shared_library_info
>
108 static amd_dbgapi_event_id_t
109 rocm_process_event_queue (amd_dbgapi_event_kind_t until_event_kind
110 = AMD_DBGAPI_EVENT_KIND_NONE
);
112 /* Return the inferior's rocm_inferior_info struct. */
113 static struct rocm_inferior_info
*
114 get_rocm_inferior_info (struct inferior
*inferior
= nullptr);
116 static const target_info rocm_ops_info
117 = { "rocm", N_ ("ROCm GPU debugging support"),
118 N_ ("ROCm GPU debugging support") };
120 static amd_dbgapi_log_level_t
get_debug_amd_dbgapi_log_level ();
122 struct rocm_target_ops final
: public target_ops
125 info () const override
127 return rocm_ops_info
;
130 stratum () const override
135 void close () override
;
136 void mourn_inferior () override
;
137 void detach (inferior
*inf
, int from_tty
) override
;
139 void async (int enable
) override
;
141 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
142 void resume (ptid_t
, int, enum gdb_signal
) override
;
143 void commit_resume () override
;
144 void stop (ptid_t ptid
) override
;
146 void fetch_registers (struct regcache
*, int) override
;
147 void store_registers (struct regcache
*, int) override
;
149 void update_thread_list () override
;
151 struct gdbarch
*thread_architecture (ptid_t
) override
;
153 std::string
pid_to_str (ptid_t ptid
) override
;
155 const char *thread_name (thread_info
*tp
) override
;
157 const char *extra_thread_info (thread_info
*tp
) override
;
159 bool thread_alive (ptid_t ptid
) override
;
161 enum target_xfer_status
xfer_partial (enum target_object object
,
162 const char *annex
, gdb_byte
*readbuf
,
163 const gdb_byte
*writebuf
,
164 ULONGEST offset
, ULONGEST len
,
165 ULONGEST
*xfered_len
) override
;
168 stopped_by_watchpoint () override
170 return !ptid_is_gpu (inferior_ptid
)
171 && beneath ()->stopped_by_watchpoint ();
175 stopped_data_address (CORE_ADDR
*addr_p
) override
177 return !ptid_is_gpu (inferior_ptid
)
178 && beneath ()->stopped_data_address (addr_p
);
182 supports_stopped_by_sw_breakpoint () override
187 bool stopped_by_sw_breakpoint () override
;
190 stopped_by_hw_breakpoint () override
192 return !ptid_is_gpu (inferior_ptid
)
193 && beneath ()->stopped_by_hw_breakpoint ();
197 /* ROCm's target vector. */
198 static struct rocm_target_ops rocm_ops
;
200 /* ROCm breakpoint ops. */
201 static struct breakpoint_ops rocm_breakpoint_ops
;
203 /* Per-inferior data key. */
204 static const struct inferior_key
<rocm_inferior_info
> rocm_inferior_data
;
206 /* The read/write ends of the pipe registered as waitable file in the
208 static int rocm_event_pipe
[2] = { -1, -1 };
210 /* Flush the event pipe. */
213 async_file_flush (void)
220 ret
= read (rocm_event_pipe
[0], &buf
, 1);
222 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
225 /* Put something (anything, doesn't matter what, or how much) in event
226 pipe, so that the select/poll in the event-loop realizes we have
227 something to process. */
230 async_file_mark (void)
234 /* It doesn't really matter what the pipe contains, as long we end
235 up with something in it. Might as well flush the previous
241 ret
= write (rocm_event_pipe
[1], "+", 1);
243 while (ret
== -1 && errno
== EINTR
);
245 /* Ignore EAGAIN. If the pipe is full, the event loop will already
246 be awakened anyway. */
249 /* Fetch the rocm_inferior_info data for the given inferior. */
251 static struct rocm_inferior_info
*
252 get_rocm_inferior_info (struct inferior
*inferior
)
255 inferior
= current_inferior ();
257 struct rocm_inferior_info
*info
= rocm_inferior_data
.get (inferior
);
260 info
= rocm_inferior_data
.emplace (inferior
);
265 /* Fetch the amd_dbgapi_process_id for the given inferior. */
267 amd_dbgapi_process_id_t
268 get_amd_dbgapi_process_id (struct inferior
*inferior
)
270 return get_rocm_inferior_info (inferior
)->process_id
;
274 rocm_breakpoint_re_set (struct breakpoint
*b
)
279 rocm_breakpoint_check_status (struct bpstats
*bs
)
281 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
282 amd_dbgapi_process_id_t process_id
= info
->process_id
;
283 amd_dbgapi_status_t status
;
286 bs
->print_it
= print_it_noop
;
288 /* Find the address the breakpoint is set at. */
289 auto it
= std::find_if (
290 info
->breakpoint_map
.begin (), info
->breakpoint_map
.end (),
291 [=] (const decltype (info
->breakpoint_map
)::value_type
&value
) {
292 return value
.second
== bs
->breakpoint_at
;
295 if (it
== info
->breakpoint_map
.end ())
296 error (_ ("Could not find breakpoint_id for breakpoint at %#lx"),
297 bs
->bp_location_at
->address
);
299 amd_dbgapi_breakpoint_id_t breakpoint_id
{ it
->first
};
300 amd_dbgapi_breakpoint_action_t action
;
302 status
= amd_dbgapi_report_breakpoint_hit (
303 process_id
, breakpoint_id
,
304 reinterpret_cast<amd_dbgapi_client_thread_id_t
> (inferior_thread ()),
307 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
308 error (_ ("amd_dbgapi_report_breakpoint_hit failed: breakpoint_%ld "
310 breakpoint_id
.handle
, bs
->bp_location_at
->address
, status
);
312 if (action
== AMD_DBGAPI_BREAKPOINT_ACTION_RESUME
)
315 /* If the action is AMD_DBGAPI_BREAKPOINT_ACTION_HALT, we need to wait until
316 a breakpoint resume event for this breakpoint_id is seen. */
318 amd_dbgapi_event_id_t resume_event_id
319 = rocm_process_event_queue (AMD_DBGAPI_EVENT_KIND_BREAKPOINT_RESUME
);
321 /* We should always get a breakpoint_resume event after processing all
322 events generated by reporting the breakpoint was hit. */
323 gdb_assert (resume_event_id
.handle
!= AMD_DBGAPI_EVENT_NONE
.handle
);
325 amd_dbgapi_breakpoint_id_t resume_breakpoint_id
;
326 status
= amd_dbgapi_event_get_info (
327 process_id
, resume_event_id
, AMD_DBGAPI_EVENT_INFO_BREAKPOINT
,
328 sizeof (resume_breakpoint_id
), &resume_breakpoint_id
);
330 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
331 error (_ ("amd_dbgapi_event_get_info failed (rc=%d)"), status
);
333 /* The debugger API guarantees that [breakpoint_hit...resume_breakpoint]
334 sequences cannot interleave, so this breakpoint resume event must be
335 for our breakpoint_id. */
336 if (resume_breakpoint_id
.handle
!= breakpoint_id
.handle
)
337 error (_ ("breakpoint resume event is not for this breakpoint. "
338 "Expected breakpoint_%ld, got breakpoint_%ld"),
339 breakpoint_id
.handle
, resume_breakpoint_id
.handle
);
341 amd_dbgapi_event_processed (process_id
, resume_event_id
);
345 rocm_target_dbgapi_activated ()
347 /* FIXME: only push on the first activation. */
348 /* Engage the ROCm target_ops and so_ops. */
349 push_target (&rocm_ops
);
353 rocm_target_dbgapi_deactivated ()
355 /* FIXME: only unpush on the last activation. */
356 /* Disengage the ROCm target_ops. */
357 unpush_target (&rocm_ops
);
361 rocm_target_ops::thread_alive (ptid_t ptid
)
363 if (!ptid_is_gpu (ptid
))
364 return beneath ()->thread_alive (ptid
);
366 /* Check that the wave_id is valid. */
368 inferior
*inf
= find_inferior_ptid (ptid
);
372 amd_dbgapi_wave_state_t state
;
373 return amd_dbgapi_wave_get_info (
374 get_amd_dbgapi_process_id (inf
), get_amd_dbgapi_wave_id (ptid
),
375 AMD_DBGAPI_WAVE_INFO_STATE
, sizeof (state
), &state
)
376 == AMD_DBGAPI_STATUS_SUCCESS
;
380 rocm_target_ops::thread_name (thread_info
*tp
)
382 if (!ptid_is_gpu (tp
->ptid
))
383 return beneath ()->thread_name (tp
);
385 /* Return the process's comm value—that is, the command name associated with
389 xsnprintf (comm_path
, sizeof (comm_path
), "/proc/%ld/comm",
390 (long)tp
->ptid
.pid ());
392 gdb_file_up comm_file
= gdb_fopen_cloexec (comm_path
, "r");
396 #if !defined(TASK_COMM_LEN)
397 #define TASK_COMM_LEN 16 /* As defined in the kernel's sched.h. */
400 static char comm_buf
[TASK_COMM_LEN
];
401 const char *comm_value
;
403 comm_value
= fgets (comm_buf
, sizeof (comm_buf
), comm_file
.get ());
404 comm_buf
[sizeof (comm_buf
) - 1] = '\0';
406 /* Make sure there is no newline at the end. */
409 for (int i
= 0; i
< sizeof (comm_buf
); i
++)
410 if (comm_buf
[i
] == '\n')
421 rocm_target_ops::pid_to_str (ptid_t ptid
)
423 if (!ptid_is_gpu (ptid
))
425 return beneath ()->pid_to_str (ptid
);
428 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
429 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (ptid
);
430 amd_dbgapi_dispatch_id_t dispatch_id
;
431 uint32_t group_ids
[3], wave_in_group
;
433 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
434 AMD_DBGAPI_WAVE_INFO_DISPATCH
,
435 sizeof (dispatch_id
), &dispatch_id
)
436 != AMD_DBGAPI_STATUS_SUCCESS
437 || amd_dbgapi_wave_get_info (process_id
, wave_id
,
438 AMD_DBGAPI_WAVE_INFO_WORK_GROUP_COORD
,
439 sizeof (group_ids
), &group_ids
)
440 != AMD_DBGAPI_STATUS_SUCCESS
441 || amd_dbgapi_wave_get_info (
443 AMD_DBGAPI_WAVE_INFO_WAVE_NUMBER_IN_WORK_GROUP
,
444 sizeof (wave_in_group
), &wave_in_group
)
445 != AMD_DBGAPI_STATUS_SUCCESS
)
446 return std::string ("AMDGPU Thread");
448 return string_printf ("AMDGPU Thread %ld.%ld (%d,%d,%d)/%d",
449 dispatch_id
.handle
, wave_id
.handle
, group_ids
[2],
450 group_ids
[1], group_ids
[0], wave_in_group
);
454 rocm_target_ops::extra_thread_info (thread_info
*tp
)
456 if (!ptid_is_gpu (tp
->ptid
))
457 beneath ()->extra_thread_info (tp
);
462 enum target_xfer_status
463 rocm_target_ops::xfer_partial (enum target_object object
, const char *annex
,
464 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
465 ULONGEST offset
, ULONGEST requested_len
,
466 ULONGEST
*xfered_len
)
468 gdb::optional
<scoped_restore_current_thread
> maybe_restore_thread
;
470 if (ptid_is_gpu (inferior_ptid
))
472 gdb_assert (requested_len
&& xfered_len
&& "checking invariants");
474 if (object
!= TARGET_OBJECT_MEMORY
)
475 return TARGET_XFER_E_IO
;
477 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
478 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (inferior_ptid
);
480 amd_dbgapi_architecture_id_t architecture_id
;
481 amd_dbgapi_address_space_id_t address_space_id
;
483 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
484 AMD_DBGAPI_WAVE_INFO_ARCHITECTURE
,
485 sizeof (architecture_id
), &architecture_id
)
486 != AMD_DBGAPI_STATUS_SUCCESS
487 || amd_dbgapi_architecture_get_info (
489 AMD_DBGAPI_ARCHITECTURE_INFO_DEFAULT_GLOBAL_ADDRESS_SPACE
,
490 sizeof (address_space_id
), &address_space_id
)
491 != AMD_DBGAPI_STATUS_SUCCESS
)
492 return TARGET_XFER_EOF
;
494 size_t len
= requested_len
;
495 amd_dbgapi_status_t status
;
498 status
= amd_dbgapi_read_memory (
499 process_id
, wave_id
, 0, address_space_id
, offset
, &len
, readbuf
);
501 status
= amd_dbgapi_write_memory (
502 process_id
, wave_id
, 0, address_space_id
, offset
, &len
, writebuf
);
504 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
505 return TARGET_XFER_EOF
;
508 return TARGET_XFER_OK
;
511 return beneath ()->xfer_partial (object
, annex
, readbuf
, writebuf
, offset
,
512 requested_len
, xfered_len
);
516 rocm_target_ops::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
518 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
523 "\e[1;34minfrun: rocm_target_ops::resume ([%d,%ld,%ld])\e[0m\n",
524 ptid
.pid (), ptid
.lwp (), ptid
.tid ());
526 /* Check if the thread focus is on the GPU device. */
527 if (ptid
== minus_one_ptid
|| !ptid_is_gpu (ptid
))
529 beneath ()->resume (ptid
, step
, signo
);
530 if (ptid
!= minus_one_ptid
)
534 /* A specific PTID means `step only this process id'. */
535 bool resume_one
= ptid
!= minus_one_ptid
&& !ptid
.is_pid ();
536 gdb_assert (resume_one
|| !step
);
539 error (_ ("internal error - unimplemented "));
541 amd_dbgapi_process_set_progress (info
->process_id
,
542 AMD_DBGAPI_PROGRESS_NO_FORWARD
);
544 amd_dbgapi_status_t status
= amd_dbgapi_wave_resume (
545 info
->process_id
, get_amd_dbgapi_wave_id (ptid
),
546 step
? AMD_DBGAPI_RESUME_MODE_SINGLE_STEP
547 : AMD_DBGAPI_RESUME_MODE_NORMAL
);
548 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
549 warning (_ ("Could not resume %s (rc=%d)"),
550 target_pid_to_str (ptid
).c_str (), status
);
552 info
->commit_resume_all_start
= true;
556 rocm_target_ops::commit_resume ()
558 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
563 "\e[1;34minfrun: rocm_target_ops::commit_resume ()\e[0m\n");
565 beneath ()->commit_resume ();
567 if (info
->commit_resume_all_start
)
569 amd_dbgapi_process_set_progress (info
->process_id
,
570 AMD_DBGAPI_PROGRESS_NORMAL
);
571 info
->commit_resume_all_start
= false;
574 if (target_can_async_p ())
579 rocm_target_stop_one_wave (ptid_t ptid
)
581 amd_dbgapi_status_t status
;
583 status
= amd_dbgapi_wave_stop (get_amd_dbgapi_process_id (),
584 get_amd_dbgapi_wave_id (ptid
));
586 if (status
== AMD_DBGAPI_STATUS_ERROR_INVALID_WAVE_ID
)
588 /* the wave must have exited, set the thread status to reflect that. */
589 auto *tp
= find_thread_ptid (ptid
);
592 tp
->state
= THREAD_EXITED
;
594 else if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
595 warning (_ ("Could not stop %s (rc=%d)"),
596 target_pid_to_str (ptid
).c_str (), status
);
600 rocm_target_ops::stop (ptid_t ptid
)
605 "\e[1;34minfrun: rocm_target_ops::stop ([%d,%ld,%ld])\e[0m\n",
606 ptid
.pid (), ptid
.lwp (), ptid
.tid ());
608 if (ptid
== minus_one_ptid
|| !ptid_is_gpu (ptid
))
610 beneath ()->stop (ptid
);
611 if (ptid
!= minus_one_ptid
)
615 if (ptid
== minus_one_ptid
)
616 error (_ ("internal error - unimplemented "));
618 rocm_target_stop_one_wave (ptid
);
622 handle_target_event (int error
, gdb_client_data client_data
)
624 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
625 amd_dbgapi_process_id_t process_id
= info
->process_id
;
627 amd_dbgapi_process_set_progress (process_id
, AMD_DBGAPI_PROGRESS_NO_FORWARD
);
629 /* Flush the async file first. */
630 if (target_is_async_p ())
633 rocm_process_event_queue ();
635 /* In all-stop mode, unless the event queue is empty (spurious wake-up),
636 we can keep the process in progress_no_forward mode. The infrun loop
637 will enable forward progress when a thread is resumed. */
638 if (non_stop
|| info
->wave_stop_events
.empty ())
639 amd_dbgapi_process_set_progress (process_id
, AMD_DBGAPI_PROGRESS_NORMAL
);
641 if (!info
->wave_stop_events
.empty ())
642 inferior_event_handler (INF_REG_EVENT
, nullptr);
646 rocm_target_ops::async (int enable
)
648 beneath ()->async (enable
);
652 if (rocm_event_pipe
[0] != -1)
655 if (gdb_pipe_cloexec (rocm_event_pipe
) == -1)
656 internal_error (__FILE__
, __LINE__
, "creating event pipe failed.");
658 ::fcntl (rocm_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
659 ::fcntl (rocm_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
661 add_file_handler (rocm_event_pipe
[0], handle_target_event
, nullptr);
663 /* There may be pending events to handle. Tell the event loop
669 if (rocm_event_pipe
[0] == -1)
672 delete_file_handler (rocm_event_pipe
[0]);
674 ::close (rocm_event_pipe
[0]);
675 ::close (rocm_event_pipe
[1]);
676 rocm_event_pipe
[0] = -1;
677 rocm_event_pipe
[1] = -1;
682 rocm_process_one_event (amd_dbgapi_event_id_t event_id
,
683 amd_dbgapi_event_kind_t event_kind
)
685 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
686 amd_dbgapi_process_id_t process_id
= info
->process_id
;
687 amd_dbgapi_status_t status
;
691 case AMD_DBGAPI_EVENT_KIND_WAVE_STOP
:
693 amd_dbgapi_wave_id_t wave_id
;
694 if ((status
= amd_dbgapi_event_get_info (process_id
, event_id
,
695 AMD_DBGAPI_EVENT_INFO_WAVE
,
696 sizeof (wave_id
), &wave_id
))
697 != AMD_DBGAPI_STATUS_SUCCESS
)
698 error (_ ("event_get_info for event_%ld failed (rc=%d)"),
699 event_id
.handle
, status
);
701 amd_dbgapi_wave_stop_reason_t stop_reason
;
702 status
= amd_dbgapi_wave_get_info (process_id
, wave_id
,
703 AMD_DBGAPI_WAVE_INFO_STOP_REASON
,
704 sizeof (stop_reason
), &stop_reason
);
706 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
707 && status
!= AMD_DBGAPI_STATUS_ERROR_INVALID_WAVE_ID
)
708 error (_ ("wave_get_info for wave_%ld failed (rc=%d)"),
709 wave_id
.handle
, status
);
711 /* The wave may have exited, or the queue went into an error
712 state. In such cases, we will see another wave command
713 terminated event, and handle the wave termination then. */
715 if (status
== AMD_DBGAPI_STATUS_SUCCESS
)
716 info
->wave_stop_events
.emplace_back (
717 std::make_pair (wave_id
, stop_reason
));
721 case AMD_DBGAPI_EVENT_KIND_CODE_OBJECT_LIST_UPDATED
:
722 amd_dbgapi_code_object_list_updated
.notify ();
725 case AMD_DBGAPI_EVENT_KIND_BREAKPOINT_RESUME
:
726 /* Breakpoint resume events should be handled by the breakpoint
727 action, and this code should not reach this. */
728 gdb_assert_not_reached (_ ("unhandled event kind"));
731 case AMD_DBGAPI_EVENT_KIND_RUNTIME
:
733 amd_dbgapi_runtime_state_t runtime_state
;
735 if ((status
= amd_dbgapi_event_get_info (
736 process_id
, event_id
, AMD_DBGAPI_EVENT_INFO_RUNTIME_STATE
,
737 sizeof (runtime_state
), &runtime_state
))
738 != AMD_DBGAPI_STATUS_SUCCESS
)
739 error (_ ("event_get_info for event_%ld failed (rc=%d)"),
740 event_id
.handle
, status
);
742 switch (runtime_state
)
744 case AMD_DBGAPI_RUNTIME_STATE_LOADED_UNSUPPORTED
:
745 warning (_ ("ROCgdb: low-level runtime version not supported"));
748 case AMD_DBGAPI_RUNTIME_STATE_LOADED_SUPPORTED
:
749 amd_dbgapi_activated
.notify ();
750 info
->activated
= true;
753 case AMD_DBGAPI_RUNTIME_STATE_UNLOADED
:
755 amd_dbgapi_deactivated
.notify ();
762 error (_ ("event kind (%d) not supported"), event_kind
);
765 amd_dbgapi_event_processed (process_id
, event_id
);
768 /* Drain the amd_dbgapi event queue until an event of the given type is seen.
769 If no particular event kind is specified (AMD_DBGAPI_EVENT_KIND_NONE), the
770 event queue is completely drained. Wave stop events that are not returned
771 are re-queued into the current's process pending wave events. */
772 static amd_dbgapi_event_id_t
773 rocm_process_event_queue (amd_dbgapi_event_kind_t until_event_kind
)
775 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
779 amd_dbgapi_event_id_t event_id
;
780 amd_dbgapi_event_kind_t event_kind
;
782 amd_dbgapi_status_t status
= amd_dbgapi_next_pending_event (
783 info
->process_id
, &event_id
, &event_kind
);
785 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
786 error (_ ("next_pending_event failed (rc=%d)"), status
);
788 if (event_id
.handle
== AMD_DBGAPI_EVENT_NONE
.handle
789 || event_kind
== until_event_kind
)
792 rocm_process_one_event (event_id
, event_kind
);
797 rocm_target_ops::wait (ptid_t ptid
, struct target_waitstatus
*ws
,
801 fprintf_unfiltered (gdb_stdlog
,
802 "\e[1;34minfrun: rocm_target_ops::wait\e[0m\n");
804 if (!ptid_is_gpu (ptid
))
806 ptid_t event_ptid
= beneath ()->wait (ptid
, ws
, target_options
);
807 if (event_ptid
!= minus_one_ptid
)
811 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
812 amd_dbgapi_process_id_t process_id
= info
->process_id
;
814 /* Drain all the events from the amd_dbgapi, and preserve the ordering. */
815 if (info
->wave_stop_events
.empty ())
817 amd_dbgapi_process_set_progress (process_id
,
818 AMD_DBGAPI_PROGRESS_NO_FORWARD
);
820 /* Flush the async file first. */
821 if (target_is_async_p ())
824 rocm_process_event_queue ();
826 /* In all-stop mode, unless the event queue is empty (spurious wake-up),
827 we can keep the process in progress_no_forward mode. The infrun loop
828 will enable forward progress when a thread is resumed. */
829 if (non_stop
|| info
->wave_stop_events
.empty ())
830 amd_dbgapi_process_set_progress (process_id
,
831 AMD_DBGAPI_PROGRESS_NORMAL
);
834 if (info
->wave_stop_events
.empty ())
835 return minus_one_ptid
;
837 amd_dbgapi_wave_id_t event_wave_id
;
838 amd_dbgapi_wave_stop_reason_t stop_reason
;
840 std::tie (event_wave_id
, stop_reason
) = info
->wave_stop_events
.front ();
841 info
->wave_stop_events
.pop_front ();
843 ptid_t
event_ptid (current_inferior ()->pid
, 1, event_wave_id
.handle
);
845 if (!find_thread_ptid (event_ptid
))
847 add_thread_silent (event_ptid
);
848 set_running (event_ptid
, 1);
849 set_executing (event_ptid
, 1);
852 /* Since we are manipulating the register cache for the event thread,
853 make sure it is the current thread. */
854 switch_to_thread (event_ptid
);
856 /* By caching the PC now, we avoid having to suspend/resume the queue
857 later when we need to access it. */
858 amd_dbgapi_global_address_t stop_pc
;
859 if (amd_dbgapi_wave_get_info (process_id
, event_wave_id
,
860 AMD_DBGAPI_WAVE_INFO_PC
, sizeof (stop_pc
),
862 == AMD_DBGAPI_STATUS_SUCCESS
)
864 struct regcache
*regcache
= get_thread_regcache (event_ptid
);
865 regcache
->raw_supply (gdbarch_pc_regnum (regcache
->arch ()), &stop_pc
);
867 ws
->kind
= TARGET_WAITKIND_STOPPED
;
869 if (stop_reason
& AMD_DBGAPI_WAVE_STOP_REASON_MEMORY_VIOLATION
)
870 ws
->value
.sig
= GDB_SIGNAL_SEGV
;
872 & (AMD_DBGAPI_WAVE_STOP_REASON_FP_INPUT_DENORMAL
873 | AMD_DBGAPI_WAVE_STOP_REASON_FP_DIVIDE_BY_0
874 | AMD_DBGAPI_WAVE_STOP_REASON_FP_OVERFLOW
875 | AMD_DBGAPI_WAVE_STOP_REASON_FP_UNDERFLOW
876 | AMD_DBGAPI_WAVE_STOP_REASON_FP_INEXACT
877 | AMD_DBGAPI_WAVE_STOP_REASON_FP_INVALID_OPERATION
878 | AMD_DBGAPI_WAVE_STOP_REASON_INT_DIVIDE_BY_0
))
879 ws
->value
.sig
= GDB_SIGNAL_FPE
;
881 & (AMD_DBGAPI_WAVE_STOP_REASON_BREAKPOINT
882 | AMD_DBGAPI_WAVE_STOP_REASON_SINGLE_STEP
883 | AMD_DBGAPI_WAVE_STOP_REASON_DEBUG_TRAP
884 | AMD_DBGAPI_WAVE_STOP_REASON_ASSERT_TRAP
885 | AMD_DBGAPI_WAVE_STOP_REASON_TRAP
))
886 ws
->value
.sig
= GDB_SIGNAL_TRAP
;
888 ws
->value
.sig
= GDB_SIGNAL_0
;
890 /* If there are more events in the list, mark the async file so that
891 rocm_target_ops::wait gets called again. */
892 if (target_is_async_p () && !info
->wave_stop_events
.empty ())
899 rocm_target_ops::stopped_by_sw_breakpoint ()
901 if (!ptid_is_gpu (inferior_ptid
))
902 return beneath ()->supports_stopped_by_sw_breakpoint ()
903 && beneath ()->stopped_by_sw_breakpoint ();
905 /* FIXME: we should check that the wave is not single-stepping. */
907 struct regcache
*regcache
= get_thread_regcache (inferior_ptid
);
909 CORE_ADDR bkpt_pc
= regcache_read_pc (regcache
)
910 - gdbarch_decr_pc_after_break (regcache
->arch ());
912 return software_breakpoint_inserted_here_p (regcache
->aspace (), bkpt_pc
);
916 rocm_target_ops::close ()
918 /* Unregister from the event loop. */
920 beneath ()->close ();
924 rocm_target_ops::mourn_inferior ()
926 auto *info
= get_rocm_inferior_info ();
927 info
->has_exited
= true;
930 amd_dbgapi_deactivated
.notify ();
931 amd_dbgapi_process_detach (info
->process_id
);
932 info
->process_id
= AMD_DBGAPI_PROCESS_NONE
;
934 beneath ()->mourn_inferior ();
936 /* FIXME: only unpush on the last activation. */
937 /* Disengage the ROCm target_ops. */
938 unpush_target (&rocm_ops
);
942 rocm_target_ops::detach (inferior
*inf
, int from_tty
)
944 auto *info
= get_rocm_inferior_info ();
947 amd_dbgapi_deactivated
.notify ();
948 amd_dbgapi_process_detach (info
->process_id
);
949 info
->process_id
= AMD_DBGAPI_PROCESS_NONE
;
951 beneath ()->detach (inf
, from_tty
);
955 rocm_target_ops::fetch_registers (struct regcache
*regcache
, int regno
)
957 struct gdbarch
*gdbarch
= regcache
->arch ();
958 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
960 /* delegate to the host routines when not on the device */
962 if (!rocm_is_amdgcn_gdbarch (gdbarch
))
964 beneath ()->fetch_registers (regcache
, regno
);
968 inferior
*inf
= find_inferior_ptid (regcache
->ptid ());
969 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id (inf
);
970 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (regcache
->ptid ());
972 gdb_byte raw
[AMDGCN_MAX_REGISTER_SIZE
];
974 amd_dbgapi_status_t status
= amd_dbgapi_read_register (
975 process_id
, wave_id
, tdep
->register_ids
[regno
], 0,
976 TYPE_LENGTH (register_type (gdbarch
, regno
)), raw
);
978 if (status
== AMD_DBGAPI_STATUS_SUCCESS
)
980 regcache
->raw_supply (regno
, raw
);
982 else if (status
!= AMD_DBGAPI_STATUS_ERROR_INVALID_REGISTER_ID
)
984 warning (_ ("Couldn't read register %s (#%d)."),
985 gdbarch_register_name (gdbarch
, regno
), regno
);
990 rocm_target_ops::store_registers (struct regcache
*regcache
, int regno
)
992 struct gdbarch
*gdbarch
= regcache
->arch ();
993 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
994 gdb_byte raw
[AMDGCN_MAX_REGISTER_SIZE
];
996 if (!rocm_is_amdgcn_gdbarch (gdbarch
))
998 beneath ()->store_registers (regcache
, regno
);
1002 inferior
*inf
= find_inferior_ptid (regcache
->ptid ());
1003 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id (inf
);
1004 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (regcache
->ptid ());
1006 regcache
->raw_collect (regno
, &raw
);
1008 amd_dbgapi_status_t status
= amd_dbgapi_write_register (
1009 process_id
, wave_id
, tdep
->register_ids
[regno
], 0,
1010 TYPE_LENGTH (register_type (gdbarch
, regno
)), raw
);
1012 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
1014 warning (_ ("Couldn't write register %s (#%d)."),
1015 gdbarch_register_name (gdbarch
, regno
), regno
);
1019 /* Fix breakpoints created with an address location while the
1020 architecture was set to the host (could be fixed in core GDB). */
1023 rocm_target_breakpoint_fixup (struct breakpoint
*b
)
1025 if (b
->location
.get ()
1026 && event_location_type (b
->location
.get ()) == ADDRESS_LOCATION
1027 && gdbarch_bfd_arch_info (b
->loc
->gdbarch
)->arch
== bfd_arch_amdgcn
1028 && gdbarch_bfd_arch_info (b
->gdbarch
)->arch
!= bfd_arch_amdgcn
)
1030 b
->gdbarch
= b
->loc
->gdbarch
;
1035 rocm_target_ops::thread_architecture (ptid_t ptid
)
1037 static std::result_of
<decltype (&ptid_t::tid
) (ptid_t
)>::type last_tid
= 0;
1038 static struct gdbarch
*cached_arch
= nullptr;
1040 if (!ptid_is_gpu (ptid
))
1041 return beneath ()->thread_architecture (ptid
);
1043 /* We can cache the gdbarch for a given wave_id (ptid::tid) because
1044 wave IDs are unique, and aren't reused. */
1045 if (ptid
.tid () == last_tid
)
1048 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
1049 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (ptid
);
1050 amd_dbgapi_architecture_id_t architecture_id
;
1052 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
1053 AMD_DBGAPI_WAVE_INFO_ARCHITECTURE
,
1054 sizeof (architecture_id
), &architecture_id
)
1055 != AMD_DBGAPI_STATUS_SUCCESS
)
1056 error (_ ("Couldn't get architecture for wave_%ld"), ptid
.tid ());
1058 uint32_t elf_amdgpu_machine
;
1059 if (amd_dbgapi_architecture_get_info (
1060 architecture_id
, AMD_DBGAPI_ARCHITECTURE_INFO_ELF_AMDGPU_MACHINE
,
1061 sizeof (elf_amdgpu_machine
), &elf_amdgpu_machine
)
1062 != AMD_DBGAPI_STATUS_SUCCESS
)
1063 error (_ ("Couldn't get elf_amdgpu_machine for architecture_%ld"),
1064 architecture_id
.handle
);
1066 struct gdbarch_info info
;
1067 gdbarch_info_init (&info
);
1069 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_amdgcn
, elf_amdgpu_machine
);
1070 info
.byte_order
= BFD_ENDIAN_LITTLE
;
1071 info
.osabi
= GDB_OSABI_AMDGPU_HSA
;
1073 last_tid
= ptid
.tid ();
1074 if (!(cached_arch
= gdbarch_find_by_info (info
)))
1075 error (_ ("Couldn't get elf_amdgpu_machine (%#x)"), elf_amdgpu_machine
);
1081 rocm_target_ops::update_thread_list ()
1083 for (inferior
*inf
: all_inferiors ())
1085 amd_dbgapi_process_id_t process_id
;
1086 amd_dbgapi_wave_id_t
*wave_list
;
1089 process_id
= get_amd_dbgapi_process_id (inf
);
1090 if (process_id
.handle
== AMD_DBGAPI_PROCESS_NONE
.handle
)
1092 /* The inferior may not be attached yet. */
1096 amd_dbgapi_changed_t changed
;
1097 amd_dbgapi_status_t status
;
1099 = amd_dbgapi_wave_list (process_id
, &count
, &wave_list
, &changed
))
1100 != AMD_DBGAPI_STATUS_SUCCESS
)
1101 error (_ ("amd_dbgapi_wave_list failed (rc=%d)"), status
);
1103 if (changed
== AMD_DBGAPI_CHANGED_NO
)
1106 /* Create a set and free the wave list. */
1107 std::set
<std::result_of
<decltype (&ptid_t::tid
) (ptid_t
)>::type
> threads
;
1108 for (size_t i
= 0; i
< count
; ++i
)
1109 threads
.emplace (wave_list
[i
].handle
);
1112 /* Then prune the wave_ids that already have a thread_info. */
1113 for (thread_info
*tp
: inf
->non_exited_threads ())
1114 if (ptid_is_gpu (tp
->ptid
))
1115 threads
.erase (tp
->ptid
.tid ());
1117 /* The wave_ids that are left require a new thread_info. */
1118 for (auto &&tid
: threads
)
1120 ptid_t
wave_ptid (inf
->pid
, 1, tid
);
1121 /* FIXME: is this really needed?
1122 amd_dbgapi_wave_state_t state;
1124 if (amd_dbgapi_wave_get_info (
1125 process_id, get_amd_dbgapi_wave_id (wave_ptid),
1126 AMD_DBGAPI_WAVE_INFO_STATE, sizeof (state), &state)
1127 != AMD_DBGAPI_STATUS_SUCCESS)
1130 add_thread_silent (wave_ptid
);
1131 set_running (wave_ptid
, 1);
1132 set_executing (wave_ptid
, 1);
1136 /* Give the beneath target a chance to do extra processing. */
1137 this->beneath ()->update_thread_list ();
1141 rocm_target_solib_loaded (struct so_list
*solib
)
1143 /* Notify the amd_dbgapi that a shared library has been loaded. */
1144 for (auto &&value
: get_rocm_inferior_info ()->notify_solib_map
)
1145 /* TODO: If we want to support file name wildcards, change this code. */
1146 if (::strstr (solib
->so_original_name
, value
.second
.compare
.c_str ())
1147 && !value
.second
.is_loaded
)
1149 value
.second
.solib
= solib
;
1150 value
.second
.is_loaded
= true;
1152 amd_dbgapi_report_shared_library (
1153 get_amd_dbgapi_process_id (),
1154 amd_dbgapi_shared_library_id_t
{ value
.first
},
1155 AMD_DBGAPI_SHARED_LIBRARY_STATE_LOADED
);
1157 /* The rocm target may not be engaged yet, we need to process the
1158 events now in case there is a runtime event pending. */
1159 rocm_process_event_queue ();
1164 rocm_target_solib_unloaded (struct so_list
*solib
)
1166 /* Notify the amd_dbgapi that a shared library will unload. */
1167 for (auto &&value
: get_rocm_inferior_info ()->notify_solib_map
)
1168 /* TODO: If we want to support file name wildcards, change this code. */
1169 if (::strstr (solib
->so_original_name
, value
.second
.compare
.c_str ())
1170 && value
.second
.is_loaded
)
1172 struct rocm_inferior_info
*info
= get_rocm_inferior_info ();
1174 amd_dbgapi_report_shared_library (
1175 info
->process_id
, amd_dbgapi_shared_library_id_t
{ value
.first
},
1176 AMD_DBGAPI_SHARED_LIBRARY_STATE_UNLOADED
);
1178 /* Delete breakpoints that were left inserted in this shared library.
1180 for (auto it
= info
->breakpoint_map
.begin ();
1181 it
!= info
->breakpoint_map
.end ();)
1182 if (solib_contains_address_p (solib
, it
->second
->loc
->address
))
1184 warning (_ ("breakpoint_%ld is still inserted after "
1185 "shared_library_%ld was unloaded"),
1186 it
->first
, value
.first
);
1187 delete_breakpoint (it
->second
);
1188 it
= info
->breakpoint_map
.erase (it
);
1193 value
.second
.solib
= nullptr;
1194 value
.second
.is_loaded
= false;
1199 rocm_target_inferior_created (struct target_ops
*target
, int from_tty
)
1201 struct inferior
*inf
= current_inferior ();
1202 auto *info
= get_rocm_inferior_info (inf
);
1203 amd_dbgapi_status_t status
;
1205 if (!target_can_async_p ())
1207 warning (_ ("ROCgdb requires target-async, GPU debugging is disabled"));
1211 gdb_assert (info
->wave_stop_events
.empty ());
1213 status
= amd_dbgapi_process_attach (
1214 reinterpret_cast<amd_dbgapi_client_process_id_t
> (inf
),
1217 if (status
== AMD_DBGAPI_STATUS_ERROR_VERSION_MISMATCH
)
1218 warning (_ ("The version of the kernel driver does not match the version "
1219 "required by the ROCm debugger library"));
1221 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
1223 warning (_ ("Could not attach to process %d"), inf
->pid
);
1227 if (amd_dbgapi_process_get_info (info
->process_id
,
1228 AMD_DBGAPI_PROCESS_INFO_NOTIFIER
,
1229 sizeof (info
->notifier
), &info
->notifier
)
1230 != AMD_DBGAPI_STATUS_SUCCESS
)
1232 warning (_ ("Could not retrieve process %d's notifier"), inf
->pid
);
1233 amd_dbgapi_process_detach (info
->process_id
);
1237 /* We add a file handler for events returned by the debugger api. We'll use
1238 this handler to signal our async handler that events are available. */
1241 [] (int error
, gdb_client_data client_data
) {
1242 auto info_
= static_cast<struct rocm_inferior_info
*> (client_data
);
1245 /* Drain the notifier pipe. */
1249 ret
= read (info_
->notifier
, &buf
, 1);
1251 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
1253 /* Signal our async handler. */
1258 /* Attaching to the inferior may have generated runtime events, process
1260 rocm_process_event_queue ();
1264 rocm_target_inferior_exit (struct inferior
*inf
)
1266 auto *info
= get_rocm_inferior_info (inf
);
1268 if (info
->notifier
!= -1)
1269 delete_file_handler (info
->notifier
);
1271 if (info
->process_id
.handle
!= AMD_DBGAPI_PROCESS_NONE
.handle
)
1272 amd_dbgapi_process_detach (info
->process_id
);
1274 /* Delete the breakpoints that are still active. */
1275 for (auto &&value
: info
->breakpoint_map
)
1276 delete_breakpoint (value
.second
);
1278 rocm_inferior_data
.clear (inf
);
1281 static cli_style_option
warning_style ("rocm_warning", ui_file_style::RED
);
1282 static cli_style_option
info_style ("rocm_info", ui_file_style::GREEN
);
1283 static cli_style_option
verbose_style ("rocm_verbose", ui_file_style::BLUE
);
1285 static amd_dbgapi_callbacks_t dbgapi_callbacks
= {
1286 /* allocate_memory. */
1287 .allocate_memory
= malloc
,
1289 /* deallocate_memory. */
1290 .deallocate_memory
= free
,
1293 .get_os_pid
= [] (amd_dbgapi_client_process_id_t client_process_id
,
1294 pid_t
*pid
) -> amd_dbgapi_status_t
{
1295 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1296 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1298 if (info
->has_exited
)
1299 return AMD_DBGAPI_STATUS_ERROR_PROCESS_EXITED
;
1302 return AMD_DBGAPI_STATUS_SUCCESS
;
1305 /* enable_notify_shared_library callback. */
1306 .enable_notify_shared_library
1307 = [] (amd_dbgapi_client_process_id_t client_process_id
,
1308 const char *library_name
, amd_dbgapi_shared_library_id_t library_id
,
1309 amd_dbgapi_shared_library_state_t
*library_state
)
1310 -> amd_dbgapi_status_t
{
1311 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1312 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1314 if (!library_name
|| !library_state
)
1315 return AMD_DBGAPI_STATUS_ERROR_INVALID_ARGUMENT
;
1317 if (info
->notify_solib_map
.find (library_id
.handle
)
1318 != info
->notify_solib_map
.end ())
1320 /* This library id is already registered. */
1321 return AMD_DBGAPI_STATUS_ERROR
;
1324 /* Check whether the library is already loaded. */
1325 bool is_loaded
= false;
1326 struct so_list
*solib
;
1327 for (solib
= inf
->pspace
->so_list
; solib
; solib
= solib
->next
)
1328 if (::strstr (solib
->so_original_name
, library_name
))
1334 /* Check that the library_name is valid. If must not be empty, and
1335 should not have wildcard characters. */
1336 if (*library_name
== '\0'
1337 || std::string (library_name
).find_first_of ("*?[]")
1338 != std::string::npos
)
1339 return AMD_DBGAPI_STATUS_ERROR_INVALID_ARGUMENT
;
1341 /* Add a new entry in the notify_solib_map. */
1342 if (!info
->notify_solib_map
1343 .emplace (std::piecewise_construct
,
1344 std::forward_as_tuple (library_id
.handle
),
1345 std::forward_as_tuple (rocm_notify_shared_library_info
{
1346 library_name
, solib
, is_loaded
}))
1348 return AMD_DBGAPI_STATUS_ERROR
;
1350 *library_state
= is_loaded
? AMD_DBGAPI_SHARED_LIBRARY_STATE_LOADED
1351 : AMD_DBGAPI_SHARED_LIBRARY_STATE_UNLOADED
;
1353 return AMD_DBGAPI_STATUS_SUCCESS
;
1356 /* disable_notify_shared_library callback. */
1357 .disable_notify_shared_library
1358 = [] (amd_dbgapi_client_process_id_t client_process_id
,
1359 amd_dbgapi_shared_library_id_t library_id
) -> amd_dbgapi_status_t
{
1360 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1361 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1363 auto it
= info
->notify_solib_map
.find (library_id
.handle
);
1364 if (it
== info
->notify_solib_map
.end ())
1365 return AMD_DBGAPI_STATUS_ERROR_INVALID_SHARED_LIBRARY_ID
;
1367 info
->notify_solib_map
.erase (it
);
1368 return AMD_DBGAPI_STATUS_SUCCESS
;
1371 /* get_symbol_address callback. */
1372 .get_symbol_address
=
1373 [] (amd_dbgapi_client_process_id_t client_process_id
,
1374 amd_dbgapi_shared_library_id_t library_id
, const char *symbol_name
,
1375 amd_dbgapi_global_address_t
*address
) {
1376 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1377 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1379 auto it
= info
->notify_solib_map
.find (library_id
.handle
);
1380 if (it
== info
->notify_solib_map
.end ())
1381 return AMD_DBGAPI_STATUS_ERROR_INVALID_SHARED_LIBRARY_ID
;
1383 struct so_list
*solib
= it
->second
.solib
;
1385 return AMD_DBGAPI_STATUS_ERROR_LIBRARY_NOT_LOADED
;
1387 solib_read_symbols (solib
, 0);
1388 gdb_assert (solib
->objfile
);
1390 struct bound_minimal_symbol msymbol
1391 = lookup_minimal_symbol (symbol_name
, NULL
, solib
->objfile
);
1393 if (!msymbol
.minsym
|| BMSYMBOL_VALUE_ADDRESS (msymbol
) == 0)
1394 return AMD_DBGAPI_STATUS_ERROR_SYMBOL_NOT_FOUND
;
1396 *address
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
1397 return AMD_DBGAPI_STATUS_SUCCESS
;
1400 /* set_breakpoint callback. */
1402 [] (amd_dbgapi_client_process_id_t client_process_id
,
1403 amd_dbgapi_shared_library_id_t shared_library_id
,
1404 amd_dbgapi_global_address_t address
,
1405 amd_dbgapi_breakpoint_id_t breakpoint_id
) {
1406 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1407 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1409 /* Initialize the breakpoint ops lazily since we depend on
1410 bkpt_breakpoint_ops and we can't control the order in which
1411 initializers are called. */
1412 if (rocm_breakpoint_ops
.check_status
== NULL
)
1414 rocm_breakpoint_ops
= bkpt_breakpoint_ops
;
1415 rocm_breakpoint_ops
.check_status
= rocm_breakpoint_check_status
;
1416 rocm_breakpoint_ops
.re_set
= rocm_breakpoint_re_set
;
1419 auto it
= info
->breakpoint_map
.find (breakpoint_id
.handle
);
1420 if (it
!= info
->breakpoint_map
.end ())
1421 return AMD_DBGAPI_STATUS_ERROR_INVALID_BREAKPOINT_ID
;
1423 /* Create a new breakpoint. */
1424 struct obj_section
*section
= find_pc_section (address
);
1425 if (!section
|| !section
->objfile
)
1426 return AMD_DBGAPI_STATUS_ERROR
;
1428 event_location_up location
1429 = new_address_location (address
, nullptr, 0);
1430 if (!create_breakpoint (
1431 get_objfile_arch (section
->objfile
), location
.get (),
1432 /*cond_string*/ NULL
, /*thread*/ -1, /*extra_sring*/ NULL
,
1433 /*parse_extra*/ 0, /*tempflag*/ 0, /*bptype*/ bp_breakpoint
,
1434 /*ignore_count*/ 0, /*pending_break*/ AUTO_BOOLEAN_FALSE
,
1435 /*ops*/ &rocm_breakpoint_ops
, /*from_tty*/ 0,
1436 /*enabled*/ 1, /*internal*/ 1, /*flags*/ 0))
1437 return AMD_DBGAPI_STATUS_ERROR
;
1439 /* Find our breakpoint in the breakpoint list. */
1440 auto bp_loc
= std::make_pair (inf
->aspace
, address
);
1441 auto bp
= breakpoint_find_if (
1442 [] (struct breakpoint
*b
, void *data
) {
1443 auto *arg
= static_cast<decltype (&bp_loc
)> (data
);
1444 if (b
->ops
== &rocm_breakpoint_ops
&& b
->loc
1445 && b
->loc
->pspace
->aspace
== arg
->first
1446 && b
->loc
->address
== arg
->second
)
1450 reinterpret_cast<void *> (&bp_loc
));
1453 error (_ ("Could not find breakpoint"));
1455 info
->breakpoint_map
.emplace (breakpoint_id
.handle
, bp
);
1456 return AMD_DBGAPI_STATUS_SUCCESS
;
1459 /* remove_breakpoint callback. */
1460 .remove_breakpoint
=
1461 [] (amd_dbgapi_client_process_id_t client_process_id
,
1462 amd_dbgapi_breakpoint_id_t breakpoint_id
) {
1463 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1464 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1466 auto it
= info
->breakpoint_map
.find (breakpoint_id
.handle
);
1467 if (it
== info
->breakpoint_map
.end ())
1468 return AMD_DBGAPI_STATUS_ERROR_INVALID_BREAKPOINT_ID
;
1470 delete_breakpoint (it
->second
);
1471 info
->breakpoint_map
.erase (it
);
1473 return AMD_DBGAPI_STATUS_SUCCESS
;
1476 /* set_breakpoint_state callback. */
1477 .set_breakpoint_state
=
1478 [] (amd_dbgapi_client_process_id_t client_process_id
,
1479 amd_dbgapi_breakpoint_id_t breakpoint_id
,
1480 amd_dbgapi_breakpoint_state_t breakpoint_state
) {
1481 inferior
*inf
= reinterpret_cast<inferior
*> (client_process_id
);
1482 struct rocm_inferior_info
*info
= get_rocm_inferior_info (inf
);
1484 auto it
= info
->breakpoint_map
.find (breakpoint_id
.handle
);
1485 if (it
== info
->breakpoint_map
.end ())
1486 return AMD_DBGAPI_STATUS_ERROR_INVALID_BREAKPOINT_ID
;
1488 if (breakpoint_state
== AMD_DBGAPI_BREAKPOINT_STATE_ENABLE
)
1489 it
->second
->enable_state
= bp_enabled
;
1490 else if (breakpoint_state
== AMD_DBGAPI_BREAKPOINT_STATE_DISABLE
)
1491 it
->second
->enable_state
= bp_disabled
;
1493 return AMD_DBGAPI_STATUS_ERROR_INVALID_ARGUMENT
;
1495 return AMD_DBGAPI_STATUS_SUCCESS
;
1499 = [] (amd_dbgapi_log_level_t level
, const char *message
) -> void {
1500 gdb::optional
<target_terminal::scoped_restore_terminal_state
> tstate
;
1502 if (level
> get_debug_amd_dbgapi_log_level ())
1505 if (target_supports_terminal_ours ())
1508 target_terminal::ours_for_output ();
1511 if (filtered_printing_initialized ())
1514 struct ui_file
*out_file
1515 = (level
>= AMD_DBGAPI_LOG_LEVEL_INFO
) ? gdb_stdlog
: gdb_stderr
;
1519 case AMD_DBGAPI_LOG_LEVEL_FATAL_ERROR
:
1520 fputs_unfiltered ("[amd-dbgapi]: ", out_file
);
1522 case AMD_DBGAPI_LOG_LEVEL_WARNING
:
1523 fputs_styled ("[amd-dbgapi]: ", warning_style
.style (), out_file
);
1525 case AMD_DBGAPI_LOG_LEVEL_INFO
:
1526 fputs_styled ("[amd-dbgapi]: ", info_style
.style (), out_file
);
1528 case AMD_DBGAPI_LOG_LEVEL_VERBOSE
:
1529 fputs_styled ("[amd-dbgapi]: ", verbose_style
.style (), out_file
);
1533 fputs_unfiltered (message
, out_file
);
1534 fputs_unfiltered ("\n", out_file
);
1538 /* Implementation of `_wave_id' variable. */
1540 static struct value
*
1541 rocm_wave_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1544 if (ptid_is_gpu (inferior_ptid
))
1546 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
1547 amd_dbgapi_wave_id_t wave_id
= get_amd_dbgapi_wave_id (inferior_ptid
);
1548 uint32_t group_ids
[3], wave_in_group
;
1550 if (amd_dbgapi_wave_get_info (process_id
, wave_id
,
1551 AMD_DBGAPI_WAVE_INFO_WORK_GROUP_COORD
,
1552 sizeof (group_ids
), &group_ids
)
1553 == AMD_DBGAPI_STATUS_SUCCESS
1554 && amd_dbgapi_wave_get_info (
1555 process_id
, wave_id
,
1556 AMD_DBGAPI_WAVE_INFO_WAVE_NUMBER_IN_WORK_GROUP
,
1557 sizeof (wave_in_group
), &wave_in_group
)
1558 == AMD_DBGAPI_STATUS_SUCCESS
)
1560 std::string wave_id_str
1561 = string_printf ("(%d,%d,%d)/%d", group_ids
[2], group_ids
[1],
1562 group_ids
[0], wave_in_group
);
1564 return value_cstring (wave_id_str
.data (), wave_id_str
.length () + 1,
1565 builtin_type (gdbarch
)->builtin_char
);
1569 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
1572 static const struct internalvar_funcs rocm_wave_id_funcs
1573 = { rocm_wave_id_make_value
, NULL
, NULL
};
1575 /* List of set/show debug amd_dbgapi commands. */
1576 struct cmd_list_element
*set_debug_amd_dbgapi_list
;
1577 struct cmd_list_element
*show_debug_amd_dbgapi_list
;
1580 set_debug_amd_dbgapi (const char *arg
, int from_tty
)
1582 help_list (set_debug_amd_dbgapi_list
, "set debug amd-dbgapi ",
1583 (enum command_class
) - 1, gdb_stdout
);
1587 show_debug_amd_dbgapi (const char *args
, int from_tty
)
1589 cmd_show_list (show_debug_amd_dbgapi_list
, from_tty
, "");
1592 constexpr char amd_dbgapi_log_level_off
[] = "off";
1593 constexpr char amd_dbgapi_log_level_error
[] = "error";
1594 constexpr char amd_dbgapi_log_level_warning
[] = "warning";
1595 constexpr char amd_dbgapi_log_level_info
[] = "info";
1596 constexpr char amd_dbgapi_log_level_verbose
[] = "verbose";
1598 constexpr const char *debug_amd_dbgapi_log_level_enums
[]
1599 = { [AMD_DBGAPI_LOG_LEVEL_NONE
] = amd_dbgapi_log_level_off
,
1600 [AMD_DBGAPI_LOG_LEVEL_FATAL_ERROR
] = amd_dbgapi_log_level_error
,
1601 [AMD_DBGAPI_LOG_LEVEL_WARNING
] = amd_dbgapi_log_level_warning
,
1602 [AMD_DBGAPI_LOG_LEVEL_INFO
] = amd_dbgapi_log_level_info
,
1603 [AMD_DBGAPI_LOG_LEVEL_VERBOSE
] = amd_dbgapi_log_level_verbose
,
1606 static const char *debug_amd_dbgapi_log_level
= amd_dbgapi_log_level_error
;
1608 static amd_dbgapi_log_level_t
1609 get_debug_amd_dbgapi_log_level ()
1612 for (pos
= 0; debug_amd_dbgapi_log_level_enums
[pos
]; ++pos
)
1613 if (debug_amd_dbgapi_log_level
== debug_amd_dbgapi_log_level_enums
[pos
])
1616 gdb_assert (debug_amd_dbgapi_log_level_enums
[pos
]);
1617 return static_cast<amd_dbgapi_log_level_t
> (pos
);
1621 set_debug_amd_dbgapi_log_level (const char *args
, int from_tty
,
1622 struct cmd_list_element
*c
)
1624 amd_dbgapi_set_log_level (get_debug_amd_dbgapi_log_level ());
1628 show_debug_amd_dbgapi_log_level (struct ui_file
*file
, int from_tty
,
1629 struct cmd_list_element
*c
,
1632 fprintf_filtered (file
, _ ("The amd-dbgapi log level is %s.\n"), value
);
1636 info_agents_command (const char *args
, int from_tty
)
1638 amd_dbgapi_process_id_t process_id
= get_amd_dbgapi_process_id ();
1639 struct ui_out
*uiout
= current_uiout
;
1640 amd_dbgapi_status_t status
;
1642 amd_dbgapi_agent_id_t
*agent_list
;
1645 if (process_id
.handle
!= AMD_DBGAPI_PROCESS_NONE
.handle
1647 = amd_dbgapi_agent_list (process_id
, &count
, &agent_list
, nullptr))
1648 != AMD_DBGAPI_STATUS_SUCCESS
)
1649 error (_ ("amd_dbgapi_agent_list failed (rc=%d)"), status
);
1651 if (!count
&& !uiout
->is_mi_like_p ())
1653 uiout
->field_string (NULL
,
1654 _ ("No agents are currently active.\n"));
1658 /* Calculate the maximum size needed to print the agents names. */
1659 std::vector
<std::string
> agent_names (count
);
1661 size_t max_name_len
= 0;
1662 for (size_t i
= 0; i
< count
; ++i
)
1666 if ((status
= amd_dbgapi_agent_get_info (
1667 process_id
, agent_list
[i
], AMD_DBGAPI_AGENT_INFO_NAME
,
1668 sizeof (agent_name
), &agent_name
))
1669 != AMD_DBGAPI_STATUS_SUCCESS
)
1671 if (status
== AMD_DBGAPI_STATUS_ERROR_INVALID_AGENT_ID
)
1672 agent_names
[i
] = "N/A";
1674 error (_ ("amd_dbgapi_agent_get_info failed (rc=%d"), status
);
1678 agent_names
[i
] = agent_name
;
1682 max_name_len
= std::max (max_name_len
, agent_names
[i
].size ());
1686 ui_out_emit_table
table_emmitter (uiout
, 7, count
, "InfoRocmDevicesTable");
1688 uiout
->table_header (2, ui_left
, "agent_id", "Id");
1689 uiout
->table_header (8, ui_left
, "location_id", "PCI Slot");
1690 uiout
->table_header (std::max (11ul, max_name_len
), ui_left
, "name",
1692 uiout
->table_header (14, ui_left
, "num_se", "Shader Engines");
1693 uiout
->table_header (13, ui_left
, "num_cu", "Compute Units");
1694 uiout
->table_header (7, ui_left
, "simd", "SIMD/CU");
1695 uiout
->table_header (15, ui_left
, "waves", "Wavefronts/SIMD");
1696 uiout
->table_body ();
1699 for (size_t i
= 0; i
< count
; ++i
)
1701 ui_out_emit_tuple
tuple_emitter (uiout
, "InfoRocmDevicesRow");
1704 uiout
->field_signed ("agent_id", agent_list
[i
].handle
);
1707 uint32_t location_id
;
1708 if ((status
= amd_dbgapi_agent_get_info (
1709 process_id
, agent_list
[i
], AMD_DBGAPI_AGENT_INFO_PCIE_SLOT
,
1710 sizeof (location_id
), &location_id
))
1711 != AMD_DBGAPI_STATUS_SUCCESS
)
1713 if (status
== AMD_DBGAPI_STATUS_ERROR_INVALID_AGENT_ID
)
1714 uiout
->field_string ("location_id", "N/A");
1716 error (_ ("amd_dbgapi_agent_get_info failed (rc=%d"), status
);
1719 uiout
->field_string (
1721 string_printf ("%02x:%02x.%d", (location_id
>> 8) & 0xFF,
1722 (location_id
>> 3) & 0x1F, location_id
& 0x7));
1725 uiout
->field_string ("name", agent_names
[i
]);
1727 /* num_se, num_cu, simd, waves */
1729 #define UIOUT_FIELD_INT(name, query) \
1731 if ((status = amd_dbgapi_agent_get_info (process_id, agent_list[i], query, \
1732 sizeof (name), &name)) \
1733 != AMD_DBGAPI_STATUS_SUCCESS) \
1735 if (status == AMD_DBGAPI_STATUS_ERROR_INVALID_AGENT_ID) \
1736 uiout->field_string (#name, "N/A"); \
1738 error (_ ("amd_dbgapi_agent_get_info failed (rc=%d"), status); \
1741 uiout->field_signed (#name, name);
1743 UIOUT_FIELD_INT (num_se
, AMD_DBGAPI_AGENT_INFO_SHADER_ENGINE_COUNT
);
1744 UIOUT_FIELD_INT (num_cu
, AMD_DBGAPI_AGENT_INFO_COMPUTE_UNIT_COUNT
);
1745 UIOUT_FIELD_INT (simd
, AMD_DBGAPI_AGENT_INFO_NUM_SIMD_PER_COMPUTE_UNIT
);
1746 UIOUT_FIELD_INT (waves
, AMD_DBGAPI_AGENT_INFO_MAX_WAVES_PER_SIMD
);
1748 #undef UIOUT_FIELD_INT
1754 gdb_flush (gdb_stdout
);
1757 /* -Wmissing-prototypes */
1758 extern initialize_file_ftype _initialize_rocm_tdep
;
1761 _initialize_rocm_tdep (void)
1763 /* Make sure the loaded debugger library version is greater than or equal to
1764 the one used to build ROCgdb. */
1765 uint32_t major
, minor
, patch
;
1766 amd_dbgapi_get_version (&major
, &minor
, &patch
);
1767 if (major
!= AMD_DBGAPI_VERSION_MAJOR
|| minor
< AMD_DBGAPI_VERSION_MINOR
)
1769 _ ("amd-dbgapi library version mismatch, got %d.%d.%d, need %d.%d+"),
1770 major
, minor
, patch
, AMD_DBGAPI_VERSION_MAJOR
,
1771 AMD_DBGAPI_VERSION_MINOR
);
1773 /* Initialize the ROCm Debug API. */
1774 amd_dbgapi_status_t status
= amd_dbgapi_initialize (&dbgapi_callbacks
);
1775 if (status
!= AMD_DBGAPI_STATUS_SUCCESS
)
1776 error (_ ("amd-dbgapi failed to initialize (rc=%d)"), status
);
1778 /* Set the initial log level. */
1779 amd_dbgapi_set_log_level (get_debug_amd_dbgapi_log_level ());
1781 /* Install observers. */
1782 gdb::observers::breakpoint_created
.attach (rocm_target_breakpoint_fixup
);
1783 gdb::observers::solib_loaded
.attach (rocm_target_solib_loaded
);
1784 gdb::observers::solib_unloaded
.attach (rocm_target_solib_unloaded
);
1785 gdb::observers::inferior_created
.attach (rocm_target_inferior_created
);
1786 gdb::observers::inferior_exit
.attach (rocm_target_inferior_exit
);
1788 amd_dbgapi_activated
.attach (rocm_target_dbgapi_activated
);
1789 amd_dbgapi_deactivated
.attach (rocm_target_dbgapi_deactivated
);
1791 create_internalvar_type_lazy ("_wave_id", &rocm_wave_id_funcs
, NULL
);
1794 "amd-dbgapi", no_class
, set_debug_amd_dbgapi
,
1795 _ ("Generic command for setting amd-dbgapi debugging flags"),
1796 &set_debug_amd_dbgapi_list
, "set debug amd-dbgapi ", 0, &setdebuglist
);
1799 "amd-dbgapi", no_class
, show_debug_amd_dbgapi
,
1800 _ ("Generic command for showing amd-dbgapi debugging flags"),
1801 &show_debug_amd_dbgapi_list
, "show debug amd-dbgapi ", 0,
1804 add_setshow_enum_cmd (
1805 "log-level", class_maintenance
, debug_amd_dbgapi_log_level_enums
,
1806 &debug_amd_dbgapi_log_level
, _ ("Set the amd-dbgapi log level."),
1807 _ ("Show the amd-dbgapi log level."),
1808 _ ("off == no logging is enabled\n"
1809 "error == fatal errors are reported\n"
1810 "warning == fatal errors and warnings are reported\n"
1811 "info == fatal errors, warnings, and info messages are reported\n"
1812 "verbose == all messages are reported"),
1813 set_debug_amd_dbgapi_log_level
, show_debug_amd_dbgapi_log_level
,
1814 &set_debug_amd_dbgapi_list
, &show_debug_amd_dbgapi_list
);
1816 add_cmd ("agents", class_info
, info_agents_command
,
1817 _ ("Info about currently active agents."), &infolist
);