ser-unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* Serial interface for local (hardwired) serial ports on Un*x like systems
  2. Copyright (C) 1992-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 "serial.h"
  16. #include "ser-base.h"
  17. #include "ser-unix.h"
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include "terminal.h"
  21. #include <sys/socket.h>
  22. #include "gdbsupport/gdb_sys_time.h"
  23. #include "gdbsupport/gdb_select.h"
  24. #include "gdbcmd.h"
  25. #include "gdbsupport/filestuff.h"
  26. #include <termios.h>
  27. #include "gdbsupport/scoped_ignore_sigttou.h"
  28. struct hardwire_ttystate
  29. {
  30. struct termios termios;
  31. };
  32. #ifdef CRTSCTS
  33. /* Boolean to explicitly enable or disable h/w flow control. */
  34. static bool serial_hwflow;
  35. static void
  36. show_serial_hwflow (struct ui_file *file, int from_tty,
  37. struct cmd_list_element *c, const char *value)
  38. {
  39. gdb_printf (file, _("Hardware flow control is %s.\n"), value);
  40. }
  41. #endif
  42. static int hardwire_open (struct serial *scb, const char *name);
  43. static void hardwire_raw (struct serial *scb);
  44. static int rate_to_code (int rate);
  45. static int hardwire_setbaudrate (struct serial *scb, int rate);
  46. static int hardwire_setparity (struct serial *scb, int parity);
  47. static void hardwire_close (struct serial *scb);
  48. static int get_tty_state (struct serial *scb,
  49. struct hardwire_ttystate * state);
  50. static int set_tty_state (struct serial *scb,
  51. struct hardwire_ttystate * state);
  52. static serial_ttystate hardwire_get_tty_state (struct serial *scb);
  53. static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
  54. static void hardwire_print_tty_state (struct serial *, serial_ttystate,
  55. struct ui_file *);
  56. static int hardwire_drain_output (struct serial *);
  57. static int hardwire_flush_output (struct serial *);
  58. static int hardwire_flush_input (struct serial *);
  59. static int hardwire_send_break (struct serial *);
  60. static int hardwire_setstopbits (struct serial *, int);
  61. /* Open up a real live device for serial I/O. */
  62. static int
  63. hardwire_open (struct serial *scb, const char *name)
  64. {
  65. scb->fd = gdb_open_cloexec (name, O_RDWR, 0).release ();
  66. if (scb->fd < 0)
  67. return -1;
  68. return 0;
  69. }
  70. static int
  71. get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
  72. {
  73. if (tcgetattr (scb->fd, &state->termios) < 0)
  74. return -1;
  75. return 0;
  76. }
  77. static int
  78. set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
  79. {
  80. if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
  81. return -1;
  82. return 0;
  83. }
  84. static serial_ttystate
  85. hardwire_get_tty_state (struct serial *scb)
  86. {
  87. struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
  88. if (get_tty_state (scb, state))
  89. {
  90. xfree (state);
  91. return NULL;
  92. }
  93. return (serial_ttystate) state;
  94. }
  95. static serial_ttystate
  96. hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
  97. {
  98. struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
  99. *state = *(struct hardwire_ttystate *) ttystate;
  100. return (serial_ttystate) state;
  101. }
  102. static int
  103. hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
  104. {
  105. struct hardwire_ttystate *state;
  106. state = (struct hardwire_ttystate *) ttystate;
  107. return set_tty_state (scb, state);
  108. }
  109. static void
  110. hardwire_print_tty_state (struct serial *scb,
  111. serial_ttystate ttystate,
  112. struct ui_file *stream)
  113. {
  114. struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
  115. int i;
  116. gdb_printf (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
  117. (int) state->termios.c_iflag,
  118. (int) state->termios.c_oflag);
  119. gdb_printf (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
  120. (int) state->termios.c_cflag,
  121. (int) state->termios.c_lflag);
  122. #if 0
  123. /* This not in POSIX, and is not really documented by those systems
  124. which have it (at least not Sun). */
  125. gdb_printf (stream, "c_line = 0x%x.\n", state->termios.c_line);
  126. #endif
  127. gdb_printf (stream, "c_cc: ");
  128. for (i = 0; i < NCCS; i += 1)
  129. gdb_printf (stream, "0x%x ", state->termios.c_cc[i]);
  130. gdb_printf (stream, "\n");
  131. }
  132. /* Wait for the output to drain away, as opposed to flushing
  133. (discarding) it. */
  134. static int
  135. hardwire_drain_output (struct serial *scb)
  136. {
  137. /* Ignore SIGTTOU which may occur during the drain. */
  138. scoped_ignore_sigttou ignore_sigttou;
  139. return tcdrain (scb->fd);
  140. }
  141. static int
  142. hardwire_flush_output (struct serial *scb)
  143. {
  144. return tcflush (scb->fd, TCOFLUSH);
  145. }
  146. static int
  147. hardwire_flush_input (struct serial *scb)
  148. {
  149. ser_base_flush_input (scb);
  150. return tcflush (scb->fd, TCIFLUSH);
  151. }
  152. static int
  153. hardwire_send_break (struct serial *scb)
  154. {
  155. return tcsendbreak (scb->fd, 0);
  156. }
  157. static void
  158. hardwire_raw (struct serial *scb)
  159. {
  160. struct hardwire_ttystate state;
  161. if (get_tty_state (scb, &state))
  162. gdb_printf (gdb_stderr, "get_tty_state failed: %s\n",
  163. safe_strerror (errno));
  164. state.termios.c_iflag = 0;
  165. state.termios.c_oflag = 0;
  166. state.termios.c_lflag = 0;
  167. state.termios.c_cflag &= ~CSIZE;
  168. state.termios.c_cflag |= CLOCAL | CS8;
  169. #ifdef CRTSCTS
  170. /* h/w flow control. */
  171. if (serial_hwflow)
  172. state.termios.c_cflag |= CRTSCTS;
  173. else
  174. state.termios.c_cflag &= ~CRTSCTS;
  175. #ifdef CRTS_IFLOW
  176. if (serial_hwflow)
  177. state.termios.c_cflag |= CRTS_IFLOW;
  178. else
  179. state.termios.c_cflag &= ~CRTS_IFLOW;
  180. #endif
  181. #endif
  182. state.termios.c_cc[VMIN] = 0;
  183. state.termios.c_cc[VTIME] = 0;
  184. if (set_tty_state (scb, &state))
  185. gdb_printf (gdb_stderr, "set_tty_state failed: %s\n",
  186. safe_strerror (errno));
  187. }
  188. #ifndef B19200
  189. #define B19200 EXTA
  190. #endif
  191. #ifndef B38400
  192. #define B38400 EXTB
  193. #endif
  194. /* Translate baud rates from integers to damn B_codes. Unix should
  195. have outgrown this crap years ago, but even POSIX wouldn't buck it. */
  196. static struct
  197. {
  198. int rate;
  199. int code;
  200. }
  201. baudtab[] =
  202. {
  203. {
  204. 50, B50
  205. }
  206. ,
  207. {
  208. 75, B75
  209. }
  210. ,
  211. {
  212. 110, B110
  213. }
  214. ,
  215. {
  216. 134, B134
  217. }
  218. ,
  219. {
  220. 150, B150
  221. }
  222. ,
  223. {
  224. 200, B200
  225. }
  226. ,
  227. {
  228. 300, B300
  229. }
  230. ,
  231. {
  232. 600, B600
  233. }
  234. ,
  235. {
  236. 1200, B1200
  237. }
  238. ,
  239. {
  240. 1800, B1800
  241. }
  242. ,
  243. {
  244. 2400, B2400
  245. }
  246. ,
  247. {
  248. 4800, B4800
  249. }
  250. ,
  251. {
  252. 9600, B9600
  253. }
  254. ,
  255. {
  256. 19200, B19200
  257. }
  258. ,
  259. {
  260. 38400, B38400
  261. }
  262. ,
  263. #ifdef B57600
  264. {
  265. 57600, B57600
  266. }
  267. ,
  268. #endif
  269. #ifdef B115200
  270. {
  271. 115200, B115200
  272. }
  273. ,
  274. #endif
  275. #ifdef B230400
  276. {
  277. 230400, B230400
  278. }
  279. ,
  280. #endif
  281. #ifdef B460800
  282. {
  283. 460800, B460800
  284. }
  285. ,
  286. #endif
  287. {
  288. -1, -1
  289. }
  290. ,
  291. };
  292. static int
  293. rate_to_code (int rate)
  294. {
  295. int i;
  296. for (i = 0; baudtab[i].rate != -1; i++)
  297. {
  298. /* test for perfect macth. */
  299. if (rate == baudtab[i].rate)
  300. return baudtab[i].code;
  301. else
  302. {
  303. /* check if it is in between valid values. */
  304. if (rate < baudtab[i].rate)
  305. {
  306. if (i)
  307. {
  308. warning (_("Invalid baud rate %d. "
  309. "Closest values are %d and %d."),
  310. rate, baudtab[i - 1].rate, baudtab[i].rate);
  311. }
  312. else
  313. {
  314. warning (_("Invalid baud rate %d. Minimum value is %d."),
  315. rate, baudtab[0].rate);
  316. }
  317. return -1;
  318. }
  319. }
  320. }
  321. /* The requested speed was too large. */
  322. warning (_("Invalid baud rate %d. Maximum value is %d."),
  323. rate, baudtab[i - 1].rate);
  324. return -1;
  325. }
  326. static int
  327. hardwire_setbaudrate (struct serial *scb, int rate)
  328. {
  329. struct hardwire_ttystate state;
  330. int baud_code = rate_to_code (rate);
  331. if (baud_code < 0)
  332. {
  333. /* The baud rate was not valid.
  334. A warning has already been issued. */
  335. errno = EINVAL;
  336. return -1;
  337. }
  338. if (get_tty_state (scb, &state))
  339. return -1;
  340. cfsetospeed (&state.termios, baud_code);
  341. cfsetispeed (&state.termios, baud_code);
  342. return set_tty_state (scb, &state);
  343. }
  344. static int
  345. hardwire_setstopbits (struct serial *scb, int num)
  346. {
  347. struct hardwire_ttystate state;
  348. int newbit;
  349. if (get_tty_state (scb, &state))
  350. return -1;
  351. switch (num)
  352. {
  353. case SERIAL_1_STOPBITS:
  354. newbit = 0;
  355. break;
  356. case SERIAL_1_AND_A_HALF_STOPBITS:
  357. case SERIAL_2_STOPBITS:
  358. newbit = 1;
  359. break;
  360. default:
  361. return 1;
  362. }
  363. if (!newbit)
  364. state.termios.c_cflag &= ~CSTOPB;
  365. else
  366. state.termios.c_cflag |= CSTOPB; /* two bits */
  367. return set_tty_state (scb, &state);
  368. }
  369. /* Implement the "setparity" serial_ops callback. */
  370. static int
  371. hardwire_setparity (struct serial *scb, int parity)
  372. {
  373. struct hardwire_ttystate state;
  374. int newparity = 0;
  375. if (get_tty_state (scb, &state))
  376. return -1;
  377. switch (parity)
  378. {
  379. case GDBPARITY_NONE:
  380. newparity = 0;
  381. break;
  382. case GDBPARITY_ODD:
  383. newparity = PARENB | PARODD;
  384. break;
  385. case GDBPARITY_EVEN:
  386. newparity = PARENB;
  387. break;
  388. default:
  389. internal_warning (__FILE__, __LINE__,
  390. "Incorrect parity value: %d", parity);
  391. return -1;
  392. }
  393. state.termios.c_cflag &= ~(PARENB | PARODD);
  394. state.termios.c_cflag |= newparity;
  395. return set_tty_state (scb, &state);
  396. }
  397. static void
  398. hardwire_close (struct serial *scb)
  399. {
  400. if (scb->fd < 0)
  401. return;
  402. close (scb->fd);
  403. scb->fd = -1;
  404. }
  405. /* The hardwire ops. */
  406. static const struct serial_ops hardwire_ops =
  407. {
  408. "hardwire",
  409. hardwire_open,
  410. hardwire_close,
  411. NULL,
  412. ser_base_readchar,
  413. ser_base_write,
  414. hardwire_flush_output,
  415. hardwire_flush_input,
  416. hardwire_send_break,
  417. hardwire_raw,
  418. hardwire_get_tty_state,
  419. hardwire_copy_tty_state,
  420. hardwire_set_tty_state,
  421. hardwire_print_tty_state,
  422. hardwire_setbaudrate,
  423. hardwire_setstopbits,
  424. hardwire_setparity,
  425. hardwire_drain_output,
  426. ser_base_async,
  427. ser_unix_read_prim,
  428. ser_unix_write_prim
  429. };
  430. void _initialize_ser_hardwire ();
  431. void
  432. _initialize_ser_hardwire ()
  433. {
  434. serial_add_interface (&hardwire_ops);
  435. #ifdef CRTSCTS
  436. add_setshow_boolean_cmd ("remoteflow", no_class,
  437. &serial_hwflow, _("\
  438. Set use of hardware flow control for remote serial I/O."), _("\
  439. Show use of hardware flow control for remote serial I/O."), _("\
  440. Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
  441. when debugging using remote targets."),
  442. NULL,
  443. show_serial_hwflow,
  444. &setlist, &showlist);
  445. #endif
  446. }
  447. int
  448. ser_unix_read_prim (struct serial *scb, size_t count)
  449. {
  450. return read (scb->fd, scb->buf, count);
  451. }
  452. int
  453. ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
  454. {
  455. return write (scb->fd, buf, len);
  456. }