1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <target/target.h>
31 #include <target/target_request.h>
32 #include <target/openrisc/jsp_server.h>
34 #include "tcl_server.h"
35 #include "telnet_server.h"
36 #include "time_support.h"
45 #include <netinet/tcp.h>
48 static struct service *services;
50 enum shutdown_reason {
51 CONTINUE_MAIN_LOOP, /* stay in main event loop */
52 SHUTDOWN_REQUESTED, /* set by shutdown command; exit the event loop and quit the debugger */
53 SHUTDOWN_WITH_ERROR_CODE, /* set by shutdown command; quit with non-zero return code */
54 SHUTDOWN_WITH_SIGNAL_CODE /* set by sig_handler; exec shutdown then exit with signal as return code */
56 static enum shutdown_reason shutdown_openocd = CONTINUE_MAIN_LOOP;
58 /* store received signal to exit application by killing ourselves */
59 static int last_signal;
61 /* set the polling period to 100ms */
62 static int polling_period = 100;
64 /* address by name on which to listen for incoming TCP/IP connections */
65 static char *bindto_name;
67 static int add_connection(struct service *service, struct command_context *cmd_ctx)
69 socklen_t address_size;
70 struct connection *c, **p;
74 c = malloc(sizeof(struct connection));
77 memset(&c->sin, 0, sizeof(c->sin));
78 c->cmd_ctx = copy_command_context(cmd_ctx);
80 c->input_pending = false;
84 if (service->type == CONNECTION_TCP) {
85 address_size = sizeof(c->sin);
87 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
90 /* This increases performance dramatically for e.g. GDB load which
91 * does not have a sliding window protocol.
93 * Ignore errors from this fn as it probably just means less performance
95 setsockopt(c->fd, /* socket affected */
96 IPPROTO_TCP, /* set option at TCP level */
97 TCP_NODELAY, /* name of option */
98 (char *)&flag, /* the cast is historical cruft */
99 sizeof(int)); /* length of option value */
101 LOG_INFO("accepting '%s' connection on tcp/%s", service->name, service->port);
102 retval = service->new_connection(c);
103 if (retval != ERROR_OK) {
105 LOG_ERROR("attempted '%s' connection rejected", service->name);
106 command_done(c->cmd_ctx);
110 } else if (service->type == CONNECTION_STDINOUT) {
112 c->fd_out = fileno(stdout);
115 /* we are using stdin/out so ignore ctrl-c under windoze */
116 SetConsoleCtrlHandler(NULL, TRUE);
119 /* do not check for new connections again on stdin */
122 LOG_INFO("accepting '%s' connection from pipe", service->name);
123 retval = service->new_connection(c);
124 if (retval != ERROR_OK) {
125 LOG_ERROR("attempted '%s' connection rejected", service->name);
126 command_done(c->cmd_ctx);
130 } else if (service->type == CONNECTION_PIPE) {
132 /* do not check for new connections again on stdin */
135 char *out_file = alloc_printf("%so", service->port);
136 c->fd_out = open(out_file, O_WRONLY);
138 if (c->fd_out == -1) {
139 LOG_ERROR("could not open %s", service->port);
140 command_done(c->cmd_ctx);
145 LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
146 retval = service->new_connection(c);
147 if (retval != ERROR_OK) {
148 LOG_ERROR("attempted '%s' connection rejected", service->name);
149 command_done(c->cmd_ctx);
155 /* add to the end of linked list */
156 for (p = &service->connections; *p; p = &(*p)->next)
160 if (service->max_connections != CONNECTION_LIMIT_UNLIMITED)
161 service->max_connections--;
166 static int remove_connection(struct service *service, struct connection *connection)
168 struct connection **p = &service->connections;
169 struct connection *c;
171 /* find connection */
173 if (c->fd == connection->fd) {
174 service->connection_closed(c);
175 if (service->type == CONNECTION_TCP)
177 else if (service->type == CONNECTION_PIPE) {
178 /* The service will listen to the pipe again */
179 c->service->fd = c->fd;
182 command_done(c->cmd_ctx);
184 /* delete connection */
188 if (service->max_connections != CONNECTION_LIMIT_UNLIMITED)
189 service->max_connections++;
194 /* redirect p to next list pointer */
201 static void free_service(struct service *c)
208 int add_service(char *name,
211 new_connection_handler_t new_connection_handler,
212 input_handler_t input_handler,
213 connection_closed_handler_t connection_closed_handler,
216 struct service *c, **p;
218 int so_reuseaddr_option = 1;
220 c = malloc(sizeof(struct service));
222 c->name = strdup(name);
223 c->port = strdup(port);
224 c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
226 c->connections = NULL;
227 c->new_connection = new_connection_handler;
228 c->input = input_handler;
229 c->connection_closed = connection_closed_handler;
233 if (strcmp(c->port, "pipe") == 0)
234 c->type = CONNECTION_STDINOUT;
237 portnumber = strtol(c->port, &end, 0);
238 if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK)) {
239 c->portnumber = portnumber;
240 c->type = CONNECTION_TCP;
242 c->type = CONNECTION_PIPE;
245 if (c->type == CONNECTION_TCP) {
246 c->max_connections = max_connections;
248 c->fd = socket(AF_INET, SOCK_STREAM, 0);
250 LOG_ERROR("error creating socket: %s", strerror(errno));
258 (void *)&so_reuseaddr_option,
261 socket_nonblock(c->fd);
263 memset(&c->sin, 0, sizeof(c->sin));
264 c->sin.sin_family = AF_INET;
266 if (bindto_name == NULL)
267 c->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
269 hp = gethostbyname(bindto_name);
271 LOG_ERROR("couldn't resolve bindto address: %s", bindto_name);
276 memcpy(&c->sin.sin_addr, hp->h_addr_list[0], hp->h_length);
278 c->sin.sin_port = htons(c->portnumber);
280 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
281 LOG_ERROR("couldn't bind %s to socket on port %d: %s", name, c->portnumber, strerror(errno));
289 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
291 int window_size = 128 * 1024;
293 /* These setsockopt()s must happen before the listen() */
295 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
296 (char *)&window_size, sizeof(window_size));
297 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
298 (char *)&window_size, sizeof(window_size));
300 if (listen(c->fd, 1) == -1) {
301 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
307 struct sockaddr_in addr_in;
308 addr_in.sin_port = 0;
309 socklen_t addr_in_size = sizeof(addr_in);
310 if (getsockname(c->fd, (struct sockaddr *)&addr_in, &addr_in_size) == 0)
311 LOG_INFO("Listening on port %hu for %s connections",
312 ntohs(addr_in.sin_port), name);
313 } else if (c->type == CONNECTION_STDINOUT) {
314 c->fd = fileno(stdin);
317 /* for win32 set stdin/stdout to binary mode */
318 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
319 LOG_WARNING("cannot change stdout mode to binary");
320 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
321 LOG_WARNING("cannot change stdin mode to binary");
322 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
323 LOG_WARNING("cannot change stderr mode to binary");
325 socket_nonblock(c->fd);
327 } else if (c->type == CONNECTION_PIPE) {
329 /* we currently do not support named pipes under win32
330 * so exit openocd for now */
331 LOG_ERROR("Named pipes currently not supported under this os");
335 /* Pipe we're reading from */
336 c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
338 LOG_ERROR("could not open %s", c->port);
345 /* add to the end of linked list */
346 for (p = &services; *p; p = &(*p)->next)
353 static void remove_connections(struct service *service)
355 struct connection *connection;
357 connection = service->connections;
360 struct connection *tmp;
362 tmp = connection->next;
363 remove_connection(service, connection);
368 int remove_service(const char *name, const char *port)
371 struct service *prev;
375 for (tmp = services; tmp; prev = tmp, tmp = tmp->next) {
376 if (!strcmp(tmp->name, name) && !strcmp(tmp->port, port)) {
377 remove_connections(tmp);
380 services = tmp->next;
382 prev->next = tmp->next;
384 if (tmp->type != CONNECTION_STDINOUT)
385 close_socket(tmp->fd);
397 static int remove_services(void)
399 struct service *c = services;
403 struct service *next = c->next;
405 remove_connections(c);
409 if (c->type == CONNECTION_PIPE) {
418 /* remember the last service for unlinking */
427 int server_loop(struct command_context *command_context)
429 struct service *service;
433 /* used in select() */
437 /* used in accept() */
441 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
442 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
445 while (shutdown_openocd == CONTINUE_MAIN_LOOP) {
446 /* monitor sockets for activity */
450 /* add service and connection fds to read_fds */
451 for (service = services; service; service = service->next) {
452 if (service->fd != -1) {
453 /* listen for new connections */
454 FD_SET(service->fd, &read_fds);
456 if (service->fd > fd_max)
457 fd_max = service->fd;
460 if (service->connections) {
461 struct connection *c;
463 for (c = service->connections; c; c = c->next) {
464 /* check for activity on the connection */
465 FD_SET(c->fd, &read_fds);
475 /* we're just polling this iteration, this is faster on embedded
478 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
480 /* Every 100ms, can be changed with "poll_period" command */
481 tv.tv_usec = polling_period * 1000;
482 /* Only while we're sleeping we'll let others run */
483 openocd_sleep_prelude();
485 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
486 openocd_sleep_postlude();
492 errno = WSAGetLastError();
494 if (errno == WSAEINTR)
497 LOG_ERROR("error during select: %s", strerror(errno));
505 LOG_ERROR("error during select: %s", strerror(errno));
512 /* We only execute these callbacks when there was nothing to do or we timed
514 target_call_timer_callbacks();
515 process_jim_events(command_context);
517 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
519 /* We timed out/there was nothing to do, timeout rather than poll next time
523 /* There was something to do, next time we'll just poll */
527 /* This is a simple back-off algorithm where we immediately
528 * re-poll if we did something this time around.
530 * This greatly improves performance of DCC.
532 poll_ok = poll_ok || target_got_message();
534 for (service = services; service; service = service->next) {
535 /* handle new connections on listeners */
536 if ((service->fd != -1)
537 && (FD_ISSET(service->fd, &read_fds))) {
538 if (service->max_connections != 0)
539 add_connection(service, command_context);
541 if (service->type == CONNECTION_TCP) {
542 struct sockaddr_in sin;
543 socklen_t address_size = sizeof(sin);
545 tmp_fd = accept(service->fd,
546 (struct sockaddr *)&service->sin,
548 close_socket(tmp_fd);
551 "rejected '%s' connection, no more connections allowed",
556 /* handle activity on connections */
557 if (service->connections) {
558 struct connection *c;
560 for (c = service->connections; c; ) {
561 if ((c->fd >= 0 && FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
562 retval = service->input(c);
563 if (retval != ERROR_OK) {
564 struct connection *next = c->next;
565 if (service->type == CONNECTION_PIPE ||
566 service->type == CONNECTION_STDINOUT) {
567 /* if connection uses a pipe then
568 * shutdown openocd on error */
569 shutdown_openocd = SHUTDOWN_REQUESTED;
571 remove_connection(service, c);
572 LOG_INFO("dropped '%s' connection",
585 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
586 if (msg.message == WM_QUIT)
587 shutdown_openocd = SHUTDOWN_WITH_SIGNAL_CODE;
592 /* when quit for signal or CTRL-C, run (eventually user implemented) "shutdown" */
593 if (shutdown_openocd == SHUTDOWN_WITH_SIGNAL_CODE)
594 command_run_line(command_context, "shutdown");
596 return shutdown_openocd == SHUTDOWN_WITH_ERROR_CODE ? ERROR_FAIL : ERROR_OK;
599 static void sig_handler(int sig)
601 /* store only first signal that hits us */
602 if (shutdown_openocd == CONTINUE_MAIN_LOOP) {
603 shutdown_openocd = SHUTDOWN_WITH_SIGNAL_CODE;
605 LOG_DEBUG("Terminating on Signal %d", sig);
607 LOG_DEBUG("Ignored extra Signal %d", sig);
612 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
614 shutdown_openocd = SHUTDOWN_WITH_SIGNAL_CODE;
618 static void sigkey_handler(int sig)
620 /* ignore keystroke generated signals if not in foreground process group */
622 if (tcgetpgrp(STDIN_FILENO) > 0)
625 LOG_DEBUG("Ignored Signal %d", sig);
630 int server_host_os_entry(void)
632 /* this currently only calls WSAStartup on native win32 systems
633 * before any socket operations are performed.
634 * This is an issue if you call init in your config script */
637 WORD wVersionRequested;
640 wVersionRequested = MAKEWORD(2, 2);
642 if (WSAStartup(wVersionRequested, &wsaData) != 0) {
643 LOG_ERROR("Failed to Open Winsock");
650 int server_host_os_close(void)
658 int server_preinit(void)
661 /* register ctrl-c handler */
662 SetConsoleCtrlHandler(ControlHandler, TRUE);
664 signal(SIGBREAK, sig_handler);
665 signal(SIGINT, sig_handler);
667 signal(SIGHUP, sig_handler);
668 signal(SIGPIPE, sig_handler);
669 signal(SIGQUIT, sigkey_handler);
670 signal(SIGINT, sigkey_handler);
672 signal(SIGTERM, sig_handler);
673 signal(SIGABRT, sig_handler);
678 int server_init(struct command_context *cmd_ctx)
680 int ret = tcl_init();
685 ret = telnet_init("Open On-Chip Debugger");
687 if (ret != ERROR_OK) {
695 int server_quit(void)
701 SetConsoleCtrlHandler(ControlHandler, FALSE);
706 /* return signal number so we can kill ourselves */
710 void server_free(void)
713 telnet_service_free();
719 void exit_on_signal(int sig)
722 /* bring back default system handler and kill yourself */
723 signal(sig, SIG_DFL);
728 int connection_write(struct connection *connection, const void *data, int len)
731 /* successful no-op. Sockets and pipes behave differently here... */
734 if (connection->service->type == CONNECTION_TCP)
735 return write_socket(connection->fd_out, data, len);
737 return write(connection->fd_out, data, len);
740 int connection_read(struct connection *connection, void *data, int len)
742 if (connection->service->type == CONNECTION_TCP)
743 return read_socket(connection->fd, data, len);
745 return read(connection->fd, data, len);
748 /* tell the server we want to shut down */
749 COMMAND_HANDLER(handle_shutdown_command)
751 LOG_USER("shutdown command invoked");
753 shutdown_openocd = SHUTDOWN_REQUESTED;
756 if (!strcmp(CMD_ARGV[0], "error")) {
757 shutdown_openocd = SHUTDOWN_WITH_ERROR_CODE;
762 return ERROR_COMMAND_CLOSE_CONNECTION;
765 COMMAND_HANDLER(handle_poll_period_command)
768 LOG_WARNING("You need to set a period value");
770 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], polling_period);
772 LOG_INFO("set servers polling period to %ums", polling_period);
777 COMMAND_HANDLER(handle_bindto_command)
781 command_print(CMD, "bindto name: %s", bindto_name);
785 bindto_name = strdup(CMD_ARGV[0]);
788 return ERROR_COMMAND_SYNTAX_ERROR;
793 static const struct command_registration server_command_handlers[] = {
796 .handler = &handle_shutdown_command,
799 .help = "shut the server down",
802 .name = "poll_period",
803 .handler = &handle_poll_period_command,
806 .help = "set the servers polling period",
810 .handler = &handle_bindto_command,
811 .mode = COMMAND_CONFIG,
813 .help = "Specify address by name on which to listen for "
814 "incoming TCP/IP connections",
816 COMMAND_REGISTRATION_DONE
819 int server_register_commands(struct command_context *cmd_ctx)
821 int retval = telnet_register_commands(cmd_ctx);
822 if (ERROR_OK != retval)
825 retval = tcl_register_commands(cmd_ctx);
826 if (ERROR_OK != retval)
829 retval = jsp_register_commands(cmd_ctx);
830 if (ERROR_OK != retval)
833 return register_commands(cmd_ctx, NULL, server_command_handlers);
836 COMMAND_HELPER(server_port_command, unsigned short *out)
840 command_print(CMD, "%d", *out);
845 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
850 return ERROR_COMMAND_SYNTAX_ERROR;
855 COMMAND_HELPER(server_pipe_command, char **out)
859 command_print(CMD, "%s", *out);
863 if (CMD_CTX->mode == COMMAND_EXEC) {
864 LOG_WARNING("unable to change server port after init");
865 return ERROR_COMMAND_ARGUMENT_INVALID;
868 *out = strdup(CMD_ARGV[0]);
872 return ERROR_COMMAND_SYNTAX_ERROR;