)]}'
{
  "commit": "cd9b374ffe372dcaf7e4c15548cf53a301d8dcdd",
  "tree": "39a53297d22403c28a922ebdda2b084f33c13793",
  "parents": [
    "7816b81e9b36ea0f57662bfd7446b573bf0c9e54"
  ],
  "author": {
    "name": "Andrew Burgess",
    "email": "aburgess@redhat.com",
    "time": "Thu Jan 25 14:25:57 2024 +0000"
  },
  "committer": {
    "name": "Andrew Burgess",
    "email": "aburgess@redhat.com",
    "time": "Mon Mar 25 17:14:19 2024 +0000"
  },
  "message": "gdb/gdbserver: share some code relating to target description creation\n\nThis commit is part of a series to share more of the x86 target\ndescription creation code between GDB and gdbserver.\n\nUnlike previous commits which were mostly refactoring, this commit is\nthe first that makes a real change, though that change should mostly\nbe for gdbserver; I\u0027ve largely adopted the \"GDB\" way of doing things\nfor gdbserver, and this fixes a real gdbserver bug.\n\nOn a x86-64 Linux target, running the test:\n\n  gdb.server/connect-with-no-symbol-file.exp\n\nresults in two core files being created.  Both of these core files are\nfrom the inferior process, created after gdbserver has detached.\n\nIn this test a gdbserver process is started and then, after gdbserver\nhas started, but before GDB attaches, we either delete the inferior\nexecutable, or change its permissions so it can\u0027t be read.  Only after\ndoing this do we attempt to connect with GDB.\n\nAs GDB connects to gdbserver, gdbserver attempts to figure out the\ntarget description so that it can send the description to GDB, this\ninvolves a call to x86_linux_read_description.\n\nIn x86_linux_read_description one of the first things we do is try to\nfigure out if the process is 32-bit or 64-bit.  To do this we look up\nthe executable via the thread-id, and then attempt to read the\narchitecture size from the executable.  This isn\u0027t going to work if\nthe executable has been deleted, or is no longer readable.\n\nAnd so, as we can\u0027t read the executable, we default to an i386 target\nand use an i386 target description.\n\nA consequence of using an i386 target description is that addresses\nare assumed to be 32-bits.  Here\u0027s an example session that shows the\nproblems this causes.  This is run on an x86-64 machine, and the test\nbinary (xx.x) is a standard 64-bit x86-64 binary:\n\n  shell_1$ gdbserver --once localhost :54321 /tmp/xx.x\n\n  shell_2$ gdb -q\n  (gdb) set sysroot\n  (gdb) shell chmod 000 /tmp/xx.x\n  (gdb) target remote :54321\n  Remote debugging using :54321\n  warning: /tmp/xx.x: Permission denied.\n  0xf7fd3110 in ?? ()\n  (gdb) show architecture\n  The target architecture is set to \"auto\" (currently \"i386\").\n  (gdb) p/x $pc\n  $1 \u003d 0xf7fd3110\n  (gdb) info proc mappings\n  process 2412639\n  Mapped address spaces:\n\n  \tStart Addr   End Addr       Size     Offset  Perms   objfile\n  \t  0x400000   0x401000     0x1000        0x0  r--p   /tmp/xx.x\n  \t  0x401000   0x402000     0x1000     0x1000  r-xp   /tmp/xx.x\n  \t  0x402000   0x403000     0x1000     0x2000  r--p   /tmp/xx.x\n  \t  0x403000   0x405000     0x2000     0x2000  rw-p   /tmp/xx.x\n  \t0xf7fcb000 0xf7fcf000     0x4000        0x0  r--p   [vvar]\n  \t0xf7fcf000 0xf7fd1000     0x2000        0x0  r-xp   [vdso]\n  \t0xf7fd1000 0xf7fd3000     0x2000        0x0  r--p   /usr/lib64/ld-2.30.so\n  \t0xf7fd3000 0xf7ff3000    0x20000     0x2000  r-xp   /usr/lib64/ld-2.30.so\n  \t0xf7ff3000 0xf7ffb000     0x8000    0x22000  r--p   /usr/lib64/ld-2.30.so\n  \t0xf7ffc000 0xf7ffe000     0x2000    0x2a000  rw-p   /usr/lib64/ld-2.30.so\n  \t0xf7ffe000 0xf7fff000     0x1000        0x0  rw-p\n  \t0xfffda000 0xfffff000    0x25000        0x0  rw-p   [stack]\n  \t0xff600000 0xff601000     0x1000        0x0  r-xp   [vsyscall]\n  (gdb) info inferiors\n    Num  Description       Connection           Executable\n  * 1    process 2412639   1 (remote :54321)\n  (gdb) shell cat /proc/2412639/maps\n  00400000-00401000 r--p 00000000 fd:03 45907133           /tmp/xx.x\n  00401000-00402000 r-xp 00001000 fd:03 45907133           /tmp/xx.x\n  00402000-00403000 r--p 00002000 fd:03 45907133           /tmp/xx.x\n  00403000-00405000 rw-p 00002000 fd:03 45907133           /tmp/xx.x\n  7ffff7fcb000-7ffff7fcf000 r--p 00000000 00:00 0          [vvar]\n  7ffff7fcf000-7ffff7fd1000 r-xp 00000000 00:00 0          [vdso]\n  7ffff7fd1000-7ffff7fd3000 r--p 00000000 fd:00 143904     /usr/lib64/ld-2.30.so\n  7ffff7fd3000-7ffff7ff3000 r-xp 00002000 fd:00 143904     /usr/lib64/ld-2.30.so\n  7ffff7ff3000-7ffff7ffb000 r--p 00022000 fd:00 143904     /usr/lib64/ld-2.30.so\n  7ffff7ffc000-7ffff7ffe000 rw-p 0002a000 fd:00 143904     /usr/lib64/ld-2.30.so\n  7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0\n  7ffffffda000-7ffffffff000 rw-p 00000000 00:00 0          [stack]\n  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0  [vsyscall]\n  (gdb)\n\nNotice the difference between the mappings reported via GDB and those\nreported directly from the kernel via /proc/PID/maps, the addresses of\nevery mapping is clamped to 32-bits for GDB, while the kernel reports\nreal 64-bit addresses.\n\nNotice also that the $pc value is a 32-bit value.  It appears to be\nwithin one of the mappings reported by GDB, but is outside any of the\nmappings reported from the kernel.\n\nAnd this is where the problem arises.  When gdbserver detaches from\nthe inferior we pass the inferior the address from which it should\nresume.  Due to the 32/64 bit confusion we tell the inferior to resume\nfrom the 32-bit $pc value, which is not within any valid mapping, and\nso, as soon as the inferior resumes, it segfaults.\n\nIf we look at how GDB (not gdbserver) figures out its target\ndescription then we see an interesting difference.  GDB doesn\u0027t try to\nread the executable.  Instead GDB uses ptrace to query the thread\u0027s\nstate, and uses this to figure out the if the thread is 32 or 64 bit.\n\nIf we update gdbserver to do it the \"GDB\" way then the above problem\nis resolved, gdbserver now sees the process as 64-bit, and when we\ndetach from the inferior we give it the correct 64-bit address, and\nthe inferior no longer segfaults.\n\nNow, I could just update the gdbserver code, but better, I think, to\nshare one copy of the code between GDB and gdbserver in gdb/nat/.\nThat is what this commit does.\n\nThe cores of x86_linux_read_description from gdbserver and\nx86_linux_nat_target::read_description from GDB are moved into a new\nfile gdb/nat/x86-linux-tdesc.c and combined into a single function\nx86_linux_tdesc_for_tid which is called from each location.\n\nThis new function does things the GDB way, the only changes are to\nallow for the sharing; we now have a callback function to call the\nfirst time that the xcr0 state is read, this allows for GDB and\ngdbserver to perform their own initialisation as needed, and\nadditionally, the new function takes a pointer for where to cache the\nxcr0 value, this isn\u0027t needed for this commit, but will be useful in a\nlater commit where gdbserver will want to read this cached xcr0\nvalue.\n\nAnother thing to note about this commit is how the functions\ni386_linux_read_description and amd64_linux_read_description are\nhandled.  For now I\u0027ve left these function as implemented separately\nin GDB and gdbserver.  I\u0027ve moved the declarations of these functions\ninto gdb/nat/x86-linux-tdesc.h, but the implementations are left as\nseparate.\n\nA later commit in this series will make these functions shared too,\nbut doing this is not trivial, so I\u0027ve left that for a separate\ncommit.  Merging the declarations as I\u0027ve done here ensures that\neveryone implements the function to the same API, and once these\nfunctions are shared (in a later commit) we\u0027ll want a shared\ndeclaration anyway.\n\nApproved-By: John Baldwin \u003cjhb@FreeBSD.org\u003e\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "331620375aed4c0c56335b8d59aa28d73ed1c952",
      "old_mode": 33188,
      "old_path": "gdb/Makefile.in",
      "new_id": "38f4d5fde98755b7e6b9d3da5d4fa056181ac346",
      "new_mode": 33188,
      "new_path": "gdb/Makefile.in"
    },
    {
      "type": "modify",
      "old_id": "a512ec5dd026c0808f7df8ae9ed768fa838abdd4",
      "old_mode": 33188,
      "old_path": "gdb/amd64-linux-tdep.c",
      "new_id": "7e0900dc6f9be639d48b1dd1f384118937a10c13",
      "new_mode": 33188,
      "new_path": "gdb/amd64-linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "2003dcda78f17d1510979eb53a0495ab03a3b059",
      "old_mode": 33188,
      "old_path": "gdb/amd64-linux-tdep.h",
      "new_id": "0ec49e7fe03c0645ec49401539dc70bb17a9fc82",
      "new_mode": 33188,
      "new_path": "gdb/amd64-linux-tdep.h"
    },
    {
      "type": "modify",
      "old_id": "8b98511cef7c653cb3457755695a733d7db58e69",
      "old_mode": 33188,
      "old_path": "gdb/configure.nat",
      "new_id": "4bcc0696027b2e20f86ea7198bdcc91ebe26b91c",
      "new_mode": 33188,
      "new_path": "gdb/configure.nat"
    },
    {
      "type": "modify",
      "old_id": "8dd7203b6c332409001f1a39aed039351d67c697",
      "old_mode": 33188,
      "old_path": "gdb/i386-linux-tdep.c",
      "new_id": "f5f7a36bf36da0c65365947eb8c82a125ecdf595",
      "new_mode": 33188,
      "new_path": "gdb/i386-linux-tdep.c"
    },
    {
      "type": "modify",
      "old_id": "07593c6a8ecc7212a35185d96793d1c2c8e71e3a",
      "old_mode": 33188,
      "old_path": "gdb/i386-linux-tdep.h",
      "new_id": "e8691cd778ef6919f4db6fbe2c35a7292d57e1b8",
      "new_mode": 33188,
      "new_path": "gdb/i386-linux-tdep.h"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "be7014d2b5f50b0e41021f50f35d088f06003477",
      "new_mode": 33188,
      "new_path": "gdb/nat/x86-linux-tdesc.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "3727a8bf95ec4b90ce8a2713f21494a0bb00e84b",
      "new_mode": 33188,
      "new_path": "gdb/nat/x86-linux-tdesc.h"
    },
    {
      "type": "modify",
      "old_id": "b39d05c401fc351ab4322a3799196d5693942b1b",
      "old_mode": 33188,
      "old_path": "gdb/x86-linux-nat.c",
      "new_id": "872e27e739f9c0d55520de1bc4ad615e288816c5",
      "new_mode": 33188,
      "new_path": "gdb/x86-linux-nat.c"
    },
    {
      "type": "modify",
      "old_id": "9e861a75088a0ce073a5b2a9747d0d98d2258c93",
      "old_mode": 33188,
      "old_path": "gdbserver/configure.srv",
      "new_id": "7a2702d78bfd4f649121e92ccde9514c85e4114c",
      "new_mode": 33188,
      "new_path": "gdbserver/configure.srv"
    },
    {
      "type": "modify",
      "old_id": "54e4c9812bb3339cbb198298d2166145737b3dfe",
      "old_mode": 33188,
      "old_path": "gdbserver/linux-amd64-ipa.cc",
      "new_id": "f97b0d6a1d93954220a040e3ed0c1fb6a563daaf",
      "new_mode": 33188,
      "new_path": "gdbserver/linux-amd64-ipa.cc"
    },
    {
      "type": "modify",
      "old_id": "2e4646f8c032b14d142b6865ae9423f468023526",
      "old_mode": 33188,
      "old_path": "gdbserver/linux-i386-ipa.cc",
      "new_id": "459b8055b5cbf7ec9a861cf83153600b4c348342",
      "new_mode": 33188,
      "new_path": "gdbserver/linux-i386-ipa.cc"
    },
    {
      "type": "modify",
      "old_id": "30d876efc5dea570e09abb5bda4a08f30ca617e4",
      "old_mode": 33188,
      "old_path": "gdbserver/linux-x86-low.cc",
      "new_id": "9bf369f8a34625da75df33e0f79c471049c1f17d",
      "new_mode": 33188,
      "new_path": "gdbserver/linux-x86-low.cc"
    },
    {
      "type": "modify",
      "old_id": "626207fc477ad77ac61ea21a5bfec21d46b95bac",
      "old_mode": 33188,
      "old_path": "gdbserver/linux-x86-tdesc.cc",
      "new_id": "9fd64d8574bdb145ebb8cdedf6ef11c8af8e01cf",
      "new_mode": 33188,
      "new_path": "gdbserver/linux-x86-tdesc.cc"
    },
    {
      "type": "modify",
      "old_id": "f9561b129ae98f66b1ff91a3a3dde97c2ac3b4dd",
      "old_mode": 33188,
      "old_path": "gdbserver/linux-x86-tdesc.h",
      "new_id": "576aaf5e1658205b9cc99b3337853aee0db9c69a",
      "new_mode": 33188,
      "new_path": "gdbserver/linux-x86-tdesc.h"
    }
  ]
}
