source: rtems/cpukit/libdebugger/rtems-debugger-cmd.c @ 2465c01

5
Last change on this file since 2465c01 was a0d4e99, checked in by Chris Johns <chrisj@…>, on 11/25/16 at 04:13:36

cpukit: Add libdebugger, a remote debugger agent for GDB.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include <errno.h>
31#include <inttypes.h>
32#include <stdlib.h>
33#include <unistd.h>
34
35#define __need_getopt_newlib
36#include <getopt.h>
37
38#include <rtems.h>
39#include <rtems/printer.h>
40#include <rtems/shell.h>
41
42#include <rtems/rtems-debugger.h>
43
44/*
45 * Debugger command for the RTEMS shell.
46 */
47
48static int rtems_shell_main_debugger(int argc, char *argv[])
49{
50  if (argc == 1) {
51    printf("RTEMS debugger is %srunning\n", rtems_debugger_running() ? "" : "not ");
52    return 0;
53  }
54
55  if (strcasecmp(argv[1], "start") == 0) {
56    rtems_printer       printer;
57    const char*         remote = "tcp";
58    const char*         device = "1122";
59    int                 timeout = RTEMS_DEBUGGER_TIMEOUT;
60    rtems_task_priority priority = 1;
61    bool                verbose = false;
62    struct getopt_data  data;
63    char*               end;
64    int                 r;
65
66    if (rtems_debugger_running()) {
67      printf("error: debugger already running.\n");
68      return 1;
69    }
70
71    memset(&data, 0, sizeof(data));
72
73    rtems_print_printer_fprintf(&printer, stdout);
74
75    argv += 1;
76    argc -= 1;
77
78    while (true) {
79      int c;
80
81      c = getopt_r(argc, argv, "vR:d:t:P:l:", &data);
82      if (c == -1)
83        break;
84
85      switch (c) {
86      case 'v':
87        verbose = true;
88        break;
89      case 'R':
90        remote = data.optarg;
91        break;
92      case 'd':
93        device = data.optarg;
94        break;
95      case 't':
96        timeout = strtoul(data.optarg, &end, 10);
97        if (timeout == 0 || *end != '\0') {
98          printf("error: invalid timeout: %s\n", data.optarg);
99          return 1;
100        }
101        break;
102      case 'P':
103        priority = strtoul(data.optarg, &end, 10);
104        if (priority == 0 || *end != '\0') {
105          printf("error: invalid priority: %s\n", data.optarg);
106          return 1;
107        }
108        break;
109      case 'l':
110        if (strcasecmp(data.optarg, "stdout") == 0)
111          rtems_print_printer_fprintf(&printer, stdout);
112        else if (strcasecmp(data.optarg, "stderr") == 0)
113          rtems_print_printer_fprintf(&printer, stderr);
114        else if (strcasecmp(data.optarg, "kernel") == 0)
115          rtems_print_printer_printk(&printer);
116        else {
117          printf("error: unknown printer (stdout, stderr, kernel): %s\n", data.optarg);
118          return 1;
119        }
120        break;
121      default:
122      case '?':
123        if (data.optarg != NULL)
124          printf("error: unknown option: %s\n", data.optarg);
125        else
126          printf("error: invalid start command\n");
127        return 1;
128      }
129    }
130
131    printf("RTEMS Debugger start: remote=%s device=%s priority=%" PRIu32 "\n",
132           remote, device, priority);
133
134    r = rtems_debugger_start(remote, device, timeout, priority, &printer);
135    if (r < 0) {
136      printf("debugger start failed\n");
137      return 1;
138    }
139
140    rtems_debugger_set_verbose(verbose);
141  }
142  else if (strcasecmp(argv[1], "stop") == 0) {
143    int r;
144
145    if (!rtems_debugger_running()) {
146      printf("error: debugger not running.\n");
147      return 1;
148    }
149
150    r = rtems_debugger_stop();
151    if (r < 0) {
152      printf("debugger stop failed\n");
153      return 1;
154    }
155  }
156  else if (strcasecmp(argv[1], "remote-debug") == 0) {
157    int r;
158
159    if (!rtems_debugger_running()) {
160      printf("error: debugger not running.\n");
161      return 1;
162    }
163
164    if (argc == 3 && strcasecmp(argv[2], "on") == 0) {
165      r = rtems_debugger_remote_debug(true);
166      if (r < 0) {
167        printf("debugger remote-debug on failed\n");
168        return 1;
169      }
170    }
171    else if (argc == 3 && strcasecmp(argv[2], "off") == 0) {
172      r = rtems_debugger_remote_debug(false);
173      if (r < 0) {
174        printf("debugger remote-debug off failed\n");
175        return 1;
176      }
177    }
178    else {
179      printf("debugger remote-debug: not on or off\n");
180      return 1;
181    }
182  }
183  else if (strcasecmp(argv[1], "help") == 0) {
184    printf("debugger [start/stop/help] ...\n" \
185           "  start -v -R remote -d device -t secs -P priority -l [stdout,stderr,kernel]\n" \
186           "  stop\n" \
187           "  remote-debug <on/off>\n" \
188           "  help\n");
189  }
190  else {
191    printf("error: unknown command: %s\n", argv[1]);
192    return 1;
193  }
194
195  return 0;
196}
197
198rtems_shell_cmd_t rtems_shell_DEBUGGER_Command = {
199  "debugger",                               /* name */
200  "debugger [start/stop] [options ...]",    /* usage */
201  "misc",                                   /* topic */
202  rtems_shell_main_debugger,                /* command */
203  NULL,                                     /* alias */
204  NULL,                                     /* next */
205  0755,
206  0,
207  0
208};
Note: See TracBrowser for help on using the repository browser.