aarch64: use symbolic opcodes instead of hex values
[openocd.git] / src / server / server.c
blobf6889a01a77f99fe003e79238037d6be07487b3d
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
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. *
15 * *
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. *
20 * *
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 ***************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include "server.h"
30 #include <target/target.h>
31 #include <target/target_request.h>
32 #include <target/openrisc/jsp_server.h>
33 #include "openocd.h"
34 #include "tcl_server.h"
35 #include "telnet_server.h"
37 #include <signal.h>
39 #ifdef HAVE_NETDB_H
40 #include <netdb.h>
41 #endif
43 #ifndef _WIN32
44 #include <netinet/tcp.h>
45 #endif
47 static struct service *services;
49 /* shutdown_openocd == 1: exit the main event loop, and quit the
50 * debugger; 2: quit with non-zero return code */
51 static int shutdown_openocd;
53 /* store received signal to exit application by killing ourselves */
54 static int last_signal;
56 /* set the polling period to 100ms */
57 static int polling_period = 100;
59 /* address by name on which to listen for incoming TCP/IP connections */
60 static char *bindto_name;
62 static int add_connection(struct service *service, struct command_context *cmd_ctx)
64 socklen_t address_size;
65 struct connection *c, **p;
66 int retval;
67 int flag = 1;
69 c = malloc(sizeof(struct connection));
70 c->fd = -1;
71 c->fd_out = -1;
72 memset(&c->sin, 0, sizeof(c->sin));
73 c->cmd_ctx = copy_command_context(cmd_ctx);
74 c->service = service;
75 c->input_pending = 0;
76 c->priv = NULL;
77 c->next = NULL;
79 if (service->type == CONNECTION_TCP) {
80 address_size = sizeof(c->sin);
82 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
83 c->fd_out = c->fd;
85 /* This increases performance dramatically for e.g. GDB load which
86 * does not have a sliding window protocol.
88 * Ignore errors from this fn as it probably just means less performance
90 setsockopt(c->fd, /* socket affected */
91 IPPROTO_TCP, /* set option at TCP level */
92 TCP_NODELAY, /* name of option */
93 (char *)&flag, /* the cast is historical cruft */
94 sizeof(int)); /* length of option value */
96 LOG_INFO("accepting '%s' connection on tcp/%s", service->name, service->port);
97 retval = service->new_connection(c);
98 if (retval != ERROR_OK) {
99 close_socket(c->fd);
100 LOG_ERROR("attempted '%s' connection rejected", service->name);
101 command_done(c->cmd_ctx);
102 free(c);
103 return retval;
105 } else if (service->type == CONNECTION_STDINOUT) {
106 c->fd = service->fd;
107 c->fd_out = fileno(stdout);
109 #ifdef _WIN32
110 /* we are using stdin/out so ignore ctrl-c under windoze */
111 SetConsoleCtrlHandler(NULL, TRUE);
112 #endif
114 /* do not check for new connections again on stdin */
115 service->fd = -1;
117 LOG_INFO("accepting '%s' connection from pipe", service->name);
118 retval = service->new_connection(c);
119 if (retval != ERROR_OK) {
120 LOG_ERROR("attempted '%s' connection rejected", service->name);
121 command_done(c->cmd_ctx);
122 free(c);
123 return retval;
125 } else if (service->type == CONNECTION_PIPE) {
126 c->fd = service->fd;
127 /* do not check for new connections again on stdin */
128 service->fd = -1;
130 char *out_file = alloc_printf("%so", service->port);
131 c->fd_out = open(out_file, O_WRONLY);
132 free(out_file);
133 if (c->fd_out == -1) {
134 LOG_ERROR("could not open %s", service->port);
135 exit(1);
138 LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
139 retval = service->new_connection(c);
140 if (retval != ERROR_OK) {
141 LOG_ERROR("attempted '%s' connection rejected", service->name);
142 command_done(c->cmd_ctx);
143 free(c);
144 return retval;
148 /* add to the end of linked list */
149 for (p = &service->connections; *p; p = &(*p)->next)
151 *p = c;
153 if (service->max_connections != CONNECTION_LIMIT_UNLIMITED)
154 service->max_connections--;
156 return ERROR_OK;
159 static int remove_connection(struct service *service, struct connection *connection)
161 struct connection **p = &service->connections;
162 struct connection *c;
164 /* find connection */
165 while ((c = *p)) {
166 if (c->fd == connection->fd) {
167 service->connection_closed(c);
168 if (service->type == CONNECTION_TCP)
169 close_socket(c->fd);
170 else if (service->type == CONNECTION_PIPE) {
171 /* The service will listen to the pipe again */
172 c->service->fd = c->fd;
175 command_done(c->cmd_ctx);
177 /* delete connection */
178 *p = c->next;
179 free(c);
181 if (service->max_connections != CONNECTION_LIMIT_UNLIMITED)
182 service->max_connections++;
184 break;
187 /* redirect p to next list pointer */
188 p = &(*p)->next;
191 return ERROR_OK;
194 /* FIX! make service return error instead of invoking exit() */
195 int add_service(char *name,
196 const char *port,
197 int max_connections,
198 new_connection_handler_t new_connection_handler,
199 input_handler_t input_handler,
200 connection_closed_handler_t connection_closed_handler,
201 void *priv)
203 struct service *c, **p;
204 struct hostent *hp;
205 int so_reuseaddr_option = 1;
207 c = malloc(sizeof(struct service));
209 c->name = strdup(name);
210 c->port = strdup(port);
211 c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
212 c->fd = -1;
213 c->connections = NULL;
214 c->new_connection = new_connection_handler;
215 c->input = input_handler;
216 c->connection_closed = connection_closed_handler;
217 c->priv = priv;
218 c->next = NULL;
219 long portnumber;
220 if (strcmp(c->port, "pipe") == 0)
221 c->type = CONNECTION_STDINOUT;
222 else {
223 char *end;
224 portnumber = strtol(c->port, &end, 0);
225 if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK)) {
226 c->portnumber = portnumber;
227 c->type = CONNECTION_TCP;
228 } else
229 c->type = CONNECTION_PIPE;
232 if (c->type == CONNECTION_TCP) {
233 c->max_connections = max_connections;
235 c->fd = socket(AF_INET, SOCK_STREAM, 0);
236 if (c->fd == -1) {
237 LOG_ERROR("error creating socket: %s", strerror(errno));
238 exit(-1);
241 setsockopt(c->fd,
242 SOL_SOCKET,
243 SO_REUSEADDR,
244 (void *)&so_reuseaddr_option,
245 sizeof(int));
247 socket_nonblock(c->fd);
249 memset(&c->sin, 0, sizeof(c->sin));
250 c->sin.sin_family = AF_INET;
252 if (bindto_name == NULL)
253 c->sin.sin_addr.s_addr = INADDR_ANY;
254 else {
255 hp = gethostbyname(bindto_name);
256 if (hp == NULL) {
257 LOG_ERROR("couldn't resolve bindto address: %s", bindto_name);
258 exit(-1);
260 memcpy(&c->sin.sin_addr, hp->h_addr_list[0], hp->h_length);
262 c->sin.sin_port = htons(c->portnumber);
264 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1) {
265 LOG_ERROR("couldn't bind %s to socket: %s", name, strerror(errno));
266 exit(-1);
269 #ifndef _WIN32
270 int segsize = 65536;
271 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
272 #endif
273 int window_size = 128 * 1024;
275 /* These setsockopt()s must happen before the listen() */
277 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
278 (char *)&window_size, sizeof(window_size));
279 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
280 (char *)&window_size, sizeof(window_size));
282 if (listen(c->fd, 1) == -1) {
283 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
284 exit(-1);
286 } else if (c->type == CONNECTION_STDINOUT) {
287 c->fd = fileno(stdin);
289 #ifdef _WIN32
290 /* for win32 set stdin/stdout to binary mode */
291 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
292 LOG_WARNING("cannot change stdout mode to binary");
293 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
294 LOG_WARNING("cannot change stdin mode to binary");
295 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
296 LOG_WARNING("cannot change stderr mode to binary");
297 #else
298 socket_nonblock(c->fd);
299 #endif
300 } else if (c->type == CONNECTION_PIPE) {
301 #ifdef _WIN32
302 /* we currenty do not support named pipes under win32
303 * so exit openocd for now */
304 LOG_ERROR("Named pipes currently not supported under this os");
305 exit(1);
306 #else
307 /* Pipe we're reading from */
308 c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
309 if (c->fd == -1) {
310 LOG_ERROR("could not open %s", c->port);
311 exit(1);
313 #endif
316 /* add to the end of linked list */
317 for (p = &services; *p; p = &(*p)->next)
319 *p = c;
321 return ERROR_OK;
324 static int remove_services(void)
326 struct service *c = services;
328 /* loop service */
329 while (c) {
330 struct service *next = c->next;
332 if (c->name)
333 free(c->name);
335 if (c->type == CONNECTION_PIPE) {
336 if (c->fd != -1)
337 close(c->fd);
339 if (c->port)
340 free(c->port);
342 if (c->priv)
343 free(c->priv);
345 /* delete service */
346 free(c);
348 /* remember the last service for unlinking */
349 c = next;
352 services = NULL;
354 return ERROR_OK;
357 int server_loop(struct command_context *command_context)
359 struct service *service;
361 bool poll_ok = true;
363 /* used in select() */
364 fd_set read_fds;
365 int fd_max;
367 /* used in accept() */
368 int retval;
370 #ifndef _WIN32
371 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
372 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
373 #endif
375 while (!shutdown_openocd) {
376 /* monitor sockets for activity */
377 fd_max = 0;
378 FD_ZERO(&read_fds);
380 /* add service and connection fds to read_fds */
381 for (service = services; service; service = service->next) {
382 if (service->fd != -1) {
383 /* listen for new connections */
384 FD_SET(service->fd, &read_fds);
386 if (service->fd > fd_max)
387 fd_max = service->fd;
390 if (service->connections) {
391 struct connection *c;
393 for (c = service->connections; c; c = c->next) {
394 /* check for activity on the connection */
395 FD_SET(c->fd, &read_fds);
396 if (c->fd > fd_max)
397 fd_max = c->fd;
402 struct timeval tv;
403 tv.tv_sec = 0;
404 if (poll_ok) {
405 /* we're just polling this iteration, this is faster on embedded
406 * hosts */
407 tv.tv_usec = 0;
408 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
409 } else {
410 /* Every 100ms, can be changed with "poll_period" command */
411 tv.tv_usec = polling_period * 1000;
412 /* Only while we're sleeping we'll let others run */
413 openocd_sleep_prelude();
414 kept_alive();
415 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
416 openocd_sleep_postlude();
419 if (retval == -1) {
420 #ifdef _WIN32
422 errno = WSAGetLastError();
424 if (errno == WSAEINTR)
425 FD_ZERO(&read_fds);
426 else {
427 LOG_ERROR("error during select: %s", strerror(errno));
428 exit(-1);
430 #else
432 if (errno == EINTR)
433 FD_ZERO(&read_fds);
434 else {
435 LOG_ERROR("error during select: %s", strerror(errno));
436 exit(-1);
438 #endif
441 if (retval == 0) {
442 /* We only execute these callbacks when there was nothing to do or we timed
443 *out */
444 target_call_timer_callbacks();
445 process_jim_events(command_context);
447 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
449 /* We timed out/there was nothing to do, timeout rather than poll next time
451 poll_ok = false;
452 } else {
453 /* There was something to do, next time we'll just poll */
454 poll_ok = true;
457 /* This is a simple back-off algorithm where we immediately
458 * re-poll if we did something this time around.
460 * This greatly improves performance of DCC.
462 poll_ok = poll_ok || target_got_message();
464 for (service = services; service; service = service->next) {
465 /* handle new connections on listeners */
466 if ((service->fd != -1)
467 && (FD_ISSET(service->fd, &read_fds))) {
468 if (service->max_connections != 0)
469 add_connection(service, command_context);
470 else {
471 if (service->type == CONNECTION_TCP) {
472 struct sockaddr_in sin;
473 socklen_t address_size = sizeof(sin);
474 int tmp_fd;
475 tmp_fd = accept(service->fd,
476 (struct sockaddr *)&service->sin,
477 &address_size);
478 close_socket(tmp_fd);
480 LOG_INFO(
481 "rejected '%s' connection, no more connections allowed",
482 service->name);
486 /* handle activity on connections */
487 if (service->connections) {
488 struct connection *c;
490 for (c = service->connections; c; ) {
491 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending) {
492 retval = service->input(c);
493 if (retval != ERROR_OK) {
494 struct connection *next = c->next;
495 if (service->type == CONNECTION_PIPE ||
496 service->type == CONNECTION_STDINOUT) {
497 /* if connection uses a pipe then
498 * shutdown openocd on error */
499 shutdown_openocd = 1;
501 remove_connection(service, c);
502 LOG_INFO("dropped '%s' connection",
503 service->name);
504 c = next;
505 continue;
508 c = c->next;
513 #ifdef _WIN32
514 MSG msg;
515 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
516 if (msg.message == WM_QUIT)
517 shutdown_openocd = 1;
519 #endif
522 return shutdown_openocd != 2 ? ERROR_OK : ERROR_FAIL;
525 #ifdef _WIN32
526 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
528 shutdown_openocd = 1;
529 return TRUE;
531 #endif
533 void sig_handler(int sig)
535 /* store only first signal that hits us */
536 if (!last_signal)
537 last_signal = sig;
538 shutdown_openocd = 1;
541 int server_preinit(void)
543 /* this currently only calls WSAStartup on native win32 systems
544 * before any socket operations are performed.
545 * This is an issue if you call init in your config script */
547 #ifdef _WIN32
548 WORD wVersionRequested;
549 WSADATA wsaData;
551 wVersionRequested = MAKEWORD(2, 2);
553 if (WSAStartup(wVersionRequested, &wsaData) != 0) {
554 LOG_ERROR("Failed to Open Winsock");
555 exit(-1);
558 /* register ctrl-c handler */
559 SetConsoleCtrlHandler(ControlHandler, TRUE);
561 signal(SIGBREAK, sig_handler);
562 #endif
563 signal(SIGINT, sig_handler);
564 signal(SIGTERM, sig_handler);
565 signal(SIGABRT, sig_handler);
567 return ERROR_OK;
570 int server_init(struct command_context *cmd_ctx)
572 int ret = tcl_init();
573 if (ERROR_OK != ret)
574 return ret;
576 return telnet_init("Open On-Chip Debugger");
579 int server_quit(void)
581 remove_services();
582 target_quit();
584 #ifdef _WIN32
585 WSACleanup();
586 SetConsoleCtrlHandler(ControlHandler, FALSE);
588 return ERROR_OK;
589 #endif
591 /* return signal number so we can kill ourselves */
592 return last_signal;
595 void exit_on_signal(int sig)
597 #ifndef _WIN32
598 /* bring back default system handler and kill yourself */
599 signal(sig, SIG_DFL);
600 kill(getpid(), sig);
601 #endif
604 int connection_write(struct connection *connection, const void *data, int len)
606 if (len == 0) {
607 /* successful no-op. Sockets and pipes behave differently here... */
608 return 0;
610 if (connection->service->type == CONNECTION_TCP)
611 return write_socket(connection->fd_out, data, len);
612 else
613 return write(connection->fd_out, data, len);
616 int connection_read(struct connection *connection, void *data, int len)
618 if (connection->service->type == CONNECTION_TCP)
619 return read_socket(connection->fd, data, len);
620 else
621 return read(connection->fd, data, len);
624 /* tell the server we want to shut down */
625 COMMAND_HANDLER(handle_shutdown_command)
627 LOG_USER("shutdown command invoked");
629 shutdown_openocd = 1;
631 if (CMD_ARGC == 1) {
632 if (!strcmp(CMD_ARGV[0], "error")) {
633 shutdown_openocd = 2;
634 return ERROR_FAIL;
638 return ERROR_COMMAND_CLOSE_CONNECTION;
641 COMMAND_HANDLER(handle_poll_period_command)
643 if (CMD_ARGC == 0)
644 LOG_WARNING("You need to set a period value");
645 else
646 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], polling_period);
648 LOG_INFO("set servers polling period to %ums", polling_period);
650 return ERROR_OK;
653 COMMAND_HANDLER(handle_bindto_command)
655 switch (CMD_ARGC) {
656 case 0:
657 command_print(CMD_CTX, "bindto name: %s", bindto_name);
658 break;
659 case 1:
660 free(bindto_name);
661 bindto_name = strdup(CMD_ARGV[0]);
662 break;
663 default:
664 return ERROR_COMMAND_SYNTAX_ERROR;
666 return ERROR_OK;
669 static const struct command_registration server_command_handlers[] = {
671 .name = "shutdown",
672 .handler = &handle_shutdown_command,
673 .mode = COMMAND_ANY,
674 .usage = "",
675 .help = "shut the server down",
678 .name = "poll_period",
679 .handler = &handle_poll_period_command,
680 .mode = COMMAND_ANY,
681 .usage = "",
682 .help = "set the servers polling period",
685 .name = "bindto",
686 .handler = &handle_bindto_command,
687 .mode = COMMAND_ANY,
688 .usage = "[name]",
689 .help = "Specify address by name on which to listen for "
690 "incoming TCP/IP connections",
692 COMMAND_REGISTRATION_DONE
695 int server_register_commands(struct command_context *cmd_ctx)
697 int retval = telnet_register_commands(cmd_ctx);
698 if (ERROR_OK != retval)
699 return retval;
701 retval = tcl_register_commands(cmd_ctx);
702 if (ERROR_OK != retval)
703 return retval;
705 retval = jsp_register_commands(cmd_ctx);
706 if (ERROR_OK != retval)
707 return retval;
709 return register_commands(cmd_ctx, NULL, server_command_handlers);
712 COMMAND_HELPER(server_port_command, unsigned short *out)
714 switch (CMD_ARGC) {
715 case 0:
716 command_print(CMD_CTX, "%d", *out);
717 break;
718 case 1:
720 uint16_t port;
721 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
722 *out = port;
723 break;
725 default:
726 return ERROR_COMMAND_SYNTAX_ERROR;
728 return ERROR_OK;
731 COMMAND_HELPER(server_pipe_command, char **out)
733 switch (CMD_ARGC) {
734 case 0:
735 command_print(CMD_CTX, "%s", *out);
736 break;
737 case 1:
739 if (CMD_CTX->mode == COMMAND_EXEC) {
740 LOG_WARNING("unable to change server port after init");
741 return ERROR_COMMAND_ARGUMENT_INVALID;
743 free(*out);
744 *out = strdup(CMD_ARGV[0]);
745 break;
747 default:
748 return ERROR_COMMAND_SYNTAX_ERROR;
750 return ERROR_OK;