• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include <stdio.h>
20 #include <getopt.h>
21 #include <gpxe/command.h>
22 #include <gpxe/gdbstub.h>
23 
24 /** @file
25  *
26  * GDB stub command
27  *
28  */
29 
30 /**
31  * "gdbstub" command syntax message
32  *
33  * @v argv		Argument list
34  */
gdbstub_syntax(char ** argv)35 static void gdbstub_syntax ( char **argv ) {
36 	printf ( "Usage:\n"
37 		 "  %s <transport> [<options>...]\n"
38 		 "\n"
39 		 "Start remote debugging using one of the following transports:\n"
40 		 "  serial           use serial port (if compiled in)\n"
41 		 "  udp <interface>  use UDP over network interface (if compiled in)\n",
42 		 argv[0] );
43 }
44 
45 /**
46  * The "gdbstub" command
47  *
48  * @v argc		Argument count
49  * @v argv		Argument list
50  * @ret rc		Exit code
51  */
gdbstub_exec(int argc,char ** argv)52 static int gdbstub_exec ( int argc, char **argv ) {
53 	static struct option longopts[] = {
54 		{ "help", 0, NULL, 'h' },
55 		{ NULL, 0, NULL, 0 },
56 	};
57 	const char *trans_name;
58 	struct gdb_transport *trans;
59 	int c;
60 
61 	/* Parse options */
62 	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
63 		switch ( c ) {
64 		case 'h':
65 			/* Display help text */
66 		default:
67 			/* Unrecognised/invalid option */
68 			gdbstub_syntax ( argv );
69 			return 1;
70 		}
71 	}
72 
73 	/* At least one argument */
74 	if ( optind == argc ) {
75 		gdbstub_syntax ( argv );
76 		return 1;
77 	}
78 
79 	trans_name = argv[optind++];
80 
81 	/* Initialise transport */
82 	trans = find_gdb_transport ( trans_name );
83 	if ( !trans ) {
84 		printf ( "%s: no such transport (is it compiled in?)\n", trans_name );
85 		return 1;
86 	}
87 
88 	if ( trans->init ) {
89 		if ( trans->init ( argc - optind, &argv[optind] ) != 0 ) {
90 			return 1;
91 		}
92 	}
93 
94 	/* Enter GDB stub */
95 	gdbstub_start ( trans );
96 	return 0;
97 }
98 
99 /** GDB stub commands */
100 struct command gdbstub_commands[] __command = {
101 	{
102 		.name = "gdbstub",
103 		.exec = gdbstub_exec,
104 	},
105 };
106