)]}'
{
  "commit": "c0abbd96b4dc45249daffbd2b00dfa46cf3fd5aa",
  "tree": "957791850e0e4b0b5bf69d76ba62edab953536fa",
  "parents": [
    "a2a176c46bcb739db47c88b5641a23c1129accf0"
  ],
  "author": {
    "name": "Simon Marchi",
    "email": "simon.marchi@efficios.com",
    "time": "Wed Jul 06 13:39:22 2022 -0400"
  },
  "committer": {
    "name": "Simon Marchi",
    "email": "simon.marchi@polymtl.ca",
    "time": "Thu Jul 07 09:55:01 2022 -0400"
  },
  "message": "gdb: fix {rs6000_nat_target,aix_thread_target}::wait to not use inferior_ptid\n\nTrying to run a simple program (empty main) on AIX, I get:\n\n    (gdb) run\n    Starting program: /scratch/simark/build/gdb/a.out\n    Child process unexpectedly missing: There are no child processes..\n    ../../src/binutils-gdb/gdb/inferior.c:304: internal-error: find_inferior_pid: Assertion `pid !\u003d 0\u0027 failed.\n    A problem internal to GDB has been detected,\n    further debugging may prove unreliable.\n    ----- Backtrace -----\n    0x10ef12a8 gdb_internal_backtrace_1()\n            ../../src/binutils-gdb/gdb/bt-utils.c:122\n    0x10ef1470 gdb_internal_backtrace()\n            ../../src/binutils-gdb/gdb/bt-utils.c:168\n    0x1004d368 internal_vproblem(internal_problem*, char const*, int, char const*, char*)\n            ../../src/binutils-gdb/gdb/utils.c:396\n    0x1004d8a8 internal_verror(char const*, int, char const*, char*)\n            ../../src/binutils-gdb/gdb/utils.c:476\n    0x1004c424 internal_error(char const*, int, char const*, ...)\n            ../../src/binutils-gdb/gdbsupport/errors.cc:55\n    0x102ab344 find_inferior_pid(process_stratum_target*, int)\n            ../../src/binutils-gdb/gdb/inferior.c:304\n    0x102ab4a4 find_inferior_ptid(process_stratum_target*, ptid_t)\n            ../../src/binutils-gdb/gdb/inferior.c:318\n    0x1061bae8 find_thread_ptid(process_stratum_target*, ptid_t)\n            ../../src/binutils-gdb/gdb/thread.c:519\n    0x10319e98 handle_inferior_event(execution_control_state*)\n            ../../src/binutils-gdb/gdb/infrun.c:5532\n    0x10315544 fetch_inferior_event()\n            ../../src/binutils-gdb/gdb/infrun.c:4221\n    0x10952e34 inferior_event_handler(inferior_event_type)\n            ../../src/binutils-gdb/gdb/inf-loop.c:41\n    0x1032640c infrun_async_inferior_event_handler(void*)\n            ../../src/binutils-gdb/gdb/infrun.c:9548\n    0x10673188 check_async_event_handlers()\n            ../../src/binutils-gdb/gdb/async-event.c:335\n    0x1066fce4 gdb_do_one_event()\n            ../../src/binutils-gdb/gdbsupport/event-loop.cc:214\n    0x10001a94 start_event_loop()\n            ../../src/binutils-gdb/gdb/main.c:411\n    0x10001ca0 captured_command_loop()\n            ../../src/binutils-gdb/gdb/main.c:471\n    0x10003d74 captured_main(void*)\n            ../../src/binutils-gdb/gdb/main.c:1329\n    0x10003e48 gdb_main(captured_main_args*)\n            ../../src/binutils-gdb/gdb/main.c:1344\n    0x10000744 main\n            ../../src/binutils-gdb/gdb/gdb.c:32\n    ---------------------\n    ../../src/binutils-gdb/gdb/inferior.c:304: internal-error: find_inferior_pid: Assertion `pid !\u003d 0\u0027 failed.\n    A problem internal to GDB has been detected,\n    further debugging may prove unreliable.\n    Quit this debugging session? (y or n)\n\nThis is due to some bit-rot in the AIX port, still relying on the entry\nvalue of inferior_ptid in the wait methods.\n\nProblem #1 is in rs6000_nat_target::wait, here:\n\n      /* Ignore terminated detached child processes.  */\n      if (!WIFSTOPPED (status) \u0026\u0026 pid !\u003d inferior_ptid.pid ())\n\tpid \u003d -1;\n\nAt this point, waitpid has returned an \"exited\" status for some pid, so\npid is non-zero.  Since inferior_ptid is set to null_ptid on entry, the\npid returned by wait is not equal to `inferior_ptid.pid ()`, so we reset\npid to -1 and go to waiting again.  Since there are not more children to\nwait for, waitpid then returns -1 so we get here:\n\n      if (pid \u003d\u003d -1)\n\t{\n\t  gdb_printf (gdb_stderr,\n\t\t      _(\"Child process unexpectedly missing: %s.\\n\"),\n\t\t      safe_strerror (save_errno));\n\n\t  /* Claim it exited with unknown signal.  */\n\t  ourstatus-\u003eset_signalled (GDB_SIGNAL_UNKNOWN);\n\t  return inferior_ptid;\n\t}\n\nWe therefore return a \"signalled\" status with a null_ptid (again,\ninferior_ptid is null_ptid).  This confuses infrun, because if the\ntarget returns a \"signalled\" status, it should be coupled with a ptid\nfor an inferior that exists.\n\nSo, the first step is to fix the snippets above to not use\ninferior_ptid.  In the first snippet, use find_inferior_pid to see if\nwe know the event process.  If there is no inferior with that pid, we\nassume it\u0027s a detached child process to we ignore the event.  That\nshould be enough to fix the problem, because it should make it so we\nwon\u0027t go into the second snippet.  But still, fix the second snippet to\nreturn an \"ignore\" status.  This is copied from inf_ptrace_target::wait,\nwhich is where rs6000_nat_target::wait appears to be copied from in the\nfirst place.\n\nThese changes, are not sufficient, as the aix_thread_target, which sits\non top of rs6000_nat_target, also relies on inferior_ptid.\naix_thread_target::wait, by calling pd_update, assumes that\nrs6000_nat_target has set inferior_ptid to the appropriate value (the\nptid of the event thread), but that\u0027s not the case.  pd_update\nreturns inferior_ptid - null_ptid - and therefore\naix_thread_target::wait returns null_ptid too, and we still hit the\nassert shown above.\n\nFix this by changing pd_activate, pd_update, sync_threadlists and\nget_signaled_thread to all avoid using inferior_ptid.  Instead, they\naccept as a parameter the pid of the process we are working on.\n\nWith this patch, I am able to run the program to completion:\n\n    (gdb) r\n    Starting program: /scratch/simark/build/gdb/a.out\n    [Inferior 1 (process 11010794) exited normally]\n\nAs well as break on main:\n\n    (gdb) b main\n    Breakpoint 1 at 0x1000036c\n    (gdb) r\n    Starting program: /scratch/simark/build/gdb/a.out\n\n    Breakpoint 1, 0x1000036c in main ()\n    (gdb) c\n    Continuing.\n    [Inferior 1 (process 26083688) exited normally]\n\nChange-Id: I7c2613bbefe487d75fa1a0c0994423471d961ee9\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "ecd8200b6928ba0dc68850d6ecef6705f8b92156",
      "old_mode": 33188,
      "old_path": "gdb/aix-thread.c",
      "new_id": "d47f5132592a4c383fac43c5ac386febd0eed03b",
      "new_mode": 33188,
      "new_path": "gdb/aix-thread.c"
    },
    {
      "type": "modify",
      "old_id": "8563aea313a27b1181941a81071af1493b6f8dce",
      "old_mode": 33188,
      "old_path": "gdb/rs6000-aix-nat.c",
      "new_id": "f604f7d503e922ee6bec09b59af5373c9b3766ff",
      "new_mode": 33188,
      "new_path": "gdb/rs6000-aix-nat.c"
    }
  ]
}
