fbsd-nat.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /* Native-dependent code for FreeBSD.
  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 "gdbsupport/block-signals.h"
  16. #include "gdbsupport/byte-vector.h"
  17. #include "gdbsupport/event-loop.h"
  18. #include "gdbcore.h"
  19. #include "inferior.h"
  20. #include "regcache.h"
  21. #include "regset.h"
  22. #include "gdbarch.h"
  23. #include "gdbcmd.h"
  24. #include "gdbthread.h"
  25. #include "gdbsupport/buildargv.h"
  26. #include "gdbsupport/gdb_wait.h"
  27. #include "inf-loop.h"
  28. #include "inf-ptrace.h"
  29. #include <sys/types.h>
  30. #ifdef HAVE_SYS_PROCCTL_H
  31. #include <sys/procctl.h>
  32. #endif
  33. #include <sys/procfs.h>
  34. #include <sys/ptrace.h>
  35. #include <sys/signal.h>
  36. #include <sys/sysctl.h>
  37. #include <sys/user.h>
  38. #include <libutil.h>
  39. #include "elf-bfd.h"
  40. #include "fbsd-nat.h"
  41. #include "fbsd-tdep.h"
  42. #include <list>
  43. /* Return the name of a file that can be opened to get the symbols for
  44. the child process identified by PID. */
  45. char *
  46. fbsd_nat_target::pid_to_exec_file (int pid)
  47. {
  48. static char buf[PATH_MAX];
  49. size_t buflen;
  50. int mib[4];
  51. mib[0] = CTL_KERN;
  52. mib[1] = KERN_PROC;
  53. mib[2] = KERN_PROC_PATHNAME;
  54. mib[3] = pid;
  55. buflen = sizeof buf;
  56. if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
  57. /* The kern.proc.pathname.<pid> sysctl returns a length of zero
  58. for processes without an associated executable such as kernel
  59. processes. */
  60. return buflen == 0 ? NULL : buf;
  61. return NULL;
  62. }
  63. /* Iterate over all the memory regions in the current inferior,
  64. calling FUNC for each memory region. DATA is passed as the last
  65. argument to FUNC. */
  66. int
  67. fbsd_nat_target::find_memory_regions (find_memory_region_ftype func,
  68. void *data)
  69. {
  70. pid_t pid = inferior_ptid.pid ();
  71. struct kinfo_vmentry *kve;
  72. uint64_t size;
  73. int i, nitems;
  74. gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
  75. vmentl (kinfo_getvmmap (pid, &nitems));
  76. if (vmentl == NULL)
  77. perror_with_name (_("Couldn't fetch VM map entries."));
  78. for (i = 0, kve = vmentl.get (); i < nitems; i++, kve++)
  79. {
  80. /* Skip unreadable segments and those where MAP_NOCORE has been set. */
  81. if (!(kve->kve_protection & KVME_PROT_READ)
  82. || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
  83. continue;
  84. /* Skip segments with an invalid type. */
  85. if (kve->kve_type != KVME_TYPE_DEFAULT
  86. && kve->kve_type != KVME_TYPE_VNODE
  87. && kve->kve_type != KVME_TYPE_SWAP
  88. && kve->kve_type != KVME_TYPE_PHYS)
  89. continue;
  90. size = kve->kve_end - kve->kve_start;
  91. if (info_verbose)
  92. {
  93. gdb_printf ("Save segment, %ld bytes at %s (%c%c%c)\n",
  94. (long) size,
  95. paddress (target_gdbarch (), kve->kve_start),
  96. kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
  97. kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
  98. kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
  99. }
  100. /* Invoke the callback function to create the corefile segment.
  101. Pass MODIFIED as true, we do not know the real modification state. */
  102. func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
  103. kve->kve_protection & KVME_PROT_WRITE,
  104. kve->kve_protection & KVME_PROT_EXEC, 1, data);
  105. }
  106. return 0;
  107. }
  108. /* Fetch the command line for a running process. */
  109. static gdb::unique_xmalloc_ptr<char>
  110. fbsd_fetch_cmdline (pid_t pid)
  111. {
  112. size_t len;
  113. int mib[4];
  114. len = 0;
  115. mib[0] = CTL_KERN;
  116. mib[1] = KERN_PROC;
  117. mib[2] = KERN_PROC_ARGS;
  118. mib[3] = pid;
  119. if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1)
  120. return nullptr;
  121. if (len == 0)
  122. return nullptr;
  123. gdb::unique_xmalloc_ptr<char> cmdline ((char *) xmalloc (len));
  124. if (sysctl (mib, 4, cmdline.get (), &len, NULL, 0) == -1)
  125. return nullptr;
  126. /* Join the arguments with spaces to form a single string. */
  127. char *cp = cmdline.get ();
  128. for (size_t i = 0; i < len - 1; i++)
  129. if (cp[i] == '\0')
  130. cp[i] = ' ';
  131. cp[len - 1] = '\0';
  132. return cmdline;
  133. }
  134. /* Fetch the external variant of the kernel's internal process
  135. structure for the process PID into KP. */
  136. static bool
  137. fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
  138. {
  139. size_t len;
  140. int mib[4];
  141. len = sizeof *kp;
  142. mib[0] = CTL_KERN;
  143. mib[1] = KERN_PROC;
  144. mib[2] = KERN_PROC_PID;
  145. mib[3] = pid;
  146. return (sysctl (mib, 4, kp, &len, NULL, 0) == 0);
  147. }
  148. /* Implement the "info_proc" target_ops method. */
  149. bool
  150. fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
  151. {
  152. gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl;
  153. int nfd = 0;
  154. struct kinfo_proc kp;
  155. pid_t pid;
  156. bool do_cmdline = false;
  157. bool do_cwd = false;
  158. bool do_exe = false;
  159. bool do_files = false;
  160. bool do_mappings = false;
  161. bool do_status = false;
  162. switch (what)
  163. {
  164. case IP_MINIMAL:
  165. do_cmdline = true;
  166. do_cwd = true;
  167. do_exe = true;
  168. break;
  169. case IP_MAPPINGS:
  170. do_mappings = true;
  171. break;
  172. case IP_STATUS:
  173. case IP_STAT:
  174. do_status = true;
  175. break;
  176. case IP_CMDLINE:
  177. do_cmdline = true;
  178. break;
  179. case IP_EXE:
  180. do_exe = true;
  181. break;
  182. case IP_CWD:
  183. do_cwd = true;
  184. break;
  185. case IP_FILES:
  186. do_files = true;
  187. break;
  188. case IP_ALL:
  189. do_cmdline = true;
  190. do_cwd = true;
  191. do_exe = true;
  192. do_files = true;
  193. do_mappings = true;
  194. do_status = true;
  195. break;
  196. default:
  197. error (_("Not supported on this target."));
  198. }
  199. gdb_argv built_argv (args);
  200. if (built_argv.count () == 0)
  201. {
  202. pid = inferior_ptid.pid ();
  203. if (pid == 0)
  204. error (_("No current process: you must name one."));
  205. }
  206. else if (built_argv.count () == 1 && isdigit (built_argv[0][0]))
  207. pid = strtol (built_argv[0], NULL, 10);
  208. else
  209. error (_("Invalid arguments."));
  210. gdb_printf (_("process %d\n"), pid);
  211. if (do_cwd || do_exe || do_files)
  212. fdtbl.reset (kinfo_getfile (pid, &nfd));
  213. if (do_cmdline)
  214. {
  215. gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid);
  216. if (cmdline != nullptr)
  217. gdb_printf ("cmdline = '%s'\n", cmdline.get ());
  218. else
  219. warning (_("unable to fetch command line"));
  220. }
  221. if (do_cwd)
  222. {
  223. const char *cwd = NULL;
  224. struct kinfo_file *kf = fdtbl.get ();
  225. for (int i = 0; i < nfd; i++, kf++)
  226. {
  227. if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD)
  228. {
  229. cwd = kf->kf_path;
  230. break;
  231. }
  232. }
  233. if (cwd != NULL)
  234. gdb_printf ("cwd = '%s'\n", cwd);
  235. else
  236. warning (_("unable to fetch current working directory"));
  237. }
  238. if (do_exe)
  239. {
  240. const char *exe = NULL;
  241. struct kinfo_file *kf = fdtbl.get ();
  242. for (int i = 0; i < nfd; i++, kf++)
  243. {
  244. if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT)
  245. {
  246. exe = kf->kf_path;
  247. break;
  248. }
  249. }
  250. if (exe == NULL)
  251. exe = pid_to_exec_file (pid);
  252. if (exe != NULL)
  253. gdb_printf ("exe = '%s'\n", exe);
  254. else
  255. warning (_("unable to fetch executable path name"));
  256. }
  257. if (do_files)
  258. {
  259. struct kinfo_file *kf = fdtbl.get ();
  260. if (nfd > 0)
  261. {
  262. fbsd_info_proc_files_header ();
  263. for (int i = 0; i < nfd; i++, kf++)
  264. fbsd_info_proc_files_entry (kf->kf_type, kf->kf_fd, kf->kf_flags,
  265. kf->kf_offset, kf->kf_vnode_type,
  266. kf->kf_sock_domain, kf->kf_sock_type,
  267. kf->kf_sock_protocol, &kf->kf_sa_local,
  268. &kf->kf_sa_peer, kf->kf_path);
  269. }
  270. else
  271. warning (_("unable to fetch list of open files"));
  272. }
  273. if (do_mappings)
  274. {
  275. int nvment;
  276. gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
  277. vmentl (kinfo_getvmmap (pid, &nvment));
  278. if (vmentl != nullptr)
  279. {
  280. int addr_bit = TARGET_CHAR_BIT * sizeof (void *);
  281. fbsd_info_proc_mappings_header (addr_bit);
  282. struct kinfo_vmentry *kve = vmentl.get ();
  283. for (int i = 0; i < nvment; i++, kve++)
  284. fbsd_info_proc_mappings_entry (addr_bit, kve->kve_start,
  285. kve->kve_end, kve->kve_offset,
  286. kve->kve_flags, kve->kve_protection,
  287. kve->kve_path);
  288. }
  289. else
  290. warning (_("unable to fetch virtual memory map"));
  291. }
  292. if (do_status)
  293. {
  294. if (!fbsd_fetch_kinfo_proc (pid, &kp))
  295. warning (_("Failed to fetch process information"));
  296. else
  297. {
  298. const char *state;
  299. int pgtok;
  300. gdb_printf ("Name: %s\n", kp.ki_comm);
  301. switch (kp.ki_stat)
  302. {
  303. case SIDL:
  304. state = "I (idle)";
  305. break;
  306. case SRUN:
  307. state = "R (running)";
  308. break;
  309. case SSTOP:
  310. state = "T (stopped)";
  311. break;
  312. case SZOMB:
  313. state = "Z (zombie)";
  314. break;
  315. case SSLEEP:
  316. state = "S (sleeping)";
  317. break;
  318. case SWAIT:
  319. state = "W (interrupt wait)";
  320. break;
  321. case SLOCK:
  322. state = "L (blocked on lock)";
  323. break;
  324. default:
  325. state = "? (unknown)";
  326. break;
  327. }
  328. gdb_printf ("State: %s\n", state);
  329. gdb_printf ("Parent process: %d\n", kp.ki_ppid);
  330. gdb_printf ("Process group: %d\n", kp.ki_pgid);
  331. gdb_printf ("Session id: %d\n", kp.ki_sid);
  332. gdb_printf ("TTY: %s\n", pulongest (kp.ki_tdev));
  333. gdb_printf ("TTY owner process group: %d\n", kp.ki_tpgid);
  334. gdb_printf ("User IDs (real, effective, saved): %d %d %d\n",
  335. kp.ki_ruid, kp.ki_uid, kp.ki_svuid);
  336. gdb_printf ("Group IDs (real, effective, saved): %d %d %d\n",
  337. kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid);
  338. gdb_printf ("Groups: ");
  339. for (int i = 0; i < kp.ki_ngroups; i++)
  340. gdb_printf ("%d ", kp.ki_groups[i]);
  341. gdb_printf ("\n");
  342. gdb_printf ("Minor faults (no memory page): %ld\n",
  343. kp.ki_rusage.ru_minflt);
  344. gdb_printf ("Minor faults, children: %ld\n",
  345. kp.ki_rusage_ch.ru_minflt);
  346. gdb_printf ("Major faults (memory page faults): %ld\n",
  347. kp.ki_rusage.ru_majflt);
  348. gdb_printf ("Major faults, children: %ld\n",
  349. kp.ki_rusage_ch.ru_majflt);
  350. gdb_printf ("utime: %s.%06ld\n",
  351. plongest (kp.ki_rusage.ru_utime.tv_sec),
  352. kp.ki_rusage.ru_utime.tv_usec);
  353. gdb_printf ("stime: %s.%06ld\n",
  354. plongest (kp.ki_rusage.ru_stime.tv_sec),
  355. kp.ki_rusage.ru_stime.tv_usec);
  356. gdb_printf ("utime, children: %s.%06ld\n",
  357. plongest (kp.ki_rusage_ch.ru_utime.tv_sec),
  358. kp.ki_rusage_ch.ru_utime.tv_usec);
  359. gdb_printf ("stime, children: %s.%06ld\n",
  360. plongest (kp.ki_rusage_ch.ru_stime.tv_sec),
  361. kp.ki_rusage_ch.ru_stime.tv_usec);
  362. gdb_printf ("'nice' value: %d\n", kp.ki_nice);
  363. gdb_printf ("Start time: %s.%06ld\n",
  364. plongest (kp.ki_start.tv_sec),
  365. kp.ki_start.tv_usec);
  366. pgtok = getpagesize () / 1024;
  367. gdb_printf ("Virtual memory size: %s kB\n",
  368. pulongest (kp.ki_size / 1024));
  369. gdb_printf ("Data size: %s kB\n",
  370. pulongest (kp.ki_dsize * pgtok));
  371. gdb_printf ("Stack size: %s kB\n",
  372. pulongest (kp.ki_ssize * pgtok));
  373. gdb_printf ("Text size: %s kB\n",
  374. pulongest (kp.ki_tsize * pgtok));
  375. gdb_printf ("Resident set size: %s kB\n",
  376. pulongest (kp.ki_rssize * pgtok));
  377. gdb_printf ("Maximum RSS: %s kB\n",
  378. pulongest (kp.ki_rusage.ru_maxrss));
  379. gdb_printf ("Pending Signals: ");
  380. for (int i = 0; i < _SIG_WORDS; i++)
  381. gdb_printf ("%08x ", kp.ki_siglist.__bits[i]);
  382. gdb_printf ("\n");
  383. gdb_printf ("Ignored Signals: ");
  384. for (int i = 0; i < _SIG_WORDS; i++)
  385. gdb_printf ("%08x ", kp.ki_sigignore.__bits[i]);
  386. gdb_printf ("\n");
  387. gdb_printf ("Caught Signals: ");
  388. for (int i = 0; i < _SIG_WORDS; i++)
  389. gdb_printf ("%08x ", kp.ki_sigcatch.__bits[i]);
  390. gdb_printf ("\n");
  391. }
  392. }
  393. return true;
  394. }
  395. /* Return the size of siginfo for the current inferior. */
  396. #ifdef __LP64__
  397. union sigval32 {
  398. int sival_int;
  399. uint32_t sival_ptr;
  400. };
  401. /* This structure matches the naming and layout of `siginfo_t' in
  402. <sys/signal.h>. In particular, the `si_foo' macros defined in that
  403. header can be used with both types to copy fields in the `_reason'
  404. union. */
  405. struct siginfo32
  406. {
  407. int si_signo;
  408. int si_errno;
  409. int si_code;
  410. __pid_t si_pid;
  411. __uid_t si_uid;
  412. int si_status;
  413. uint32_t si_addr;
  414. union sigval32 si_value;
  415. union
  416. {
  417. struct
  418. {
  419. int _trapno;
  420. } _fault;
  421. struct
  422. {
  423. int _timerid;
  424. int _overrun;
  425. } _timer;
  426. struct
  427. {
  428. int _mqd;
  429. } _mesgq;
  430. struct
  431. {
  432. int32_t _band;
  433. } _poll;
  434. struct
  435. {
  436. int32_t __spare1__;
  437. int __spare2__[7];
  438. } __spare__;
  439. } _reason;
  440. };
  441. #endif
  442. static size_t
  443. fbsd_siginfo_size ()
  444. {
  445. #ifdef __LP64__
  446. struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
  447. /* Is the inferior 32-bit? If so, use the 32-bit siginfo size. */
  448. if (gdbarch_long_bit (gdbarch) == 32)
  449. return sizeof (struct siginfo32);
  450. #endif
  451. return sizeof (siginfo_t);
  452. }
  453. /* Convert a native 64-bit siginfo object to a 32-bit object. Note
  454. that FreeBSD doesn't support writing to $_siginfo, so this only
  455. needs to convert one way. */
  456. static void
  457. fbsd_convert_siginfo (siginfo_t *si)
  458. {
  459. #ifdef __LP64__
  460. struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
  461. /* Is the inferior 32-bit? If not, nothing to do. */
  462. if (gdbarch_long_bit (gdbarch) != 32)
  463. return;
  464. struct siginfo32 si32;
  465. si32.si_signo = si->si_signo;
  466. si32.si_errno = si->si_errno;
  467. si32.si_code = si->si_code;
  468. si32.si_pid = si->si_pid;
  469. si32.si_uid = si->si_uid;
  470. si32.si_status = si->si_status;
  471. si32.si_addr = (uintptr_t) si->si_addr;
  472. /* If sival_ptr is being used instead of sival_int on a big-endian
  473. platform, then sival_int will be zero since it holds the upper
  474. 32-bits of the pointer value. */
  475. #if _BYTE_ORDER == _BIG_ENDIAN
  476. if (si->si_value.sival_int == 0)
  477. si32.si_value.sival_ptr = (uintptr_t) si->si_value.sival_ptr;
  478. else
  479. si32.si_value.sival_int = si->si_value.sival_int;
  480. #else
  481. si32.si_value.sival_int = si->si_value.sival_int;
  482. #endif
  483. /* Always copy the spare fields and then possibly overwrite them for
  484. signal-specific or code-specific fields. */
  485. si32._reason.__spare__.__spare1__ = si->_reason.__spare__.__spare1__;
  486. for (int i = 0; i < 7; i++)
  487. si32._reason.__spare__.__spare2__[i] = si->_reason.__spare__.__spare2__[i];
  488. switch (si->si_signo) {
  489. case SIGILL:
  490. case SIGFPE:
  491. case SIGSEGV:
  492. case SIGBUS:
  493. si32.si_trapno = si->si_trapno;
  494. break;
  495. }
  496. switch (si->si_code) {
  497. case SI_TIMER:
  498. si32.si_timerid = si->si_timerid;
  499. si32.si_overrun = si->si_overrun;
  500. break;
  501. case SI_MESGQ:
  502. si32.si_mqd = si->si_mqd;
  503. break;
  504. }
  505. memcpy(si, &si32, sizeof (si32));
  506. #endif
  507. }
  508. /* Implement the "xfer_partial" target_ops method. */
  509. enum target_xfer_status
  510. fbsd_nat_target::xfer_partial (enum target_object object,
  511. const char *annex, gdb_byte *readbuf,
  512. const gdb_byte *writebuf,
  513. ULONGEST offset, ULONGEST len,
  514. ULONGEST *xfered_len)
  515. {
  516. pid_t pid = inferior_ptid.pid ();
  517. switch (object)
  518. {
  519. case TARGET_OBJECT_SIGNAL_INFO:
  520. {
  521. struct ptrace_lwpinfo pl;
  522. size_t siginfo_size;
  523. /* FreeBSD doesn't support writing to $_siginfo. */
  524. if (writebuf != NULL)
  525. return TARGET_XFER_E_IO;
  526. if (inferior_ptid.lwp_p ())
  527. pid = inferior_ptid.lwp ();
  528. siginfo_size = fbsd_siginfo_size ();
  529. if (offset > siginfo_size)
  530. return TARGET_XFER_E_IO;
  531. if (ptrace (PT_LWPINFO, pid, (PTRACE_TYPE_ARG3) &pl, sizeof (pl)) == -1)
  532. return TARGET_XFER_E_IO;
  533. if (!(pl.pl_flags & PL_FLAG_SI))
  534. return TARGET_XFER_E_IO;
  535. fbsd_convert_siginfo (&pl.pl_siginfo);
  536. if (offset + len > siginfo_size)
  537. len = siginfo_size - offset;
  538. memcpy (readbuf, ((gdb_byte *) &pl.pl_siginfo) + offset, len);
  539. *xfered_len = len;
  540. return TARGET_XFER_OK;
  541. }
  542. #ifdef KERN_PROC_AUXV
  543. case TARGET_OBJECT_AUXV:
  544. {
  545. gdb::byte_vector buf_storage;
  546. gdb_byte *buf;
  547. size_t buflen;
  548. int mib[4];
  549. if (writebuf != NULL)
  550. return TARGET_XFER_E_IO;
  551. mib[0] = CTL_KERN;
  552. mib[1] = KERN_PROC;
  553. mib[2] = KERN_PROC_AUXV;
  554. mib[3] = pid;
  555. if (offset == 0)
  556. {
  557. buf = readbuf;
  558. buflen = len;
  559. }
  560. else
  561. {
  562. buflen = offset + len;
  563. buf_storage.resize (buflen);
  564. buf = buf_storage.data ();
  565. }
  566. if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
  567. {
  568. if (offset != 0)
  569. {
  570. if (buflen > offset)
  571. {
  572. buflen -= offset;
  573. memcpy (readbuf, buf + offset, buflen);
  574. }
  575. else
  576. buflen = 0;
  577. }
  578. *xfered_len = buflen;
  579. return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
  580. }
  581. return TARGET_XFER_E_IO;
  582. }
  583. #endif
  584. #if defined(KERN_PROC_VMMAP) && defined(KERN_PROC_PS_STRINGS)
  585. case TARGET_OBJECT_FREEBSD_VMMAP:
  586. case TARGET_OBJECT_FREEBSD_PS_STRINGS:
  587. {
  588. gdb::byte_vector buf_storage;
  589. gdb_byte *buf;
  590. size_t buflen;
  591. int mib[4];
  592. int proc_target;
  593. uint32_t struct_size;
  594. switch (object)
  595. {
  596. case TARGET_OBJECT_FREEBSD_VMMAP:
  597. proc_target = KERN_PROC_VMMAP;
  598. struct_size = sizeof (struct kinfo_vmentry);
  599. break;
  600. case TARGET_OBJECT_FREEBSD_PS_STRINGS:
  601. proc_target = KERN_PROC_PS_STRINGS;
  602. struct_size = sizeof (void *);
  603. break;
  604. }
  605. if (writebuf != NULL)
  606. return TARGET_XFER_E_IO;
  607. mib[0] = CTL_KERN;
  608. mib[1] = KERN_PROC;
  609. mib[2] = proc_target;
  610. mib[3] = pid;
  611. if (sysctl (mib, 4, NULL, &buflen, NULL, 0) != 0)
  612. return TARGET_XFER_E_IO;
  613. buflen += sizeof (struct_size);
  614. if (offset >= buflen)
  615. {
  616. *xfered_len = 0;
  617. return TARGET_XFER_EOF;
  618. }
  619. buf_storage.resize (buflen);
  620. buf = buf_storage.data ();
  621. memcpy (buf, &struct_size, sizeof (struct_size));
  622. buflen -= sizeof (struct_size);
  623. if (sysctl (mib, 4, buf + sizeof (struct_size), &buflen, NULL, 0) != 0)
  624. return TARGET_XFER_E_IO;
  625. buflen += sizeof (struct_size);
  626. if (buflen - offset < len)
  627. len = buflen - offset;
  628. memcpy (readbuf, buf + offset, len);
  629. *xfered_len = len;
  630. return TARGET_XFER_OK;
  631. }
  632. #endif
  633. default:
  634. return inf_ptrace_target::xfer_partial (object, annex,
  635. readbuf, writebuf, offset,
  636. len, xfered_len);
  637. }
  638. }
  639. static bool debug_fbsd_lwp;
  640. static bool debug_fbsd_nat;
  641. static void
  642. show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
  643. struct cmd_list_element *c, const char *value)
  644. {
  645. gdb_printf (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
  646. }
  647. static void
  648. show_fbsd_nat_debug (struct ui_file *file, int from_tty,
  649. struct cmd_list_element *c, const char *value)
  650. {
  651. gdb_printf (file, _("Debugging of FreeBSD native target is %s.\n"),
  652. value);
  653. }
  654. #define fbsd_lwp_debug_printf(fmt, ...) \
  655. debug_prefixed_printf_cond (debug_fbsd_lwp, "fbsd-lwp", fmt, ##__VA_ARGS__)
  656. #define fbsd_nat_debug_printf(fmt, ...) \
  657. debug_prefixed_printf_cond (debug_fbsd_nat, "fbsd-nat", fmt, ##__VA_ARGS__)
  658. /*
  659. FreeBSD's first thread support was via a "reentrant" version of libc
  660. (libc_r) that first shipped in 2.2.7. This library multiplexed all
  661. of the threads in a process onto a single kernel thread. This
  662. library was supported via the bsd-uthread target.
  663. FreeBSD 5.1 introduced two new threading libraries that made use of
  664. multiple kernel threads. The first (libkse) scheduled M user
  665. threads onto N (<= M) kernel threads (LWPs). The second (libthr)
  666. bound each user thread to a dedicated kernel thread. libkse shipped
  667. as the default threading library (libpthread).
  668. FreeBSD 5.3 added a libthread_db to abstract the interface across
  669. the various thread libraries (libc_r, libkse, and libthr).
  670. FreeBSD 7.0 switched the default threading library from from libkse
  671. to libpthread and removed libc_r.
  672. FreeBSD 8.0 removed libkse and the in-kernel support for it. The
  673. only threading library supported by 8.0 and later is libthr which
  674. ties each user thread directly to an LWP. To simplify the
  675. implementation, this target only supports LWP-backed threads using
  676. ptrace directly rather than libthread_db.
  677. FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
  678. */
  679. /* Return true if PTID is still active in the inferior. */
  680. bool
  681. fbsd_nat_target::thread_alive (ptid_t ptid)
  682. {
  683. if (ptid.lwp_p ())
  684. {
  685. struct ptrace_lwpinfo pl;
  686. if (ptrace (PT_LWPINFO, ptid.lwp (), (caddr_t) &pl, sizeof pl)
  687. == -1)
  688. return false;
  689. #ifdef PL_FLAG_EXITED
  690. if (pl.pl_flags & PL_FLAG_EXITED)
  691. return false;
  692. #endif
  693. }
  694. return true;
  695. }
  696. /* Convert PTID to a string. */
  697. std::string
  698. fbsd_nat_target::pid_to_str (ptid_t ptid)
  699. {
  700. lwpid_t lwp;
  701. lwp = ptid.lwp ();
  702. if (lwp != 0)
  703. {
  704. int pid = ptid.pid ();
  705. return string_printf ("LWP %d of process %d", lwp, pid);
  706. }
  707. return normal_pid_to_str (ptid);
  708. }
  709. #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
  710. /* Return the name assigned to a thread by an application. Returns
  711. the string in a static buffer. */
  712. const char *
  713. fbsd_nat_target::thread_name (struct thread_info *thr)
  714. {
  715. struct ptrace_lwpinfo pl;
  716. struct kinfo_proc kp;
  717. int pid = thr->ptid.pid ();
  718. long lwp = thr->ptid.lwp ();
  719. static char buf[sizeof pl.pl_tdname + 1];
  720. /* Note that ptrace_lwpinfo returns the process command in pl_tdname
  721. if a name has not been set explicitly. Return a NULL name in
  722. that case. */
  723. if (!fbsd_fetch_kinfo_proc (pid, &kp))
  724. return nullptr;
  725. if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
  726. return nullptr;
  727. if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
  728. return NULL;
  729. xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
  730. return buf;
  731. }
  732. #endif
  733. /* Enable additional event reporting on new processes.
  734. To catch fork events, PTRACE_FORK is set on every traced process
  735. to enable stops on returns from fork or vfork. Note that both the
  736. parent and child will always stop, even if system call stops are
  737. not enabled.
  738. To catch LWP events, PTRACE_EVENTS is set on every traced process.
  739. This enables stops on the birth for new LWPs (excluding the "main" LWP)
  740. and the death of LWPs (excluding the last LWP in a process). Note
  741. that unlike fork events, the LWP that creates a new LWP does not
  742. report an event. */
  743. static void
  744. fbsd_enable_proc_events (pid_t pid)
  745. {
  746. #ifdef PT_GET_EVENT_MASK
  747. int events;
  748. if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
  749. sizeof (events)) == -1)
  750. perror_with_name (("ptrace (PT_GET_EVENT_MASK)"));
  751. events |= PTRACE_FORK | PTRACE_LWP;
  752. #ifdef PTRACE_VFORK
  753. events |= PTRACE_VFORK;
  754. #endif
  755. if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
  756. sizeof (events)) == -1)
  757. perror_with_name (("ptrace (PT_SET_EVENT_MASK)"));
  758. #else
  759. #ifdef TDP_RFPPWAIT
  760. if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
  761. perror_with_name (("ptrace (PT_FOLLOW_FORK)"));
  762. #endif
  763. #ifdef PT_LWP_EVENTS
  764. if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
  765. perror_with_name (("ptrace (PT_LWP_EVENTS)"));
  766. #endif
  767. #endif
  768. }
  769. /* Add threads for any new LWPs in a process.
  770. When LWP events are used, this function is only used to detect existing
  771. threads when attaching to a process. On older systems, this function is
  772. called to discover new threads each time the thread list is updated. */
  773. static void
  774. fbsd_add_threads (fbsd_nat_target *target, pid_t pid)
  775. {
  776. int i, nlwps;
  777. gdb_assert (!in_thread_list (target, ptid_t (pid)));
  778. nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
  779. if (nlwps == -1)
  780. perror_with_name (("ptrace (PT_GETNUMLWPS)"));
  781. gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps));
  782. nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps);
  783. if (nlwps == -1)
  784. perror_with_name (("ptrace (PT_GETLWPLIST)"));
  785. for (i = 0; i < nlwps; i++)
  786. {
  787. ptid_t ptid = ptid_t (pid, lwps[i]);
  788. if (!in_thread_list (target, ptid))
  789. {
  790. #ifdef PT_LWP_EVENTS
  791. struct ptrace_lwpinfo pl;
  792. /* Don't add exited threads. Note that this is only called
  793. when attaching to a multi-threaded process. */
  794. if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
  795. perror_with_name (("ptrace (PT_LWPINFO)"));
  796. if (pl.pl_flags & PL_FLAG_EXITED)
  797. continue;
  798. #endif
  799. fbsd_lwp_debug_printf ("adding thread for LWP %u", lwps[i]);
  800. add_thread (target, ptid);
  801. }
  802. }
  803. }
  804. /* Implement the "update_thread_list" target_ops method. */
  805. void
  806. fbsd_nat_target::update_thread_list ()
  807. {
  808. #ifdef PT_LWP_EVENTS
  809. /* With support for thread events, threads are added/deleted from the
  810. list as events are reported, so just try deleting exited threads. */
  811. delete_exited_threads ();
  812. #else
  813. prune_threads ();
  814. fbsd_add_threads (this, inferior_ptid.pid ());
  815. #endif
  816. }
  817. /* Async mode support. */
  818. /* Implement the "can_async_p" target method. */
  819. bool
  820. fbsd_nat_target::can_async_p ()
  821. {
  822. /* This flag should be checked in the common target.c code. */
  823. gdb_assert (target_async_permitted);
  824. /* Otherwise, this targets is always able to support async mode. */
  825. return true;
  826. }
  827. /* SIGCHLD handler notifies the event-loop in async mode. */
  828. static void
  829. sigchld_handler (int signo)
  830. {
  831. int old_errno = errno;
  832. fbsd_nat_target::async_file_mark_if_open ();
  833. errno = old_errno;
  834. }
  835. /* Callback registered with the target events file descriptor. */
  836. static void
  837. handle_target_event (int error, gdb_client_data client_data)
  838. {
  839. inferior_event_handler (INF_REG_EVENT);
  840. }
  841. /* Implement the "async" target method. */
  842. void
  843. fbsd_nat_target::async (int enable)
  844. {
  845. if ((enable != 0) == is_async_p ())
  846. return;
  847. /* Block SIGCHILD while we create/destroy the pipe, as the handler
  848. writes to it. */
  849. gdb::block_signals blocker;
  850. if (enable)
  851. {
  852. if (!async_file_open ())
  853. internal_error (__FILE__, __LINE__, "failed to create event pipe.");
  854. add_file_handler (async_wait_fd (), handle_target_event, NULL, "fbsd-nat");
  855. /* Trigger a poll in case there are pending events to
  856. handle. */
  857. async_file_mark ();
  858. }
  859. else
  860. {
  861. delete_file_handler (async_wait_fd ());
  862. async_file_close ();
  863. }
  864. }
  865. #ifdef TDP_RFPPWAIT
  866. /*
  867. To catch fork events, PT_FOLLOW_FORK is set on every traced process
  868. to enable stops on returns from fork or vfork. Note that both the
  869. parent and child will always stop, even if system call stops are not
  870. enabled.
  871. After a fork, both the child and parent process will stop and report
  872. an event. However, there is no guarantee of order. If the parent
  873. reports its stop first, then fbsd_wait explicitly waits for the new
  874. child before returning. If the child reports its stop first, then
  875. the event is saved on a list and ignored until the parent's stop is
  876. reported. fbsd_wait could have been changed to fetch the parent PID
  877. of the new child and used that to wait for the parent explicitly.
  878. However, if two threads in the parent fork at the same time, then
  879. the wait on the parent might return the "wrong" fork event.
  880. The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
  881. the new child process. This flag could be inferred by treating any
  882. events for an unknown pid as a new child.
  883. In addition, the initial version of PT_FOLLOW_FORK did not report a
  884. stop event for the parent process of a vfork until after the child
  885. process executed a new program or exited. The kernel was changed to
  886. defer the wait for exit or exec of the child until after posting the
  887. stop event shortly after the change to introduce PL_FLAG_CHILD.
  888. This could be worked around by reporting a vfork event when the
  889. child event posted and ignoring the subsequent event from the
  890. parent.
  891. This implementation requires both of these fixes for simplicity's
  892. sake. FreeBSD versions newer than 9.1 contain both fixes.
  893. */
  894. static std::list<ptid_t> fbsd_pending_children;
  895. /* Record a new child process event that is reported before the
  896. corresponding fork event in the parent. */
  897. static void
  898. fbsd_remember_child (ptid_t pid)
  899. {
  900. fbsd_pending_children.push_front (pid);
  901. }
  902. /* Check for a previously-recorded new child process event for PID.
  903. If one is found, remove it from the list and return the PTID. */
  904. static ptid_t
  905. fbsd_is_child_pending (pid_t pid)
  906. {
  907. for (auto it = fbsd_pending_children.begin ();
  908. it != fbsd_pending_children.end (); it++)
  909. if (it->pid () == pid)
  910. {
  911. ptid_t ptid = *it;
  912. fbsd_pending_children.erase (it);
  913. return ptid;
  914. }
  915. return null_ptid;
  916. }
  917. #ifndef PTRACE_VFORK
  918. static std::forward_list<ptid_t> fbsd_pending_vfork_done;
  919. /* Record a pending vfork done event. */
  920. static void
  921. fbsd_add_vfork_done (ptid_t pid)
  922. {
  923. fbsd_pending_vfork_done.push_front (pid);
  924. /* If we're in async mode, need to tell the event loop there's
  925. something here to process. */
  926. if (target_is_async_p ())
  927. async_file_mark ();
  928. }
  929. /* Check for a pending vfork done event for a specific PID. */
  930. static int
  931. fbsd_is_vfork_done_pending (pid_t pid)
  932. {
  933. for (auto it = fbsd_pending_vfork_done.begin ();
  934. it != fbsd_pending_vfork_done.end (); it++)
  935. if (it->pid () == pid)
  936. return 1;
  937. return 0;
  938. }
  939. /* Check for a pending vfork done event. If one is found, remove it
  940. from the list and return the PTID. */
  941. static ptid_t
  942. fbsd_next_vfork_done (void)
  943. {
  944. if (!fbsd_pending_vfork_done.empty ())
  945. {
  946. ptid_t ptid = fbsd_pending_vfork_done.front ();
  947. fbsd_pending_vfork_done.pop_front ();
  948. return ptid;
  949. }
  950. return null_ptid;
  951. }
  952. #endif
  953. #endif
  954. /* Implement the "resume" target_ops method. */
  955. void
  956. fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
  957. {
  958. #if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK)
  959. pid_t pid;
  960. /* Don't PT_CONTINUE a process which has a pending vfork done event. */
  961. if (minus_one_ptid == ptid)
  962. pid = inferior_ptid.pid ();
  963. else
  964. pid = ptid.pid ();
  965. if (fbsd_is_vfork_done_pending (pid))
  966. return;
  967. #endif
  968. fbsd_nat_debug_printf ("[%s], step %d, signo %d (%s)",
  969. target_pid_to_str (ptid).c_str (), step, signo,
  970. gdb_signal_to_name (signo));
  971. if (ptid.lwp_p ())
  972. {
  973. /* If ptid is a specific LWP, suspend all other LWPs in the process. */
  974. inferior *inf = find_inferior_ptid (this, ptid);
  975. for (thread_info *tp : inf->non_exited_threads ())
  976. {
  977. int request;
  978. if (tp->ptid.lwp () == ptid.lwp ())
  979. request = PT_RESUME;
  980. else
  981. request = PT_SUSPEND;
  982. if (ptrace (request, tp->ptid.lwp (), NULL, 0) == -1)
  983. perror_with_name (request == PT_RESUME ?
  984. ("ptrace (PT_RESUME)") :
  985. ("ptrace (PT_SUSPEND)"));
  986. if (request == PT_RESUME)
  987. low_prepare_to_resume (tp);
  988. }
  989. }
  990. else
  991. {
  992. /* If ptid is a wildcard, resume all matching threads (they won't run
  993. until the process is continued however). */
  994. for (thread_info *tp : all_non_exited_threads (this, ptid))
  995. {
  996. if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
  997. perror_with_name (("ptrace (PT_RESUME)"));
  998. low_prepare_to_resume (tp);
  999. }
  1000. ptid = inferior_ptid;
  1001. }
  1002. #if __FreeBSD_version < 1200052
  1003. /* When multiple threads within a process wish to report STOPPED
  1004. events from wait(), the kernel picks one thread event as the
  1005. thread event to report. The chosen thread event is retrieved via
  1006. PT_LWPINFO by passing the process ID as the request pid. If
  1007. multiple events are pending, then the subsequent wait() after
  1008. resuming a process will report another STOPPED event after
  1009. resuming the process to handle the next thread event and so on.
  1010. A single thread event is cleared as a side effect of resuming the
  1011. process with PT_CONTINUE, PT_STEP, etc. In older kernels,
  1012. however, the request pid was used to select which thread's event
  1013. was cleared rather than always clearing the event that was just
  1014. reported. To avoid clearing the event of the wrong LWP, always
  1015. pass the process ID instead of an LWP ID to PT_CONTINUE or
  1016. PT_SYSCALL.
  1017. In the case of stepping, the process ID cannot be used with
  1018. PT_STEP since it would step the thread that reported an event
  1019. which may not be the thread indicated by PTID. For stepping, use
  1020. PT_SETSTEP to enable stepping on the desired thread before
  1021. resuming the process via PT_CONTINUE instead of using
  1022. PT_STEP. */
  1023. if (step)
  1024. {
  1025. if (ptrace (PT_SETSTEP, get_ptrace_pid (ptid), NULL, 0) == -1)
  1026. perror_with_name (("ptrace (PT_SETSTEP)"));
  1027. step = 0;
  1028. }
  1029. ptid = ptid_t (ptid.pid ());
  1030. #endif
  1031. inf_ptrace_target::resume (ptid, step, signo);
  1032. }
  1033. #ifdef USE_SIGTRAP_SIGINFO
  1034. /* Handle breakpoint and trace traps reported via SIGTRAP. If the
  1035. trap was a breakpoint or trace trap that should be reported to the
  1036. core, return true. */
  1037. static bool
  1038. fbsd_handle_debug_trap (fbsd_nat_target *target, ptid_t ptid,
  1039. const struct ptrace_lwpinfo &pl)
  1040. {
  1041. /* Ignore traps without valid siginfo or for signals other than
  1042. SIGTRAP.
  1043. FreeBSD kernels prior to r341800 can return stale siginfo for at
  1044. least some events, but those events can be identified by
  1045. additional flags set in pl_flags. True breakpoint and
  1046. single-step traps should not have other flags set in
  1047. pl_flags. */
  1048. if (pl.pl_flags != PL_FLAG_SI || pl.pl_siginfo.si_signo != SIGTRAP)
  1049. return false;
  1050. /* Trace traps are either a single step or a hardware watchpoint or
  1051. breakpoint. */
  1052. if (pl.pl_siginfo.si_code == TRAP_TRACE)
  1053. {
  1054. fbsd_nat_debug_printf ("trace trap for LWP %ld", ptid.lwp ());
  1055. return true;
  1056. }
  1057. if (pl.pl_siginfo.si_code == TRAP_BRKPT)
  1058. {
  1059. /* Fixup PC for the software breakpoint. */
  1060. struct regcache *regcache = get_thread_regcache (target, ptid);
  1061. struct gdbarch *gdbarch = regcache->arch ();
  1062. int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
  1063. fbsd_nat_debug_printf ("sw breakpoint trap for LWP %ld", ptid.lwp ());
  1064. if (decr_pc != 0)
  1065. {
  1066. CORE_ADDR pc;
  1067. pc = regcache_read_pc (regcache);
  1068. regcache_write_pc (regcache, pc - decr_pc);
  1069. }
  1070. return true;
  1071. }
  1072. return false;
  1073. }
  1074. #endif
  1075. /* Wait for the child specified by PTID to do something. Return the
  1076. process ID of the child, or MINUS_ONE_PTID in case of error; store
  1077. the status in *OURSTATUS. */
  1078. ptid_t
  1079. fbsd_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
  1080. target_wait_flags target_options)
  1081. {
  1082. ptid_t wptid;
  1083. while (1)
  1084. {
  1085. #ifndef PTRACE_VFORK
  1086. wptid = fbsd_next_vfork_done ();
  1087. if (wptid != null_ptid)
  1088. {
  1089. ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
  1090. return wptid;
  1091. }
  1092. #endif
  1093. wptid = inf_ptrace_target::wait (ptid, ourstatus, target_options);
  1094. if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
  1095. {
  1096. struct ptrace_lwpinfo pl;
  1097. pid_t pid;
  1098. int status;
  1099. pid = wptid.pid ();
  1100. if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
  1101. perror_with_name (("ptrace (PT_LWPINFO)"));
  1102. wptid = ptid_t (pid, pl.pl_lwpid);
  1103. if (debug_fbsd_nat)
  1104. {
  1105. fbsd_nat_debug_printf ("stop for LWP %u event %d flags %#x",
  1106. pl.pl_lwpid, pl.pl_event, pl.pl_flags);
  1107. if (pl.pl_flags & PL_FLAG_SI)
  1108. fbsd_nat_debug_printf ("si_signo %u si_code %u",
  1109. pl.pl_siginfo.si_signo,
  1110. pl.pl_siginfo.si_code);
  1111. }
  1112. #ifdef PT_LWP_EVENTS
  1113. if (pl.pl_flags & PL_FLAG_EXITED)
  1114. {
  1115. /* If GDB attaches to a multi-threaded process, exiting
  1116. threads might be skipped during post_attach that
  1117. have not yet reported their PL_FLAG_EXITED event.
  1118. Ignore EXITED events for an unknown LWP. */
  1119. thread_info *thr = find_thread_ptid (this, wptid);
  1120. if (thr != nullptr)
  1121. {
  1122. fbsd_lwp_debug_printf ("deleting thread for LWP %u",
  1123. pl.pl_lwpid);
  1124. if (print_thread_events)
  1125. gdb_printf (_("[%s exited]\n"),
  1126. target_pid_to_str (wptid).c_str ());
  1127. low_delete_thread (thr);
  1128. delete_thread (thr);
  1129. }
  1130. if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
  1131. perror_with_name (("ptrace (PT_CONTINUE)"));
  1132. continue;
  1133. }
  1134. #endif
  1135. /* Switch to an LWP PTID on the first stop in a new process.
  1136. This is done after handling PL_FLAG_EXITED to avoid
  1137. switching to an exited LWP. It is done before checking
  1138. PL_FLAG_BORN in case the first stop reported after
  1139. attaching to an existing process is a PL_FLAG_BORN
  1140. event. */
  1141. if (in_thread_list (this, ptid_t (pid)))
  1142. {
  1143. fbsd_lwp_debug_printf ("using LWP %u for first thread",
  1144. pl.pl_lwpid);
  1145. thread_change_ptid (this, ptid_t (pid), wptid);
  1146. }
  1147. #ifdef PT_LWP_EVENTS
  1148. if (pl.pl_flags & PL_FLAG_BORN)
  1149. {
  1150. /* If GDB attaches to a multi-threaded process, newborn
  1151. threads might be added by fbsd_add_threads that have
  1152. not yet reported their PL_FLAG_BORN event. Ignore
  1153. BORN events for an already-known LWP. */
  1154. if (!in_thread_list (this, wptid))
  1155. {
  1156. fbsd_lwp_debug_printf ("adding thread for LWP %u",
  1157. pl.pl_lwpid);
  1158. add_thread (this, wptid);
  1159. }
  1160. ourstatus->set_spurious ();
  1161. return wptid;
  1162. }
  1163. #endif
  1164. #ifdef TDP_RFPPWAIT
  1165. if (pl.pl_flags & PL_FLAG_FORKED)
  1166. {
  1167. #ifndef PTRACE_VFORK
  1168. struct kinfo_proc kp;
  1169. #endif
  1170. bool is_vfork = false;
  1171. ptid_t child_ptid;
  1172. pid_t child;
  1173. child = pl.pl_child_pid;
  1174. #ifdef PTRACE_VFORK
  1175. if (pl.pl_flags & PL_FLAG_VFORKED)
  1176. is_vfork = true;
  1177. #endif
  1178. /* Make sure the other end of the fork is stopped too. */
  1179. child_ptid = fbsd_is_child_pending (child);
  1180. if (child_ptid == null_ptid)
  1181. {
  1182. pid = waitpid (child, &status, 0);
  1183. if (pid == -1)
  1184. perror_with_name (("waitpid"));
  1185. gdb_assert (pid == child);
  1186. if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
  1187. perror_with_name (("ptrace (PT_LWPINFO)"));
  1188. gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
  1189. child_ptid = ptid_t (child, pl.pl_lwpid);
  1190. }
  1191. /* Enable additional events on the child process. */
  1192. fbsd_enable_proc_events (child_ptid.pid ());
  1193. #ifndef PTRACE_VFORK
  1194. /* For vfork, the child process will have the P_PPWAIT
  1195. flag set. */
  1196. if (fbsd_fetch_kinfo_proc (child, &kp))
  1197. {
  1198. if (kp.ki_flag & P_PPWAIT)
  1199. is_vfork = true;
  1200. }
  1201. else
  1202. warning (_("Failed to fetch process information"));
  1203. #endif
  1204. low_new_fork (wptid, child);
  1205. if (is_vfork)
  1206. ourstatus->set_vforked (child_ptid);
  1207. else
  1208. ourstatus->set_forked (child_ptid);
  1209. return wptid;
  1210. }
  1211. if (pl.pl_flags & PL_FLAG_CHILD)
  1212. {
  1213. /* Remember that this child forked, but do not report it
  1214. until the parent reports its corresponding fork
  1215. event. */
  1216. fbsd_remember_child (wptid);
  1217. continue;
  1218. }
  1219. #ifdef PTRACE_VFORK
  1220. if (pl.pl_flags & PL_FLAG_VFORK_DONE)
  1221. {
  1222. ourstatus->set_vfork_done ();
  1223. return wptid;
  1224. }
  1225. #endif
  1226. #endif
  1227. if (pl.pl_flags & PL_FLAG_EXEC)
  1228. {
  1229. ourstatus->set_execd
  1230. (make_unique_xstrdup (pid_to_exec_file (pid)));
  1231. return wptid;
  1232. }
  1233. #ifdef USE_SIGTRAP_SIGINFO
  1234. if (fbsd_handle_debug_trap (this, wptid, pl))
  1235. return wptid;
  1236. #endif
  1237. /* Note that PL_FLAG_SCE is set for any event reported while
  1238. a thread is executing a system call in the kernel. In
  1239. particular, signals that interrupt a sleep in a system
  1240. call will report this flag as part of their event. Stops
  1241. explicitly for system call entry and exit always use
  1242. SIGTRAP, so only treat SIGTRAP events as system call
  1243. entry/exit events. */
  1244. if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)
  1245. && ourstatus->sig () == SIGTRAP)
  1246. {
  1247. #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
  1248. if (catch_syscall_enabled ())
  1249. {
  1250. if (catching_syscall_number (pl.pl_syscall_code))
  1251. {
  1252. if (pl.pl_flags & PL_FLAG_SCE)
  1253. ourstatus->set_syscall_entry (pl.pl_syscall_code);
  1254. else
  1255. ourstatus->set_syscall_return (pl.pl_syscall_code);
  1256. return wptid;
  1257. }
  1258. }
  1259. #endif
  1260. /* If the core isn't interested in this event, just
  1261. continue the process explicitly and wait for another
  1262. event. Note that PT_SYSCALL is "sticky" on FreeBSD
  1263. and once system call stops are enabled on a process
  1264. it stops for all system call entries and exits. */
  1265. if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
  1266. perror_with_name (("ptrace (PT_CONTINUE)"));
  1267. continue;
  1268. }
  1269. }
  1270. return wptid;
  1271. }
  1272. }
  1273. ptid_t
  1274. fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  1275. target_wait_flags target_options)
  1276. {
  1277. ptid_t wptid;
  1278. fbsd_nat_debug_printf ("[%s], [%s]", target_pid_to_str (ptid).c_str (),
  1279. target_options_to_string (target_options).c_str ());
  1280. /* Ensure any subsequent events trigger a new event in the loop. */
  1281. if (is_async_p ())
  1282. async_file_flush ();
  1283. wptid = wait_1 (ptid, ourstatus, target_options);
  1284. /* If we are in async mode and found an event, there may still be
  1285. another event pending. Trigger the event pipe so that that the
  1286. event loop keeps polling until no event is returned. */
  1287. if (is_async_p ()
  1288. && ((ourstatus->kind () != TARGET_WAITKIND_IGNORE
  1289. && ourstatus->kind() != TARGET_WAITKIND_NO_RESUMED)
  1290. || ptid != minus_one_ptid))
  1291. async_file_mark ();
  1292. fbsd_nat_debug_printf ("returning [%s], [%s]",
  1293. target_pid_to_str (wptid).c_str (),
  1294. ourstatus->to_string ().c_str ());
  1295. return wptid;
  1296. }
  1297. #ifdef USE_SIGTRAP_SIGINFO
  1298. /* Implement the "stopped_by_sw_breakpoint" target_ops method. */
  1299. bool
  1300. fbsd_nat_target::stopped_by_sw_breakpoint ()
  1301. {
  1302. struct ptrace_lwpinfo pl;
  1303. if (ptrace (PT_LWPINFO, get_ptrace_pid (inferior_ptid), (caddr_t) &pl,
  1304. sizeof pl) == -1)
  1305. return false;
  1306. return (pl.pl_flags == PL_FLAG_SI
  1307. && pl.pl_siginfo.si_signo == SIGTRAP
  1308. && pl.pl_siginfo.si_code == TRAP_BRKPT);
  1309. }
  1310. /* Implement the "supports_stopped_by_sw_breakpoint" target_ops
  1311. method. */
  1312. bool
  1313. fbsd_nat_target::supports_stopped_by_sw_breakpoint ()
  1314. {
  1315. return true;
  1316. }
  1317. #endif
  1318. #ifdef PROC_ASLR_CTL
  1319. class maybe_disable_address_space_randomization
  1320. {
  1321. public:
  1322. explicit maybe_disable_address_space_randomization (bool disable_randomization)
  1323. {
  1324. if (disable_randomization)
  1325. {
  1326. if (procctl (P_PID, getpid (), PROC_ASLR_STATUS, &m_aslr_ctl) == -1)
  1327. {
  1328. warning (_("Failed to fetch current address space randomization "
  1329. "status: %s"), safe_strerror (errno));
  1330. return;
  1331. }
  1332. m_aslr_ctl &= ~PROC_ASLR_ACTIVE;
  1333. if (m_aslr_ctl == PROC_ASLR_FORCE_DISABLE)
  1334. return;
  1335. int ctl = PROC_ASLR_FORCE_DISABLE;
  1336. if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &ctl) == -1)
  1337. {
  1338. warning (_("Error disabling address space randomization: %s"),
  1339. safe_strerror (errno));
  1340. return;
  1341. }
  1342. m_aslr_ctl_set = true;
  1343. }
  1344. }
  1345. ~maybe_disable_address_space_randomization ()
  1346. {
  1347. if (m_aslr_ctl_set)
  1348. {
  1349. if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &m_aslr_ctl) == -1)
  1350. warning (_("Error restoring address space randomization: %s"),
  1351. safe_strerror (errno));
  1352. }
  1353. }
  1354. DISABLE_COPY_AND_ASSIGN (maybe_disable_address_space_randomization);
  1355. private:
  1356. bool m_aslr_ctl_set = false;
  1357. int m_aslr_ctl = 0;
  1358. };
  1359. #endif
  1360. void
  1361. fbsd_nat_target::create_inferior (const char *exec_file,
  1362. const std::string &allargs,
  1363. char **env, int from_tty)
  1364. {
  1365. #ifdef PROC_ASLR_CTL
  1366. maybe_disable_address_space_randomization restore_aslr_ctl
  1367. (disable_randomization);
  1368. #endif
  1369. inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
  1370. }
  1371. #ifdef TDP_RFPPWAIT
  1372. /* Target hook for follow_fork. On entry and at return inferior_ptid is
  1373. the ptid of the followed inferior. */
  1374. void
  1375. fbsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
  1376. target_waitkind fork_kind, bool follow_child,
  1377. bool detach_fork)
  1378. {
  1379. inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
  1380. follow_child, detach_fork);
  1381. if (!follow_child && detach_fork)
  1382. {
  1383. pid_t child_pid = child_ptid.pid ();
  1384. /* Breakpoints have already been detached from the child by
  1385. infrun.c. */
  1386. if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
  1387. perror_with_name (("ptrace (PT_DETACH)"));
  1388. #ifndef PTRACE_VFORK
  1389. if (fork_kind () == TARGET_WAITKIND_VFORKED)
  1390. {
  1391. /* We can't insert breakpoints until the child process has
  1392. finished with the shared memory region. The parent
  1393. process doesn't wait for the child process to exit or
  1394. exec until after it has been resumed from the ptrace stop
  1395. to report the fork. Once it has been resumed it doesn't
  1396. stop again before returning to userland, so there is no
  1397. reliable way to wait on the parent.
  1398. We can't stay attached to the child to wait for an exec
  1399. or exit because it may invoke ptrace(PT_TRACE_ME)
  1400. (e.g. if the parent process is a debugger forking a new
  1401. child process).
  1402. In the end, the best we can do is to make sure it runs
  1403. for a little while. Hopefully it will be out of range of
  1404. any breakpoints we reinsert. Usually this is only the
  1405. single-step breakpoint at vfork's return point. */
  1406. usleep (10000);
  1407. /* Schedule a fake VFORK_DONE event to report on the next
  1408. wait. */
  1409. fbsd_add_vfork_done (inferior_ptid);
  1410. }
  1411. #endif
  1412. }
  1413. }
  1414. int
  1415. fbsd_nat_target::insert_fork_catchpoint (int pid)
  1416. {
  1417. return 0;
  1418. }
  1419. int
  1420. fbsd_nat_target::remove_fork_catchpoint (int pid)
  1421. {
  1422. return 0;
  1423. }
  1424. int
  1425. fbsd_nat_target::insert_vfork_catchpoint (int pid)
  1426. {
  1427. return 0;
  1428. }
  1429. int
  1430. fbsd_nat_target::remove_vfork_catchpoint (int pid)
  1431. {
  1432. return 0;
  1433. }
  1434. #endif
  1435. /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
  1436. void
  1437. fbsd_nat_target::post_startup_inferior (ptid_t pid)
  1438. {
  1439. fbsd_enable_proc_events (pid.pid ());
  1440. }
  1441. /* Implement the "post_attach" target_ops method. */
  1442. void
  1443. fbsd_nat_target::post_attach (int pid)
  1444. {
  1445. fbsd_enable_proc_events (pid);
  1446. fbsd_add_threads (this, pid);
  1447. }
  1448. /* Traced processes always stop after exec. */
  1449. int
  1450. fbsd_nat_target::insert_exec_catchpoint (int pid)
  1451. {
  1452. return 0;
  1453. }
  1454. int
  1455. fbsd_nat_target::remove_exec_catchpoint (int pid)
  1456. {
  1457. return 0;
  1458. }
  1459. #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
  1460. int
  1461. fbsd_nat_target::set_syscall_catchpoint (int pid, bool needed,
  1462. int any_count,
  1463. gdb::array_view<const int> syscall_counts)
  1464. {
  1465. /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
  1466. will catch all system call entries and exits. The system calls
  1467. are filtered by GDB rather than the kernel. */
  1468. return 0;
  1469. }
  1470. #endif
  1471. bool
  1472. fbsd_nat_target::supports_multi_process ()
  1473. {
  1474. return true;
  1475. }
  1476. bool
  1477. fbsd_nat_target::supports_disable_randomization ()
  1478. {
  1479. #ifdef PROC_ASLR_CTL
  1480. return true;
  1481. #else
  1482. return false;
  1483. #endif
  1484. }
  1485. /* See fbsd-nat.h. */
  1486. bool
  1487. fbsd_nat_target::fetch_register_set (struct regcache *regcache, int regnum,
  1488. int fetch_op, const struct regset *regset,
  1489. void *regs, size_t size)
  1490. {
  1491. const struct regcache_map_entry *map
  1492. = (const struct regcache_map_entry *) regset->regmap;
  1493. pid_t pid = get_ptrace_pid (regcache->ptid ());
  1494. if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
  1495. size))
  1496. {
  1497. if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
  1498. perror_with_name (_("Couldn't get registers"));
  1499. regcache->supply_regset (regset, regnum, regs, size);
  1500. return true;
  1501. }
  1502. return false;
  1503. }
  1504. /* See fbsd-nat.h. */
  1505. bool
  1506. fbsd_nat_target::store_register_set (struct regcache *regcache, int regnum,
  1507. int fetch_op, int store_op,
  1508. const struct regset *regset, void *regs,
  1509. size_t size)
  1510. {
  1511. const struct regcache_map_entry *map
  1512. = (const struct regcache_map_entry *) regset->regmap;
  1513. pid_t pid = get_ptrace_pid (regcache->ptid ());
  1514. if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
  1515. size))
  1516. {
  1517. if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
  1518. perror_with_name (_("Couldn't get registers"));
  1519. regcache->collect_regset (regset, regnum, regs, size);
  1520. if (ptrace (store_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
  1521. perror_with_name (_("Couldn't write registers"));
  1522. return true;
  1523. }
  1524. return false;
  1525. }
  1526. /* See fbsd-nat.h. */
  1527. bool
  1528. fbsd_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
  1529. {
  1530. struct ptrace_lwpinfo pl;
  1531. pid_t pid = get_ptrace_pid (ptid);
  1532. if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
  1533. return false;
  1534. if (!(pl.pl_flags & PL_FLAG_SI))
  1535. return false;;
  1536. *siginfo = pl.pl_siginfo;
  1537. return (true);
  1538. }
  1539. void _initialize_fbsd_nat ();
  1540. void
  1541. _initialize_fbsd_nat ()
  1542. {
  1543. add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
  1544. &debug_fbsd_lwp, _("\
  1545. Set debugging of FreeBSD lwp module."), _("\
  1546. Show debugging of FreeBSD lwp module."), _("\
  1547. Enables printf debugging output."),
  1548. NULL,
  1549. &show_fbsd_lwp_debug,
  1550. &setdebuglist, &showdebuglist);
  1551. add_setshow_boolean_cmd ("fbsd-nat", class_maintenance,
  1552. &debug_fbsd_nat, _("\
  1553. Set debugging of FreeBSD native target."), _("\
  1554. Show debugging of FreeBSD native target."), _("\
  1555. Enables printf debugging output."),
  1556. NULL,
  1557. &show_fbsd_nat_debug,
  1558. &setdebuglist, &showdebuglist);
  1559. /* Install a SIGCHLD handler. */
  1560. signal (SIGCHLD, sigchld_handler);
  1561. }