Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:41

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup GDB
0007  *
0008  * @brief pc386 gdb select
0009  *
0010  * This file contains a routine to enable and select the UART the gdb stub
0011  * connects too. Currently limited to COM1 and COM2. See
0012  * shared/comm/i386-stub-glue.c file.
0013  */
0014 
0015 /*
0016  *  COPYRIGHT (c) 2016.
0017  *  Chris Johns <chrisj@rtems.org>
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions
0021  * are met:
0022  * 1. Redistributions of source code must retain the above copyright
0023  *    notice, this list of conditions and the following disclaimer.
0024  * 2. Redistributions in binary form must reproduce the above copyright
0025  *    notice, this list of conditions and the following disclaimer in the
0026  *    documentation and/or other materials provided with the distribution.
0027  *
0028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0038  * POSSIBILITY OF SUCH DAMAGE.
0039  */
0040 
0041 #include <stdlib.h>
0042 #include <limits.h>
0043 
0044 #include <bsp.h>
0045 #include <rtems/libio.h>
0046 #include <rtems/console.h>
0047 #include <rtems/termiostypes.h>
0048 #include <libchip/serial.h>
0049 #include <libchip/ns16550.h>
0050 #include <bsp/bspimpl.h>
0051 
0052 #include "../../shared/dev/serial/legacy-console.h"
0053 
0054 /*
0055  * Used in the stub to print output.
0056  */
0057 int remote_debug;
0058 /*
0059  * Defined in the stub, used here.
0060  */
0061 void set_debug_traps(void);
0062 
0063 /*
0064  * Added here to get a valid baudrate. Needs to go once we
0065  * move to the standard UART driver.
0066  */
0067 int BSPBaseBaud;
0068 
0069 static bool gdb_port_probe(int minor)
0070 {
0071   /* Return false as GDB has claimed the port */
0072   return false;
0073 }
0074 
0075 void pc386_parse_gdb_arguments(void)
0076 {
0077   static const char *opt;
0078 
0079   /*
0080    * Check the command line to see if com1-com4 are disabled.
0081    */
0082   opt = bsp_cmdline_arg("--gdb=");
0083   if ( opt ) {
0084     const char                *option;
0085     const char                *comma;
0086     size_t                     length;
0087     size_t                     index;
0088     rtems_device_minor_number  minor;
0089     uint32_t                   baudrate = 115200;
0090     bool                       halt = false;
0091     console_tbl               *port;
0092 
0093     /*
0094      * Fine the length, there can be more command line visible.
0095      */
0096     length = 0;
0097     while ((opt[length] != ' ') && (opt[length] != '\0')) {
0098       ++length;
0099       if (length > NAME_MAX) {
0100         printk("invalid option (--gdb): too long\n");
0101         return;
0102       }
0103     }
0104 
0105     /*
0106      * Only match up to a comma or NULL
0107      */
0108     index = 0;
0109     while ((opt[index] != '=') && (index < length)) {
0110       ++index;
0111     }
0112 
0113     if (opt[index] != '=') {
0114       printk("invalid option (--gdb): no equals\n");
0115       return;
0116     }
0117 
0118     ++index;
0119     option = &opt[index];
0120 
0121     while ((opt[index] != ',') && (index < length)) {
0122       ++index;
0123     }
0124 
0125     if (opt[index] == ',')
0126       comma = &opt[index];
0127     else
0128       comma = NULL;
0129 
0130     length = &opt[index] - option;
0131 
0132     port = console_find_console_entry( option, length, &minor );
0133 
0134     if ( port == NULL ) {
0135       printk("invalid option (--gdb): port not found\n");
0136       return;
0137     }
0138 
0139     if (comma) {
0140       option = comma + 1;
0141       baudrate = strtoul(option, 0, 10);
0142       switch (baudrate) {
0143         case 115200:
0144         case 57600:
0145         case 38400:
0146         case 19200:
0147         case 9600:
0148         case 4800:
0149           port->pDeviceParams = (void*) baudrate;
0150           BSPBaseBaud = baudrate; /* REMOVE ME */
0151           break;
0152         default:
0153           printk("invalid option (--gdb): bad baudrate\n");
0154           return;
0155       }
0156     }
0157 
0158     /*
0159      * Provide a probe that fails so the device is not part of termios. All
0160      * functions are polling.
0161      */
0162     port->deviceProbe = gdb_port_probe;
0163     port->pDeviceFns = &ns16550_fns_polled;
0164 
0165     opt = bsp_cmdline_arg("--gdb-remote-debug");
0166     if ( opt ) {
0167       remote_debug = 1;
0168     }
0169 
0170     opt = bsp_cmdline_arg("--gdb-break");
0171     if ( opt ) {
0172       halt = true;
0173     }
0174 
0175     printk("GDB stub: enable %s%s%s\n",
0176            port->sDeviceName,
0177            remote_debug ? ", remote-debug" : "",
0178            halt ? ", halting" : "");
0179 
0180     i386_stub_glue_init(minor);
0181     set_debug_traps();
0182     i386_stub_glue_init_breakin();
0183 
0184     if ( halt ) {
0185       printk("GDB stub: waiting for remote connection..\n");
0186       breakpoint();
0187     }
0188   }
0189 }