gdbderver: fix gdb pipe startup overflow
[openocd.git] / src / helper / options.c
1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "configuration.h"
28 // @todo the inclusion of server.h here is a layering violation
29 #include <server/server.h>
30
31 #include <getopt.h>
32
33 static int help_flag, version_flag;
34
35 static const struct option long_options[] =
36 {
37 {"help", no_argument, &help_flag, 1},
38 {"version", no_argument, &version_flag, 1},
39 {"debug", optional_argument, 0, 'd'},
40 {"file", required_argument, 0, 'f'},
41 {"search", required_argument, 0, 's'},
42 {"log_output", required_argument, 0, 'l'},
43 {"command", required_argument, 0, 'c'},
44 {"pipe", no_argument, 0, 'p'},
45 {0, 0, 0, 0}
46 };
47
48 int configuration_output_handler(struct command_context *context, const char* line)
49 {
50 LOG_USER_N("%s", line);
51
52 return ERROR_OK;
53 }
54
55 static void add_default_dirs(void)
56 {
57 #ifdef _WIN32
58 /* Add the parent of the directory where openocd.exe resides to the
59 * config script search path.
60 * Directory layout:
61 * bin\openocd.exe
62 * lib\openocd
63 * event\at91eb40a_reset.cfg
64 * target\at91eb40a.cfg
65 */
66 {
67 char strExePath [MAX_PATH];
68 GetModuleFileName (NULL, strExePath, MAX_PATH);
69 /* Either this code will *always* work or it will SEGFAULT giving
70 * excellent information on the culprit.
71 */
72 *strrchr(strExePath, '\\') = 0;
73 strcat(strExePath, "\\..");
74 add_script_search_dir(strExePath);
75 }
76 /*
77 * Add support for the default (as of 20091118) layout when
78 * using autotools and cygwin/MinGW to build native binary.
79 * Path separator is converted to UNIX style so that MinGW is
80 * pleased.
81 *
82 * bin/openocd.exe
83 * share/openocd/scripts/interface/dummy.cfg
84 * share/openocd/scripts/target/at91eb40a.cfg
85 */
86 {
87 char strExePath [MAX_PATH];
88 char *p;
89 GetModuleFileName (NULL, strExePath, MAX_PATH);
90 *strrchr(strExePath, '\\') = 0;
91 strcat(strExePath, "/../share/"PACKAGE"/scripts");
92 for (p = strExePath; *p; p++) {
93 if (*p == '\\')
94 *p = '/';
95 }
96 add_script_search_dir(strExePath);
97 }
98 #else
99 /*
100 * The directory containing OpenOCD-supplied scripts should be
101 * listed last in the built-in search order, so the user can
102 * override these scripts with site-specific customizations.
103 */
104
105 const char *home = getenv("HOME");
106
107 if (home)
108 {
109 char *path;
110
111 path = alloc_printf("%s/.openocd", home);
112
113 if (path)
114 {
115 add_script_search_dir(path);
116 free(path);
117 }
118 }
119
120 add_script_search_dir(PKGDATADIR "/site");
121 add_script_search_dir(PKGDATADIR "/scripts");
122 #endif
123 }
124
125 int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
126 {
127 int c;
128 char command_buffer[128];
129
130 while (1)
131 {
132 /* getopt_long stores the option index here. */
133 int option_index = 0;
134
135 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
136
137 /* Detect the end of the options. */
138 if (c == -1)
139 break;
140
141 switch (c)
142 {
143 case 0:
144 break;
145 case 'h': /* --help | -h */
146 help_flag = 1;
147 break;
148 case 'v': /* --version | -v */
149 version_flag = 1;
150 break;
151 case 'f': /* --file | -f */
152 {
153 snprintf(command_buffer, 128, "script {%s}", optarg);
154 add_config_command(command_buffer);
155 break;
156 }
157 case 's': /* --search | -s */
158 add_script_search_dir(optarg);
159 break;
160 case 'd': /* --debug | -d */
161 if (optarg)
162 snprintf(command_buffer, 128, "debug_level %s", optarg);
163 else
164 snprintf(command_buffer, 128, "debug_level 3");
165 command_run_line(cmd_ctx, command_buffer);
166 break;
167 case 'l': /* --log_output | -l */
168 if (optarg)
169 {
170 snprintf(command_buffer, 128, "log_output %s", optarg);
171 command_run_line(cmd_ctx, command_buffer);
172 }
173 break;
174 case 'c': /* --command | -c */
175 if (optarg)
176 {
177 add_config_command(optarg);
178 }
179 break;
180 case 'p':
181 /* to replicate the old syntax this needs to be synchronous
182 * otherwise the gdb stdin will overflow with the warning message */
183 command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
184 LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; log_output openocd.log\"' instead.");
185 break;
186 }
187 }
188
189 if (help_flag)
190 {
191 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
192 LOG_OUTPUT("--help | -h\tdisplay this help\n");
193 LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
194 LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
195 LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
196 LOG_OUTPUT("--debug | -d\tset debug level <0-3>\n");
197 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
198 LOG_OUTPUT("--command | -c\trun <command>\n");
199 exit(-1);
200 }
201
202 if (version_flag)
203 {
204 /* Nothing to do, version gets printed automatically. */
205 // It is not an error to request the VERSION number.
206 exit(0);
207 }
208
209 /* paths specified on the command line take precedence over these
210 * built-in paths
211 */
212 add_default_dirs();
213
214 return ERROR_OK;
215 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)