Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /*
0002  * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>.
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
0015  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0017  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
0018  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0019  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0020  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0021  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0022  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0023  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0024  * SUCH DAMAGE.
0025  */
0026 
0027 #ifdef HAVE_CONFIG_H
0028 #include "config.h"
0029 #endif
0030 
0031 #include <errno.h>
0032 #include <inttypes.h>
0033 #include <stdlib.h>
0034 #include <unistd.h>
0035 
0036 #define __need_getopt_newlib
0037 #include <getopt.h>
0038 
0039 #include <rtems.h>
0040 #include <rtems/printer.h>
0041 #include <rtems/shell.h>
0042 
0043 #include <rtems/rtems-debugger.h>
0044 
0045 /*
0046  * Debugger command for the RTEMS shell.
0047  */
0048 
0049 static int rtems_shell_main_debugger(int argc, char *argv[])
0050 {
0051   if (argc == 1) {
0052     printf("RTEMS debugger is %srunning\n", rtems_debugger_running() ? "" : "not ");
0053     return 0;
0054   }
0055 
0056   if (strcasecmp(argv[1], "start") == 0) {
0057     rtems_printer       printer;
0058     const char*         remote = "tcp";
0059     const char*         device = "1122";
0060     int                 timeout = RTEMS_DEBUGGER_TIMEOUT;
0061     rtems_task_priority priority = 1;
0062     bool                verbose = false;
0063     struct getopt_data  data;
0064     char*               end;
0065     int                 r;
0066 
0067     if (rtems_debugger_running()) {
0068       printf("error: debugger already running.\n");
0069       return 1;
0070     }
0071 
0072     memset(&data, 0, sizeof(data));
0073 
0074     rtems_print_printer_fprintf(&printer, stdout);
0075 
0076     argv += 1;
0077     argc -= 1;
0078 
0079     while (true) {
0080       int c;
0081 
0082       c = getopt_r(argc, argv, "vR:d:t:P:l:", &data);
0083       if (c == -1)
0084         break;
0085 
0086       switch (c) {
0087       case 'v':
0088         verbose = true;
0089         break;
0090       case 'R':
0091         remote = data.optarg;
0092         break;
0093       case 'd':
0094         device = data.optarg;
0095         break;
0096       case 't':
0097         timeout = strtoul(data.optarg, &end, 10);
0098         if (timeout == 0 || *end != '\0') {
0099           printf("error: invalid timeout: %s\n", data.optarg);
0100           return 1;
0101         }
0102         break;
0103       case 'P':
0104         priority = strtoul(data.optarg, &end, 10);
0105         if (priority == 0 || *end != '\0') {
0106           printf("error: invalid priority: %s\n", data.optarg);
0107           return 1;
0108         }
0109         break;
0110       case 'l':
0111         if (strcasecmp(data.optarg, "stdout") == 0)
0112           rtems_print_printer_fprintf(&printer, stdout);
0113         else if (strcasecmp(data.optarg, "stderr") == 0)
0114           rtems_print_printer_fprintf(&printer, stderr);
0115         else if (strcasecmp(data.optarg, "kernel") == 0)
0116           rtems_print_printer_printk(&printer);
0117         else {
0118           printf("error: unknown printer (stdout, stderr, kernel): %s\n", data.optarg);
0119           return 1;
0120         }
0121         break;
0122       default:
0123       case '?':
0124         if (data.optarg != NULL)
0125           printf("error: unknown option: %s\n", data.optarg);
0126         else
0127           printf("error: invalid start command\n");
0128         return 1;
0129       }
0130     }
0131 
0132     printf("RTEMS Debugger start: remote=%s device=%s priority=%" PRIu32 "\n",
0133            remote, device, priority);
0134 
0135     r = rtems_debugger_start(remote, device, timeout, priority, &printer);
0136     if (r < 0) {
0137       printf("debugger start failed\n");
0138       return 1;
0139     }
0140 
0141     rtems_debugger_set_verbose(verbose);
0142   }
0143   else if (strcasecmp(argv[1], "stop") == 0) {
0144     int r;
0145 
0146     if (!rtems_debugger_running()) {
0147       printf("error: debugger not running.\n");
0148       return 1;
0149     }
0150 
0151     r = rtems_debugger_stop();
0152     if (r < 0) {
0153       printf("debugger stop failed\n");
0154       return 1;
0155     }
0156   }
0157   else if (strcasecmp(argv[1], "remote-debug") == 0) {
0158     int r;
0159 
0160     if (!rtems_debugger_running()) {
0161       printf("error: debugger not running.\n");
0162       return 1;
0163     }
0164 
0165     if (argc == 3 && strcasecmp(argv[2], "on") == 0) {
0166       r = rtems_debugger_remote_debug(true);
0167       if (r < 0) {
0168         printf("debugger remote-debug on failed\n");
0169         return 1;
0170       }
0171     }
0172     else if (argc == 3 && strcasecmp(argv[2], "off") == 0) {
0173       r = rtems_debugger_remote_debug(false);
0174       if (r < 0) {
0175         printf("debugger remote-debug off failed\n");
0176         return 1;
0177       }
0178     }
0179     else {
0180       printf("debugger remote-debug: not on or off\n");
0181       return 1;
0182     }
0183   }
0184   else if (strcasecmp(argv[1], "verbose") == 0) {
0185     if (!rtems_debugger_running()) {
0186       printf("error: debugger not running.\n");
0187       return 1;
0188     }
0189 
0190     if (argc == 3 && strcasecmp(argv[2], "on") == 0) {
0191       rtems_debugger_set_verbose(true);
0192     }
0193     else if (argc == 3 && strcasecmp(argv[2], "off") == 0) {
0194       rtems_debugger_set_verbose(false);
0195     }
0196     else {
0197       printf("debugger verbose: not on or off\n");
0198       return 1;
0199     }
0200   }
0201   else if (strcasecmp(argv[1], "help") == 0) {
0202     printf("debugger [start/stop/help] ...\n" \
0203            "  start -v -R remote -d device -t secs -P priority -l [stdout,stderr,kernel]\n" \
0204            "  stop\n" \
0205            "  remote-debug <on/off>\n" \
0206            "  verbose <on/off>\n" \
0207            "  help\n");
0208   }
0209   else {
0210     printf("error: unknown command: %s\n", argv[1]);
0211     return 1;
0212   }
0213 
0214   return 0;
0215 }
0216 
0217 rtems_shell_cmd_t rtems_shell_DEBUGGER_Command = {
0218   "debugger",                               /* name */
0219   "debugger [start/stop] [options ...]",    /* usage */
0220   "misc",                                   /* topic */
0221   rtems_shell_main_debugger,                /* command */
0222   NULL,                                     /* alias */
0223   NULL,                                     /* next */
0224   0755,
0225   0,
0226   0
0227 };