callback.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /* Remote target callback routines.
  2. Copyright 1995-2015 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This file provides a standard way for targets to talk to the host OS
  16. level. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "cconfig.h"
  19. #endif
  20. #include "config.h"
  21. #include "ansidecl.h"
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. #include <string.h>
  29. #else
  30. #ifdef HAVE_STRINGS_H
  31. #include <strings.h>
  32. #endif
  33. #endif
  34. #ifdef HAVE_LIMITS_H
  35. /* For PIPE_BUF. */
  36. #include <limits.h>
  37. #endif
  38. #include <errno.h>
  39. #include <fcntl.h>
  40. #include <time.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include "gdb/callback.h"
  44. #include "targ-vals.h"
  45. /* For xmalloc. */
  46. #include "libiberty.h"
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h>
  49. #endif
  50. #ifndef PIPE_BUF
  51. #define PIPE_BUF 512
  52. #endif
  53. /* ??? sim_cb_printf should be cb_printf, but until the callback support is
  54. broken out of the simulator directory, these are here to not require
  55. sim-utils.h. */
  56. void sim_cb_printf (host_callback *, const char *, ...);
  57. void sim_cb_eprintf (host_callback *, const char *, ...);
  58. extern CB_TARGET_DEFS_MAP cb_init_syscall_map[];
  59. extern CB_TARGET_DEFS_MAP cb_init_errno_map[];
  60. extern CB_TARGET_DEFS_MAP cb_init_open_map[];
  61. /* Set the callback copy of errno from what we see now. */
  62. static int
  63. wrap (host_callback *p, int val)
  64. {
  65. p->last_errno = errno;
  66. return val;
  67. }
  68. /* Make sure the FD provided is ok. If not, return non-zero
  69. and set errno. */
  70. static int
  71. fdbad (host_callback *p, int fd)
  72. {
  73. if (fd < 0 || fd > MAX_CALLBACK_FDS || p->fd_buddy[fd] < 0)
  74. {
  75. p->last_errno = EBADF;
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. static int
  81. fdmap (host_callback *p, int fd)
  82. {
  83. return p->fdmap[fd];
  84. }
  85. static int
  86. os_close (host_callback *p, int fd)
  87. {
  88. int result;
  89. int i, next;
  90. result = fdbad (p, fd);
  91. if (result)
  92. return result;
  93. /* If this file descripter has one or more buddies (originals /
  94. duplicates from a dup), just remove it from the circular list. */
  95. for (i = fd; (next = p->fd_buddy[i]) != fd; )
  96. i = next;
  97. if (fd != i)
  98. p->fd_buddy[i] = p->fd_buddy[fd];
  99. else
  100. {
  101. if (p->ispipe[fd])
  102. {
  103. int other = p->ispipe[fd];
  104. int reader, writer;
  105. if (other > 0)
  106. {
  107. /* Closing the read side. */
  108. reader = fd;
  109. writer = other;
  110. }
  111. else
  112. {
  113. /* Closing the write side. */
  114. writer = fd;
  115. reader = -other;
  116. }
  117. /* If there was data in the buffer, make a last "now empty"
  118. call, then deallocate data. */
  119. if (p->pipe_buffer[writer].buffer != NULL)
  120. {
  121. (*p->pipe_empty) (p, reader, writer);
  122. free (p->pipe_buffer[writer].buffer);
  123. p->pipe_buffer[writer].buffer = NULL;
  124. }
  125. /* Clear pipe data for this side. */
  126. p->pipe_buffer[fd].size = 0;
  127. p->ispipe[fd] = 0;
  128. /* If this was the first close, mark the other side as the
  129. only remaining side. */
  130. if (fd != abs (other))
  131. p->ispipe[abs (other)] = -other;
  132. p->fd_buddy[fd] = -1;
  133. return 0;
  134. }
  135. result = wrap (p, close (fdmap (p, fd)));
  136. }
  137. p->fd_buddy[fd] = -1;
  138. return result;
  139. }
  140. /* taken from gdb/util.c:notice_quit() - should be in a library */
  141. #if defined(__GO32__) || defined (_MSC_VER)
  142. static int
  143. os_poll_quit (host_callback *p)
  144. {
  145. #if defined(__GO32__)
  146. int kbhit ();
  147. int getkey ();
  148. if (kbhit ())
  149. {
  150. int k = getkey ();
  151. if (k == 1)
  152. {
  153. return 1;
  154. }
  155. else if (k == 2)
  156. {
  157. return 1;
  158. }
  159. else
  160. {
  161. sim_cb_eprintf (p, "CTRL-A to quit, CTRL-B to quit harder\n");
  162. }
  163. }
  164. #endif
  165. #if defined (_MSC_VER)
  166. /* NB - this will not compile! */
  167. int k = win32pollquit ();
  168. if (k == 1)
  169. return 1;
  170. else if (k == 2)
  171. return 1;
  172. #endif
  173. return 0;
  174. }
  175. #else
  176. #define os_poll_quit 0
  177. #endif /* defined(__GO32__) || defined(_MSC_VER) */
  178. static int
  179. os_get_errno (host_callback *p)
  180. {
  181. return cb_host_to_target_errno (p, p->last_errno);
  182. }
  183. static int
  184. os_isatty (host_callback *p, int fd)
  185. {
  186. int result;
  187. result = fdbad (p, fd);
  188. if (result)
  189. return result;
  190. result = wrap (p, isatty (fdmap (p, fd)));
  191. return result;
  192. }
  193. static int
  194. os_lseek (host_callback *p, int fd, long off, int way)
  195. {
  196. int result;
  197. result = fdbad (p, fd);
  198. if (result)
  199. return result;
  200. result = wrap (p, lseek (fdmap (p, fd), off, way));
  201. return result;
  202. }
  203. static int
  204. os_open (host_callback *p, const char *name, int flags)
  205. {
  206. int i;
  207. for (i = 0; i < MAX_CALLBACK_FDS; i++)
  208. {
  209. if (p->fd_buddy[i] < 0)
  210. {
  211. int f = open (name, cb_target_to_host_open (p, flags), 0644);
  212. if (f < 0)
  213. {
  214. p->last_errno = errno;
  215. return f;
  216. }
  217. p->fd_buddy[i] = i;
  218. p->fdmap[i] = f;
  219. return i;
  220. }
  221. }
  222. p->last_errno = EMFILE;
  223. return -1;
  224. }
  225. static int
  226. os_read (host_callback *p, int fd, char *buf, int len)
  227. {
  228. int result;
  229. result = fdbad (p, fd);
  230. if (result)
  231. return result;
  232. if (p->ispipe[fd])
  233. {
  234. int writer = p->ispipe[fd];
  235. /* Can't read from the write-end. */
  236. if (writer < 0)
  237. {
  238. p->last_errno = EBADF;
  239. return -1;
  240. }
  241. /* Nothing to read if nothing is written. */
  242. if (p->pipe_buffer[writer].size == 0)
  243. return 0;
  244. /* Truncate read request size to buffer size minus what's already
  245. read. */
  246. if (len > p->pipe_buffer[writer].size - p->pipe_buffer[fd].size)
  247. len = p->pipe_buffer[writer].size - p->pipe_buffer[fd].size;
  248. memcpy (buf, p->pipe_buffer[writer].buffer + p->pipe_buffer[fd].size,
  249. len);
  250. /* Account for what we just read. */
  251. p->pipe_buffer[fd].size += len;
  252. /* If we've read everything, empty and deallocate the buffer and
  253. signal buffer-empty to client. (This isn't expected to be a
  254. hot path in the simulator, so we don't hold on to the buffer.) */
  255. if (p->pipe_buffer[fd].size == p->pipe_buffer[writer].size)
  256. {
  257. free (p->pipe_buffer[writer].buffer);
  258. p->pipe_buffer[writer].buffer = NULL;
  259. p->pipe_buffer[fd].size = 0;
  260. p->pipe_buffer[writer].size = 0;
  261. (*p->pipe_empty) (p, fd, writer);
  262. }
  263. return len;
  264. }
  265. result = wrap (p, read (fdmap (p, fd), buf, len));
  266. return result;
  267. }
  268. static int
  269. os_read_stdin (host_callback *p, char *buf, int len)
  270. {
  271. return wrap (p, read (0, buf, len));
  272. }
  273. static int
  274. os_write (host_callback *p, int fd, const char *buf, int len)
  275. {
  276. int result;
  277. int real_fd;
  278. result = fdbad (p, fd);
  279. if (result)
  280. return result;
  281. if (p->ispipe[fd])
  282. {
  283. int reader = -p->ispipe[fd];
  284. /* Can't write to the read-end. */
  285. if (reader < 0)
  286. {
  287. p->last_errno = EBADF;
  288. return -1;
  289. }
  290. /* Can't write to pipe with closed read end.
  291. FIXME: We should send a SIGPIPE. */
  292. if (reader == fd)
  293. {
  294. p->last_errno = EPIPE;
  295. return -1;
  296. }
  297. /* As a sanity-check, we bail out it the buffered contents is much
  298. larger than the size of the buffer on the host. We don't want
  299. to run out of memory in the simulator due to a target program
  300. bug if we can help it. Unfortunately, regarding the value that
  301. reaches the simulated program, it's no use returning *less*
  302. than the requested amount, because cb_syscall loops calling
  303. this function until the whole amount is done. */
  304. if (p->pipe_buffer[fd].size + len > 10 * PIPE_BUF)
  305. {
  306. p->last_errno = EFBIG;
  307. return -1;
  308. }
  309. p->pipe_buffer[fd].buffer
  310. = xrealloc (p->pipe_buffer[fd].buffer, p->pipe_buffer[fd].size + len);
  311. memcpy (p->pipe_buffer[fd].buffer + p->pipe_buffer[fd].size,
  312. buf, len);
  313. p->pipe_buffer[fd].size += len;
  314. (*p->pipe_nonempty) (p, reader, fd);
  315. return len;
  316. }
  317. real_fd = fdmap (p, fd);
  318. switch (real_fd)
  319. {
  320. default:
  321. result = wrap (p, write (real_fd, buf, len));
  322. break;
  323. case 1:
  324. result = p->write_stdout (p, buf, len);
  325. break;
  326. case 2:
  327. result = p->write_stderr (p, buf, len);
  328. break;
  329. }
  330. return result;
  331. }
  332. static int
  333. os_write_stdout (host_callback *p ATTRIBUTE_UNUSED, const char *buf, int len)
  334. {
  335. return fwrite (buf, 1, len, stdout);
  336. }
  337. static void
  338. os_flush_stdout (host_callback *p ATTRIBUTE_UNUSED)
  339. {
  340. fflush (stdout);
  341. }
  342. static int
  343. os_write_stderr (host_callback *p ATTRIBUTE_UNUSED, const char *buf, int len)
  344. {
  345. return fwrite (buf, 1, len, stderr);
  346. }
  347. static void
  348. os_flush_stderr (host_callback *p ATTRIBUTE_UNUSED)
  349. {
  350. fflush (stderr);
  351. }
  352. static int
  353. os_rename (host_callback *p, const char *f1, const char *f2)
  354. {
  355. return wrap (p, rename (f1, f2));
  356. }
  357. static int
  358. os_system (host_callback *p, const char *s)
  359. {
  360. return wrap (p, system (s));
  361. }
  362. static long
  363. os_time (host_callback *p, long *t)
  364. {
  365. return wrap (p, time (t));
  366. }
  367. static int
  368. os_unlink (host_callback *p, const char *f1)
  369. {
  370. return wrap (p, unlink (f1));
  371. }
  372. static int
  373. os_stat (host_callback *p, const char *file, struct stat *buf)
  374. {
  375. /* ??? There is an issue of when to translate to the target layout.
  376. One could do that inside this function, or one could have the
  377. caller do it. It's more flexible to let the caller do it, though
  378. I'm not sure the flexibility will ever be useful. */
  379. return wrap (p, stat (file, buf));
  380. }
  381. static int
  382. os_fstat (host_callback *p, int fd, struct stat *buf)
  383. {
  384. if (fdbad (p, fd))
  385. return -1;
  386. if (p->ispipe[fd])
  387. {
  388. #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
  389. time_t t = (*p->time) (p, NULL);
  390. #endif
  391. /* We have to fake the struct stat contents, since the pipe is
  392. made up in the simulator. */
  393. memset (buf, 0, sizeof (*buf));
  394. #ifdef HAVE_STRUCT_STAT_ST_MODE
  395. buf->st_mode = S_IFIFO;
  396. #endif
  397. /* If more accurate tracking than current-time is needed (for
  398. example, on GNU/Linux we get accurate numbers), the p->time
  399. callback (which may be something other than os_time) should
  400. happen for each read and write, and we'd need to keep track of
  401. atime, ctime and mtime. */
  402. #ifdef HAVE_STRUCT_STAT_ST_ATIME
  403. buf->st_atime = t;
  404. #endif
  405. #ifdef HAVE_STRUCT_STAT_ST_CTIME
  406. buf->st_ctime = t;
  407. #endif
  408. #ifdef HAVE_STRUCT_STAT_ST_MTIME
  409. buf->st_mtime = t;
  410. #endif
  411. return 0;
  412. }
  413. /* ??? There is an issue of when to translate to the target layout.
  414. One could do that inside this function, or one could have the
  415. caller do it. It's more flexible to let the caller do it, though
  416. I'm not sure the flexibility will ever be useful. */
  417. return wrap (p, fstat (fdmap (p, fd), buf));
  418. }
  419. static int
  420. os_lstat (host_callback *p, const char *file, struct stat *buf)
  421. {
  422. /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
  423. #ifdef HAVE_LSTAT
  424. return wrap (p, lstat (file, buf));
  425. #else
  426. return wrap (p, stat (file, buf));
  427. #endif
  428. }
  429. static int
  430. os_ftruncate (host_callback *p, int fd, long len)
  431. {
  432. int result;
  433. result = fdbad (p, fd);
  434. if (p->ispipe[fd])
  435. {
  436. p->last_errno = EINVAL;
  437. return -1;
  438. }
  439. if (result)
  440. return result;
  441. #ifdef HAVE_FTRUNCATE
  442. result = wrap (p, ftruncate (fdmap (p, fd), len));
  443. #else
  444. p->last_errno = EINVAL;
  445. result = -1;
  446. #endif
  447. return result;
  448. }
  449. static int
  450. os_truncate (host_callback *p, const char *file, long len)
  451. {
  452. #ifdef HAVE_TRUNCATE
  453. return wrap (p, truncate (file, len));
  454. #else
  455. p->last_errno = EINVAL;
  456. return -1;
  457. #endif
  458. }
  459. static int
  460. os_pipe (host_callback *p, int *filedes)
  461. {
  462. int i;
  463. /* We deliberately don't use fd 0. It's probably stdin anyway. */
  464. for (i = 1; i < MAX_CALLBACK_FDS; i++)
  465. {
  466. int j;
  467. if (p->fd_buddy[i] < 0)
  468. for (j = i + 1; j < MAX_CALLBACK_FDS; j++)
  469. if (p->fd_buddy[j] < 0)
  470. {
  471. /* Found two free fd:s. Set stat to allocated and mark
  472. pipeness. */
  473. p->fd_buddy[i] = i;
  474. p->fd_buddy[j] = j;
  475. p->ispipe[i] = j;
  476. p->ispipe[j] = -i;
  477. filedes[0] = i;
  478. filedes[1] = j;
  479. /* Poison the FD map to make bugs apparent. */
  480. p->fdmap[i] = -1;
  481. p->fdmap[j] = -1;
  482. return 0;
  483. }
  484. }
  485. p->last_errno = EMFILE;
  486. return -1;
  487. }
  488. /* Stub functions for pipe support. They should always be overridden in
  489. targets using the pipe support, but that's up to the target. */
  490. /* Called when the simulator says that the pipe at (reader, writer) is
  491. now empty (so the writer should leave its waiting state). */
  492. static void
  493. os_pipe_empty (host_callback *p, int reader, int writer)
  494. {
  495. }
  496. /* Called when the simulator says the pipe at (reader, writer) is now
  497. non-empty (so the writer should wait). */
  498. static void
  499. os_pipe_nonempty (host_callback *p, int reader, int writer)
  500. {
  501. }
  502. static int
  503. os_shutdown (host_callback *p)
  504. {
  505. int i, next, j;
  506. for (i = 0; i < MAX_CALLBACK_FDS; i++)
  507. {
  508. int do_close = 1;
  509. /* Zero out all pipe state. Don't call callbacks for non-empty
  510. pipes; the target program has likely terminated at this point
  511. or we're called at initialization time. */
  512. p->ispipe[i] = 0;
  513. p->pipe_buffer[i].size = 0;
  514. p->pipe_buffer[i].buffer = NULL;
  515. next = p->fd_buddy[i];
  516. if (next < 0)
  517. continue;
  518. do
  519. {
  520. j = next;
  521. if (j == MAX_CALLBACK_FDS)
  522. do_close = 0;
  523. next = p->fd_buddy[j];
  524. p->fd_buddy[j] = -1;
  525. /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
  526. if (next < 0)
  527. {
  528. p->fd_buddy[i] = -1;
  529. do_close = 0;
  530. break;
  531. }
  532. }
  533. while (j != i);
  534. if (do_close)
  535. close (p->fdmap[i]);
  536. }
  537. return 1;
  538. }
  539. static int
  540. os_init (host_callback *p)
  541. {
  542. int i;
  543. os_shutdown (p);
  544. for (i = 0; i < 3; i++)
  545. {
  546. p->fdmap[i] = i;
  547. p->fd_buddy[i] = i - 1;
  548. }
  549. p->fd_buddy[0] = MAX_CALLBACK_FDS;
  550. p->fd_buddy[MAX_CALLBACK_FDS] = 2;
  551. p->syscall_map = cb_init_syscall_map;
  552. p->errno_map = cb_init_errno_map;
  553. p->open_map = cb_init_open_map;
  554. return 1;
  555. }
  556. /* DEPRECATED */
  557. /* VARARGS */
  558. static void
  559. os_printf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)
  560. {
  561. va_list args;
  562. va_start (args, format);
  563. vfprintf (stdout, format, args);
  564. va_end (args);
  565. }
  566. /* VARARGS */
  567. static void
  568. os_vprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)
  569. {
  570. vprintf (format, args);
  571. }
  572. /* VARARGS */
  573. static void
  574. os_evprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)
  575. {
  576. vfprintf (stderr, format, args);
  577. }
  578. /* VARARGS */
  579. #ifdef __GNUC__
  580. __attribute__ ((__noreturn__))
  581. #endif
  582. static void
  583. os_error (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)
  584. {
  585. va_list args;
  586. va_start (args, format);
  587. vfprintf (stderr, format, args);
  588. fprintf (stderr, "\n");
  589. va_end (args);
  590. exit (1);
  591. }
  592. host_callback default_callback =
  593. {
  594. os_close,
  595. os_get_errno,
  596. os_isatty,
  597. os_lseek,
  598. os_open,
  599. os_read,
  600. os_read_stdin,
  601. os_rename,
  602. os_system,
  603. os_time,
  604. os_unlink,
  605. os_write,
  606. os_write_stdout,
  607. os_flush_stdout,
  608. os_write_stderr,
  609. os_flush_stderr,
  610. os_stat,
  611. os_fstat,
  612. os_lstat,
  613. os_ftruncate,
  614. os_truncate,
  615. os_pipe,
  616. os_pipe_empty,
  617. os_pipe_nonempty,
  618. os_poll_quit,
  619. os_shutdown,
  620. os_init,
  621. os_printf_filtered, /* deprecated */
  622. os_vprintf_filtered,
  623. os_evprintf_filtered,
  624. os_error,
  625. 0, /* last errno */
  626. { 0, }, /* fdmap */
  627. { -1, }, /* fd_buddy */
  628. { 0, }, /* ispipe */
  629. { { 0, 0 }, }, /* pipe_buffer */
  630. 0, /* syscall_map */
  631. 0, /* errno_map */
  632. 0, /* open_map */
  633. 0, /* signal_map */
  634. 0, /* stat_map */
  635. /* Defaults expected to be overridden at initialization, where needed. */
  636. BFD_ENDIAN_UNKNOWN, /* target_endian */
  637. 4, /* target_sizeof_int */
  638. HOST_CALLBACK_MAGIC,
  639. };
  640. /* Read in a file describing the target's system call values.
  641. E.g. maybe someone will want to use something other than newlib.
  642. This assumes that the basic system call recognition and value passing/
  643. returning is supported. So maybe some coding/recompilation will be
  644. necessary, but not as much.
  645. If an error occurs, the existing mapping is not changed. */
  646. CB_RC
  647. cb_read_target_syscall_maps (host_callback *cb, const char *file)
  648. {
  649. CB_TARGET_DEFS_MAP *syscall_map, *errno_map, *open_map, *signal_map;
  650. const char *stat_map;
  651. FILE *f;
  652. if ((f = fopen (file, "r")) == NULL)
  653. return CB_RC_ACCESS;
  654. /* ... read in and parse file ... */
  655. fclose (f);
  656. return CB_RC_NO_MEM; /* FIXME:wip */
  657. /* Free storage allocated for any existing maps. */
  658. if (cb->syscall_map)
  659. free (cb->syscall_map);
  660. if (cb->errno_map)
  661. free (cb->errno_map);
  662. if (cb->open_map)
  663. free (cb->open_map);
  664. if (cb->signal_map)
  665. free (cb->signal_map);
  666. if (cb->stat_map)
  667. free ((PTR) cb->stat_map);
  668. cb->syscall_map = syscall_map;
  669. cb->errno_map = errno_map;
  670. cb->open_map = open_map;
  671. cb->signal_map = signal_map;
  672. cb->stat_map = stat_map;
  673. return CB_RC_OK;
  674. }
  675. /* General utility functions to search a map for a value. */
  676. static const CB_TARGET_DEFS_MAP *
  677. cb_target_map_entry (const CB_TARGET_DEFS_MAP map[], int target_val)
  678. {
  679. const CB_TARGET_DEFS_MAP *m;
  680. for (m = &map[0]; m->target_val != -1; ++m)
  681. if (m->target_val == target_val)
  682. return m;
  683. return NULL;
  684. }
  685. static const CB_TARGET_DEFS_MAP *
  686. cb_host_map_entry (const CB_TARGET_DEFS_MAP map[], int host_val)
  687. {
  688. const CB_TARGET_DEFS_MAP *m;
  689. for (m = &map[0]; m->host_val != -1; ++m)
  690. if (m->host_val == host_val)
  691. return m;
  692. return NULL;
  693. }
  694. /* Translate the target's version of a syscall number to the host's.
  695. This isn't actually the host's version, rather a canonical form.
  696. ??? Perhaps this should be renamed to ..._canon_syscall. */
  697. int
  698. cb_target_to_host_syscall (host_callback *cb, int target_val)
  699. {
  700. const CB_TARGET_DEFS_MAP *m =
  701. cb_target_map_entry (cb->syscall_map, target_val);
  702. return m ? m->host_val : -1;
  703. }
  704. /* FIXME: sort tables if large.
  705. Alternatively, an obvious improvement for errno conversion is
  706. to machine generate a function with a large switch(). */
  707. /* Translate the host's version of errno to the target's. */
  708. int
  709. cb_host_to_target_errno (host_callback *cb, int host_val)
  710. {
  711. const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->errno_map, host_val);
  712. /* ??? Which error to return in this case is up for grabs.
  713. Note that some missing values may have standard alternatives.
  714. For now return 0 and require caller to deal with it. */
  715. return m ? m->target_val : 0;
  716. }
  717. /* Given a set of target bitmasks for the open system call,
  718. return the host equivalent.
  719. Mapping open flag values is best done by looping so there's no need
  720. to machine generate this function. */
  721. int
  722. cb_target_to_host_open (host_callback *cb, int target_val)
  723. {
  724. int host_val = 0;
  725. CB_TARGET_DEFS_MAP *m;
  726. for (m = &cb->open_map[0]; m->host_val != -1; ++m)
  727. {
  728. switch (m->target_val)
  729. {
  730. /* O_RDONLY can be (and usually is) 0 which needs to be treated
  731. specially. */
  732. case TARGET_O_RDONLY :
  733. case TARGET_O_WRONLY :
  734. case TARGET_O_RDWR :
  735. if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR))
  736. == m->target_val)
  737. host_val |= m->host_val;
  738. /* Handle the host/target differentiating between binary and
  739. text mode. Only one case is of importance */
  740. #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
  741. host_val |= O_BINARY;
  742. #endif
  743. break;
  744. default :
  745. if ((m->target_val & target_val) == m->target_val)
  746. host_val |= m->host_val;
  747. break;
  748. }
  749. }
  750. return host_val;
  751. }
  752. /* Utility for e.g. cb_host_to_target_stat to store values in the target's
  753. stat struct.
  754. ??? The "val" must be as big as target word size. */
  755. void
  756. cb_store_target_endian (host_callback *cb, char *p, int size, long val)
  757. {
  758. if (cb->target_endian == BFD_ENDIAN_BIG)
  759. {
  760. p += size;
  761. while (size-- > 0)
  762. {
  763. *--p = val;
  764. val >>= 8;
  765. }
  766. }
  767. else
  768. {
  769. while (size-- > 0)
  770. {
  771. *p++ = val;
  772. val >>= 8;
  773. }
  774. }
  775. }
  776. /* Translate a host's stat struct into a target's.
  777. If HS is NULL, just compute the length of the buffer required,
  778. TS is ignored.
  779. The result is the size of the target's stat struct,
  780. or zero if an error occurred during the translation. */
  781. int
  782. cb_host_to_target_stat (host_callback *cb, const struct stat *hs, PTR ts)
  783. {
  784. const char *m = cb->stat_map;
  785. char *p;
  786. if (hs == NULL)
  787. ts = NULL;
  788. p = ts;
  789. while (m)
  790. {
  791. char *q = strchr (m, ',');
  792. int size;
  793. /* FIXME: Use sscanf? */
  794. if (q == NULL)
  795. {
  796. /* FIXME: print error message */
  797. return 0;
  798. }
  799. size = atoi (q + 1);
  800. if (size == 0)
  801. {
  802. /* FIXME: print error message */
  803. return 0;
  804. }
  805. if (hs != NULL)
  806. {
  807. if (0)
  808. ;
  809. /* Defined here to avoid emacs indigestion on a lone "else". */
  810. #undef ST_x
  811. #define ST_x(FLD) \
  812. else if (strncmp (m, #FLD, q - m) == 0) \
  813. cb_store_target_endian (cb, p, size, hs->FLD)
  814. #ifdef HAVE_STRUCT_STAT_ST_DEV
  815. ST_x (st_dev);
  816. #endif
  817. #ifdef HAVE_STRUCT_STAT_ST_INO
  818. ST_x (st_ino);
  819. #endif
  820. #ifdef HAVE_STRUCT_STAT_ST_MODE
  821. ST_x (st_mode);
  822. #endif
  823. #ifdef HAVE_STRUCT_STAT_ST_NLINK
  824. ST_x (st_nlink);
  825. #endif
  826. #ifdef HAVE_STRUCT_STAT_ST_UID
  827. ST_x (st_uid);
  828. #endif
  829. #ifdef HAVE_STRUCT_STAT_ST_GID
  830. ST_x (st_gid);
  831. #endif
  832. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  833. ST_x (st_rdev);
  834. #endif
  835. #ifdef HAVE_STRUCT_STAT_ST_SIZE
  836. ST_x (st_size);
  837. #endif
  838. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  839. ST_x (st_blksize);
  840. #endif
  841. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  842. ST_x (st_blocks);
  843. #endif
  844. #ifdef HAVE_STRUCT_STAT_ST_ATIME
  845. ST_x (st_atime);
  846. #endif
  847. #ifdef HAVE_STRUCT_STAT_ST_MTIME
  848. ST_x (st_mtime);
  849. #endif
  850. #ifdef HAVE_STRUCT_STAT_ST_CTIME
  851. ST_x (st_ctime);
  852. #endif
  853. #undef ST_x
  854. /* FIXME:wip */
  855. else
  856. /* Unsupported field, store 0. */
  857. cb_store_target_endian (cb, p, size, 0);
  858. }
  859. p += size;
  860. m = strchr (q, ':');
  861. if (m)
  862. ++m;
  863. }
  864. return p - (char *) ts;
  865. }
  866. /* Cover functions to the vfprintf callbacks.
  867. ??? If one thinks of the callbacks as a subsystem onto itself [or part of
  868. a larger "remote target subsystem"] with a well defined interface, then
  869. one would think that the subsystem would provide these. However, until
  870. one is allowed to create such a subsystem (with its own source tree
  871. independent of any particular user), such a critter can't exist. Thus
  872. these functions are here for the time being. */
  873. void
  874. sim_cb_printf (host_callback *p, const char *fmt, ...)
  875. {
  876. va_list ap;
  877. va_start (ap, fmt);
  878. p->vprintf_filtered (p, fmt, ap);
  879. va_end (ap);
  880. }
  881. void
  882. sim_cb_eprintf (host_callback *p, const char *fmt, ...)
  883. {
  884. va_list ap;
  885. va_start (ap, fmt);
  886. p->evprintf_filtered (p, fmt, ap);
  887. va_end (ap);
  888. }
  889. int
  890. cb_is_stdin (host_callback *cb, int fd)
  891. {
  892. return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 0;
  893. }
  894. int
  895. cb_is_stdout (host_callback *cb, int fd)
  896. {
  897. return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 1;
  898. }
  899. int
  900. cb_is_stderr (host_callback *cb, int fd)
  901. {
  902. return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 2;
  903. }
  904. const char *
  905. cb_host_str_syscall (host_callback *cb, int host_val)
  906. {
  907. const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->syscall_map, host_val);
  908. return m ? m->name : NULL;
  909. }
  910. const char *
  911. cb_host_str_errno (host_callback *cb, int host_val)
  912. {
  913. const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->errno_map, host_val);
  914. return m ? m->name : NULL;
  915. }
  916. const char *
  917. cb_host_str_signal (host_callback *cb, int host_val)
  918. {
  919. const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->signal_map, host_val);
  920. return m ? m->name : NULL;
  921. }
  922. const char *
  923. cb_target_str_syscall (host_callback *cb, int target_val)
  924. {
  925. const CB_TARGET_DEFS_MAP *m =
  926. cb_target_map_entry (cb->syscall_map, target_val);
  927. return m ? m->name : NULL;
  928. }
  929. const char *
  930. cb_target_str_errno (host_callback *cb, int target_val)
  931. {
  932. const CB_TARGET_DEFS_MAP *m =
  933. cb_target_map_entry (cb->errno_map, target_val);
  934. return m ? m->name : NULL;
  935. }
  936. const char *
  937. cb_target_str_signal (host_callback *cb, int target_val)
  938. {
  939. const CB_TARGET_DEFS_MAP *m =
  940. cb_target_map_entry (cb->signal_map, target_val);
  941. return m ? m->name : NULL;
  942. }