]> git.sur5r.net Git - openocd/blob - src/openocd.c
server/server: Remove all exit() calls
[openocd] / src / openocd.c
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 Richard Missenden                                  *
9  *   richard.missenden@googlemail.com                                      *
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  ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "openocd.h"
30 #include <jtag/driver.h>
31 #include <jtag/jtag.h>
32 #include <transport/transport.h>
33 #include <helper/ioutil.h>
34 #include <helper/util.h>
35 #include <helper/configuration.h>
36 #include <flash/nor/core.h>
37 #include <flash/nand/core.h>
38 #include <pld/pld.h>
39 #include <flash/mflash.h>
40
41 #include <server/server.h>
42 #include <server/gdb_server.h>
43
44 #ifdef HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
47
48 #ifdef PKGBLDDATE
49 #define OPENOCD_VERSION \
50         "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
51 #else
52 #define OPENOCD_VERSION \
53         "Open On-Chip Debugger " VERSION RELSTR
54 #endif
55
56 static const char openocd_startup_tcl[] = {
57 #include "startup_tcl.inc"
58 0 /* Terminate with zero */
59 };
60
61 /* Give scripts and TELNET a way to find out what version this is */
62 static int jim_version_command(Jim_Interp *interp, int argc,
63         Jim_Obj * const *argv)
64 {
65         if (argc > 2)
66                 return JIM_ERR;
67         const char *str = "";
68         char *version_str;
69         version_str = OPENOCD_VERSION;
70
71         if (argc == 2)
72                 str = Jim_GetString(argv[1], NULL);
73
74         if (strcmp("git", str) == 0)
75                 version_str = GITVERSION;
76
77         Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
78
79         return JIM_OK;
80 }
81
82 static int log_target_callback_event_handler(struct target *target,
83         enum target_event event,
84         void *priv)
85 {
86         switch (event) {
87                 case TARGET_EVENT_GDB_START:
88                         target->display = 0;
89                         break;
90                 case TARGET_EVENT_GDB_END:
91                         target->display = 1;
92                         break;
93                 case TARGET_EVENT_HALTED:
94                         if (target->display) {
95                                 /* do not display information when debugger caused the halt */
96                                 target_arch_state(target);
97                         }
98                         break;
99                 default:
100                         break;
101         }
102
103         return ERROR_OK;
104 }
105
106 static bool init_at_startup = true;
107
108 COMMAND_HANDLER(handle_noinit_command)
109 {
110         if (CMD_ARGC != 0)
111                 return ERROR_COMMAND_SYNTAX_ERROR;
112         init_at_startup = false;
113         return ERROR_OK;
114 }
115
116 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
117 COMMAND_HANDLER(handle_init_command)
118 {
119
120         if (CMD_ARGC != 0)
121                 return ERROR_COMMAND_SYNTAX_ERROR;
122
123         int retval;
124         static int initialized;
125         if (initialized)
126                 return ERROR_OK;
127
128         initialized = 1;
129
130         retval = command_run_line(CMD_CTX, "target init");
131         if (ERROR_OK != retval)
132                 return ERROR_FAIL;
133
134         retval = adapter_init(CMD_CTX);
135         if (retval != ERROR_OK) {
136                 /* we must be able to set up the debug adapter */
137                 return retval;
138         }
139
140         LOG_DEBUG("Debug Adapter init complete");
141
142         /* "transport init" verifies the expected devices are present;
143          * for JTAG, it checks the list of configured TAPs against
144          * what's discoverable, possibly with help from the platform's
145          * JTAG event handlers.  (which require COMMAND_EXEC)
146          */
147         command_context_mode(CMD_CTX, COMMAND_EXEC);
148
149         retval = command_run_line(CMD_CTX, "transport init");
150         if (ERROR_OK != retval)
151                 return ERROR_FAIL;
152
153         LOG_DEBUG("Examining targets...");
154         if (target_examine() != ERROR_OK)
155                 LOG_DEBUG("target examination failed");
156
157         command_context_mode(CMD_CTX, COMMAND_CONFIG);
158
159         if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
160                 return ERROR_FAIL;
161
162         if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
163                 return ERROR_FAIL;
164
165         if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
166                 return ERROR_FAIL;
167
168         if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
169                 return ERROR_FAIL;
170         command_context_mode(CMD_CTX, COMMAND_EXEC);
171
172         /* initialize telnet subsystem */
173         gdb_target_add_all(all_targets);
174
175         target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
176
177         return ERROR_OK;
178 }
179
180 COMMAND_HANDLER(handle_add_script_search_dir_command)
181 {
182         if (CMD_ARGC != 1)
183                 return ERROR_COMMAND_SYNTAX_ERROR;
184
185         add_script_search_dir(CMD_ARGV[0]);
186
187         return ERROR_OK;
188 }
189
190 static const struct command_registration openocd_command_handlers[] = {
191         {
192                 .name = "version",
193                 .jim_handler = jim_version_command,
194                 .mode = COMMAND_ANY,
195                 .help = "show program version",
196         },
197         {
198                 .name = "noinit",
199                 .handler = &handle_noinit_command,
200                 .mode = COMMAND_CONFIG,
201                 .help = "Prevent 'init' from being called at startup.",
202                 .usage = ""
203         },
204         {
205                 .name = "init",
206                 .handler = &handle_init_command,
207                 .mode = COMMAND_ANY,
208                 .help = "Initializes configured targets and servers.  "
209                         "Changes command mode from CONFIG to EXEC.  "
210                         "Unless 'noinit' is called, this command is "
211                         "called automatically at the end of startup.",
212                 .usage = ""
213         },
214         {
215                 .name = "add_script_search_dir",
216                 .handler = &handle_add_script_search_dir_command,
217                 .mode = COMMAND_ANY,
218                 .help = "dir to search for config files and scripts",
219                 .usage = "<directory>"
220         },
221         COMMAND_REGISTRATION_DONE
222 };
223
224 static int openocd_register_commands(struct command_context *cmd_ctx)
225 {
226         return register_commands(cmd_ctx, NULL, openocd_command_handlers);
227 }
228
229 struct command_context *global_cmd_ctx;
230
231 /* NB! this fn can be invoked outside this file for non PC hosted builds
232  * NB! do not change to 'static'!!!!
233  */
234 struct command_context *setup_command_handler(Jim_Interp *interp)
235 {
236         log_init();
237         LOG_DEBUG("log_init: complete");
238
239         struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
240
241         /* register subsystem commands */
242         typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
243         static const command_registrant_t command_registrants[] = {
244                 &openocd_register_commands,
245                 &server_register_commands,
246                 &gdb_register_commands,
247                 &log_register_commands,
248                 &transport_register_commands,
249                 &interface_register_commands,
250                 &target_register_commands,
251                 &flash_register_commands,
252                 &nand_register_commands,
253                 &pld_register_commands,
254                 &mflash_register_commands,
255                 NULL
256         };
257         for (unsigned i = 0; NULL != command_registrants[i]; i++) {
258                 int retval = (*command_registrants[i])(cmd_ctx);
259                 if (ERROR_OK != retval) {
260                         command_done(cmd_ctx);
261                         return NULL;
262                 }
263         }
264         LOG_DEBUG("command registration: complete");
265
266         LOG_OUTPUT(OPENOCD_VERSION "\n"
267                 "Licensed under GNU GPL v2\n");
268
269         global_cmd_ctx = cmd_ctx;
270
271         return cmd_ctx;
272 }
273
274 /** OpenOCD runtime meat that can become single-thread in future. It parse
275  * commandline, reads configuration, sets up the target and starts server loop.
276  * Commandline arguments are passed into this function from openocd_main().
277  */
278 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
279 {
280         int ret;
281
282         if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
283                 return ERROR_FAIL;
284
285         if (server_preinit() != ERROR_OK)
286                 return ERROR_FAIL;
287
288         ret = parse_config_file(cmd_ctx);
289         if (ret == ERROR_COMMAND_CLOSE_CONNECTION)
290                 return ERROR_OK;
291         else if (ret != ERROR_OK)
292                 return ERROR_FAIL;
293
294         ret = server_init(cmd_ctx);
295         if (ERROR_OK != ret)
296                 return ERROR_FAIL;
297
298         if (init_at_startup) {
299                 ret = command_run_line(cmd_ctx, "init");
300                 if (ERROR_OK != ret) {
301                         server_quit();
302                         return ERROR_FAIL;
303                 }
304         }
305
306         ret = server_loop(cmd_ctx);
307
308         int last_signal = server_quit();
309         if (last_signal != ERROR_OK)
310                 return last_signal;
311
312         if (ret != ERROR_OK)
313                 return ERROR_FAIL;
314         return ERROR_OK;
315 }
316
317 /* normally this is the main() function entry, but if OpenOCD is linked
318  * into application, then this fn will not be invoked, but rather that
319  * application will have it's own implementation of main(). */
320 int openocd_main(int argc, char *argv[])
321 {
322         int ret;
323
324         /* initialize commandline interface */
325         struct command_context *cmd_ctx;
326
327         cmd_ctx = setup_command_handler(NULL);
328
329         if (util_init(cmd_ctx) != ERROR_OK)
330                 return EXIT_FAILURE;
331
332         if (ioutil_init(cmd_ctx) != ERROR_OK)
333                 return EXIT_FAILURE;
334
335         LOG_OUTPUT("For bug reports, read\n\t"
336                 "http://openocd.org/doc/doxygen/bugs.html"
337                 "\n");
338
339         command_context_mode(cmd_ctx, COMMAND_CONFIG);
340         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
341
342         /* Start the executable meat that can evolve into thread in future. */
343         ret = openocd_thread(argc, argv, cmd_ctx);
344
345         unregister_all_commands(cmd_ctx, NULL);
346
347         /* free commandline interface */
348         command_done(cmd_ctx);
349
350         adapter_quit();
351
352         if (ERROR_FAIL == ret)
353                 return EXIT_FAILURE;
354         else if (ERROR_OK != ret)
355                 exit_on_signal(ret);
356
357         return ret;
358 }