gdbserver: Fix exec stop reply reporting conditions
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2015 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "server.h"
20 #include "terminal.h"
21 #include "target.h"
22 #include "gdbthread.h"
23 #include "tdesc.h"
24 #include "dll.h"
25 #include "rsp-low.h"
26 #include <ctype.h>
27 #if HAVE_SYS_IOCTL_H
28 #include <sys/ioctl.h>
29 #endif
30 #if HAVE_SYS_FILE_H
31 #include <sys/file.h>
32 #endif
33 #if HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #if HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #if HAVE_NETDB_H
40 #include <netdb.h>
41 #endif
42 #if HAVE_NETINET_TCP_H
43 #include <netinet/tcp.h>
44 #endif
45 #if HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #if HAVE_SIGNAL_H
49 #include <signal.h>
50 #endif
51 #if HAVE_FCNTL_H
52 #include <fcntl.h>
53 #endif
54 #include "gdb_sys_time.h"
55 #include <unistd.h>
56 #if HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59 #include <sys/stat.h>
60
61 #if USE_WIN32API
62 #include <winsock2.h>
63 #endif
64
65 #if __QNX__
66 #include <sys/iomgr.h>
67 #endif /* __QNX__ */
68
69 #ifndef HAVE_SOCKLEN_T
70 typedef int socklen_t;
71 #endif
72
73 #ifndef IN_PROCESS_AGENT
74
75 #if USE_WIN32API
76 # define INVALID_DESCRIPTOR INVALID_SOCKET
77 #else
78 # define INVALID_DESCRIPTOR -1
79 #endif
80
81 /* Extra value for readchar_callback. */
82 enum {
83 /* The callback is currently not scheduled. */
84 NOT_SCHEDULED = -1
85 };
86
87 /* Status of the readchar callback.
88 Either NOT_SCHEDULED or the callback id. */
89 static int readchar_callback = NOT_SCHEDULED;
90
91 static int readchar (void);
92 static void reset_readchar (void);
93 static void reschedule (void);
94
95 /* A cache entry for a successfully looked-up symbol. */
96 struct sym_cache
97 {
98 char *name;
99 CORE_ADDR addr;
100 struct sym_cache *next;
101 };
102
103 int remote_debug = 0;
104 struct ui_file *gdb_stdlog;
105
106 static int remote_is_stdio = 0;
107
108 static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR;
109 static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR;
110
111 /* FIXME headerize? */
112 extern int using_threads;
113 extern int debug_threads;
114
115 /* If true, then GDB has requested noack mode. */
116 int noack_mode = 0;
117 /* If true, then we tell GDB to use noack mode by default. */
118 int transport_is_reliable = 0;
119
120 #ifdef USE_WIN32API
121 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
122 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
123 #endif
124
125 int
126 gdb_connected (void)
127 {
128 return remote_desc != INVALID_DESCRIPTOR;
129 }
130
131 /* Return true if the remote connection is over stdio. */
132
133 int
134 remote_connection_is_stdio (void)
135 {
136 return remote_is_stdio;
137 }
138
139 static void
140 enable_async_notification (int fd)
141 {
142 #if defined(F_SETFL) && defined (FASYNC)
143 int save_fcntl_flags;
144
145 save_fcntl_flags = fcntl (fd, F_GETFL, 0);
146 fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
147 #if defined (F_SETOWN)
148 fcntl (fd, F_SETOWN, getpid ());
149 #endif
150 #endif
151 }
152
153 static int
154 handle_accept_event (int err, gdb_client_data client_data)
155 {
156 struct sockaddr_in sockaddr;
157 socklen_t tmp;
158
159 if (debug_threads)
160 debug_printf ("handling possible accept event\n");
161
162 tmp = sizeof (sockaddr);
163 remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
164 if (remote_desc == -1)
165 perror_with_name ("Accept failed");
166
167 /* Enable TCP keep alive process. */
168 tmp = 1;
169 setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
170 (char *) &tmp, sizeof (tmp));
171
172 /* Tell TCP not to delay small packets. This greatly speeds up
173 interactive response. */
174 tmp = 1;
175 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
176 (char *) &tmp, sizeof (tmp));
177
178 #ifndef USE_WIN32API
179 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
180 exits when the remote side dies. */
181 #endif
182
183 if (run_once)
184 {
185 #ifndef USE_WIN32API
186 close (listen_desc); /* No longer need this */
187 #else
188 closesocket (listen_desc); /* No longer need this */
189 #endif
190 }
191
192 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
193 descriptor open for add_file_handler to wait for a new connection. */
194 delete_file_handler (listen_desc);
195
196 /* Convert IP address to string. */
197 fprintf (stderr, "Remote debugging from host %s\n",
198 inet_ntoa (sockaddr.sin_addr));
199
200 enable_async_notification (remote_desc);
201
202 /* Register the event loop handler. */
203 add_file_handler (remote_desc, handle_serial_event, NULL);
204
205 /* We have a new GDB connection now. If we were disconnected
206 tracing, there's a window where the target could report a stop
207 event to the event loop, and since we have a connection now, we'd
208 try to send vStopped notifications to GDB. But, don't do that
209 until GDB as selected all-stop/non-stop, and has queried the
210 threads' status ('?'). */
211 target_async (0);
212
213 return 0;
214 }
215
216 /* Prepare for a later connection to a remote debugger.
217 NAME is the filename used for communication. */
218
219 void
220 remote_prepare (char *name)
221 {
222 char *port_str;
223 #ifdef USE_WIN32API
224 static int winsock_initialized;
225 #endif
226 int port;
227 struct sockaddr_in sockaddr;
228 socklen_t tmp;
229 char *port_end;
230
231 remote_is_stdio = 0;
232 if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
233 {
234 /* We need to record fact that we're using stdio sooner than the
235 call to remote_open so start_inferior knows the connection is
236 via stdio. */
237 remote_is_stdio = 1;
238 transport_is_reliable = 1;
239 return;
240 }
241
242 port_str = strchr (name, ':');
243 if (port_str == NULL)
244 {
245 transport_is_reliable = 0;
246 return;
247 }
248
249 port = strtoul (port_str + 1, &port_end, 10);
250 if (port_str[1] == '\0' || *port_end != '\0')
251 error ("Bad port argument: %s", name);
252
253 #ifdef USE_WIN32API
254 if (!winsock_initialized)
255 {
256 WSADATA wsad;
257
258 WSAStartup (MAKEWORD (1, 0), &wsad);
259 winsock_initialized = 1;
260 }
261 #endif
262
263 listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
264 if (listen_desc == -1)
265 perror_with_name ("Can't open socket");
266
267 /* Allow rapid reuse of this port. */
268 tmp = 1;
269 setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
270 sizeof (tmp));
271
272 sockaddr.sin_family = PF_INET;
273 sockaddr.sin_port = htons (port);
274 sockaddr.sin_addr.s_addr = INADDR_ANY;
275
276 if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
277 || listen (listen_desc, 1))
278 perror_with_name ("Can't bind address");
279
280 transport_is_reliable = 1;
281 }
282
283 /* Open a connection to a remote debugger.
284 NAME is the filename used for communication. */
285
286 void
287 remote_open (char *name)
288 {
289 char *port_str;
290
291 port_str = strchr (name, ':');
292 #ifdef USE_WIN32API
293 if (port_str == NULL)
294 error ("Only <host>:<port> is supported on this platform.");
295 #endif
296
297 if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
298 {
299 fprintf (stderr, "Remote debugging using stdio\n");
300
301 /* Use stdin as the handle of the connection.
302 We only select on reads, for example. */
303 remote_desc = fileno (stdin);
304
305 enable_async_notification (remote_desc);
306
307 /* Register the event loop handler. */
308 add_file_handler (remote_desc, handle_serial_event, NULL);
309 }
310 #ifndef USE_WIN32API
311 else if (port_str == NULL)
312 {
313 struct stat statbuf;
314
315 if (stat (name, &statbuf) == 0
316 && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
317 remote_desc = open (name, O_RDWR);
318 else
319 {
320 errno = EINVAL;
321 remote_desc = -1;
322 }
323
324 if (remote_desc < 0)
325 perror_with_name ("Could not open remote device");
326
327 #ifdef HAVE_TERMIOS
328 {
329 struct termios termios;
330 tcgetattr (remote_desc, &termios);
331
332 termios.c_iflag = 0;
333 termios.c_oflag = 0;
334 termios.c_lflag = 0;
335 termios.c_cflag &= ~(CSIZE | PARENB);
336 termios.c_cflag |= CLOCAL | CS8;
337 termios.c_cc[VMIN] = 1;
338 termios.c_cc[VTIME] = 0;
339
340 tcsetattr (remote_desc, TCSANOW, &termios);
341 }
342 #endif
343
344 #ifdef HAVE_TERMIO
345 {
346 struct termio termio;
347 ioctl (remote_desc, TCGETA, &termio);
348
349 termio.c_iflag = 0;
350 termio.c_oflag = 0;
351 termio.c_lflag = 0;
352 termio.c_cflag &= ~(CSIZE | PARENB);
353 termio.c_cflag |= CLOCAL | CS8;
354 termio.c_cc[VMIN] = 1;
355 termio.c_cc[VTIME] = 0;
356
357 ioctl (remote_desc, TCSETA, &termio);
358 }
359 #endif
360
361 #ifdef HAVE_SGTTY
362 {
363 struct sgttyb sg;
364
365 ioctl (remote_desc, TIOCGETP, &sg);
366 sg.sg_flags = RAW;
367 ioctl (remote_desc, TIOCSETP, &sg);
368 }
369 #endif
370
371 fprintf (stderr, "Remote debugging using %s\n", name);
372
373 enable_async_notification (remote_desc);
374
375 /* Register the event loop handler. */
376 add_file_handler (remote_desc, handle_serial_event, NULL);
377 }
378 #endif /* USE_WIN32API */
379 else
380 {
381 int port;
382 socklen_t len;
383 struct sockaddr_in sockaddr;
384
385 len = sizeof (sockaddr);
386 if (getsockname (listen_desc,
387 (struct sockaddr *) &sockaddr, &len) < 0
388 || len < sizeof (sockaddr))
389 perror_with_name ("Can't determine port");
390 port = ntohs (sockaddr.sin_port);
391
392 fprintf (stderr, "Listening on port %d\n", port);
393 fflush (stderr);
394
395 /* Register the event loop handler. */
396 add_file_handler (listen_desc, handle_accept_event, NULL);
397 }
398 }
399
400 void
401 remote_close (void)
402 {
403 delete_file_handler (remote_desc);
404
405 #ifdef USE_WIN32API
406 closesocket (remote_desc);
407 #else
408 if (! remote_connection_is_stdio ())
409 close (remote_desc);
410 #endif
411 remote_desc = INVALID_DESCRIPTOR;
412
413 reset_readchar ();
414 }
415
416 #endif
417
418 #ifndef IN_PROCESS_AGENT
419
420 void
421 decode_address (CORE_ADDR *addrp, const char *start, int len)
422 {
423 CORE_ADDR addr;
424 char ch;
425 int i;
426
427 addr = 0;
428 for (i = 0; i < len; i++)
429 {
430 ch = start[i];
431 addr = addr << 4;
432 addr = addr | (fromhex (ch) & 0x0f);
433 }
434 *addrp = addr;
435 }
436
437 const char *
438 decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
439 {
440 const char *end;
441
442 end = start;
443 while (*end != '\0' && *end != ';')
444 end++;
445
446 decode_address (addrp, start, end - start);
447
448 if (*end == ';')
449 end++;
450 return end;
451 }
452
453 #endif
454
455 #ifndef IN_PROCESS_AGENT
456
457 /* Look for a sequence of characters which can be run-length encoded.
458 If there are any, update *CSUM and *P. Otherwise, output the
459 single character. Return the number of characters consumed. */
460
461 static int
462 try_rle (char *buf, int remaining, unsigned char *csum, char **p)
463 {
464 int n;
465
466 /* Always output the character. */
467 *csum += buf[0];
468 *(*p)++ = buf[0];
469
470 /* Don't go past '~'. */
471 if (remaining > 97)
472 remaining = 97;
473
474 for (n = 1; n < remaining; n++)
475 if (buf[n] != buf[0])
476 break;
477
478 /* N is the index of the first character not the same as buf[0].
479 buf[0] is counted twice, so by decrementing N, we get the number
480 of characters the RLE sequence will replace. */
481 n--;
482
483 if (n < 3)
484 return 1;
485
486 /* Skip the frame characters. The manual says to skip '+' and '-'
487 also, but there's no reason to. Unfortunately these two unusable
488 characters double the encoded length of a four byte zero
489 value. */
490 while (n + 29 == '$' || n + 29 == '#')
491 n--;
492
493 *csum += '*';
494 *(*p)++ = '*';
495 *csum += n + 29;
496 *(*p)++ = n + 29;
497
498 return n + 1;
499 }
500
501 #endif
502
503 #ifndef IN_PROCESS_AGENT
504
505 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
506
507 char *
508 write_ptid (char *buf, ptid_t ptid)
509 {
510 int pid, tid;
511
512 if (multi_process)
513 {
514 pid = ptid_get_pid (ptid);
515 if (pid < 0)
516 buf += sprintf (buf, "p-%x.", -pid);
517 else
518 buf += sprintf (buf, "p%x.", pid);
519 }
520 tid = ptid_get_lwp (ptid);
521 if (tid < 0)
522 buf += sprintf (buf, "-%x", -tid);
523 else
524 buf += sprintf (buf, "%x", tid);
525
526 return buf;
527 }
528
529 ULONGEST
530 hex_or_minus_one (char *buf, char **obuf)
531 {
532 ULONGEST ret;
533
534 if (startswith (buf, "-1"))
535 {
536 ret = (ULONGEST) -1;
537 buf += 2;
538 }
539 else
540 buf = unpack_varlen_hex (buf, &ret);
541
542 if (obuf)
543 *obuf = buf;
544
545 return ret;
546 }
547
548 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
549 passed the last parsed char. Returns null_ptid on error. */
550 ptid_t
551 read_ptid (char *buf, char **obuf)
552 {
553 char *p = buf;
554 char *pp;
555 ULONGEST pid = 0, tid = 0;
556
557 if (*p == 'p')
558 {
559 /* Multi-process ptid. */
560 pp = unpack_varlen_hex (p + 1, &pid);
561 if (*pp != '.')
562 error ("invalid remote ptid: %s\n", p);
563
564 p = pp + 1;
565
566 tid = hex_or_minus_one (p, &pp);
567
568 if (obuf)
569 *obuf = pp;
570 return ptid_build (pid, tid, 0);
571 }
572
573 /* No multi-process. Just a tid. */
574 tid = hex_or_minus_one (p, &pp);
575
576 /* Since GDB is not sending a process id (multi-process extensions
577 are off), then there's only one process. Default to the first in
578 the list. */
579 pid = pid_of (get_first_process ());
580
581 if (obuf)
582 *obuf = pp;
583 return ptid_build (pid, tid, 0);
584 }
585
586 /* Write COUNT bytes in BUF to the client.
587 The result is the number of bytes written or -1 if error.
588 This may return less than COUNT. */
589
590 static int
591 write_prim (const void *buf, int count)
592 {
593 if (remote_connection_is_stdio ())
594 return write (fileno (stdout), buf, count);
595 else
596 return write (remote_desc, buf, count);
597 }
598
599 /* Read COUNT bytes from the client and store in BUF.
600 The result is the number of bytes read or -1 if error.
601 This may return less than COUNT. */
602
603 static int
604 read_prim (void *buf, int count)
605 {
606 if (remote_connection_is_stdio ())
607 return read (fileno (stdin), buf, count);
608 else
609 return read (remote_desc, buf, count);
610 }
611
612 /* Send a packet to the remote machine, with error checking.
613 The data of the packet is in BUF, and the length of the
614 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
615
616 static int
617 putpkt_binary_1 (char *buf, int cnt, int is_notif)
618 {
619 int i;
620 unsigned char csum = 0;
621 char *buf2;
622 char *p;
623 int cc;
624
625 buf2 = xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
626
627 /* Copy the packet into buffer BUF2, encapsulating it
628 and giving it a checksum. */
629
630 p = buf2;
631 if (is_notif)
632 *p++ = '%';
633 else
634 *p++ = '$';
635
636 for (i = 0; i < cnt;)
637 i += try_rle (buf + i, cnt - i, &csum, &p);
638
639 *p++ = '#';
640 *p++ = tohex ((csum >> 4) & 0xf);
641 *p++ = tohex (csum & 0xf);
642
643 *p = '\0';
644
645 /* Send it over and over until we get a positive ack. */
646
647 do
648 {
649 if (write_prim (buf2, p - buf2) != p - buf2)
650 {
651 perror ("putpkt(write)");
652 free (buf2);
653 return -1;
654 }
655
656 if (noack_mode || is_notif)
657 {
658 /* Don't expect an ack then. */
659 if (remote_debug)
660 {
661 if (is_notif)
662 fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2);
663 else
664 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2);
665 fflush (stderr);
666 }
667 break;
668 }
669
670 if (remote_debug)
671 {
672 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
673 fflush (stderr);
674 }
675
676 cc = readchar ();
677
678 if (cc < 0)
679 {
680 free (buf2);
681 return -1;
682 }
683
684 if (remote_debug)
685 {
686 fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc);
687 fflush (stderr);
688 }
689
690 /* Check for an input interrupt while we're here. */
691 if (cc == '\003' && current_thread != NULL)
692 (*the_target->request_interrupt) ();
693 }
694 while (cc != '+');
695
696 free (buf2);
697 return 1; /* Success! */
698 }
699
700 int
701 putpkt_binary (char *buf, int cnt)
702 {
703 return putpkt_binary_1 (buf, cnt, 0);
704 }
705
706 /* Send a packet to the remote machine, with error checking. The data
707 of the packet is in BUF, and the packet should be a NUL-terminated
708 string. Returns >= 0 on success, -1 otherwise. */
709
710 int
711 putpkt (char *buf)
712 {
713 return putpkt_binary (buf, strlen (buf));
714 }
715
716 int
717 putpkt_notif (char *buf)
718 {
719 return putpkt_binary_1 (buf, strlen (buf), 1);
720 }
721
722 /* Come here when we get an input interrupt from the remote side. This
723 interrupt should only be active while we are waiting for the child to do
724 something. Thus this assumes readchar:bufcnt is 0.
725 About the only thing that should come through is a ^C, which
726 will cause us to request child interruption. */
727
728 static void
729 input_interrupt (int unused)
730 {
731 fd_set readset;
732 struct timeval immediate = { 0, 0 };
733
734 /* Protect against spurious interrupts. This has been observed to
735 be a problem under NetBSD 1.4 and 1.5. */
736
737 FD_ZERO (&readset);
738 FD_SET (remote_desc, &readset);
739 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
740 {
741 int cc;
742 char c = 0;
743
744 cc = read_prim (&c, 1);
745
746 if (cc == 0)
747 {
748 fprintf (stderr, "client connection closed\n");
749 return;
750 }
751 else if (cc != 1 || c != '\003')
752 {
753 fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
754 if (isprint (c))
755 fprintf (stderr, "('%c')\n", c);
756 else
757 fprintf (stderr, "('\\x%02x')\n", c & 0xff);
758 return;
759 }
760
761 (*the_target->request_interrupt) ();
762 }
763 }
764
765 /* Check if the remote side sent us an interrupt request (^C). */
766 void
767 check_remote_input_interrupt_request (void)
768 {
769 /* This function may be called before establishing communications,
770 therefore we need to validate the remote descriptor. */
771
772 if (remote_desc == INVALID_DESCRIPTOR)
773 return;
774
775 input_interrupt (0);
776 }
777
778 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
779 accept Control-C from the client, and must be disabled when talking to
780 the client. */
781
782 static void
783 unblock_async_io (void)
784 {
785 #ifndef USE_WIN32API
786 sigset_t sigio_set;
787
788 sigemptyset (&sigio_set);
789 sigaddset (&sigio_set, SIGIO);
790 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
791 #endif
792 }
793
794 #ifdef __QNX__
795 static void
796 nto_comctrl (int enable)
797 {
798 struct sigevent event;
799
800 if (enable)
801 {
802 event.sigev_notify = SIGEV_SIGNAL_THREAD;
803 event.sigev_signo = SIGIO;
804 event.sigev_code = 0;
805 event.sigev_value.sival_ptr = NULL;
806 event.sigev_priority = -1;
807 ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
808 &event);
809 }
810 else
811 ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL);
812 }
813 #endif /* __QNX__ */
814
815
816 /* Current state of asynchronous I/O. */
817 static int async_io_enabled;
818
819 /* Enable asynchronous I/O. */
820 void
821 enable_async_io (void)
822 {
823 if (async_io_enabled)
824 return;
825
826 #ifndef USE_WIN32API
827 signal (SIGIO, input_interrupt);
828 #endif
829 async_io_enabled = 1;
830 #ifdef __QNX__
831 nto_comctrl (1);
832 #endif /* __QNX__ */
833 }
834
835 /* Disable asynchronous I/O. */
836 void
837 disable_async_io (void)
838 {
839 if (!async_io_enabled)
840 return;
841
842 #ifndef USE_WIN32API
843 signal (SIGIO, SIG_IGN);
844 #endif
845 async_io_enabled = 0;
846 #ifdef __QNX__
847 nto_comctrl (0);
848 #endif /* __QNX__ */
849
850 }
851
852 void
853 initialize_async_io (void)
854 {
855 /* Make sure that async I/O starts disabled. */
856 async_io_enabled = 1;
857 disable_async_io ();
858
859 /* Make sure the signal is unblocked. */
860 unblock_async_io ();
861 }
862
863 /* Internal buffer used by readchar.
864 These are global to readchar because reschedule_remote needs to be
865 able to tell whether the buffer is empty. */
866
867 static unsigned char readchar_buf[BUFSIZ];
868 static int readchar_bufcnt = 0;
869 static unsigned char *readchar_bufp;
870
871 /* Returns next char from remote GDB. -1 if error. */
872
873 static int
874 readchar (void)
875 {
876 int ch;
877
878 if (readchar_bufcnt == 0)
879 {
880 readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
881
882 if (readchar_bufcnt <= 0)
883 {
884 if (readchar_bufcnt == 0)
885 fprintf (stderr, "readchar: Got EOF\n");
886 else
887 perror ("readchar");
888
889 return -1;
890 }
891
892 readchar_bufp = readchar_buf;
893 }
894
895 readchar_bufcnt--;
896 ch = *readchar_bufp++;
897 reschedule ();
898 return ch;
899 }
900
901 /* Reset the readchar state machine. */
902
903 static void
904 reset_readchar (void)
905 {
906 readchar_bufcnt = 0;
907 if (readchar_callback != NOT_SCHEDULED)
908 {
909 delete_callback_event (readchar_callback);
910 readchar_callback = NOT_SCHEDULED;
911 }
912 }
913
914 /* Process remaining data in readchar_buf. */
915
916 static int
917 process_remaining (void *context)
918 {
919 int res;
920
921 /* This is a one-shot event. */
922 readchar_callback = NOT_SCHEDULED;
923
924 if (readchar_bufcnt > 0)
925 res = handle_serial_event (0, NULL);
926 else
927 res = 0;
928
929 return res;
930 }
931
932 /* If there is still data in the buffer, queue another event to process it,
933 we can't sleep in select yet. */
934
935 static void
936 reschedule (void)
937 {
938 if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
939 readchar_callback = append_callback_event (process_remaining, NULL);
940 }
941
942 /* Read a packet from the remote machine, with error checking,
943 and store it in BUF. Returns length of packet, or negative if error. */
944
945 int
946 getpkt (char *buf)
947 {
948 char *bp;
949 unsigned char csum, c1, c2;
950 int c;
951
952 while (1)
953 {
954 csum = 0;
955
956 while (1)
957 {
958 c = readchar ();
959 if (c == '$')
960 break;
961 if (remote_debug)
962 {
963 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
964 fflush (stderr);
965 }
966
967 if (c < 0)
968 return -1;
969 }
970
971 bp = buf;
972 while (1)
973 {
974 c = readchar ();
975 if (c < 0)
976 return -1;
977 if (c == '#')
978 break;
979 *bp++ = c;
980 csum += c;
981 }
982 *bp = 0;
983
984 c1 = fromhex (readchar ());
985 c2 = fromhex (readchar ());
986
987 if (csum == (c1 << 4) + c2)
988 break;
989
990 if (noack_mode)
991 {
992 fprintf (stderr,
993 "Bad checksum, sentsum=0x%x, csum=0x%x, "
994 "buf=%s [no-ack-mode, Bad medium?]\n",
995 (c1 << 4) + c2, csum, buf);
996 /* Not much we can do, GDB wasn't expecting an ack/nac. */
997 break;
998 }
999
1000 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1001 (c1 << 4) + c2, csum, buf);
1002 if (write_prim ("-", 1) != 1)
1003 return -1;
1004 }
1005
1006 if (!noack_mode)
1007 {
1008 if (remote_debug)
1009 {
1010 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
1011 fflush (stderr);
1012 }
1013
1014 if (write_prim ("+", 1) != 1)
1015 return -1;
1016
1017 if (remote_debug)
1018 {
1019 fprintf (stderr, "[sent ack]\n");
1020 fflush (stderr);
1021 }
1022 }
1023 else
1024 {
1025 if (remote_debug)
1026 {
1027 fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf);
1028 fflush (stderr);
1029 }
1030 }
1031
1032 return bp - buf;
1033 }
1034
1035 void
1036 write_ok (char *buf)
1037 {
1038 buf[0] = 'O';
1039 buf[1] = 'K';
1040 buf[2] = '\0';
1041 }
1042
1043 void
1044 write_enn (char *buf)
1045 {
1046 /* Some day, we should define the meanings of the error codes... */
1047 buf[0] = 'E';
1048 buf[1] = '0';
1049 buf[2] = '1';
1050 buf[3] = '\0';
1051 }
1052
1053 #endif
1054
1055 #ifndef IN_PROCESS_AGENT
1056
1057 static char *
1058 outreg (struct regcache *regcache, int regno, char *buf)
1059 {
1060 if ((regno >> 12) != 0)
1061 *buf++ = tohex ((regno >> 12) & 0xf);
1062 if ((regno >> 8) != 0)
1063 *buf++ = tohex ((regno >> 8) & 0xf);
1064 *buf++ = tohex ((regno >> 4) & 0xf);
1065 *buf++ = tohex (regno & 0xf);
1066 *buf++ = ':';
1067 collect_register_as_string (regcache, regno, buf);
1068 buf += 2 * register_size (regcache->tdesc, regno);
1069 *buf++ = ';';
1070
1071 return buf;
1072 }
1073
1074 void
1075 new_thread_notify (int id)
1076 {
1077 char own_buf[256];
1078
1079 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1080 if (1)
1081 return;
1082
1083 if (server_waiting == 0)
1084 return;
1085
1086 sprintf (own_buf, "n%x", id);
1087 disable_async_io ();
1088 putpkt (own_buf);
1089 enable_async_io ();
1090 }
1091
1092 void
1093 dead_thread_notify (int id)
1094 {
1095 char own_buf[256];
1096
1097 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1098 if (1)
1099 return;
1100
1101 sprintf (own_buf, "x%x", id);
1102 disable_async_io ();
1103 putpkt (own_buf);
1104 enable_async_io ();
1105 }
1106
1107 void
1108 prepare_resume_reply (char *buf, ptid_t ptid,
1109 struct target_waitstatus *status)
1110 {
1111 if (debug_threads)
1112 debug_printf ("Writing resume reply for %s:%d\n",
1113 target_pid_to_str (ptid), status->kind);
1114
1115 switch (status->kind)
1116 {
1117 case TARGET_WAITKIND_STOPPED:
1118 case TARGET_WAITKIND_FORKED:
1119 case TARGET_WAITKIND_VFORKED:
1120 case TARGET_WAITKIND_VFORK_DONE:
1121 case TARGET_WAITKIND_EXECD:
1122 {
1123 struct thread_info *saved_thread;
1124 const char **regp;
1125 struct regcache *regcache;
1126
1127 if ((status->kind == TARGET_WAITKIND_FORKED && report_fork_events)
1128 || (status->kind == TARGET_WAITKIND_VFORKED && report_vfork_events))
1129 {
1130 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1131 const char *event = (status->kind == TARGET_WAITKIND_FORKED
1132 ? "fork" : "vfork");
1133
1134 sprintf (buf, "T%02x%s:", signal, event);
1135 buf += strlen (buf);
1136 buf = write_ptid (buf, status->value.related_pid);
1137 strcat (buf, ";");
1138 }
1139 else if (status->kind == TARGET_WAITKIND_VFORK_DONE && report_vfork_events)
1140 {
1141 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1142
1143 sprintf (buf, "T%02xvforkdone:;", signal);
1144 }
1145 else if (status->kind == TARGET_WAITKIND_EXECD && report_exec_events)
1146 {
1147 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1148 const char *event = "exec";
1149 char hexified_pathname[PATH_MAX * 2];
1150
1151 sprintf (buf, "T%02x%s:", signal, event);
1152 buf += strlen (buf);
1153
1154 /* Encode pathname to hexified format. */
1155 bin2hex ((const gdb_byte *) status->value.execd_pathname,
1156 hexified_pathname,
1157 strlen (status->value.execd_pathname));
1158
1159 sprintf (buf, "%s;", hexified_pathname);
1160 xfree (status->value.execd_pathname);
1161 status->value.execd_pathname = NULL;
1162 buf += strlen (buf);
1163 }
1164 else
1165 sprintf (buf, "T%02x", status->value.sig);
1166
1167 buf += strlen (buf);
1168
1169 saved_thread = current_thread;
1170
1171 current_thread = find_thread_ptid (ptid);
1172
1173 regp = current_target_desc ()->expedite_regs;
1174
1175 regcache = get_thread_regcache (current_thread, 1);
1176
1177 if (the_target->stopped_by_watchpoint != NULL
1178 && (*the_target->stopped_by_watchpoint) ())
1179 {
1180 CORE_ADDR addr;
1181 int i;
1182
1183 strncpy (buf, "watch:", 6);
1184 buf += 6;
1185
1186 addr = (*the_target->stopped_data_address) ();
1187
1188 /* Convert each byte of the address into two hexadecimal
1189 chars. Note that we take sizeof (void *) instead of
1190 sizeof (addr); this is to avoid sending a 64-bit
1191 address to a 32-bit GDB. */
1192 for (i = sizeof (void *) * 2; i > 0; i--)
1193 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
1194 *buf++ = ';';
1195 }
1196 else if (swbreak_feature && target_stopped_by_sw_breakpoint ())
1197 {
1198 sprintf (buf, "swbreak:;");
1199 buf += strlen (buf);
1200 }
1201 else if (hwbreak_feature && target_stopped_by_hw_breakpoint ())
1202 {
1203 sprintf (buf, "hwbreak:;");
1204 buf += strlen (buf);
1205 }
1206
1207 while (*regp)
1208 {
1209 buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
1210 regp ++;
1211 }
1212 *buf = '\0';
1213
1214 /* Formerly, if the debugger had not used any thread features
1215 we would not burden it with a thread status response. This
1216 was for the benefit of GDB 4.13 and older. However, in
1217 recent GDB versions the check (``if (cont_thread != 0)'')
1218 does not have the desired effect because of sillyness in
1219 the way that the remote protocol handles specifying a
1220 thread. Since thread support relies on qSymbol support
1221 anyway, assume GDB can handle threads. */
1222
1223 if (using_threads && !disable_packet_Tthread)
1224 {
1225 /* This if (1) ought to be unnecessary. But remote_wait
1226 in GDB will claim this event belongs to inferior_ptid
1227 if we do not specify a thread, and there's no way for
1228 gdbserver to know what inferior_ptid is. */
1229 if (1 || !ptid_equal (general_thread, ptid))
1230 {
1231 int core = -1;
1232 /* In non-stop, don't change the general thread behind
1233 GDB's back. */
1234 if (!non_stop)
1235 general_thread = ptid;
1236 sprintf (buf, "thread:");
1237 buf += strlen (buf);
1238 buf = write_ptid (buf, ptid);
1239 strcat (buf, ";");
1240 buf += strlen (buf);
1241
1242 core = target_core_of_thread (ptid);
1243
1244 if (core != -1)
1245 {
1246 sprintf (buf, "core:");
1247 buf += strlen (buf);
1248 sprintf (buf, "%x", core);
1249 strcat (buf, ";");
1250 buf += strlen (buf);
1251 }
1252 }
1253 }
1254
1255 if (dlls_changed)
1256 {
1257 strcpy (buf, "library:;");
1258 buf += strlen (buf);
1259 dlls_changed = 0;
1260 }
1261
1262 current_thread = saved_thread;
1263 }
1264 break;
1265 case TARGET_WAITKIND_EXITED:
1266 if (multi_process)
1267 sprintf (buf, "W%x;process:%x",
1268 status->value.integer, ptid_get_pid (ptid));
1269 else
1270 sprintf (buf, "W%02x", status->value.integer);
1271 break;
1272 case TARGET_WAITKIND_SIGNALLED:
1273 if (multi_process)
1274 sprintf (buf, "X%x;process:%x",
1275 status->value.sig, ptid_get_pid (ptid));
1276 else
1277 sprintf (buf, "X%02x", status->value.sig);
1278 break;
1279 default:
1280 error ("unhandled waitkind");
1281 break;
1282 }
1283 }
1284
1285 void
1286 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
1287 {
1288 int i = 0, j = 0;
1289 char ch;
1290 *mem_addr_ptr = *len_ptr = 0;
1291
1292 while ((ch = from[i++]) != ',')
1293 {
1294 *mem_addr_ptr = *mem_addr_ptr << 4;
1295 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1296 }
1297
1298 for (j = 0; j < 4; j++)
1299 {
1300 if ((ch = from[i++]) == 0)
1301 break;
1302 *len_ptr = *len_ptr << 4;
1303 *len_ptr |= fromhex (ch) & 0x0f;
1304 }
1305 }
1306
1307 void
1308 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
1309 unsigned char **to_p)
1310 {
1311 int i = 0;
1312 char ch;
1313 *mem_addr_ptr = *len_ptr = 0;
1314
1315 while ((ch = from[i++]) != ',')
1316 {
1317 *mem_addr_ptr = *mem_addr_ptr << 4;
1318 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1319 }
1320
1321 while ((ch = from[i++]) != ':')
1322 {
1323 *len_ptr = *len_ptr << 4;
1324 *len_ptr |= fromhex (ch) & 0x0f;
1325 }
1326
1327 if (*to_p == NULL)
1328 *to_p = xmalloc (*len_ptr);
1329
1330 hex2bin (&from[i++], *to_p, *len_ptr);
1331 }
1332
1333 int
1334 decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1335 unsigned int *len_ptr, unsigned char **to_p)
1336 {
1337 int i = 0;
1338 char ch;
1339 *mem_addr_ptr = *len_ptr = 0;
1340
1341 while ((ch = from[i++]) != ',')
1342 {
1343 *mem_addr_ptr = *mem_addr_ptr << 4;
1344 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1345 }
1346
1347 while ((ch = from[i++]) != ':')
1348 {
1349 *len_ptr = *len_ptr << 4;
1350 *len_ptr |= fromhex (ch) & 0x0f;
1351 }
1352
1353 if (*to_p == NULL)
1354 *to_p = xmalloc (*len_ptr);
1355
1356 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1357 *to_p, *len_ptr) != *len_ptr)
1358 return -1;
1359
1360 return 0;
1361 }
1362
1363 /* Decode a qXfer write request. */
1364
1365 int
1366 decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
1367 unsigned int *len, unsigned char *data)
1368 {
1369 char ch;
1370 char *b = buf;
1371
1372 /* Extract the offset. */
1373 *offset = 0;
1374 while ((ch = *buf++) != ':')
1375 {
1376 *offset = *offset << 4;
1377 *offset |= fromhex (ch) & 0x0f;
1378 }
1379
1380 /* Get encoded data. */
1381 packet_len -= buf - b;
1382 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1383 data, packet_len);
1384 return 0;
1385 }
1386
1387 /* Decode the parameters of a qSearch:memory packet. */
1388
1389 int
1390 decode_search_memory_packet (const char *buf, int packet_len,
1391 CORE_ADDR *start_addrp,
1392 CORE_ADDR *search_space_lenp,
1393 gdb_byte *pattern, unsigned int *pattern_lenp)
1394 {
1395 const char *p = buf;
1396
1397 p = decode_address_to_semicolon (start_addrp, p);
1398 p = decode_address_to_semicolon (search_space_lenp, p);
1399 packet_len -= p - buf;
1400 *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1401 pattern, packet_len);
1402 return 0;
1403 }
1404
1405 static void
1406 free_sym_cache (struct sym_cache *sym)
1407 {
1408 if (sym != NULL)
1409 {
1410 free (sym->name);
1411 free (sym);
1412 }
1413 }
1414
1415 void
1416 clear_symbol_cache (struct sym_cache **symcache_p)
1417 {
1418 struct sym_cache *sym, *next;
1419
1420 /* Check the cache first. */
1421 for (sym = *symcache_p; sym; sym = next)
1422 {
1423 next = sym->next;
1424 free_sym_cache (sym);
1425 }
1426
1427 *symcache_p = NULL;
1428 }
1429
1430 /* Get the address of NAME, and return it in ADDRP if found. if
1431 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1432 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1433
1434 int
1435 look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
1436 {
1437 char own_buf[266], *p, *q;
1438 int len;
1439 struct sym_cache *sym;
1440 struct process_info *proc;
1441
1442 proc = current_process ();
1443
1444 /* Check the cache first. */
1445 for (sym = proc->symbol_cache; sym; sym = sym->next)
1446 if (strcmp (name, sym->name) == 0)
1447 {
1448 *addrp = sym->addr;
1449 return 1;
1450 }
1451
1452 /* It might not be an appropriate time to look up a symbol,
1453 e.g. while we're trying to fetch registers. */
1454 if (!may_ask_gdb)
1455 return 0;
1456
1457 /* Send the request. */
1458 strcpy (own_buf, "qSymbol:");
1459 bin2hex ((const gdb_byte *) name, own_buf + strlen ("qSymbol:"),
1460 strlen (name));
1461 if (putpkt (own_buf) < 0)
1462 return -1;
1463
1464 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1465 len = getpkt (own_buf);
1466 if (len < 0)
1467 return -1;
1468
1469 /* We ought to handle pretty much any packet at this point while we
1470 wait for the qSymbol "response". That requires re-entering the
1471 main loop. For now, this is an adequate approximation; allow
1472 GDB to read from memory while it figures out the address of the
1473 symbol. */
1474 while (own_buf[0] == 'm')
1475 {
1476 CORE_ADDR mem_addr;
1477 unsigned char *mem_buf;
1478 unsigned int mem_len;
1479
1480 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
1481 mem_buf = xmalloc (mem_len);
1482 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1483 bin2hex (mem_buf, own_buf, mem_len);
1484 else
1485 write_enn (own_buf);
1486 free (mem_buf);
1487 if (putpkt (own_buf) < 0)
1488 return -1;
1489 len = getpkt (own_buf);
1490 if (len < 0)
1491 return -1;
1492 }
1493
1494 if (!startswith (own_buf, "qSymbol:"))
1495 {
1496 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf);
1497 return -1;
1498 }
1499
1500 p = own_buf + strlen ("qSymbol:");
1501 q = p;
1502 while (*q && *q != ':')
1503 q++;
1504
1505 /* Make sure we found a value for the symbol. */
1506 if (p == q || *q == '\0')
1507 return 0;
1508
1509 decode_address (addrp, p, q - p);
1510
1511 /* Save the symbol in our cache. */
1512 sym = XNEW (struct sym_cache);
1513 sym->name = xstrdup (name);
1514 sym->addr = *addrp;
1515 sym->next = proc->symbol_cache;
1516 proc->symbol_cache = sym;
1517
1518 return 1;
1519 }
1520
1521 /* Relocate an instruction to execute at a different address. OLDLOC
1522 is the address in the inferior memory where the instruction to
1523 relocate is currently at. On input, TO points to the destination
1524 where we want the instruction to be copied (and possibly adjusted)
1525 to. On output, it points to one past the end of the resulting
1526 instruction(s). The effect of executing the instruction at TO
1527 shall be the same as if executing it at OLDLOC. For example, call
1528 instructions that implicitly push the return address on the stack
1529 should be adjusted to return to the instruction after OLDLOC;
1530 relative branches, and other PC-relative instructions need the
1531 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1532
1533 int
1534 relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1535 {
1536 char own_buf[266];
1537 int len;
1538 ULONGEST written = 0;
1539
1540 /* Send the request. */
1541 strcpy (own_buf, "qRelocInsn:");
1542 sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1543 paddress (*to));
1544 if (putpkt (own_buf) < 0)
1545 return -1;
1546
1547 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1548 len = getpkt (own_buf);
1549 if (len < 0)
1550 return -1;
1551
1552 /* We ought to handle pretty much any packet at this point while we
1553 wait for the qRelocInsn "response". That requires re-entering
1554 the main loop. For now, this is an adequate approximation; allow
1555 GDB to access memory. */
1556 while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X')
1557 {
1558 CORE_ADDR mem_addr;
1559 unsigned char *mem_buf = NULL;
1560 unsigned int mem_len;
1561
1562 if (own_buf[0] == 'm')
1563 {
1564 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
1565 mem_buf = xmalloc (mem_len);
1566 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1567 bin2hex (mem_buf, own_buf, mem_len);
1568 else
1569 write_enn (own_buf);
1570 }
1571 else if (own_buf[0] == 'X')
1572 {
1573 if (decode_X_packet (&own_buf[1], len - 1, &mem_addr,
1574 &mem_len, &mem_buf) < 0
1575 || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
1576 write_enn (own_buf);
1577 else
1578 write_ok (own_buf);
1579 }
1580 else
1581 {
1582 decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf);
1583 if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1584 write_ok (own_buf);
1585 else
1586 write_enn (own_buf);
1587 }
1588 free (mem_buf);
1589 if (putpkt (own_buf) < 0)
1590 return -1;
1591 len = getpkt (own_buf);
1592 if (len < 0)
1593 return -1;
1594 }
1595
1596 if (own_buf[0] == 'E')
1597 {
1598 warning ("An error occurred while relocating an instruction: %s\n",
1599 own_buf);
1600 return -1;
1601 }
1602
1603 if (!startswith (own_buf, "qRelocInsn:"))
1604 {
1605 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1606 own_buf);
1607 return -1;
1608 }
1609
1610 unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written);
1611
1612 *to += written;
1613 return 0;
1614 }
1615
1616 void
1617 monitor_output (const char *msg)
1618 {
1619 int len = strlen (msg);
1620 char *buf = xmalloc (len * 2 + 2);
1621
1622 buf[0] = 'O';
1623 bin2hex ((const gdb_byte *) msg, buf + 1, len);
1624
1625 putpkt (buf);
1626 free (buf);
1627 }
1628
1629 #endif
This page took 0.062549 seconds and 4 git commands to generate.