)]}'
{
  "commit": "f2c4f78c813a9cef38b7e9c9ad18822fb9e19345",
  "tree": "e0104e68a28680250d04925115a0167109f4d781",
  "parents": [
    "3d38b301bb50f1822e9d07d2aacef1ebe1a97073"
  ],
  "author": {
    "name": "Andrew Burgess",
    "email": "aburgess@redhat.com",
    "time": "Thu Sep 21 16:35:30 2023 +0100"
  },
  "committer": {
    "name": "Andrew Burgess",
    "email": "aburgess@redhat.com",
    "time": "Thu Oct 05 12:21:46 2023 +0100"
  },
  "message": "gdb: fix reread_symbols when an objfile has target: prefix\n\nWhen using a remote target, it is possible to tell GDB that the\nexecutable to be debugged is located on the remote machine, like this:\n\n  (gdb) target extended-remote :54321\n  ... snip ...\n  (gdb) file target:/tmp/hello.x\n  Reading /tmp/hello.x from remote target...\n  warning: File transfers from remote targets can be slow. Use \"set sysroot\" to access files locally instead.\n  Reading /tmp/hello.x from remote target...\n  Reading symbols from target:/tmp/hello.x...\n  (gdb)\n\nSo far so good.  However, when we try to start the inferior we run\ninto a small problem:\n\n  (gdb) set remote exec-file /tmp/hello.x\n  (gdb) start\n  `target:/tmp/hello.x\u0027 has disappeared; keeping its symbols.\n  Temporary breakpoint 1 at 0x401198: file /tmp/hello.c, line 18.\n  Starting program: target:/tmp/hello.x\n  ... snip ...\n\n  Temporary breakpoint 1, main () at /tmp/hello.c:18\n  18\t  printf (\"Hello World\\n\");\n  (gdb)\n\nNotice this line:\n\n  `target:/tmp/hello.x\u0027 has disappeared; keeping its symbols.\n\nThat\u0027s wrong, the executable hasn\u0027t been removed, GDB just doesn\u0027t\nknow how to check if the remote file has changed, and so falls back to\nassuming that the file has been removed.\n\nIn this commit I update reread_symbols to use bfd_stat instead of\na direct stat call, this adds support for target: files, and fixes the\nproblem.\n\nThis change was proposed before in this commit:\n\n  https://inbox.sourceware.org/gdb-patches/20200114210956.25115-3-tromey@adacore.com/\n\nHowever, that patch never got merged, and seemed to get stuck\ndiscussing issues around gnulib stat vs system stat as used by BFD.\n\nI didn\u0027t 100% understand the issues discussed in that thread, however,\nI think the problem with the previous thread related to the changes in\ngdb_bfd.c, rather than to the change in symfile.c.  As such, I think\nthis change might be acceptable, my reasoning is:\n\n  - the objfile::mtime field is set by a call to bfd_get_mtime (see\n    objfiles.c), which calls bfd_stat under the hood.  This will end\n    up using the system stat,\n\n  - In symfile.c we currently call stat directly, which will call the\n    gnulib stat, which, if I understand the above thread correctly,\n    might give a different result to the system stat in some cases,\n\n  - By switching to using bfd_stat in symfile.c we should now be\n    consistently calling the system stat.\n\nThere is another issue that came up during testing that this commit\nfixes.  Consider this GDB session:\n\n  $ gdb -q\n  (gdb) target extended-remote | ./gdbserver/gdbserver --multi --once -\n  Remote debugging using | ./gdbserver/gdbserver --multi --once -\n  Remote debugging using stdio\n  (gdb) file /tmp/hello.x\n  Reading symbols from /tmp/hello.x...\n  (gdb) set remote exec-file /tmp/hello.x\n  (gdb) start\n  ... snip ...\n  (gdb) load\n  `system-supplied DSO at 0x7ffff7fcf000\u0027 has disappeared; keeping its symbols.\n  Loading section .interp, size 0x1c lma 0x4002a8\n  ... snip ...\n  Start address 0x0000000000401050, load size 2004\n  Transfer rate: 326 KB/sec, 87 bytes/write.\n\nNotice this line:\n\n  `system-supplied DSO at 0x7ffff7fcf000\u0027 has disappeared; keeping its symbols.\n\nWe actually see the same output, for the same reasons, when using a\nnative target, like this:\n\n  $ gdb -q\n  (gdb) file /tmp/hello.x\n  Reading symbols from /tmp/hello.x...\n  (gdb) start\n  ... snip ...\n  (gdb) load\n  `system-supplied DSO at 0x7ffff7fcf000\u0027 has disappeared; keeping its symbols.\n  You can\u0027t do that when your target is `native\u0027\n  (gdb)\n\nIn both cases this line appears because load_command (symfile.c) calls\nreread_symbols, and reread_symbols loops over every currently loaded\nobjfile and tries to check if the file has changed on disk by calling\nstat.\n\nHowever, the `system-supplied DSO at 0x7ffff7fcf000\u0027 is an in-memory\nBFD, the filename for this BFD is literally the string\n\u0027system-supplied DSO at 0x7ffff7fcf000\u0027.\n\nBefore this commit GDB would try to use the system \u0027stat\u0027 call to stat\nthe file `system-supplied DSO at 0x7ffff7fcf000\u0027, which obviously\nfails; there\u0027s no file with that name (usually).  As a consequence of\nthe stat failing GDB prints the \u0027 .... has disappeared ...\u0027 line.\n\nInitially, all this commit did was switch from using \u0027stat\u0027 to using\n\u0027bfd_stat\u0027.  Calling bfd_stat on an in-memory BFD works just fine,\nhowever, BFD just fills the \u0027struct stat\u0027 buffer with zeros (except\nfor the file size), see memory_bstat in bfd/bfdio.c.\n\nHowever, there is a bit of a weirdness about in-memory BFDs.  When\nthey are initially created the libbfd caches an mtime within the bfd\nobject, this is done in bfd_from_remote_memory (elfcode.h), the cached\nmtime is the time at which the in-memory BFD is created.\n\nWhat this means is that when GDB creates the in-memory BFD, and we\ncall bfd_get_mtime(), the value returned, which GDB caches within\nobjfile::mtime is the creation time of the in-memory BFD.  But, when\nthis patch changes to use bfd_stat() we now get back 0, and so we\nbelieve that the in-memory BFD has changed.  This is a change in\nbehaviour.\n\nTo avoid this change in behaviour, in this commit, I propose that we\nalways skip in-memory BFDs in reread_symbols.  This preserves the\nbehaviour from before this commit -- mostly.\n\nAs I\u0027m not specifically checking for, and then skipping, in-memory\nBFDs, we no longer see this line:\n\n  `system-supplied DSO at 0x7ffff7fcf000\u0027 has disappeared; keeping its symbols.\n\nWhich I think is an improvement.\n\nCo-Authored-By: Tom Tromey \u003ctromey@adacore.com\u003e\nApproved-By: Tom Tromey \u003ctom@tromey.com\u003e\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "30147b7c3df2e6765579505b12fac4aef3c62064",
      "old_mode": 33188,
      "old_path": "gdb/symfile.c",
      "new_id": "584a62cd1d68f61d9112857925d191a52288aa6f",
      "new_mode": 33188,
      "new_path": "gdb/symfile.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "3a264f239ed7b1ef63cf23adcfb2169a695a80ba",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.server/target-exec-file.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "9260df8b88dff5d703931cbf131c15560b6f63b7",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.server/target-exec-file.exp"
    }
  ]
}
