xref: /dflybsd-src/contrib/gdb-7/gdb/serial.h (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Remote serial support interface definitions for GDB, the GNU Debugger.
2*ef5ccd6cSJohn Marino    Copyright (C) 1992-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    This file is part of GDB.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert    (at your option) any later version.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145796c8dcSSimon Schubert    GNU General Public License for more details.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert #ifndef SERIAL_H
205796c8dcSSimon Schubert #define SERIAL_H
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #ifdef USE_WIN32API
23cf7f2e2dSJohn Marino #include <winsock2.h>
245796c8dcSSimon Schubert #include <windows.h>
255796c8dcSSimon Schubert #endif
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert struct ui_file;
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert /* For most routines, if a failure is indicated, then errno should be
305796c8dcSSimon Schubert    examined.  */
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert /* Terminal state pointer.  This is specific to each type of
335796c8dcSSimon Schubert    interface.  */
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert typedef void *serial_ttystate;
365796c8dcSSimon Schubert struct serial;
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert /* Try to open NAME.  Returns a new `struct serial *' on success, NULL
39*ef5ccd6cSJohn Marino    on failure.  The new serial object has a reference count of 1.
40*ef5ccd6cSJohn Marino    Note that some open calls can block and, if possible, should be
41*ef5ccd6cSJohn Marino    written to be non-blocking, with calls to ui_look_hook so they can
42*ef5ccd6cSJohn Marino    be cancelled.  An async interface for open could be added to GDB if
43*ef5ccd6cSJohn Marino    necessary.  */
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert extern struct serial *serial_open (const char *name);
465796c8dcSSimon Schubert 
47*ef5ccd6cSJohn Marino /* Returns true if SCB is open.  */
48*ef5ccd6cSJohn Marino 
49*ef5ccd6cSJohn Marino extern int serial_is_open (struct serial *scb);
50*ef5ccd6cSJohn Marino 
515796c8dcSSimon Schubert /* Find an already opened serial stream using a file handle.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert extern struct serial *serial_for_fd (int fd);
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* Open a new serial stream using a file handle.  */
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert extern struct serial *serial_fdopen (const int fd);
585796c8dcSSimon Schubert 
59*ef5ccd6cSJohn Marino /* Push out all buffers, close the device and unref SCB.  */
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert extern void serial_close (struct serial *scb);
625796c8dcSSimon Schubert 
63*ef5ccd6cSJohn Marino /* Increment reference count of SCB.  */
64*ef5ccd6cSJohn Marino 
65*ef5ccd6cSJohn Marino extern void serial_ref (struct serial *scb);
66*ef5ccd6cSJohn Marino 
67*ef5ccd6cSJohn Marino /* Decrement reference count of SCB.  */
68*ef5ccd6cSJohn Marino 
69*ef5ccd6cSJohn Marino extern void serial_unref (struct serial *scb);
70*ef5ccd6cSJohn Marino 
71c50c785cSJohn Marino /* Create a pipe, and put the read end in files[0], and the write end
72c50c785cSJohn Marino    in filde[1].  Returns 0 for success, negative value for error (in
73c50c785cSJohn Marino    which case errno contains the error).  */
74c50c785cSJohn Marino 
75c50c785cSJohn Marino extern int gdb_pipe (int fildes[2]);
76c50c785cSJohn Marino 
77c50c785cSJohn Marino /* Create a pipe with each end wrapped in a `struct serial' interface.
78c50c785cSJohn Marino    Put the read end in scbs[0], and the write end in scbs[1].  Returns
79c50c785cSJohn Marino    0 for success, negative value for error (in which case errno
80c50c785cSJohn Marino    contains the error).  */
81c50c785cSJohn Marino 
82c50c785cSJohn Marino extern int serial_pipe (struct serial *scbs[2]);
83c50c785cSJohn Marino 
845796c8dcSSimon Schubert /* Push out all buffers and destroy SCB without closing the device.  */
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert extern void serial_un_fdopen (struct serial *scb);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* Read one char from the serial device with TIMEOUT seconds to wait
895796c8dcSSimon Schubert    or -1 to wait forever.  Use timeout of 0 to effect a poll.
905796c8dcSSimon Schubert    Infinite waits are not permitted.  Returns unsigned char if ok, else
915796c8dcSSimon Schubert    one of the following codes.  Note that all error return-codes are
925796c8dcSSimon Schubert    guaranteed to be < 0.  */
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert enum serial_rc {
955796c8dcSSimon Schubert   SERIAL_ERROR = -1,	/* General error.  */
965796c8dcSSimon Schubert   SERIAL_TIMEOUT = -2,	/* Timeout or data-not-ready during read.
975796c8dcSSimon Schubert 			   Unfortunately, through
985796c8dcSSimon Schubert 			   deprecated_ui_loop_hook (), this can also
995796c8dcSSimon Schubert 			   be a QUIT indication.  */
1005796c8dcSSimon Schubert   SERIAL_EOF = -3	/* General end-of-file or remote target
1015796c8dcSSimon Schubert 			   connection closed, indication.  Includes
1025796c8dcSSimon Schubert 			   things like the line dropping dead.  */
1035796c8dcSSimon Schubert };
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert extern int serial_readchar (struct serial *scb, int timeout);
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert /* Write LEN chars from STRING to the port SCB.  Returns 0 for
1085796c8dcSSimon Schubert    success, non-zero for failure.  */
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert extern int serial_write (struct serial *scb, const char *str, int len);
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert /* Write a printf style string onto the serial port.  */
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert extern void serial_printf (struct serial *desc,
115cf7f2e2dSJohn Marino 			   const char *,...) ATTRIBUTE_PRINTF (2, 3);
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert /* Allow pending output to drain.  */
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert extern int serial_drain_output (struct serial *);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert /* Flush (discard) pending output.  Might also flush input (if this
1225796c8dcSSimon Schubert    system can't flush only output).  */
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert extern int serial_flush_output (struct serial *);
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* Flush pending input.  Might also flush output (if this system can't
1275796c8dcSSimon Schubert    flush only input).  */
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert extern int serial_flush_input (struct serial *);
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert /* Send a break between 0.25 and 0.5 seconds long.  */
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert extern int serial_send_break (struct serial *scb);
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert /* Turn the port into raw mode.  */
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert extern void serial_raw (struct serial *scb);
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert /* Return a pointer to a newly malloc'd ttystate containing the state
1405796c8dcSSimon Schubert    of the tty.  */
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert extern serial_ttystate serial_get_tty_state (struct serial *scb);
1435796c8dcSSimon Schubert 
144c50c785cSJohn Marino /* Return a pointer to a newly malloc'd ttystate containing a copy
145c50c785cSJohn Marino    of the state in TTYSTATE.  */
146c50c785cSJohn Marino 
147c50c785cSJohn Marino extern serial_ttystate serial_copy_tty_state (struct serial *scb,
148c50c785cSJohn Marino 					      serial_ttystate ttystate);
149c50c785cSJohn Marino 
1505796c8dcSSimon Schubert /* Set the state of the tty to TTYSTATE.  The change is immediate.
1515796c8dcSSimon Schubert    When changing to or from raw mode, input might be discarded.
1525796c8dcSSimon Schubert    Returns 0 for success, negative value for error (in which case
1535796c8dcSSimon Schubert    errno contains the error).  */
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert extern int serial_set_tty_state (struct serial *scb, serial_ttystate ttystate);
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert /* printf_filtered a user-comprehensible description of ttystate on
1585796c8dcSSimon Schubert    the specified STREAM.  FIXME: At present this sends output to the
1595796c8dcSSimon Schubert    default stream - GDB_STDOUT.  */
1605796c8dcSSimon Schubert 
161c50c785cSJohn Marino extern void serial_print_tty_state (struct serial *scb,
162c50c785cSJohn Marino 				    serial_ttystate ttystate,
163c50c785cSJohn Marino 				    struct ui_file *);
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert /* Set the tty state to NEW_TTYSTATE, where OLD_TTYSTATE is the
1665796c8dcSSimon Schubert    current state (generally obtained from a recent call to
1675796c8dcSSimon Schubert    serial_get_tty_state()), but be careful not to discard any input.
1685796c8dcSSimon Schubert    This means that we never switch in or out of raw mode, even if
1695796c8dcSSimon Schubert    NEW_TTYSTATE specifies a switch.  */
1705796c8dcSSimon Schubert 
171c50c785cSJohn Marino extern int serial_noflush_set_tty_state (struct serial *scb,
172c50c785cSJohn Marino 					 serial_ttystate new_ttystate,
173c50c785cSJohn Marino 					 serial_ttystate old_ttystate);
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert /* Set the baudrate to the decimal value supplied.  Returns 0 for
1765796c8dcSSimon Schubert    success, -1 for failure.  */
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert extern int serial_setbaudrate (struct serial *scb, int rate);
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert /* Set the number of stop bits to the value specified.  Returns 0 for
1815796c8dcSSimon Schubert    success, -1 for failure.  */
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert #define SERIAL_1_STOPBITS 1
1845796c8dcSSimon Schubert #define SERIAL_1_AND_A_HALF_STOPBITS 2	/* 1.5 bits, snicker...  */
1855796c8dcSSimon Schubert #define SERIAL_2_STOPBITS 3
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert extern int serial_setstopbits (struct serial *scb, int num);
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert /* Asynchronous serial interface: */
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert /* Can the serial device support asynchronous mode?  */
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert extern int serial_can_async_p (struct serial *scb);
1945796c8dcSSimon Schubert 
1955796c8dcSSimon Schubert /* Has the serial device been put in asynchronous mode?  */
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert extern int serial_is_async_p (struct serial *scb);
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert /* For ASYNC enabled devices, register a callback and enable
2005796c8dcSSimon Schubert    asynchronous mode.  To disable asynchronous mode, register a NULL
2015796c8dcSSimon Schubert    callback.  */
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert typedef void (serial_event_ftype) (struct serial *scb, void *context);
204c50c785cSJohn Marino extern void serial_async (struct serial *scb,
205c50c785cSJohn Marino 			  serial_event_ftype *handler, void *context);
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert /* Trace/debug mechanism.
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert    serial_debug() enables/disables internal debugging.
2105796c8dcSSimon Schubert    serial_debug_p() indicates the current debug state.  */
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert extern void serial_debug (struct serial *scb, int debug_p);
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert extern int serial_debug_p (struct serial *scb);
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert 
217c50c785cSJohn Marino /* Details of an instance of a serial object.  */
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert struct serial
2205796c8dcSSimon Schubert   {
221*ef5ccd6cSJohn Marino     /* serial objects are ref counted (but not the underlying
222*ef5ccd6cSJohn Marino        connection, just the object's lifetime in memory).  */
223*ef5ccd6cSJohn Marino     int refcnt;
224*ef5ccd6cSJohn Marino 
2255796c8dcSSimon Schubert     int fd;			/* File descriptor */
2265796c8dcSSimon Schubert     /* File descriptor for a separate error stream that should be
2275796c8dcSSimon Schubert        immediately forwarded to gdb_stderr.  This may be -1.
2285796c8dcSSimon Schubert        If != -1, this descriptor should be non-blocking or
2295796c8dcSSimon Schubert        ops->avail should be non-NULL.  */
2305796c8dcSSimon Schubert     int error_fd;
2315796c8dcSSimon Schubert     struct serial_ops *ops;	/* Function vector */
2325796c8dcSSimon Schubert     void *state;       		/* Local context info for open FD */
2335796c8dcSSimon Schubert     serial_ttystate ttystate;	/* Not used (yet) */
2345796c8dcSSimon Schubert     int bufcnt;			/* Amount of data remaining in receive
2355796c8dcSSimon Schubert 				   buffer.  -ve for sticky errors.  */
2365796c8dcSSimon Schubert     unsigned char *bufp;	/* Current byte */
2375796c8dcSSimon Schubert     unsigned char buf[BUFSIZ];	/* Da buffer itself */
2385796c8dcSSimon Schubert     int current_timeout;	/* (ser-unix.c termio{,s} only), last
2395796c8dcSSimon Schubert 				   value of VTIME */
2405796c8dcSSimon Schubert     int timeout_remaining;	/* (ser-unix.c termio{,s} only), we
2415796c8dcSSimon Schubert 				   still need to wait for this many
2425796c8dcSSimon Schubert 				   more seconds.  */
2435796c8dcSSimon Schubert     char *name;			/* The name of the device or host */
2445796c8dcSSimon Schubert     struct serial *next;	/* Pointer to the next `struct serial *' */
2455796c8dcSSimon Schubert     int debug_p;		/* Trace this serial devices operation.  */
2465796c8dcSSimon Schubert     int async_state;		/* Async internal state.  */
2475796c8dcSSimon Schubert     void *async_context;	/* Async event thread's context */
2485796c8dcSSimon Schubert     serial_event_ftype *async_handler;/* Async event handler */
2495796c8dcSSimon Schubert   };
2505796c8dcSSimon Schubert 
2515796c8dcSSimon Schubert struct serial_ops
2525796c8dcSSimon Schubert   {
2535796c8dcSSimon Schubert     char *name;
2545796c8dcSSimon Schubert     struct serial_ops *next;
2555796c8dcSSimon Schubert     int (*open) (struct serial *, const char *name);
2565796c8dcSSimon Schubert     void (*close) (struct serial *);
257c50c785cSJohn Marino     int (*fdopen) (struct serial *, int fd);
2585796c8dcSSimon Schubert     int (*readchar) (struct serial *, int timeout);
2595796c8dcSSimon Schubert     int (*write) (struct serial *, const char *str, int len);
2605796c8dcSSimon Schubert     /* Discard pending output */
2615796c8dcSSimon Schubert     int (*flush_output) (struct serial *);
2625796c8dcSSimon Schubert     /* Discard pending input */
2635796c8dcSSimon Schubert     int (*flush_input) (struct serial *);
2645796c8dcSSimon Schubert     int (*send_break) (struct serial *);
2655796c8dcSSimon Schubert     void (*go_raw) (struct serial *);
2665796c8dcSSimon Schubert     serial_ttystate (*get_tty_state) (struct serial *);
267c50c785cSJohn Marino     serial_ttystate (*copy_tty_state) (struct serial *, serial_ttystate);
2685796c8dcSSimon Schubert     int (*set_tty_state) (struct serial *, serial_ttystate);
2695796c8dcSSimon Schubert     void (*print_tty_state) (struct serial *, serial_ttystate,
2705796c8dcSSimon Schubert 			     struct ui_file *);
2715796c8dcSSimon Schubert     int (*noflush_set_tty_state) (struct serial *, serial_ttystate,
2725796c8dcSSimon Schubert 				  serial_ttystate);
2735796c8dcSSimon Schubert     int (*setbaudrate) (struct serial *, int rate);
2745796c8dcSSimon Schubert     int (*setstopbits) (struct serial *, int num);
275c50c785cSJohn Marino     /* Wait for output to drain.  */
2765796c8dcSSimon Schubert     int (*drain_output) (struct serial *);
2775796c8dcSSimon Schubert     /* Change the serial device into/out of asynchronous mode, call
2785796c8dcSSimon Schubert        the specified function when ever there is something
2795796c8dcSSimon Schubert        interesting.  */
2805796c8dcSSimon Schubert     void (*async) (struct serial *scb, int async_p);
2815796c8dcSSimon Schubert     /* Perform a low-level read operation, reading (at most) COUNT
2825796c8dcSSimon Schubert        bytes into SCB->BUF.  Return zero at end of file.  */
2835796c8dcSSimon Schubert     int (*read_prim)(struct serial *scb, size_t count);
2845796c8dcSSimon Schubert     /* Perform a low-level write operation, writing (at most) COUNT
2855796c8dcSSimon Schubert        bytes from BUF.  */
2865796c8dcSSimon Schubert     int (*write_prim)(struct serial *scb, const void *buf, size_t count);
2875796c8dcSSimon Schubert     /* Return that number of bytes that can be read from FD
2885796c8dcSSimon Schubert        without blocking.  Return value of -1 means that the
289a45ae5f8SJohn Marino        read will not block even if less that requested bytes
2905796c8dcSSimon Schubert        are available.  */
2915796c8dcSSimon Schubert     int (*avail)(struct serial *scb, int fd);
2925796c8dcSSimon Schubert 
2935796c8dcSSimon Schubert #ifdef USE_WIN32API
2945796c8dcSSimon Schubert     /* Return a handle to wait on, indicating available data from SCB
2955796c8dcSSimon Schubert        when signaled, in *READ.  Return a handle indicating errors
2965796c8dcSSimon Schubert        in *EXCEPT.  */
2975796c8dcSSimon Schubert     void (*wait_handle) (struct serial *scb, HANDLE *read, HANDLE *except);
2985796c8dcSSimon Schubert     void (*done_wait_handle) (struct serial *scb);
2995796c8dcSSimon Schubert #endif /* USE_WIN32API */
3005796c8dcSSimon Schubert   };
3015796c8dcSSimon Schubert 
302c50c785cSJohn Marino /* Add a new serial interface to the interface list.  */
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert extern void serial_add_interface (struct serial_ops * optable);
3055796c8dcSSimon Schubert 
306c50c785cSJohn Marino /* File in which to record the remote debugging session.  */
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert extern void serial_log_command (const char *);
3095796c8dcSSimon Schubert 
3105796c8dcSSimon Schubert #ifdef USE_WIN32API
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert /* Windows-only: find or create handles that we can wait on for this
3135796c8dcSSimon Schubert    serial device.  */
3145796c8dcSSimon Schubert extern void serial_wait_handle (struct serial *, HANDLE *, HANDLE *);
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert /* Windows-only: signal that we are done with the wait handles.  */
3175796c8dcSSimon Schubert extern void serial_done_wait_handle (struct serial *);
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert #endif /* USE_WIN32API */
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert #endif /* SERIAL_H */
322