)]}'
{
  "commit": "e2140f3072cbbe608ef4c03bf652fd235629d14c",
  "tree": "47b36b6debd66ebe001cfe96a693cce4b4f4807b",
  "parents": [
    "38797d2e73da87422d883bcec61ed791f0fc1894"
  ],
  "author": {
    "name": "Lancelot Six",
    "email": "lancelot.six@amd.com",
    "time": "Mon Jul 31 09:59:44 2023 +0000"
  },
  "committer": {
    "name": "Lancelot SIX",
    "email": "lancelot.six@amd.com",
    "time": "Mon Jul 31 15:27:19 2023 +0100"
  },
  "message": "gdb/amdgpu: Fix debugging multiple inferiors using the ROCm runtime\n\nWhen debugging a multi-process application where a parent spawns\nmultiple child processes using the ROCm runtime, I see the following\nassertion failure:\n\n    ../../gdb/amd-dbgapi-target.c:1071: internal-error: process_one_event: Assertion `runtime_state \u003d\u003d AMD_DBGAPI_RUNTIME_STATE_UNLOADED\u0027 failed.\n    A problem internal to GDB has been detected,\n    further debugging may prove unreliable.\n    ----- Backtrace -----\n    0x556e9a318540 gdb_internal_backtrace_1\n            ../../gdb/bt-utils.c:122\n    0x556e9a318540 _Z22gdb_internal_backtracev\n            ../../gdb/bt-utils.c:168\n    0x556e9a730224 internal_vproblem\n            ../../gdb/utils.c:396\n    0x556e9a7304e0 _Z15internal_verrorPKciS0_P13__va_list_tag\n            ../../gdb/utils.c:476\n    0x556e9a87aeb4 _Z18internal_error_locPKciS0_z\n            ../../gdbsupport/errors.cc:58\n    0x556e9a29f446 process_one_event\n            ../../gdb/amd-dbgapi-target.c:1071\n    0x556e9a29f446 process_event_queue\n            ../../gdb/amd-dbgapi-target.c:1156\n    0x556e9a29faf2 _ZN17amd_dbgapi_target4waitE6ptid_tP17target_waitstatus10enum_flagsI16target_wait_flagE\n            ../../gdb/amd-dbgapi-target.c:1262\n    0x556e9a6b0965 _Z11target_wait6ptid_tP17target_waitstatus10enum_flagsI16target_wait_flagE\n            ../../gdb/target.c:2586\n    0x556e9a4c221f do_target_wait_1\n            ../../gdb/infrun.c:3876\n    0x556e9a4d8489 operator()\n            ../../gdb/infrun.c:3935\n    0x556e9a4d8489 do_target_wait\n            ../../gdb/infrun.c:3964\n    0x556e9a4d8489 _Z20fetch_inferior_eventv\n            ../../gdb/infrun.c:4365\n    0x556e9a87b915 gdb_wait_for_event\n            ../../gdbsupport/event-loop.cc:694\n    0x556e9a87c3a9 gdb_wait_for_event\n            ../../gdbsupport/event-loop.cc:593\n    0x556e9a87c3a9 _Z16gdb_do_one_eventi\n            ../../gdbsupport/event-loop.cc:217\n    0x556e9a521689 start_event_loop\n            ../../gdb/main.c:412\n    0x556e9a521689 captured_command_loop\n            ../../gdb/main.c:476\n    0x556e9a523c04 captured_main\n            ../../gdb/main.c:1320\n    0x556e9a523c04 _Z8gdb_mainP18captured_main_args\n            ../../gdb/main.c:1339\n    0x556e9a24b1bf main\n            ../../gdb/gdb.c:32\n    ---------------------\n    ../../gdb/amd-dbgapi-target.c:1071: internal-error: process_one_event: Assertion `runtime_state \u003d\u003d AMD_DBGAPI_RUNTIME_STATE_UNLOADED\u0027 failed.\n    A problem internal to GDB has been detected,\n\nBefore diving into why this error appears, let\u0027s explore how things are\nexpected to work in normal circumstances.  When a process being debugged\nstarts using the ROCm runtime, the following happens:\n\n- The runtime registers itself to the driver.\n- The driver creates a \"runtime loaded\" event and notifies the debugger\n  that a new event is available by writing to a file descriptor which is\n  registered in GDB\u0027s main event loop.\n- GDB core calls the callback associated with this file descriptor\n  (dbgapi_notifier_handler).  Because the amd-dbgapi-target is not\n  pushed at this point, the handler pulls the \"runtime loaded\" event\n  from the driver (this is the only event which can be available at this\n  point) and eventually pushes the amd-dbgapi-target on the inferior\u0027s\n  target stack.\n\nIn a nutshell, this is the expected AMDGPU runtime activation process.\n\nFrom there, when new events are available regarding the GPU threads, the\nsame file descriptor is written to.  The callback sees that the\namd-dbgapi-target is pushed so marks the amd_dbgapi_async_event_handler.\nThis will later cause amd_dbgapi_target::wait to be called.  The wait\nmethod pulls all the available events from the driver and handles them.\nThe wait method returns the information conveyed by the first event, the\nother events are cached for later calls of the wait method.\n\nNote that because we are under the wait method, we know that the\namd-dbgapi-target is pushed on the inferior target stack.  This implies\nthat the runtime activation event has been seen already.  As a\nconsequence, we cannot receive another event indicating that the runtime\ngets activated.  This is what the failing assertion checks.\n\nIn the case when we have multiple inferiors however, there is a flaw in\nwhat have been described above.  If one inferior (let\u0027s call it inferior\n1) already has the amd-dbgapi-target pushed to its target stack and\nanother inferior (inferior 2) activates the ROCm runtime, here is what\ncan happen:\n\n- The driver creates the runtime activation for inferior 2 and writes to\n  the associated file descriptor.\n- GDB has inferior 1 selected and calls target_wait for some reason.\n- This prompts amd_dbgapi_target::wait to be called.  The method pulls\n  all events from the driver, including the runtime activation event for\n  inferior 2, leading to the assertion failure.\n\nThe fix for this problem is simple.  To avoid such problem, we need to\nmake sure that amd_dbgapi_target::wait only pulls events for the current\ninferior from the driver.  This is what this patch implements.\n\nThis patch also includes a testcase which could fail before this patch.\n\nThis patch has been tested on a system with multiple GPUs which had more\nchances to reproduce the original bug.  It has also been tested on top\nof the downstream ROCgdb port which has more AMDGPU related tests.  The\ntestcase has been tested with `make check check-read1 check-readmore`.\n\nApproved-By: Pedro Alves \u003cpedro@palves.net\u003e\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "40f24b5fc2f18ef1ee05fc71c863a5108d694909",
      "old_mode": 33188,
      "old_path": "gdb/amd-dbgapi-target.c",
      "new_id": "e90628c8183b72999dd9b3cc8ab207df9547db92",
      "new_mode": 33188,
      "new_path": "gdb/amd-dbgapi-target.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "ca869233b589e1eedfcacb63cc09cf792febbc37",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "18b4172ff09e1706997c95f6662a9cb9f57d444f",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp"
    }
  ]
}
