fbsd-tdep.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402
  1. /* Target-dependent code for FreeBSD, architecture-independent.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "auxv.h"
  16. #include "gdbcore.h"
  17. #include "inferior.h"
  18. #include "objfiles.h"
  19. #include "regcache.h"
  20. #include "regset.h"
  21. #include "gdbthread.h"
  22. #include "objfiles.h"
  23. #include "xml-syscall.h"
  24. #include <sys/socket.h>
  25. #include <arpa/inet.h>
  26. #include "elf-bfd.h"
  27. #include "fbsd-tdep.h"
  28. #include "gcore-elf.h"
  29. /* This enum is derived from FreeBSD's <sys/signal.h>. */
  30. enum
  31. {
  32. FREEBSD_SIGHUP = 1,
  33. FREEBSD_SIGINT = 2,
  34. FREEBSD_SIGQUIT = 3,
  35. FREEBSD_SIGILL = 4,
  36. FREEBSD_SIGTRAP = 5,
  37. FREEBSD_SIGABRT = 6,
  38. FREEBSD_SIGEMT = 7,
  39. FREEBSD_SIGFPE = 8,
  40. FREEBSD_SIGKILL = 9,
  41. FREEBSD_SIGBUS = 10,
  42. FREEBSD_SIGSEGV = 11,
  43. FREEBSD_SIGSYS = 12,
  44. FREEBSD_SIGPIPE = 13,
  45. FREEBSD_SIGALRM = 14,
  46. FREEBSD_SIGTERM = 15,
  47. FREEBSD_SIGURG = 16,
  48. FREEBSD_SIGSTOP = 17,
  49. FREEBSD_SIGTSTP = 18,
  50. FREEBSD_SIGCONT = 19,
  51. FREEBSD_SIGCHLD = 20,
  52. FREEBSD_SIGTTIN = 21,
  53. FREEBSD_SIGTTOU = 22,
  54. FREEBSD_SIGIO = 23,
  55. FREEBSD_SIGXCPU = 24,
  56. FREEBSD_SIGXFSZ = 25,
  57. FREEBSD_SIGVTALRM = 26,
  58. FREEBSD_SIGPROF = 27,
  59. FREEBSD_SIGWINCH = 28,
  60. FREEBSD_SIGINFO = 29,
  61. FREEBSD_SIGUSR1 = 30,
  62. FREEBSD_SIGUSR2 = 31,
  63. FREEBSD_SIGTHR = 32,
  64. FREEBSD_SIGLIBRT = 33,
  65. FREEBSD_SIGRTMIN = 65,
  66. FREEBSD_SIGRTMAX = 126,
  67. };
  68. /* Constants for values of si_code as defined in FreeBSD's
  69. <sys/signal.h>. */
  70. #define FBSD_SI_USER 0x10001
  71. #define FBSD_SI_QUEUE 0x10002
  72. #define FBSD_SI_TIMER 0x10003
  73. #define FBSD_SI_ASYNCIO 0x10004
  74. #define FBSD_SI_MESGQ 0x10005
  75. #define FBSD_SI_KERNEL 0x10006
  76. #define FBSD_SI_LWP 0x10007
  77. #define FBSD_ILL_ILLOPC 1
  78. #define FBSD_ILL_ILLOPN 2
  79. #define FBSD_ILL_ILLADR 3
  80. #define FBSD_ILL_ILLTRP 4
  81. #define FBSD_ILL_PRVOPC 5
  82. #define FBSD_ILL_PRVREG 6
  83. #define FBSD_ILL_COPROC 7
  84. #define FBSD_ILL_BADSTK 8
  85. #define FBSD_BUS_ADRALN 1
  86. #define FBSD_BUS_ADRERR 2
  87. #define FBSD_BUS_OBJERR 3
  88. #define FBSD_BUS_OOMERR 100
  89. #define FBSD_SEGV_MAPERR 1
  90. #define FBSD_SEGV_ACCERR 2
  91. #define FBSD_SEGV_PKUERR 100
  92. #define FBSD_FPE_INTOVF 1
  93. #define FBSD_FPE_INTDIV 2
  94. #define FBSD_FPE_FLTDIV 3
  95. #define FBSD_FPE_FLTOVF 4
  96. #define FBSD_FPE_FLTUND 5
  97. #define FBSD_FPE_FLTRES 6
  98. #define FBSD_FPE_FLTINV 7
  99. #define FBSD_FPE_FLTSUB 8
  100. #define FBSD_TRAP_BRKPT 1
  101. #define FBSD_TRAP_TRACE 2
  102. #define FBSD_TRAP_DTRACE 3
  103. #define FBSD_TRAP_CAP 4
  104. #define FBSD_CLD_EXITED 1
  105. #define FBSD_CLD_KILLED 2
  106. #define FBSD_CLD_DUMPED 3
  107. #define FBSD_CLD_TRAPPED 4
  108. #define FBSD_CLD_STOPPED 5
  109. #define FBSD_CLD_CONTINUED 6
  110. #define FBSD_POLL_IN 1
  111. #define FBSD_POLL_OUT 2
  112. #define FBSD_POLL_MSG 3
  113. #define FBSD_POLL_ERR 4
  114. #define FBSD_POLL_PRI 5
  115. #define FBSD_POLL_HUP 6
  116. /* FreeBSD kernels 12.0 and later include a copy of the
  117. 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
  118. operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
  119. The constants below define the offset of field members and flags in
  120. this structure used by methods in this file. Note that the
  121. 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
  122. containing the size of the structure. */
  123. #define LWPINFO_OFFSET 0x4
  124. /* Offsets in ptrace_lwpinfo. */
  125. #define LWPINFO_PL_FLAGS 0x8
  126. #define LWPINFO64_PL_SIGINFO 0x30
  127. #define LWPINFO32_PL_SIGINFO 0x2c
  128. /* Flags in pl_flags. */
  129. #define PL_FLAG_SI 0x20 /* siginfo is valid */
  130. /* Sizes of siginfo_t. */
  131. #define SIZE64_SIGINFO_T 80
  132. #define SIZE32_SIGINFO_T 64
  133. /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
  134. dump notes. See <sys/user.h> for the definition of struct
  135. kinfo_vmentry. This data structure should have the same layout on
  136. all architectures.
  137. Note that FreeBSD 7.0 used an older version of this structure
  138. (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
  139. dump note wasn't introduced until FreeBSD 9.2. As a result, the
  140. core dump note has always used the 7.1 and later structure
  141. format. */
  142. #define KVE_STRUCTSIZE 0x0
  143. #define KVE_START 0x8
  144. #define KVE_END 0x10
  145. #define KVE_OFFSET 0x18
  146. #define KVE_FLAGS 0x2c
  147. #define KVE_PROTECTION 0x38
  148. #define KVE_PATH 0x88
  149. /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
  150. match the KVME_PROT_* constants in <sys/user.h>. */
  151. #define KINFO_VME_PROT_READ 0x00000001
  152. #define KINFO_VME_PROT_WRITE 0x00000002
  153. #define KINFO_VME_PROT_EXEC 0x00000004
  154. /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
  155. match the KVME_FLAG_* constants in <sys/user.h>. */
  156. #define KINFO_VME_FLAG_COW 0x00000001
  157. #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
  158. #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
  159. #define KINFO_VME_FLAG_SUPER 0x00000008
  160. #define KINFO_VME_FLAG_GROWS_UP 0x00000010
  161. #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
  162. /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
  163. dump notes. See <sys/user.h> for the definition of struct
  164. kinfo_file. This data structure should have the same layout on all
  165. architectures.
  166. Note that FreeBSD 7.0 used an older version of this structure
  167. (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
  168. note wasn't introduced until FreeBSD 9.2. As a result, the core
  169. dump note has always used the 7.1 and later structure format. */
  170. #define KF_STRUCTSIZE 0x0
  171. #define KF_TYPE 0x4
  172. #define KF_FD 0x8
  173. #define KF_FLAGS 0x10
  174. #define KF_OFFSET 0x18
  175. #define KF_VNODE_TYPE 0x20
  176. #define KF_SOCK_DOMAIN 0x24
  177. #define KF_SOCK_TYPE 0x28
  178. #define KF_SOCK_PROTOCOL 0x2c
  179. #define KF_SA_LOCAL 0x30
  180. #define KF_SA_PEER 0xb0
  181. #define KF_PATH 0x170
  182. /* Constants for the 'kf_type' field in struct kinfo_file. These
  183. match the KF_TYPE_* constants in <sys/user.h>. */
  184. #define KINFO_FILE_TYPE_VNODE 1
  185. #define KINFO_FILE_TYPE_SOCKET 2
  186. #define KINFO_FILE_TYPE_PIPE 3
  187. #define KINFO_FILE_TYPE_FIFO 4
  188. #define KINFO_FILE_TYPE_KQUEUE 5
  189. #define KINFO_FILE_TYPE_CRYPTO 6
  190. #define KINFO_FILE_TYPE_MQUEUE 7
  191. #define KINFO_FILE_TYPE_SHM 8
  192. #define KINFO_FILE_TYPE_SEM 9
  193. #define KINFO_FILE_TYPE_PTS 10
  194. #define KINFO_FILE_TYPE_PROCDESC 11
  195. /* Special values for the 'kf_fd' field in struct kinfo_file. These
  196. match the KF_FD_TYPE_* constants in <sys/user.h>. */
  197. #define KINFO_FILE_FD_TYPE_CWD -1
  198. #define KINFO_FILE_FD_TYPE_ROOT -2
  199. #define KINFO_FILE_FD_TYPE_JAIL -3
  200. #define KINFO_FILE_FD_TYPE_TRACE -4
  201. #define KINFO_FILE_FD_TYPE_TEXT -5
  202. #define KINFO_FILE_FD_TYPE_CTTY -6
  203. /* Flags in the 'kf_flags' field in struct kinfo_file. These match
  204. the KF_FLAG_* constants in <sys/user.h>. */
  205. #define KINFO_FILE_FLAG_READ 0x00000001
  206. #define KINFO_FILE_FLAG_WRITE 0x00000002
  207. #define KINFO_FILE_FLAG_APPEND 0x00000004
  208. #define KINFO_FILE_FLAG_ASYNC 0x00000008
  209. #define KINFO_FILE_FLAG_FSYNC 0x00000010
  210. #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
  211. #define KINFO_FILE_FLAG_DIRECT 0x00000040
  212. #define KINFO_FILE_FLAG_HASLOCK 0x00000080
  213. #define KINFO_FILE_FLAG_EXEC 0x00004000
  214. /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
  215. These match the KF_VTYPE_* constants in <sys/user.h>. */
  216. #define KINFO_FILE_VTYPE_VREG 1
  217. #define KINFO_FILE_VTYPE_VDIR 2
  218. #define KINFO_FILE_VTYPE_VCHR 4
  219. #define KINFO_FILE_VTYPE_VLNK 5
  220. #define KINFO_FILE_VTYPE_VSOCK 6
  221. #define KINFO_FILE_VTYPE_VFIFO 7
  222. /* Constants for socket address families. These match AF_* constants
  223. in <sys/socket.h>. */
  224. #define FBSD_AF_UNIX 1
  225. #define FBSD_AF_INET 2
  226. #define FBSD_AF_INET6 28
  227. /* Constants for socket types. These match SOCK_* constants in
  228. <sys/socket.h>. */
  229. #define FBSD_SOCK_STREAM 1
  230. #define FBSD_SOCK_DGRAM 2
  231. #define FBSD_SOCK_SEQPACKET 5
  232. /* Constants for IP protocols. These match IPPROTO_* constants in
  233. <netinet/in.h>. */
  234. #define FBSD_IPPROTO_ICMP 1
  235. #define FBSD_IPPROTO_TCP 6
  236. #define FBSD_IPPROTO_UDP 17
  237. #define FBSD_IPPROTO_SCTP 132
  238. /* Socket address structures. These have the same layout on all
  239. FreeBSD architectures. In addition, multibyte fields such as IP
  240. addresses are always stored in network byte order. */
  241. struct fbsd_sockaddr_in
  242. {
  243. uint8_t sin_len;
  244. uint8_t sin_family;
  245. uint8_t sin_port[2];
  246. uint8_t sin_addr[4];
  247. char sin_zero[8];
  248. };
  249. struct fbsd_sockaddr_in6
  250. {
  251. uint8_t sin6_len;
  252. uint8_t sin6_family;
  253. uint8_t sin6_port[2];
  254. uint32_t sin6_flowinfo;
  255. uint8_t sin6_addr[16];
  256. uint32_t sin6_scope_id;
  257. };
  258. struct fbsd_sockaddr_un
  259. {
  260. uint8_t sun_len;
  261. uint8_t sun_family;
  262. char sun_path[104];
  263. };
  264. /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
  265. <sys/_sigset.h> and is the same value on all architectures. */
  266. #define SIG_WORDS 4
  267. /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
  268. dump notes. See <sys/user.h> for the definition of struct
  269. kinfo_proc. This data structure has different layouts on different
  270. architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
  271. uses a 32-bit time_t while all other architectures use a 64-bit
  272. time_t.
  273. The core dump note actually contains one kinfo_proc structure for
  274. each thread, but all of the process-wide data can be obtained from
  275. the first structure. One result of this note's format is that some
  276. of the process-wide status available in the native target method
  277. from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
  278. is not available from a core dump. Instead, the per-thread data
  279. structures contain the value of these fields for individual
  280. threads. */
  281. struct kinfo_proc_layout
  282. {
  283. /* Offsets of struct kinfo_proc members. */
  284. int ki_layout;
  285. int ki_pid;
  286. int ki_ppid;
  287. int ki_pgid;
  288. int ki_tpgid;
  289. int ki_sid;
  290. int ki_tdev_freebsd11;
  291. int ki_sigignore;
  292. int ki_sigcatch;
  293. int ki_uid;
  294. int ki_ruid;
  295. int ki_svuid;
  296. int ki_rgid;
  297. int ki_svgid;
  298. int ki_ngroups;
  299. int ki_groups;
  300. int ki_size;
  301. int ki_rssize;
  302. int ki_tsize;
  303. int ki_dsize;
  304. int ki_ssize;
  305. int ki_start;
  306. int ki_nice;
  307. int ki_comm;
  308. int ki_tdev;
  309. int ki_rusage;
  310. int ki_rusage_ch;
  311. /* Offsets of struct rusage members. */
  312. int ru_utime;
  313. int ru_stime;
  314. int ru_maxrss;
  315. int ru_minflt;
  316. int ru_majflt;
  317. };
  318. const struct kinfo_proc_layout kinfo_proc_layout_32 =
  319. {
  320. .ki_layout = 0x4,
  321. .ki_pid = 0x28,
  322. .ki_ppid = 0x2c,
  323. .ki_pgid = 0x30,
  324. .ki_tpgid = 0x34,
  325. .ki_sid = 0x38,
  326. .ki_tdev_freebsd11 = 0x44,
  327. .ki_sigignore = 0x68,
  328. .ki_sigcatch = 0x78,
  329. .ki_uid = 0x88,
  330. .ki_ruid = 0x8c,
  331. .ki_svuid = 0x90,
  332. .ki_rgid = 0x94,
  333. .ki_svgid = 0x98,
  334. .ki_ngroups = 0x9c,
  335. .ki_groups = 0xa0,
  336. .ki_size = 0xe0,
  337. .ki_rssize = 0xe4,
  338. .ki_tsize = 0xec,
  339. .ki_dsize = 0xf0,
  340. .ki_ssize = 0xf4,
  341. .ki_start = 0x118,
  342. .ki_nice = 0x145,
  343. .ki_comm = 0x17f,
  344. .ki_tdev = 0x1f0,
  345. .ki_rusage = 0x220,
  346. .ki_rusage_ch = 0x278,
  347. .ru_utime = 0x0,
  348. .ru_stime = 0x10,
  349. .ru_maxrss = 0x20,
  350. .ru_minflt = 0x30,
  351. .ru_majflt = 0x34,
  352. };
  353. const struct kinfo_proc_layout kinfo_proc_layout_i386 =
  354. {
  355. .ki_layout = 0x4,
  356. .ki_pid = 0x28,
  357. .ki_ppid = 0x2c,
  358. .ki_pgid = 0x30,
  359. .ki_tpgid = 0x34,
  360. .ki_sid = 0x38,
  361. .ki_tdev_freebsd11 = 0x44,
  362. .ki_sigignore = 0x68,
  363. .ki_sigcatch = 0x78,
  364. .ki_uid = 0x88,
  365. .ki_ruid = 0x8c,
  366. .ki_svuid = 0x90,
  367. .ki_rgid = 0x94,
  368. .ki_svgid = 0x98,
  369. .ki_ngroups = 0x9c,
  370. .ki_groups = 0xa0,
  371. .ki_size = 0xe0,
  372. .ki_rssize = 0xe4,
  373. .ki_tsize = 0xec,
  374. .ki_dsize = 0xf0,
  375. .ki_ssize = 0xf4,
  376. .ki_start = 0x118,
  377. .ki_nice = 0x135,
  378. .ki_comm = 0x16f,
  379. .ki_tdev = 0x1e0,
  380. .ki_rusage = 0x210,
  381. .ki_rusage_ch = 0x258,
  382. .ru_utime = 0x0,
  383. .ru_stime = 0x8,
  384. .ru_maxrss = 0x10,
  385. .ru_minflt = 0x20,
  386. .ru_majflt = 0x24,
  387. };
  388. const struct kinfo_proc_layout kinfo_proc_layout_64 =
  389. {
  390. .ki_layout = 0x4,
  391. .ki_pid = 0x48,
  392. .ki_ppid = 0x4c,
  393. .ki_pgid = 0x50,
  394. .ki_tpgid = 0x54,
  395. .ki_sid = 0x58,
  396. .ki_tdev_freebsd11 = 0x64,
  397. .ki_sigignore = 0x88,
  398. .ki_sigcatch = 0x98,
  399. .ki_uid = 0xa8,
  400. .ki_ruid = 0xac,
  401. .ki_svuid = 0xb0,
  402. .ki_rgid = 0xb4,
  403. .ki_svgid = 0xb8,
  404. .ki_ngroups = 0xbc,
  405. .ki_groups = 0xc0,
  406. .ki_size = 0x100,
  407. .ki_rssize = 0x108,
  408. .ki_tsize = 0x118,
  409. .ki_dsize = 0x120,
  410. .ki_ssize = 0x128,
  411. .ki_start = 0x150,
  412. .ki_nice = 0x185,
  413. .ki_comm = 0x1bf,
  414. .ki_tdev = 0x230,
  415. .ki_rusage = 0x260,
  416. .ki_rusage_ch = 0x2f0,
  417. .ru_utime = 0x0,
  418. .ru_stime = 0x10,
  419. .ru_maxrss = 0x20,
  420. .ru_minflt = 0x40,
  421. .ru_majflt = 0x48,
  422. };
  423. static struct gdbarch_data *fbsd_gdbarch_data_handle;
  424. struct fbsd_gdbarch_data
  425. {
  426. struct type *siginfo_type;
  427. };
  428. static void *
  429. init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
  430. {
  431. return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
  432. }
  433. static struct fbsd_gdbarch_data *
  434. get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
  435. {
  436. return ((struct fbsd_gdbarch_data *)
  437. gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
  438. }
  439. struct fbsd_pspace_data
  440. {
  441. /* Offsets in the runtime linker's 'Obj_Entry' structure. */
  442. LONGEST off_linkmap = 0;
  443. LONGEST off_tlsindex = 0;
  444. bool rtld_offsets_valid = false;
  445. /* vDSO mapping range. */
  446. struct mem_range vdso_range {};
  447. /* Zero if the range hasn't been searched for, > 0 if a range was
  448. found, or < 0 if a range was not found. */
  449. int vdso_range_p = 0;
  450. };
  451. /* Per-program-space data for FreeBSD architectures. */
  452. static const struct program_space_key<fbsd_pspace_data>
  453. fbsd_pspace_data_handle;
  454. static struct fbsd_pspace_data *
  455. get_fbsd_pspace_data (struct program_space *pspace)
  456. {
  457. struct fbsd_pspace_data *data;
  458. data = fbsd_pspace_data_handle.get (pspace);
  459. if (data == NULL)
  460. data = fbsd_pspace_data_handle.emplace (pspace);
  461. return data;
  462. }
  463. /* This is how we want PTIDs from core files to be printed. */
  464. static std::string
  465. fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
  466. {
  467. if (ptid.lwp () != 0)
  468. return string_printf ("LWP %ld", ptid.lwp ());
  469. return normal_pid_to_str (ptid);
  470. }
  471. /* Extract the name assigned to a thread from a core. Returns the
  472. string in a static buffer. */
  473. static const char *
  474. fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
  475. {
  476. static char buf[80];
  477. struct bfd_section *section;
  478. bfd_size_type size;
  479. if (thr->ptid.lwp () != 0)
  480. {
  481. /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
  482. whose contents are defined by a "struct thrmisc" declared in
  483. <sys/procfs.h> on FreeBSD. The per-thread name is stored as
  484. a null-terminated string as the first member of the
  485. structure. Rather than define the full structure here, just
  486. extract the null-terminated name from the start of the
  487. note. */
  488. thread_section_name section_name (".thrmisc", thr->ptid);
  489. section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
  490. if (section != NULL && bfd_section_size (section) > 0)
  491. {
  492. /* Truncate the name if it is longer than "buf". */
  493. size = bfd_section_size (section);
  494. if (size > sizeof buf - 1)
  495. size = sizeof buf - 1;
  496. if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
  497. size)
  498. && buf[0] != '\0')
  499. {
  500. buf[size] = '\0';
  501. /* Note that each thread will report the process command
  502. as its thread name instead of an empty name if a name
  503. has not been set explicitly. Return a NULL name in
  504. that case. */
  505. if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
  506. return buf;
  507. }
  508. }
  509. }
  510. return NULL;
  511. }
  512. /* Implement the "core_xfer_siginfo" gdbarch method. */
  513. static LONGEST
  514. fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
  515. ULONGEST offset, ULONGEST len)
  516. {
  517. size_t siginfo_size;
  518. if (gdbarch_long_bit (gdbarch) == 32)
  519. siginfo_size = SIZE32_SIGINFO_T;
  520. else
  521. siginfo_size = SIZE64_SIGINFO_T;
  522. if (offset > siginfo_size)
  523. return -1;
  524. thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
  525. asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
  526. if (section == NULL)
  527. return -1;
  528. gdb_byte buf[4];
  529. if (!bfd_get_section_contents (core_bfd, section, buf,
  530. LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
  531. return -1;
  532. int pl_flags = extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
  533. if (!(pl_flags & PL_FLAG_SI))
  534. return -1;
  535. if (offset + len > siginfo_size)
  536. len = siginfo_size - offset;
  537. ULONGEST siginfo_offset;
  538. if (gdbarch_long_bit (gdbarch) == 32)
  539. siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
  540. else
  541. siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
  542. if (!bfd_get_section_contents (core_bfd, section, readbuf,
  543. siginfo_offset + offset, len))
  544. return -1;
  545. return len;
  546. }
  547. static int
  548. find_signalled_thread (struct thread_info *info, void *data)
  549. {
  550. if (info->stop_signal () != GDB_SIGNAL_0
  551. && info->ptid.pid () == inferior_ptid.pid ())
  552. return 1;
  553. return 0;
  554. }
  555. /* Return a byte_vector containing the contents of a core dump note
  556. for the target object of type OBJECT. If STRUCTSIZE is non-zero,
  557. the data is prefixed with a 32-bit integer size to match the format
  558. used in FreeBSD NT_PROCSTAT_* notes. */
  559. static gdb::optional<gdb::byte_vector>
  560. fbsd_make_note_desc (enum target_object object, uint32_t structsize)
  561. {
  562. gdb::optional<gdb::byte_vector> buf =
  563. target_read_alloc (current_inferior ()->top_target (), object, NULL);
  564. if (!buf || buf->empty ())
  565. return {};
  566. if (structsize == 0)
  567. return buf;
  568. gdb::byte_vector desc (sizeof (structsize) + buf->size ());
  569. memcpy (desc.data (), &structsize, sizeof (structsize));
  570. memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
  571. return desc;
  572. }
  573. /* Create appropriate note sections for a corefile, returning them in
  574. allocated memory. */
  575. static gdb::unique_xmalloc_ptr<char>
  576. fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
  577. {
  578. gdb::unique_xmalloc_ptr<char> note_data;
  579. Elf_Internal_Ehdr *i_ehdrp;
  580. struct thread_info *curr_thr, *signalled_thr;
  581. /* Put a "FreeBSD" label in the ELF header. */
  582. i_ehdrp = elf_elfheader (obfd);
  583. i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
  584. gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
  585. if (get_exec_file (0))
  586. {
  587. const char *fname = lbasename (get_exec_file (0));
  588. std::string psargs = fname;
  589. const std::string &infargs = current_inferior ()->args ();
  590. if (!infargs.empty ())
  591. psargs += ' ' + infargs;
  592. note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
  593. note_size, fname,
  594. psargs.c_str ()));
  595. }
  596. /* Thread register information. */
  597. try
  598. {
  599. update_thread_list ();
  600. }
  601. catch (const gdb_exception_error &e)
  602. {
  603. exception_print (gdb_stderr, e);
  604. }
  605. /* Like the kernel, prefer dumping the signalled thread first.
  606. "First thread" is what tools use to infer the signalled thread.
  607. In case there's more than one signalled thread, prefer the
  608. current thread, if it is signalled. */
  609. curr_thr = inferior_thread ();
  610. if (curr_thr->stop_signal () != GDB_SIGNAL_0)
  611. signalled_thr = curr_thr;
  612. else
  613. {
  614. signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
  615. if (signalled_thr == NULL)
  616. signalled_thr = curr_thr;
  617. }
  618. enum gdb_signal stop_signal = signalled_thr->stop_signal ();
  619. gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, stop_signal,
  620. obfd, &note_data, note_size);
  621. for (thread_info *thr : current_inferior ()->non_exited_threads ())
  622. {
  623. if (thr == signalled_thr)
  624. continue;
  625. gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal,
  626. obfd, &note_data, note_size);
  627. }
  628. /* Auxiliary vector. */
  629. uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
  630. gdb::optional<gdb::byte_vector> note_desc =
  631. fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
  632. if (note_desc && !note_desc->empty ())
  633. {
  634. note_data.reset (elfcore_write_note (obfd, note_data.release (),
  635. note_size, "FreeBSD",
  636. NT_FREEBSD_PROCSTAT_AUXV,
  637. note_desc->data (),
  638. note_desc->size ()));
  639. if (!note_data)
  640. return NULL;
  641. }
  642. /* Virtual memory mappings. */
  643. note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
  644. if (note_desc && !note_desc->empty ())
  645. {
  646. note_data.reset (elfcore_write_note (obfd, note_data.release (),
  647. note_size, "FreeBSD",
  648. NT_FREEBSD_PROCSTAT_VMMAP,
  649. note_desc->data (),
  650. note_desc->size ()));
  651. if (!note_data)
  652. return NULL;
  653. }
  654. note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
  655. if (note_desc && !note_desc->empty ())
  656. {
  657. note_data.reset (elfcore_write_note (obfd, note_data.release (),
  658. note_size, "FreeBSD",
  659. NT_FREEBSD_PROCSTAT_PSSTRINGS,
  660. note_desc->data (),
  661. note_desc->size ()));
  662. if (!note_data)
  663. return NULL;
  664. }
  665. /* Include the target description when possible. */
  666. gcore_elf_make_tdesc_note (obfd, &note_data, note_size);
  667. return note_data;
  668. }
  669. /* Helper function to generate the file descriptor description for a
  670. single open file in 'info proc files'. */
  671. static const char *
  672. fbsd_file_fd (int kf_fd)
  673. {
  674. switch (kf_fd)
  675. {
  676. case KINFO_FILE_FD_TYPE_CWD:
  677. return "cwd";
  678. case KINFO_FILE_FD_TYPE_ROOT:
  679. return "root";
  680. case KINFO_FILE_FD_TYPE_JAIL:
  681. return "jail";
  682. case KINFO_FILE_FD_TYPE_TRACE:
  683. return "trace";
  684. case KINFO_FILE_FD_TYPE_TEXT:
  685. return "text";
  686. case KINFO_FILE_FD_TYPE_CTTY:
  687. return "ctty";
  688. default:
  689. return int_string (kf_fd, 10, 1, 0, 0);
  690. }
  691. }
  692. /* Helper function to generate the file type for a single open file in
  693. 'info proc files'. */
  694. static const char *
  695. fbsd_file_type (int kf_type, int kf_vnode_type)
  696. {
  697. switch (kf_type)
  698. {
  699. case KINFO_FILE_TYPE_VNODE:
  700. switch (kf_vnode_type)
  701. {
  702. case KINFO_FILE_VTYPE_VREG:
  703. return "file";
  704. case KINFO_FILE_VTYPE_VDIR:
  705. return "dir";
  706. case KINFO_FILE_VTYPE_VCHR:
  707. return "chr";
  708. case KINFO_FILE_VTYPE_VLNK:
  709. return "link";
  710. case KINFO_FILE_VTYPE_VSOCK:
  711. return "socket";
  712. case KINFO_FILE_VTYPE_VFIFO:
  713. return "fifo";
  714. default:
  715. {
  716. char *str = get_print_cell ();
  717. xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
  718. return str;
  719. }
  720. }
  721. case KINFO_FILE_TYPE_SOCKET:
  722. return "socket";
  723. case KINFO_FILE_TYPE_PIPE:
  724. return "pipe";
  725. case KINFO_FILE_TYPE_FIFO:
  726. return "fifo";
  727. case KINFO_FILE_TYPE_KQUEUE:
  728. return "kqueue";
  729. case KINFO_FILE_TYPE_CRYPTO:
  730. return "crypto";
  731. case KINFO_FILE_TYPE_MQUEUE:
  732. return "mqueue";
  733. case KINFO_FILE_TYPE_SHM:
  734. return "shm";
  735. case KINFO_FILE_TYPE_SEM:
  736. return "sem";
  737. case KINFO_FILE_TYPE_PTS:
  738. return "pts";
  739. case KINFO_FILE_TYPE_PROCDESC:
  740. return "proc";
  741. default:
  742. return int_string (kf_type, 10, 1, 0, 0);
  743. }
  744. }
  745. /* Helper function to generate the file flags for a single open file in
  746. 'info proc files'. */
  747. static const char *
  748. fbsd_file_flags (int kf_flags)
  749. {
  750. static char file_flags[10];
  751. file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
  752. file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
  753. file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
  754. file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
  755. file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
  756. file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
  757. file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
  758. file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
  759. file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
  760. file_flags[9] = '\0';
  761. return file_flags;
  762. }
  763. /* Helper function to generate the name of an IP protocol. */
  764. static const char *
  765. fbsd_ipproto (int protocol)
  766. {
  767. switch (protocol)
  768. {
  769. case FBSD_IPPROTO_ICMP:
  770. return "icmp";
  771. case FBSD_IPPROTO_TCP:
  772. return "tcp";
  773. case FBSD_IPPROTO_UDP:
  774. return "udp";
  775. case FBSD_IPPROTO_SCTP:
  776. return "sctp";
  777. default:
  778. {
  779. char *str = get_print_cell ();
  780. xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
  781. return str;
  782. }
  783. }
  784. }
  785. /* Helper function to print out an IPv4 socket address. */
  786. static void
  787. fbsd_print_sockaddr_in (const void *sockaddr)
  788. {
  789. const struct fbsd_sockaddr_in *sin =
  790. reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
  791. char buf[INET_ADDRSTRLEN];
  792. if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
  793. error (_("Failed to format IPv4 address"));
  794. gdb_printf ("%s:%u", buf,
  795. (sin->sin_port[0] << 8) | sin->sin_port[1]);
  796. }
  797. /* Helper function to print out an IPv6 socket address. */
  798. static void
  799. fbsd_print_sockaddr_in6 (const void *sockaddr)
  800. {
  801. const struct fbsd_sockaddr_in6 *sin6 =
  802. reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
  803. char buf[INET6_ADDRSTRLEN];
  804. if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
  805. error (_("Failed to format IPv6 address"));
  806. gdb_printf ("%s.%u", buf,
  807. (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
  808. }
  809. /* See fbsd-tdep.h. */
  810. void
  811. fbsd_info_proc_files_header ()
  812. {
  813. gdb_printf (_("Open files:\n\n"));
  814. gdb_printf (" %6s %6s %10s %9s %s\n",
  815. "FD", "Type", "Offset", "Flags ", "Name");
  816. }
  817. /* See fbsd-tdep.h. */
  818. void
  819. fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
  820. LONGEST kf_offset, int kf_vnode_type,
  821. int kf_sock_domain, int kf_sock_type,
  822. int kf_sock_protocol, const void *kf_sa_local,
  823. const void *kf_sa_peer, const void *kf_path)
  824. {
  825. gdb_printf (" %6s %6s %10s %8s ",
  826. fbsd_file_fd (kf_fd),
  827. fbsd_file_type (kf_type, kf_vnode_type),
  828. kf_offset > -1 ? hex_string (kf_offset) : "-",
  829. fbsd_file_flags (kf_flags));
  830. if (kf_type == KINFO_FILE_TYPE_SOCKET)
  831. {
  832. switch (kf_sock_domain)
  833. {
  834. case FBSD_AF_UNIX:
  835. {
  836. switch (kf_sock_type)
  837. {
  838. case FBSD_SOCK_STREAM:
  839. gdb_printf ("unix stream:");
  840. break;
  841. case FBSD_SOCK_DGRAM:
  842. gdb_printf ("unix dgram:");
  843. break;
  844. case FBSD_SOCK_SEQPACKET:
  845. gdb_printf ("unix seqpacket:");
  846. break;
  847. default:
  848. gdb_printf ("unix <%d>:", kf_sock_type);
  849. break;
  850. }
  851. /* For local sockets, print out the first non-nul path
  852. rather than both paths. */
  853. const struct fbsd_sockaddr_un *saddr_un
  854. = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
  855. if (saddr_un->sun_path[0] == 0)
  856. saddr_un = reinterpret_cast<const struct fbsd_sockaddr_un *>
  857. (kf_sa_peer);
  858. gdb_printf ("%s", saddr_un->sun_path);
  859. break;
  860. }
  861. case FBSD_AF_INET:
  862. gdb_printf ("%s4 ", fbsd_ipproto (kf_sock_protocol));
  863. fbsd_print_sockaddr_in (kf_sa_local);
  864. gdb_printf (" -> ");
  865. fbsd_print_sockaddr_in (kf_sa_peer);
  866. break;
  867. case FBSD_AF_INET6:
  868. gdb_printf ("%s6 ", fbsd_ipproto (kf_sock_protocol));
  869. fbsd_print_sockaddr_in6 (kf_sa_local);
  870. gdb_printf (" -> ");
  871. fbsd_print_sockaddr_in6 (kf_sa_peer);
  872. break;
  873. }
  874. }
  875. else
  876. gdb_printf ("%s", reinterpret_cast<const char *> (kf_path));
  877. gdb_printf ("\n");
  878. }
  879. /* Implement "info proc files" for a corefile. */
  880. static void
  881. fbsd_core_info_proc_files (struct gdbarch *gdbarch)
  882. {
  883. asection *section
  884. = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
  885. if (section == NULL)
  886. {
  887. warning (_("unable to find open files in core file"));
  888. return;
  889. }
  890. size_t note_size = bfd_section_size (section);
  891. if (note_size < 4)
  892. error (_("malformed core note - too short for header"));
  893. gdb::def_vector<unsigned char> contents (note_size);
  894. if (!bfd_get_section_contents (core_bfd, section, contents.data (),
  895. 0, note_size))
  896. error (_("could not get core note contents"));
  897. unsigned char *descdata = contents.data ();
  898. unsigned char *descend = descdata + note_size;
  899. /* Skip over the structure size. */
  900. descdata += 4;
  901. fbsd_info_proc_files_header ();
  902. while (descdata + KF_PATH < descend)
  903. {
  904. ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
  905. if (structsize < KF_PATH)
  906. error (_("malformed core note - file structure too small"));
  907. LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
  908. LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
  909. LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
  910. LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
  911. LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
  912. descdata + KF_VNODE_TYPE);
  913. LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
  914. descdata + KF_SOCK_DOMAIN);
  915. LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
  916. LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
  917. descdata + KF_SOCK_PROTOCOL);
  918. fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
  919. sock_domain, sock_type, sock_protocol,
  920. descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
  921. descdata + KF_PATH);
  922. descdata += structsize;
  923. }
  924. }
  925. /* Helper function to generate mappings flags for a single VM map
  926. entry in 'info proc mappings'. */
  927. static const char *
  928. fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
  929. {
  930. static char vm_flags[9];
  931. vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
  932. vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
  933. vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
  934. vm_flags[3] = ' ';
  935. vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
  936. vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
  937. vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
  938. vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
  939. : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
  940. vm_flags[8] = '\0';
  941. return vm_flags;
  942. }
  943. /* See fbsd-tdep.h. */
  944. void
  945. fbsd_info_proc_mappings_header (int addr_bit)
  946. {
  947. gdb_printf (_("Mapped address spaces:\n\n"));
  948. if (addr_bit == 64)
  949. {
  950. gdb_printf (" %18s %18s %10s %10s %9s %s\n",
  951. "Start Addr",
  952. " End Addr",
  953. " Size", " Offset", "Flags ", "File");
  954. }
  955. else
  956. {
  957. gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
  958. "Start Addr",
  959. " End Addr",
  960. " Size", " Offset", "Flags ", "File");
  961. }
  962. }
  963. /* See fbsd-tdep.h. */
  964. void
  965. fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
  966. ULONGEST kve_end, ULONGEST kve_offset,
  967. int kve_flags, int kve_protection,
  968. const void *kve_path)
  969. {
  970. if (addr_bit == 64)
  971. {
  972. gdb_printf (" %18s %18s %10s %10s %9s %s\n",
  973. hex_string (kve_start),
  974. hex_string (kve_end),
  975. hex_string (kve_end - kve_start),
  976. hex_string (kve_offset),
  977. fbsd_vm_map_entry_flags (kve_flags, kve_protection),
  978. reinterpret_cast<const char *> (kve_path));
  979. }
  980. else
  981. {
  982. gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
  983. hex_string (kve_start),
  984. hex_string (kve_end),
  985. hex_string (kve_end - kve_start),
  986. hex_string (kve_offset),
  987. fbsd_vm_map_entry_flags (kve_flags, kve_protection),
  988. reinterpret_cast<const char *> (kve_path));
  989. }
  990. }
  991. /* Implement "info proc mappings" for a corefile. */
  992. static void
  993. fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
  994. {
  995. asection *section;
  996. unsigned char *descdata, *descend;
  997. size_t note_size;
  998. section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
  999. if (section == NULL)
  1000. {
  1001. warning (_("unable to find mappings in core file"));
  1002. return;
  1003. }
  1004. note_size = bfd_section_size (section);
  1005. if (note_size < 4)
  1006. error (_("malformed core note - too short for header"));
  1007. gdb::def_vector<unsigned char> contents (note_size);
  1008. if (!bfd_get_section_contents (core_bfd, section, contents.data (),
  1009. 0, note_size))
  1010. error (_("could not get core note contents"));
  1011. descdata = contents.data ();
  1012. descend = descdata + note_size;
  1013. /* Skip over the structure size. */
  1014. descdata += 4;
  1015. fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
  1016. while (descdata + KVE_PATH < descend)
  1017. {
  1018. ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
  1019. if (structsize < KVE_PATH)
  1020. error (_("malformed core note - vmmap entry too small"));
  1021. ULONGEST start = bfd_get_64 (core_bfd, descdata + KVE_START);
  1022. ULONGEST end = bfd_get_64 (core_bfd, descdata + KVE_END);
  1023. ULONGEST offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
  1024. LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KVE_FLAGS);
  1025. LONGEST prot = bfd_get_signed_32 (core_bfd, descdata + KVE_PROTECTION);
  1026. fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
  1027. offset, flags, prot, descdata + KVE_PATH);
  1028. descdata += structsize;
  1029. }
  1030. }
  1031. /* Fetch the pathname of a vnode for a single file descriptor from the
  1032. file table core note. */
  1033. static gdb::unique_xmalloc_ptr<char>
  1034. fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
  1035. {
  1036. asection *section;
  1037. unsigned char *descdata, *descend;
  1038. size_t note_size;
  1039. section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
  1040. if (section == NULL)
  1041. return nullptr;
  1042. note_size = bfd_section_size (section);
  1043. if (note_size < 4)
  1044. error (_("malformed core note - too short for header"));
  1045. gdb::def_vector<unsigned char> contents (note_size);
  1046. if (!bfd_get_section_contents (core_bfd, section, contents.data (),
  1047. 0, note_size))
  1048. error (_("could not get core note contents"));
  1049. descdata = contents.data ();
  1050. descend = descdata + note_size;
  1051. /* Skip over the structure size. */
  1052. descdata += 4;
  1053. while (descdata + KF_PATH < descend)
  1054. {
  1055. ULONGEST structsize;
  1056. structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
  1057. if (structsize < KF_PATH)
  1058. error (_("malformed core note - file structure too small"));
  1059. if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
  1060. && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
  1061. {
  1062. char *path = (char *) descdata + KF_PATH;
  1063. return make_unique_xstrdup (path);
  1064. }
  1065. descdata += structsize;
  1066. }
  1067. return nullptr;
  1068. }
  1069. /* Helper function to read a struct timeval. */
  1070. static void
  1071. fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
  1072. LONGEST &sec, ULONGEST &usec)
  1073. {
  1074. if (gdbarch_addr_bit (gdbarch) == 64)
  1075. {
  1076. sec = bfd_get_signed_64 (core_bfd, data);
  1077. usec = bfd_get_64 (core_bfd, data + 8);
  1078. }
  1079. else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
  1080. {
  1081. sec = bfd_get_signed_32 (core_bfd, data);
  1082. usec = bfd_get_32 (core_bfd, data + 4);
  1083. }
  1084. else
  1085. {
  1086. sec = bfd_get_signed_64 (core_bfd, data);
  1087. usec = bfd_get_32 (core_bfd, data + 8);
  1088. }
  1089. }
  1090. /* Print out the contents of a signal set. */
  1091. static void
  1092. fbsd_print_sigset (const char *descr, unsigned char *sigset)
  1093. {
  1094. gdb_printf ("%s: ", descr);
  1095. for (int i = 0; i < SIG_WORDS; i++)
  1096. gdb_printf ("%08x ",
  1097. (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
  1098. gdb_printf ("\n");
  1099. }
  1100. /* Implement "info proc status" for a corefile. */
  1101. static void
  1102. fbsd_core_info_proc_status (struct gdbarch *gdbarch)
  1103. {
  1104. const struct kinfo_proc_layout *kp;
  1105. asection *section;
  1106. unsigned char *descdata;
  1107. int addr_bit, long_bit;
  1108. size_t note_size;
  1109. ULONGEST value;
  1110. LONGEST sec;
  1111. section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
  1112. if (section == NULL)
  1113. {
  1114. warning (_("unable to find process info in core file"));
  1115. return;
  1116. }
  1117. addr_bit = gdbarch_addr_bit (gdbarch);
  1118. if (addr_bit == 64)
  1119. kp = &kinfo_proc_layout_64;
  1120. else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
  1121. kp = &kinfo_proc_layout_i386;
  1122. else
  1123. kp = &kinfo_proc_layout_32;
  1124. long_bit = gdbarch_long_bit (gdbarch);
  1125. /*
  1126. * Ensure that the note is large enough for all of the fields fetched
  1127. * by this function. In particular, the note must contain the 32-bit
  1128. * structure size, then it must be long enough to access the last
  1129. * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
  1130. */
  1131. note_size = bfd_section_size (section);
  1132. if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
  1133. + long_bit / TARGET_CHAR_BIT))
  1134. error (_("malformed core note - too short"));
  1135. gdb::def_vector<unsigned char> contents (note_size);
  1136. if (!bfd_get_section_contents (core_bfd, section, contents.data (),
  1137. 0, note_size))
  1138. error (_("could not get core note contents"));
  1139. descdata = contents.data ();
  1140. /* Skip over the structure size. */
  1141. descdata += 4;
  1142. /* Verify 'ki_layout' is 0. */
  1143. if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
  1144. {
  1145. warning (_("unsupported process information in core file"));
  1146. return;
  1147. }
  1148. gdb_printf ("Name: %.19s\n", descdata + kp->ki_comm);
  1149. gdb_printf ("Process ID: %s\n",
  1150. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
  1151. gdb_printf ("Parent process: %s\n",
  1152. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
  1153. gdb_printf ("Process group: %s\n",
  1154. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
  1155. gdb_printf ("Session id: %s\n",
  1156. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
  1157. /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
  1158. kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
  1159. kernels the 64-bit 'ki_tdev' field is in a reserved section of
  1160. the structure that is cleared to zero. Assume that a zero value
  1161. in ki_tdev indicates a core dump from an older kernel and use the
  1162. value in 'ki_tdev_freebsd11' instead. */
  1163. value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
  1164. if (value == 0)
  1165. value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
  1166. gdb_printf ("TTY: %s\n", pulongest (value));
  1167. gdb_printf ("TTY owner process group: %s\n",
  1168. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
  1169. gdb_printf ("User IDs (real, effective, saved): %s %s %s\n",
  1170. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
  1171. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
  1172. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
  1173. gdb_printf ("Group IDs (real, effective, saved): %s %s %s\n",
  1174. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
  1175. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
  1176. pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
  1177. gdb_printf ("Groups: ");
  1178. uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
  1179. for (int i = 0; i < ngroups; i++)
  1180. gdb_printf ("%s ",
  1181. pulongest (bfd_get_32 (core_bfd,
  1182. descdata + kp->ki_groups + i * 4)));
  1183. gdb_printf ("\n");
  1184. value = bfd_get (long_bit, core_bfd,
  1185. descdata + kp->ki_rusage + kp->ru_minflt);
  1186. gdb_printf ("Minor faults (no memory page): %s\n", pulongest (value));
  1187. value = bfd_get (long_bit, core_bfd,
  1188. descdata + kp->ki_rusage_ch + kp->ru_minflt);
  1189. gdb_printf ("Minor faults, children: %s\n", pulongest (value));
  1190. value = bfd_get (long_bit, core_bfd,
  1191. descdata + kp->ki_rusage + kp->ru_majflt);
  1192. gdb_printf ("Major faults (memory page faults): %s\n",
  1193. pulongest (value));
  1194. value = bfd_get (long_bit, core_bfd,
  1195. descdata + kp->ki_rusage_ch + kp->ru_majflt);
  1196. gdb_printf ("Major faults, children: %s\n", pulongest (value));
  1197. fbsd_core_fetch_timeval (gdbarch,
  1198. descdata + kp->ki_rusage + kp->ru_utime,
  1199. sec, value);
  1200. gdb_printf ("utime: %s.%06d\n", plongest (sec), (int) value);
  1201. fbsd_core_fetch_timeval (gdbarch,
  1202. descdata + kp->ki_rusage + kp->ru_stime,
  1203. sec, value);
  1204. gdb_printf ("stime: %s.%06d\n", plongest (sec), (int) value);
  1205. fbsd_core_fetch_timeval (gdbarch,
  1206. descdata + kp->ki_rusage_ch + kp->ru_utime,
  1207. sec, value);
  1208. gdb_printf ("utime, children: %s.%06d\n", plongest (sec), (int) value);
  1209. fbsd_core_fetch_timeval (gdbarch,
  1210. descdata + kp->ki_rusage_ch + kp->ru_stime,
  1211. sec, value);
  1212. gdb_printf ("stime, children: %s.%06d\n", plongest (sec), (int) value);
  1213. gdb_printf ("'nice' value: %d\n",
  1214. (int) bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
  1215. fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
  1216. gdb_printf ("Start time: %s.%06d\n", plongest (sec), (int) value);
  1217. gdb_printf ("Virtual memory size: %s kB\n",
  1218. pulongest (bfd_get (addr_bit, core_bfd,
  1219. descdata + kp->ki_size) / 1024));
  1220. gdb_printf ("Data size: %s pages\n",
  1221. pulongest (bfd_get (addr_bit, core_bfd,
  1222. descdata + kp->ki_dsize)));
  1223. gdb_printf ("Stack size: %s pages\n",
  1224. pulongest (bfd_get (addr_bit, core_bfd,
  1225. descdata + kp->ki_ssize)));
  1226. gdb_printf ("Text size: %s pages\n",
  1227. pulongest (bfd_get (addr_bit, core_bfd,
  1228. descdata + kp->ki_tsize)));
  1229. gdb_printf ("Resident set size: %s pages\n",
  1230. pulongest (bfd_get (addr_bit, core_bfd,
  1231. descdata + kp->ki_rssize)));
  1232. gdb_printf ("Maximum RSS: %s pages\n",
  1233. pulongest (bfd_get (long_bit, core_bfd,
  1234. descdata + kp->ki_rusage
  1235. + kp->ru_maxrss)));
  1236. fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
  1237. fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
  1238. }
  1239. /* Implement the "core_info_proc" gdbarch method. */
  1240. static void
  1241. fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
  1242. enum info_proc_what what)
  1243. {
  1244. bool do_cmdline = false;
  1245. bool do_cwd = false;
  1246. bool do_exe = false;
  1247. bool do_files = false;
  1248. bool do_mappings = false;
  1249. bool do_status = false;
  1250. int pid;
  1251. switch (what)
  1252. {
  1253. case IP_MINIMAL:
  1254. do_cmdline = true;
  1255. do_cwd = true;
  1256. do_exe = true;
  1257. break;
  1258. case IP_MAPPINGS:
  1259. do_mappings = true;
  1260. break;
  1261. case IP_STATUS:
  1262. case IP_STAT:
  1263. do_status = true;
  1264. break;
  1265. case IP_CMDLINE:
  1266. do_cmdline = true;
  1267. break;
  1268. case IP_EXE:
  1269. do_exe = true;
  1270. break;
  1271. case IP_CWD:
  1272. do_cwd = true;
  1273. break;
  1274. case IP_FILES:
  1275. do_files = true;
  1276. break;
  1277. case IP_ALL:
  1278. do_cmdline = true;
  1279. do_cwd = true;
  1280. do_exe = true;
  1281. do_files = true;
  1282. do_mappings = true;
  1283. do_status = true;
  1284. break;
  1285. default:
  1286. return;
  1287. }
  1288. pid = bfd_core_file_pid (core_bfd);
  1289. if (pid != 0)
  1290. gdb_printf (_("process %d\n"), pid);
  1291. if (do_cmdline)
  1292. {
  1293. const char *cmdline;
  1294. cmdline = bfd_core_file_failing_command (core_bfd);
  1295. if (cmdline)
  1296. gdb_printf ("cmdline = '%s'\n", cmdline);
  1297. else
  1298. warning (_("Command line unavailable"));
  1299. }
  1300. if (do_cwd)
  1301. {
  1302. gdb::unique_xmalloc_ptr<char> cwd =
  1303. fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
  1304. if (cwd)
  1305. gdb_printf ("cwd = '%s'\n", cwd.get ());
  1306. else
  1307. warning (_("unable to read current working directory"));
  1308. }
  1309. if (do_exe)
  1310. {
  1311. gdb::unique_xmalloc_ptr<char> exe =
  1312. fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
  1313. if (exe)
  1314. gdb_printf ("exe = '%s'\n", exe.get ());
  1315. else
  1316. warning (_("unable to read executable path name"));
  1317. }
  1318. if (do_files)
  1319. fbsd_core_info_proc_files (gdbarch);
  1320. if (do_mappings)
  1321. fbsd_core_info_proc_mappings (gdbarch);
  1322. if (do_status)
  1323. fbsd_core_info_proc_status (gdbarch);
  1324. }
  1325. /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
  1326. static void
  1327. fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
  1328. CORE_ADDR type, CORE_ADDR val)
  1329. {
  1330. const char *name = "???";
  1331. const char *description = "";
  1332. enum auxv_format format = AUXV_FORMAT_HEX;
  1333. switch (type)
  1334. {
  1335. case AT_NULL:
  1336. case AT_IGNORE:
  1337. case AT_EXECFD:
  1338. case AT_PHDR:
  1339. case AT_PHENT:
  1340. case AT_PHNUM:
  1341. case AT_PAGESZ:
  1342. case AT_BASE:
  1343. case AT_FLAGS:
  1344. case AT_ENTRY:
  1345. case AT_NOTELF:
  1346. case AT_UID:
  1347. case AT_EUID:
  1348. case AT_GID:
  1349. case AT_EGID:
  1350. default_print_auxv_entry (gdbarch, file, type, val);
  1351. return;
  1352. #define _TAGNAME(tag) #tag
  1353. #define TAGNAME(tag) _TAGNAME(AT_##tag)
  1354. #define TAG(tag, text, kind) \
  1355. case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
  1356. TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
  1357. TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
  1358. TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
  1359. TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
  1360. TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
  1361. TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
  1362. TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
  1363. TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
  1364. TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
  1365. TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
  1366. TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
  1367. TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
  1368. TAG (BSDFLAGS, _("ELF BSD flags"), AUXV_FORMAT_HEX);
  1369. TAG (ARGC, _("Argument count"), AUXV_FORMAT_DEC);
  1370. TAG (ARGV, _("Argument vector"), AUXV_FORMAT_HEX);
  1371. TAG (ENVC, _("Environment count"), AUXV_FORMAT_DEC);
  1372. TAG (ENVV, _("Environment vector"), AUXV_FORMAT_HEX);
  1373. TAG (PS_STRINGS, _("Pointer to ps_strings"), AUXV_FORMAT_HEX);
  1374. TAG (FXRNG, _("Pointer to root RNG seed version"), AUXV_FORMAT_HEX);
  1375. TAG (KPRELOAD, _("Base address of vDSO"), AUXV_FORMAT_HEX);
  1376. }
  1377. fprint_auxv_entry (file, name, description, format, type, val);
  1378. }
  1379. /* Implement the "get_siginfo_type" gdbarch method. */
  1380. static struct type *
  1381. fbsd_get_siginfo_type (struct gdbarch *gdbarch)
  1382. {
  1383. struct fbsd_gdbarch_data *fbsd_gdbarch_data;
  1384. struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
  1385. struct type *uid_type, *pid_type;
  1386. struct type *sigval_type, *reason_type;
  1387. struct type *siginfo_type;
  1388. struct type *type;
  1389. fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
  1390. if (fbsd_gdbarch_data->siginfo_type != NULL)
  1391. return fbsd_gdbarch_data->siginfo_type;
  1392. int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  1393. 0, "int");
  1394. int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
  1395. uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
  1396. long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  1397. 0, "long");
  1398. void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
  1399. /* union sigval */
  1400. sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
  1401. sigval_type->set_name (xstrdup ("sigval"));
  1402. append_composite_type_field (sigval_type, "sival_int", int_type);
  1403. append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
  1404. /* __pid_t */
  1405. pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  1406. TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
  1407. TYPE_TARGET_TYPE (pid_type) = int32_type;
  1408. pid_type->set_target_is_stub (true);
  1409. /* __uid_t */
  1410. uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  1411. TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
  1412. "__uid_t");
  1413. TYPE_TARGET_TYPE (uid_type) = uint32_type;
  1414. pid_type->set_target_is_stub (true);
  1415. /* _reason */
  1416. reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
  1417. /* _fault */
  1418. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  1419. append_composite_type_field (type, "si_trapno", int_type);
  1420. append_composite_type_field (reason_type, "_fault", type);
  1421. /* _timer */
  1422. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  1423. append_composite_type_field (type, "si_timerid", int_type);
  1424. append_composite_type_field (type, "si_overrun", int_type);
  1425. append_composite_type_field (reason_type, "_timer", type);
  1426. /* _mesgq */
  1427. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  1428. append_composite_type_field (type, "si_mqd", int_type);
  1429. append_composite_type_field (reason_type, "_mesgq", type);
  1430. /* _poll */
  1431. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  1432. append_composite_type_field (type, "si_band", long_type);
  1433. append_composite_type_field (reason_type, "_poll", type);
  1434. /* __spare__ */
  1435. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  1436. append_composite_type_field (type, "__spare1__", long_type);
  1437. append_composite_type_field (type, "__spare2__",
  1438. init_vector_type (int_type, 7));
  1439. append_composite_type_field (reason_type, "__spare__", type);
  1440. /* struct siginfo */
  1441. siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  1442. siginfo_type->set_name (xstrdup ("siginfo"));
  1443. append_composite_type_field (siginfo_type, "si_signo", int_type);
  1444. append_composite_type_field (siginfo_type, "si_errno", int_type);
  1445. append_composite_type_field (siginfo_type, "si_code", int_type);
  1446. append_composite_type_field (siginfo_type, "si_pid", pid_type);
  1447. append_composite_type_field (siginfo_type, "si_uid", uid_type);
  1448. append_composite_type_field (siginfo_type, "si_status", int_type);
  1449. append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
  1450. append_composite_type_field (siginfo_type, "si_value", sigval_type);
  1451. append_composite_type_field (siginfo_type, "_reason", reason_type);
  1452. fbsd_gdbarch_data->siginfo_type = siginfo_type;
  1453. return siginfo_type;
  1454. }
  1455. /* Implement the "gdb_signal_from_target" gdbarch method. */
  1456. static enum gdb_signal
  1457. fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
  1458. {
  1459. switch (signal)
  1460. {
  1461. case 0:
  1462. return GDB_SIGNAL_0;
  1463. case FREEBSD_SIGHUP:
  1464. return GDB_SIGNAL_HUP;
  1465. case FREEBSD_SIGINT:
  1466. return GDB_SIGNAL_INT;
  1467. case FREEBSD_SIGQUIT:
  1468. return GDB_SIGNAL_QUIT;
  1469. case FREEBSD_SIGILL:
  1470. return GDB_SIGNAL_ILL;
  1471. case FREEBSD_SIGTRAP:
  1472. return GDB_SIGNAL_TRAP;
  1473. case FREEBSD_SIGABRT:
  1474. return GDB_SIGNAL_ABRT;
  1475. case FREEBSD_SIGEMT:
  1476. return GDB_SIGNAL_EMT;
  1477. case FREEBSD_SIGFPE:
  1478. return GDB_SIGNAL_FPE;
  1479. case FREEBSD_SIGKILL:
  1480. return GDB_SIGNAL_KILL;
  1481. case FREEBSD_SIGBUS:
  1482. return GDB_SIGNAL_BUS;
  1483. case FREEBSD_SIGSEGV:
  1484. return GDB_SIGNAL_SEGV;
  1485. case FREEBSD_SIGSYS:
  1486. return GDB_SIGNAL_SYS;
  1487. case FREEBSD_SIGPIPE:
  1488. return GDB_SIGNAL_PIPE;
  1489. case FREEBSD_SIGALRM:
  1490. return GDB_SIGNAL_ALRM;
  1491. case FREEBSD_SIGTERM:
  1492. return GDB_SIGNAL_TERM;
  1493. case FREEBSD_SIGURG:
  1494. return GDB_SIGNAL_URG;
  1495. case FREEBSD_SIGSTOP:
  1496. return GDB_SIGNAL_STOP;
  1497. case FREEBSD_SIGTSTP:
  1498. return GDB_SIGNAL_TSTP;
  1499. case FREEBSD_SIGCONT:
  1500. return GDB_SIGNAL_CONT;
  1501. case FREEBSD_SIGCHLD:
  1502. return GDB_SIGNAL_CHLD;
  1503. case FREEBSD_SIGTTIN:
  1504. return GDB_SIGNAL_TTIN;
  1505. case FREEBSD_SIGTTOU:
  1506. return GDB_SIGNAL_TTOU;
  1507. case FREEBSD_SIGIO:
  1508. return GDB_SIGNAL_IO;
  1509. case FREEBSD_SIGXCPU:
  1510. return GDB_SIGNAL_XCPU;
  1511. case FREEBSD_SIGXFSZ:
  1512. return GDB_SIGNAL_XFSZ;
  1513. case FREEBSD_SIGVTALRM:
  1514. return GDB_SIGNAL_VTALRM;
  1515. case FREEBSD_SIGPROF:
  1516. return GDB_SIGNAL_PROF;
  1517. case FREEBSD_SIGWINCH:
  1518. return GDB_SIGNAL_WINCH;
  1519. case FREEBSD_SIGINFO:
  1520. return GDB_SIGNAL_INFO;
  1521. case FREEBSD_SIGUSR1:
  1522. return GDB_SIGNAL_USR1;
  1523. case FREEBSD_SIGUSR2:
  1524. return GDB_SIGNAL_USR2;
  1525. /* SIGTHR is the same as SIGLWP on FreeBSD. */
  1526. case FREEBSD_SIGTHR:
  1527. return GDB_SIGNAL_LWP;
  1528. case FREEBSD_SIGLIBRT:
  1529. return GDB_SIGNAL_LIBRT;
  1530. }
  1531. if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
  1532. {
  1533. int offset = signal - FREEBSD_SIGRTMIN;
  1534. return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
  1535. }
  1536. return GDB_SIGNAL_UNKNOWN;
  1537. }
  1538. /* Implement the "gdb_signal_to_target" gdbarch method. */
  1539. static int
  1540. fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
  1541. enum gdb_signal signal)
  1542. {
  1543. switch (signal)
  1544. {
  1545. case GDB_SIGNAL_0:
  1546. return 0;
  1547. case GDB_SIGNAL_HUP:
  1548. return FREEBSD_SIGHUP;
  1549. case GDB_SIGNAL_INT:
  1550. return FREEBSD_SIGINT;
  1551. case GDB_SIGNAL_QUIT:
  1552. return FREEBSD_SIGQUIT;
  1553. case GDB_SIGNAL_ILL:
  1554. return FREEBSD_SIGILL;
  1555. case GDB_SIGNAL_TRAP:
  1556. return FREEBSD_SIGTRAP;
  1557. case GDB_SIGNAL_ABRT:
  1558. return FREEBSD_SIGABRT;
  1559. case GDB_SIGNAL_EMT:
  1560. return FREEBSD_SIGEMT;
  1561. case GDB_SIGNAL_FPE:
  1562. return FREEBSD_SIGFPE;
  1563. case GDB_SIGNAL_KILL:
  1564. return FREEBSD_SIGKILL;
  1565. case GDB_SIGNAL_BUS:
  1566. return FREEBSD_SIGBUS;
  1567. case GDB_SIGNAL_SEGV:
  1568. return FREEBSD_SIGSEGV;
  1569. case GDB_SIGNAL_SYS:
  1570. return FREEBSD_SIGSYS;
  1571. case GDB_SIGNAL_PIPE:
  1572. return FREEBSD_SIGPIPE;
  1573. case GDB_SIGNAL_ALRM:
  1574. return FREEBSD_SIGALRM;
  1575. case GDB_SIGNAL_TERM:
  1576. return FREEBSD_SIGTERM;
  1577. case GDB_SIGNAL_URG:
  1578. return FREEBSD_SIGURG;
  1579. case GDB_SIGNAL_STOP:
  1580. return FREEBSD_SIGSTOP;
  1581. case GDB_SIGNAL_TSTP:
  1582. return FREEBSD_SIGTSTP;
  1583. case GDB_SIGNAL_CONT:
  1584. return FREEBSD_SIGCONT;
  1585. case GDB_SIGNAL_CHLD:
  1586. return FREEBSD_SIGCHLD;
  1587. case GDB_SIGNAL_TTIN:
  1588. return FREEBSD_SIGTTIN;
  1589. case GDB_SIGNAL_TTOU:
  1590. return FREEBSD_SIGTTOU;
  1591. case GDB_SIGNAL_IO:
  1592. return FREEBSD_SIGIO;
  1593. case GDB_SIGNAL_XCPU:
  1594. return FREEBSD_SIGXCPU;
  1595. case GDB_SIGNAL_XFSZ:
  1596. return FREEBSD_SIGXFSZ;
  1597. case GDB_SIGNAL_VTALRM:
  1598. return FREEBSD_SIGVTALRM;
  1599. case GDB_SIGNAL_PROF:
  1600. return FREEBSD_SIGPROF;
  1601. case GDB_SIGNAL_WINCH:
  1602. return FREEBSD_SIGWINCH;
  1603. case GDB_SIGNAL_INFO:
  1604. return FREEBSD_SIGINFO;
  1605. case GDB_SIGNAL_USR1:
  1606. return FREEBSD_SIGUSR1;
  1607. case GDB_SIGNAL_USR2:
  1608. return FREEBSD_SIGUSR2;
  1609. case GDB_SIGNAL_LWP:
  1610. return FREEBSD_SIGTHR;
  1611. case GDB_SIGNAL_LIBRT:
  1612. return FREEBSD_SIGLIBRT;
  1613. }
  1614. if (signal >= GDB_SIGNAL_REALTIME_65
  1615. && signal <= GDB_SIGNAL_REALTIME_126)
  1616. {
  1617. int offset = signal - GDB_SIGNAL_REALTIME_65;
  1618. return FREEBSD_SIGRTMIN + offset;
  1619. }
  1620. return -1;
  1621. }
  1622. /* Implement the "get_syscall_number" gdbarch method. */
  1623. static LONGEST
  1624. fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
  1625. {
  1626. /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
  1627. native targets fetch the system call number from the
  1628. 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
  1629. However, system call catching requires this function to be
  1630. set. */
  1631. internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
  1632. }
  1633. /* Read an integer symbol value from the current target. */
  1634. static LONGEST
  1635. fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
  1636. {
  1637. bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
  1638. if (ms.minsym == NULL)
  1639. error (_("Unable to resolve symbol '%s'"), name);
  1640. gdb_byte buf[4];
  1641. if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms), buf, sizeof buf) != 0)
  1642. error (_("Unable to read value of '%s'"), name);
  1643. return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
  1644. }
  1645. /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
  1646. structure needed to determine the TLS index of an object file. */
  1647. static void
  1648. fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
  1649. {
  1650. try
  1651. {
  1652. /* Fetch offsets from debug symbols in rtld. */
  1653. struct symbol *obj_entry_sym
  1654. = lookup_symbol_in_language ("Struct_Obj_Entry", NULL, STRUCT_DOMAIN,
  1655. language_c, NULL).symbol;
  1656. if (obj_entry_sym == NULL)
  1657. error (_("Unable to find Struct_Obj_Entry symbol"));
  1658. data->off_linkmap = lookup_struct_elt (obj_entry_sym->type (),
  1659. "linkmap", 0).offset / 8;
  1660. data->off_tlsindex = lookup_struct_elt (obj_entry_sym->type (),
  1661. "tlsindex", 0).offset / 8;
  1662. data->rtld_offsets_valid = true;
  1663. return;
  1664. }
  1665. catch (const gdb_exception_error &e)
  1666. {
  1667. data->off_linkmap = -1;
  1668. }
  1669. try
  1670. {
  1671. /* Fetch offsets from global variables in libthr. Note that
  1672. this does not work for single-threaded processes that are not
  1673. linked against libthr. */
  1674. data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
  1675. "_thread_off_linkmap");
  1676. data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
  1677. "_thread_off_tlsindex");
  1678. data->rtld_offsets_valid = true;
  1679. return;
  1680. }
  1681. catch (const gdb_exception_error &e)
  1682. {
  1683. data->off_linkmap = -1;
  1684. }
  1685. }
  1686. /* Helper function to read the TLS index of an object file associated
  1687. with a link map entry at LM_ADDR. */
  1688. static LONGEST
  1689. fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
  1690. {
  1691. struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
  1692. if (!data->rtld_offsets_valid)
  1693. fbsd_fetch_rtld_offsets (gdbarch, data);
  1694. if (data->off_linkmap == -1)
  1695. throw_error (TLS_GENERIC_ERROR,
  1696. _("Cannot fetch runtime linker structure offsets"));
  1697. /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
  1698. pointer and then compute the offset of the tlsindex member. */
  1699. CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
  1700. gdb_byte buf[4];
  1701. if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
  1702. throw_error (TLS_GENERIC_ERROR,
  1703. _("Cannot find thread-local variables on this target"));
  1704. return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
  1705. }
  1706. /* See fbsd-tdep.h. */
  1707. CORE_ADDR
  1708. fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
  1709. CORE_ADDR lm_addr, CORE_ADDR offset)
  1710. {
  1711. LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
  1712. gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
  1713. if (target_read_memory (dtv_addr, buf, sizeof buf) != 0)
  1714. throw_error (TLS_GENERIC_ERROR,
  1715. _("Cannot find thread-local variables on this target"));
  1716. const struct builtin_type *builtin = builtin_type (gdbarch);
  1717. CORE_ADDR addr = gdbarch_pointer_to_address (gdbarch,
  1718. builtin->builtin_data_ptr, buf);
  1719. addr += (tls_index + 1) * TYPE_LENGTH (builtin->builtin_data_ptr);
  1720. if (target_read_memory (addr, buf, sizeof buf) != 0)
  1721. throw_error (TLS_GENERIC_ERROR,
  1722. _("Cannot find thread-local variables on this target"));
  1723. addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr, buf);
  1724. return addr + offset;
  1725. }
  1726. /* See fbsd-tdep.h. */
  1727. CORE_ADDR
  1728. fbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
  1729. {
  1730. struct bound_minimal_symbol msym = lookup_bound_minimal_symbol ("_rtld_bind");
  1731. if (msym.minsym != nullptr && BMSYMBOL_VALUE_ADDRESS (msym) == pc)
  1732. return frame_unwind_caller_pc (get_current_frame ());
  1733. return 0;
  1734. }
  1735. /* Return description of signal code or nullptr. */
  1736. static const char *
  1737. fbsd_signal_cause (enum gdb_signal siggnal, int code)
  1738. {
  1739. /* Signal-independent causes. */
  1740. switch (code)
  1741. {
  1742. case FBSD_SI_USER:
  1743. return _("Sent by kill()");
  1744. case FBSD_SI_QUEUE:
  1745. return _("Sent by sigqueue()");
  1746. case FBSD_SI_TIMER:
  1747. return _("Timer expired");
  1748. case FBSD_SI_ASYNCIO:
  1749. return _("Asynchronous I/O request completed");
  1750. case FBSD_SI_MESGQ:
  1751. return _("Message arrived on empty message queue");
  1752. case FBSD_SI_KERNEL:
  1753. return _("Sent by kernel");
  1754. case FBSD_SI_LWP:
  1755. return _("Sent by thr_kill()");
  1756. }
  1757. switch (siggnal)
  1758. {
  1759. case GDB_SIGNAL_ILL:
  1760. switch (code)
  1761. {
  1762. case FBSD_ILL_ILLOPC:
  1763. return _("Illegal opcode");
  1764. case FBSD_ILL_ILLOPN:
  1765. return _("Illegal operand");
  1766. case FBSD_ILL_ILLADR:
  1767. return _("Illegal addressing mode");
  1768. case FBSD_ILL_ILLTRP:
  1769. return _("Illegal trap");
  1770. case FBSD_ILL_PRVOPC:
  1771. return _("Privileged opcode");
  1772. case FBSD_ILL_PRVREG:
  1773. return _("Privileged register");
  1774. case FBSD_ILL_COPROC:
  1775. return _("Coprocessor error");
  1776. case FBSD_ILL_BADSTK:
  1777. return _("Internal stack error");
  1778. }
  1779. break;
  1780. case GDB_SIGNAL_BUS:
  1781. switch (code)
  1782. {
  1783. case FBSD_BUS_ADRALN:
  1784. return _("Invalid address alignment");
  1785. case FBSD_BUS_ADRERR:
  1786. return _("Address not present");
  1787. case FBSD_BUS_OBJERR:
  1788. return _("Object-specific hardware error");
  1789. case FBSD_BUS_OOMERR:
  1790. return _("Out of memory");
  1791. }
  1792. break;
  1793. case GDB_SIGNAL_SEGV:
  1794. switch (code)
  1795. {
  1796. case FBSD_SEGV_MAPERR:
  1797. return _("Address not mapped to object");
  1798. case FBSD_SEGV_ACCERR:
  1799. return _("Invalid permissions for mapped object");
  1800. case FBSD_SEGV_PKUERR:
  1801. return _("PKU violation");
  1802. }
  1803. break;
  1804. case GDB_SIGNAL_FPE:
  1805. switch (code)
  1806. {
  1807. case FBSD_FPE_INTOVF:
  1808. return _("Integer overflow");
  1809. case FBSD_FPE_INTDIV:
  1810. return _("Integer divide by zero");
  1811. case FBSD_FPE_FLTDIV:
  1812. return _("Floating point divide by zero");
  1813. case FBSD_FPE_FLTOVF:
  1814. return _("Floating point overflow");
  1815. case FBSD_FPE_FLTUND:
  1816. return _("Floating point underflow");
  1817. case FBSD_FPE_FLTRES:
  1818. return _("Floating point inexact result");
  1819. case FBSD_FPE_FLTINV:
  1820. return _("Invalid floating point operation");
  1821. case FBSD_FPE_FLTSUB:
  1822. return _("Subscript out of range");
  1823. }
  1824. break;
  1825. case GDB_SIGNAL_TRAP:
  1826. switch (code)
  1827. {
  1828. case FBSD_TRAP_BRKPT:
  1829. return _("Breakpoint");
  1830. case FBSD_TRAP_TRACE:
  1831. return _("Trace trap");
  1832. case FBSD_TRAP_DTRACE:
  1833. return _("DTrace-induced trap");
  1834. case FBSD_TRAP_CAP:
  1835. return _("Capability violation");
  1836. }
  1837. break;
  1838. case GDB_SIGNAL_CHLD:
  1839. switch (code)
  1840. {
  1841. case FBSD_CLD_EXITED:
  1842. return _("Child has exited");
  1843. case FBSD_CLD_KILLED:
  1844. return _("Child has terminated abnormally");
  1845. case FBSD_CLD_DUMPED:
  1846. return _("Child has dumped core");
  1847. case FBSD_CLD_TRAPPED:
  1848. return _("Traced child has trapped");
  1849. case FBSD_CLD_STOPPED:
  1850. return _("Child has stopped");
  1851. case FBSD_CLD_CONTINUED:
  1852. return _("Stopped child has continued");
  1853. }
  1854. break;
  1855. case GDB_SIGNAL_POLL:
  1856. switch (code)
  1857. {
  1858. case FBSD_POLL_IN:
  1859. return _("Data input available");
  1860. case FBSD_POLL_OUT:
  1861. return _("Output buffers available");
  1862. case FBSD_POLL_MSG:
  1863. return _("Input message available");
  1864. case FBSD_POLL_ERR:
  1865. return _("I/O error");
  1866. case FBSD_POLL_PRI:
  1867. return _("High priority input available");
  1868. case FBSD_POLL_HUP:
  1869. return _("Device disconnected");
  1870. }
  1871. break;
  1872. }
  1873. return nullptr;
  1874. }
  1875. /* Report additional details for a signal stop. */
  1876. static void
  1877. fbsd_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout,
  1878. enum gdb_signal siggnal)
  1879. {
  1880. LONGEST code, mqd, pid, status, timerid, uid;
  1881. try
  1882. {
  1883. code = parse_and_eval_long ("$_siginfo.si_code");
  1884. pid = parse_and_eval_long ("$_siginfo.si_pid");
  1885. uid = parse_and_eval_long ("$_siginfo.si_uid");
  1886. status = parse_and_eval_long ("$_siginfo.si_status");
  1887. timerid = parse_and_eval_long ("$_siginfo._reason._timer.si_timerid");
  1888. mqd = parse_and_eval_long ("$_siginfo._reason._mesgq.si_mqd");
  1889. }
  1890. catch (const gdb_exception_error &e)
  1891. {
  1892. return;
  1893. }
  1894. const char *meaning = fbsd_signal_cause (siggnal, code);
  1895. if (meaning == nullptr)
  1896. return;
  1897. uiout->text (".\n");
  1898. uiout->field_string ("sigcode-meaning", meaning);
  1899. switch (code)
  1900. {
  1901. case FBSD_SI_USER:
  1902. case FBSD_SI_QUEUE:
  1903. case FBSD_SI_LWP:
  1904. uiout->text (" from pid ");
  1905. uiout->field_string ("sending-pid", plongest (pid));
  1906. uiout->text (" and user ");
  1907. uiout->field_string ("sending-uid", plongest (uid));
  1908. return;
  1909. case FBSD_SI_TIMER:
  1910. uiout->text (": timerid ");
  1911. uiout->field_string ("timerid", plongest (timerid));
  1912. return;
  1913. case FBSD_SI_MESGQ:
  1914. uiout->text (": message queue ");
  1915. uiout->field_string ("message-queue", plongest (mqd));
  1916. return;
  1917. case FBSD_SI_ASYNCIO:
  1918. return;
  1919. }
  1920. if (siggnal == GDB_SIGNAL_CHLD)
  1921. {
  1922. uiout->text (": pid ");
  1923. uiout->field_string ("child-pid", plongest (pid));
  1924. uiout->text (", uid ");
  1925. uiout->field_string ("child-uid", plongest (uid));
  1926. if (code == FBSD_CLD_EXITED)
  1927. {
  1928. uiout->text (", exit status ");
  1929. uiout->field_string ("exit-status", plongest (status));
  1930. }
  1931. else
  1932. {
  1933. uiout->text (", signal ");
  1934. uiout->field_string ("signal", plongest (status));
  1935. }
  1936. }
  1937. }
  1938. /* Search a list of struct kinfo_vmmap entries in the ENTRIES buffer
  1939. of LEN bytes to find the length of the entry starting at ADDR.
  1940. Returns the length of the entry or zero if no entry was found. */
  1941. static ULONGEST
  1942. fbsd_vmmap_length (struct gdbarch *gdbarch, unsigned char *entries, size_t len,
  1943. CORE_ADDR addr)
  1944. {
  1945. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1946. unsigned char *descdata = entries;
  1947. unsigned char *descend = descdata + len;
  1948. /* Skip over the structure size. */
  1949. descdata += 4;
  1950. while (descdata + KVE_PATH < descend)
  1951. {
  1952. ULONGEST structsize = extract_unsigned_integer (descdata
  1953. + KVE_STRUCTSIZE, 4,
  1954. byte_order);
  1955. if (structsize < KVE_PATH)
  1956. return false;
  1957. ULONGEST start = extract_unsigned_integer (descdata + KVE_START, 8,
  1958. byte_order);
  1959. ULONGEST end = extract_unsigned_integer (descdata + KVE_END, 8,
  1960. byte_order);
  1961. if (start == addr)
  1962. return end - start;
  1963. descdata += structsize;
  1964. }
  1965. return 0;
  1966. }
  1967. /* Helper for fbsd_vsyscall_range that does the real work of finding
  1968. the vDSO's address range. */
  1969. static bool
  1970. fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
  1971. {
  1972. struct target_ops *ops = current_inferior ()->top_target ();
  1973. if (target_auxv_search (ops, AT_FREEBSD_KPRELOAD, &range->start) <= 0)
  1974. return false;
  1975. if (!target_has_execution ())
  1976. {
  1977. /* Search for the ending address in the NT_PROCSTAT_VMMAP note. */
  1978. asection *section = bfd_get_section_by_name (core_bfd,
  1979. ".note.freebsdcore.vmmap");
  1980. if (section == nullptr)
  1981. return false;
  1982. size_t note_size = bfd_section_size (section);
  1983. if (note_size < 4)
  1984. return false;
  1985. gdb::def_vector<unsigned char> contents (note_size);
  1986. if (!bfd_get_section_contents (core_bfd, section, contents.data (),
  1987. 0, note_size))
  1988. return false;
  1989. range->length = fbsd_vmmap_length (gdbarch, contents.data (), note_size,
  1990. range->start);
  1991. }
  1992. else
  1993. {
  1994. /* Fetch the list of address space entries from the running target. */
  1995. gdb::optional<gdb::byte_vector> buf =
  1996. target_read_alloc (ops, TARGET_OBJECT_FREEBSD_VMMAP, nullptr);
  1997. if (!buf || buf->empty ())
  1998. return false;
  1999. range->length = fbsd_vmmap_length (gdbarch, buf->data (), buf->size (),
  2000. range->start);
  2001. }
  2002. return range->length != 0;
  2003. }
  2004. /* Return the address range of the vDSO for the current inferior. */
  2005. static int
  2006. fbsd_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
  2007. {
  2008. struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
  2009. if (data->vdso_range_p == 0)
  2010. {
  2011. if (fbsd_vdso_range (gdbarch, &data->vdso_range))
  2012. data->vdso_range_p = 1;
  2013. else
  2014. data->vdso_range_p = -1;
  2015. }
  2016. if (data->vdso_range_p < 0)
  2017. return 0;
  2018. *range = data->vdso_range;
  2019. return 1;
  2020. }
  2021. /* To be called from GDB_OSABI_FREEBSD handlers. */
  2022. void
  2023. fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  2024. {
  2025. set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
  2026. set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
  2027. set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
  2028. set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
  2029. set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
  2030. set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
  2031. set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
  2032. set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
  2033. set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
  2034. set_gdbarch_report_signal_info (gdbarch, fbsd_report_signal_info);
  2035. set_gdbarch_skip_solib_resolver (gdbarch, fbsd_skip_solib_resolver);
  2036. set_gdbarch_vsyscall_range (gdbarch, fbsd_vsyscall_range);
  2037. /* `catch syscall' */
  2038. set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
  2039. set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
  2040. }
  2041. void _initialize_fbsd_tdep ();
  2042. void
  2043. _initialize_fbsd_tdep ()
  2044. {
  2045. fbsd_gdbarch_data_handle =
  2046. gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
  2047. }