proc-api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Machine independent support for Solaris /proc (process file system) for GDB.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Michael Snyder at Cygnus Solutions.
  4. Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /*
  17. * Pretty-print trace of api calls to the /proc api
  18. */
  19. #include "defs.h"
  20. #include "gdbcmd.h"
  21. #include "completer.h"
  22. #include <sys/types.h>
  23. #include <sys/procfs.h>
  24. #include <sys/proc.h> /* for struct proc */
  25. #include <sys/user.h> /* for struct user */
  26. #include <fcntl.h> /* for O_RDWR etc. */
  27. #include "gdbsupport/gdb_wait.h"
  28. #include "proc-utils.h"
  29. /* Much of the information used in the /proc interface, particularly for
  30. printing status information, is kept as tables of structures of the
  31. following form. These tables can be used to map numeric values to
  32. their symbolic names and to a string that describes their specific use. */
  33. struct trans {
  34. long value; /* The numeric value */
  35. const char *name; /* The equivalent symbolic value */
  36. const char *desc; /* Short description of value */
  37. };
  38. static bool procfs_trace = false;
  39. static FILE *procfs_file = NULL;
  40. static std::string procfs_filename = "procfs_trace";
  41. static void
  42. prepare_to_trace (void)
  43. {
  44. if (procfs_trace) /* if procfs tracing turned on */
  45. if (procfs_file == NULL) /* if output file not yet open */
  46. procfs_file = fopen (procfs_filename.c_str (), "a"); /* open output file */
  47. }
  48. static void
  49. set_procfs_trace_cmd (const char *args,
  50. int from_tty, struct cmd_list_element *c)
  51. {
  52. #if 0 /* not sure what I might actually need to do here, if anything */
  53. if (procfs_file)
  54. fflush (procfs_file);
  55. #endif
  56. }
  57. static void
  58. set_procfs_file_cmd (const char *args,
  59. int from_tty, struct cmd_list_element *c)
  60. {
  61. /* Just changed the filename for procfs tracing.
  62. If a file was already open, close it. */
  63. if (procfs_file)
  64. fclose (procfs_file);
  65. procfs_file = NULL;
  66. }
  67. static struct trans rw_table[] = {
  68. { PCAGENT, "PCAGENT", "create agent lwp with regs from argument" },
  69. { PCCFAULT, "PCCFAULT", "clear current fault" },
  70. { PCCSIG, "PCCSIG", "clear current signal" },
  71. { PCDSTOP, "PCDSTOP", "post stop request" },
  72. { PCKILL, "PCKILL", "post a signal" },
  73. { PCNICE, "PCNICE", "set nice priority" },
  74. { PCREAD, "PCREAD", "read from the address space" },
  75. { PCWRITE, "PCWRITE", "write to the address space" },
  76. { PCRUN, "PCRUN", "make process/lwp runnable" },
  77. { PCSASRS, "PCSASRS", "set ancillary state registers" },
  78. { PCSCRED, "PCSCRED", "set process credentials" },
  79. { PCSENTRY, "PCSENTRY", "set traced syscall entry set" },
  80. { PCSET, "PCSET", "set modes" },
  81. { PCSEXIT, "PCSEXIT", "set traced syscall exit set" },
  82. { PCSFAULT, "PCSFAULT", "set traced fault set" },
  83. { PCSFPREG, "PCSFPREG", "set floating point registers" },
  84. { PCSHOLD, "PCSHOLD", "set signal mask" },
  85. { PCSREG, "PCSREG", "set general registers" },
  86. { PCSSIG, "PCSSIG", "set current signal" },
  87. { PCSTOP, "PCSTOP", "post stop request and wait" },
  88. { PCSTRACE, "PCSTRACE", "set traced signal set" },
  89. { PCSVADDR, "PCSVADDR", "set pc virtual address" },
  90. { PCSXREG, "PCSXREG", "set extra registers" },
  91. { PCTWSTOP, "PCTWSTOP", "wait for stop, with timeout arg" },
  92. { PCUNKILL, "PCUNKILL", "delete a pending signal" },
  93. { PCUNSET, "PCUNSET", "unset modes" },
  94. { PCWATCH, "PCWATCH", "set/unset watched memory area" },
  95. { PCWSTOP, "PCWSTOP", "wait for process/lwp to stop, no timeout" },
  96. { 0, NULL, NULL }
  97. };
  98. static off_t lseek_offset;
  99. int
  100. write_with_trace (int fd, void *varg, size_t len, char *file, int line)
  101. {
  102. int i = ARRAY_SIZE (rw_table) - 1;
  103. int ret;
  104. procfs_ctl_t *arg = (procfs_ctl_t *) varg;
  105. prepare_to_trace ();
  106. if (procfs_trace)
  107. {
  108. procfs_ctl_t opcode = arg[0];
  109. for (i = 0; rw_table[i].name != NULL; i++)
  110. if (rw_table[i].value == opcode)
  111. break;
  112. if (info_verbose)
  113. fprintf (procfs_file ? procfs_file : stdout,
  114. "%s:%d -- ", file, line);
  115. switch (opcode) {
  116. case PCSET:
  117. fprintf (procfs_file ? procfs_file : stdout,
  118. "write (PCSET, %s) %s\n",
  119. arg[1] == PR_FORK ? "PR_FORK" :
  120. arg[1] == PR_RLC ? "PR_RLC" :
  121. arg[1] == PR_ASYNC ? "PR_ASYNC" :
  122. "<unknown flag>",
  123. info_verbose ? rw_table[i].desc : "");
  124. break;
  125. case PCUNSET:
  126. fprintf (procfs_file ? procfs_file : stdout,
  127. "write (PCRESET, %s) %s\n",
  128. arg[1] == PR_FORK ? "PR_FORK" :
  129. arg[1] == PR_RLC ? "PR_RLC" :
  130. arg[1] == PR_ASYNC ? "PR_ASYNC" :
  131. "<unknown flag>",
  132. info_verbose ? rw_table[i].desc : "");
  133. break;
  134. case PCSTRACE:
  135. fprintf (procfs_file ? procfs_file : stdout,
  136. "write (PCSTRACE) ");
  137. proc_prettyfprint_signalset (procfs_file ? procfs_file : stdout,
  138. (sigset_t *) &arg[1], 0);
  139. break;
  140. case PCSFAULT:
  141. fprintf (procfs_file ? procfs_file : stdout,
  142. "write (PCSFAULT) ");
  143. proc_prettyfprint_faultset (procfs_file ? procfs_file : stdout,
  144. (fltset_t *) &arg[1], 0);
  145. break;
  146. case PCSENTRY:
  147. fprintf (procfs_file ? procfs_file : stdout,
  148. "write (PCSENTRY) ");
  149. proc_prettyfprint_syscalls (procfs_file ? procfs_file : stdout,
  150. (sysset_t *) &arg[1], 0);
  151. break;
  152. case PCSEXIT:
  153. fprintf (procfs_file ? procfs_file : stdout,
  154. "write (PCSEXIT) ");
  155. proc_prettyfprint_syscalls (procfs_file ? procfs_file : stdout,
  156. (sysset_t *) &arg[1], 0);
  157. break;
  158. case PCSHOLD:
  159. fprintf (procfs_file ? procfs_file : stdout,
  160. "write (PCSHOLD) ");
  161. proc_prettyfprint_signalset (procfs_file ? procfs_file : stdout,
  162. (sigset_t *) &arg[1], 0);
  163. break;
  164. case PCSSIG:
  165. fprintf (procfs_file ? procfs_file : stdout,
  166. "write (PCSSIG) ");
  167. proc_prettyfprint_signal (procfs_file ? procfs_file : stdout,
  168. arg[1] ? ((siginfo_t *) &arg[1])->si_signo
  169. : 0,
  170. 0);
  171. fprintf (procfs_file ? procfs_file : stdout, "\n");
  172. break;
  173. case PCRUN:
  174. fprintf (procfs_file ? procfs_file : stdout,
  175. "write (PCRUN) ");
  176. if (arg[1] & PRCSIG)
  177. fprintf (procfs_file ? procfs_file : stdout, "clearSig ");
  178. if (arg[1] & PRCFAULT)
  179. fprintf (procfs_file ? procfs_file : stdout, "clearFlt ");
  180. if (arg[1] & PRSTEP)
  181. fprintf (procfs_file ? procfs_file : stdout, "step ");
  182. if (arg[1] & PRSABORT)
  183. fprintf (procfs_file ? procfs_file : stdout, "syscallAbort ");
  184. if (arg[1] & PRSTOP)
  185. fprintf (procfs_file ? procfs_file : stdout, "stopReq ");
  186. fprintf (procfs_file ? procfs_file : stdout, "\n");
  187. break;
  188. case PCKILL:
  189. fprintf (procfs_file ? procfs_file : stdout,
  190. "write (PCKILL) ");
  191. proc_prettyfprint_signal (procfs_file ? procfs_file : stdout,
  192. arg[1], 0);
  193. fprintf (procfs_file ? procfs_file : stdout, "\n");
  194. break;
  195. default:
  196. {
  197. if (rw_table[i].name)
  198. fprintf (procfs_file ? procfs_file : stdout,
  199. "write (%s) %s\n",
  200. rw_table[i].name,
  201. info_verbose ? rw_table[i].desc : "");
  202. else
  203. {
  204. if (lseek_offset != -1)
  205. fprintf (procfs_file ? procfs_file : stdout,
  206. "write (<unknown>, %lud bytes at 0x%08lx) \n",
  207. (unsigned long) len, (unsigned long) lseek_offset);
  208. else
  209. fprintf (procfs_file ? procfs_file : stdout,
  210. "write (<unknown>, %lud bytes) \n",
  211. (unsigned long) len);
  212. }
  213. break;
  214. }
  215. }
  216. if (procfs_file)
  217. fflush (procfs_file);
  218. }
  219. errno = 0;
  220. ret = write (fd, (void *) arg, len);
  221. if (procfs_trace && ret != len)
  222. {
  223. fprintf (procfs_file ? procfs_file : stdout,
  224. "[write (%s) FAILED! (%s)]\n",
  225. rw_table[i].name != NULL ?
  226. rw_table[i].name : "<unknown>",
  227. safe_strerror (errno));
  228. if (procfs_file)
  229. fflush (procfs_file);
  230. }
  231. lseek_offset = -1;
  232. return ret;
  233. }
  234. off_t
  235. lseek_with_trace (int fd, off_t offset, int whence, char *file, int line)
  236. {
  237. off_t ret;
  238. prepare_to_trace ();
  239. errno = 0;
  240. ret = lseek (fd, offset, whence);
  241. lseek_offset = ret;
  242. if (procfs_trace && (ret == -1 || errno != 0))
  243. {
  244. fprintf (procfs_file ? procfs_file : stdout,
  245. "[lseek (0x%08lx) FAILED! (%s)]\n",
  246. (unsigned long) offset, safe_strerror (errno));
  247. if (procfs_file)
  248. fflush (procfs_file);
  249. }
  250. return ret;
  251. }
  252. int
  253. open_with_trace (char *filename, int mode, char *file, int line)
  254. {
  255. int ret;
  256. prepare_to_trace ();
  257. errno = 0;
  258. ret = open (filename, mode);
  259. if (procfs_trace)
  260. {
  261. if (info_verbose)
  262. fprintf (procfs_file ? procfs_file : stdout,
  263. "%s:%d -- ", file, line);
  264. if (errno)
  265. {
  266. fprintf (procfs_file ? procfs_file : stdout,
  267. "[open FAILED! (%s) line %d]\\n",
  268. safe_strerror (errno), line);
  269. }
  270. else
  271. {
  272. fprintf (procfs_file ? procfs_file : stdout,
  273. "%d = open (%s, ", ret, filename);
  274. if (mode == O_RDONLY)
  275. fprintf (procfs_file ? procfs_file : stdout, "O_RDONLY) %d\n",
  276. line);
  277. else if (mode == O_WRONLY)
  278. fprintf (procfs_file ? procfs_file : stdout, "O_WRONLY) %d\n",
  279. line);
  280. else if (mode == O_RDWR)
  281. fprintf (procfs_file ? procfs_file : stdout, "O_RDWR) %d\n",
  282. line);
  283. }
  284. if (procfs_file)
  285. fflush (procfs_file);
  286. }
  287. return ret;
  288. }
  289. int
  290. close_with_trace (int fd, char *file, int line)
  291. {
  292. int ret;
  293. prepare_to_trace ();
  294. errno = 0;
  295. ret = close (fd);
  296. if (procfs_trace)
  297. {
  298. if (info_verbose)
  299. fprintf (procfs_file ? procfs_file : stdout,
  300. "%s:%d -- ", file, line);
  301. if (errno)
  302. fprintf (procfs_file ? procfs_file : stdout,
  303. "[close FAILED! (%s)]\n", safe_strerror (errno));
  304. else
  305. fprintf (procfs_file ? procfs_file : stdout,
  306. "%d = close (%d)\n", ret, fd);
  307. if (procfs_file)
  308. fflush (procfs_file);
  309. }
  310. return ret;
  311. }
  312. pid_t
  313. wait_with_trace (int *wstat, char *file, int line)
  314. {
  315. int ret, lstat = 0;
  316. prepare_to_trace ();
  317. if (procfs_trace)
  318. {
  319. if (info_verbose)
  320. fprintf (procfs_file ? procfs_file : stdout,
  321. "%s:%d -- ", file, line);
  322. fprintf (procfs_file ? procfs_file : stdout,
  323. "wait (line %d) ", line);
  324. if (procfs_file)
  325. fflush (procfs_file);
  326. }
  327. errno = 0;
  328. ret = wait (&lstat);
  329. if (procfs_trace)
  330. {
  331. if (errno)
  332. fprintf (procfs_file ? procfs_file : stdout,
  333. "[wait FAILED! (%s)]\n", safe_strerror (errno));
  334. else
  335. fprintf (procfs_file ? procfs_file : stdout,
  336. "returned pid %d, status 0x%x\n", ret, lstat);
  337. if (procfs_file)
  338. fflush (procfs_file);
  339. }
  340. if (wstat)
  341. *wstat = lstat;
  342. return ret;
  343. }
  344. void
  345. procfs_note (const char *msg, const char *file, int line)
  346. {
  347. prepare_to_trace ();
  348. if (procfs_trace)
  349. {
  350. if (info_verbose)
  351. fprintf (procfs_file ? procfs_file : stdout,
  352. "%s:%d -- ", file, line);
  353. fprintf (procfs_file ? procfs_file : stdout, "%s", msg);
  354. if (procfs_file)
  355. fflush (procfs_file);
  356. }
  357. }
  358. void
  359. proc_prettyfprint_status (long flags, int why, int what, int thread)
  360. {
  361. prepare_to_trace ();
  362. if (procfs_trace)
  363. {
  364. if (thread)
  365. fprintf (procfs_file ? procfs_file : stdout,
  366. "Thread %d: ", thread);
  367. proc_prettyfprint_flags (procfs_file ? procfs_file : stdout,
  368. flags, 0);
  369. if (flags & (PR_STOPPED | PR_ISTOP))
  370. proc_prettyfprint_why (procfs_file ? procfs_file : stdout,
  371. why, what, 0);
  372. if (procfs_file)
  373. fflush (procfs_file);
  374. }
  375. }
  376. void _initialize_proc_api ();
  377. void
  378. _initialize_proc_api ()
  379. {
  380. add_setshow_boolean_cmd ("procfs-trace", no_class, &procfs_trace, _("\
  381. Set tracing for /proc api calls."), _("\
  382. Show tracing for /proc api calls."), NULL,
  383. set_procfs_trace_cmd,
  384. NULL, /* FIXME: i18n: */
  385. &setlist, &showlist);
  386. add_setshow_filename_cmd ("procfs-file", no_class, &procfs_filename, _("\
  387. Set filename for /proc tracefile."), _("\
  388. Show filename for /proc tracefile."), NULL,
  389. set_procfs_file_cmd,
  390. NULL, /* FIXME: i18n: */
  391. &setlist, &showlist);
  392. }