Skip to contentSkip to conversation

Serial port GDB for Xen debugging

PATCH Dan Doucette 2 participants 2 messages Dec 17, 2007 – Dec 19, 2007

lore
Share & tools
lore
#0 lore raw

Hello,

I have had a bit of success in using GDB over the serial port to debug Xen itself. If anyone else is interested in debugging in this fashion, please read on.

I have managed to get the gdb stubs working for x86_64 which enable the user to break into Xen (via the '%' debug key from the Xen console), set/clear break points, and to resume execution without a Xen panic. There are still some stability issues, depending on where you set breakpoints. These will probably require someone with more x86 and Xen experience than myself.

In summary:

- fixed register reads/writes for x86_64. Different register numbering/format than x86_32.
- added SMP pause/resume.
- fixed serial port high-bit multiplexing to avoid sending carriage returns down the wrong pipe.
- misc. other changes such as watchdog enable/disable and do_int3 entry points.

I have tested these changes on xen-unstable/x86_64 using HVM. I have successfully booted up a WinXP guest and broke in and resumed with no errors.

Currently, when stopping execution and resuming, the dom0 linux kernel outputs the 'softlockup' back traces, but these have no permanent effect. I did look into the HVM parameters, specifically the 'delay_for_missed_ticks' option, but this seems to not help the passage of time in dom0.

I am using gdb and the Xen console through a single serial port. In order to get this working, you need to fix the serial port driver (patch below). Also, for serial port demuxing on the gdb host side, I could not get the nsplitd working, and didnt understand how it could be made to work. So after googling for an hour or so and not finding a replacement or simple instructions, I created my own. Probably re-invented the wheel, but just in case someone else is in the same situation, I have attached the source code. It does not require any system configuration or mucking with /etc/xinit.d or /etc/services or anything else.

To compile:

% gcc -Wall ssplitd.c -o ssplitd

To use:

    % ./ssplitd  -t /dev/ttyS0  -b 115200 -l 4001 -h 4002

will setup on ttyS0 at baud rate 115200 and will listen on localhost sockets 4001 for the 'high-bit-unset' serial stream and on 4002 for the 'high-bit-set' serial stream.

For example, if you setup Xen as follows (from grub menu):

     kernel /xen-3.2.gz console=com1H com1=115200,8n1 gdb=com1

The console will use the 'high-bit-set' stream and would be available on localhost port 4002

     % ./tools/misc/xenconsole 127.0.0.1 4002

GDB would use the 'high-bit-unset' stream and would be available on localhost port 4001

% gdb xen-syms
(gdb) target remote 127.0.0.1:4001

If you are interested in debugging the gdb stubs, you can use the '-g X' option to ssplitd. This will cause it to output the gdb protocol strings. I found this a bit helpful when getting things working. You simply specify which serial port stream gdb is on. For the above example you would specify '-g 0' because gdb is using the 'high-bit-unset' serial stream.

The changes required are:

[root@localhost xen-unstable.hg]# hg status
M xen/arch/x86/gdbstub.c
M xen/arch/x86/nmi.c
M xen/arch/x86/setup.c
M xen/arch/x86/traps.c
M xen/arch/x86/x86_64/Makefile
M xen/common/gdbstub.c
M xen/common/keyhandler.c
M xen/drivers/char/serial.c
M xen/include/xen/gdbstub.h
? xen/arch/x86/x86_32/gdbstub.c
? xen/arch/x86/x86_64/gdbstub.c

arch/x86/gdbstub.c

-- Remove the register read/write routines. These are specific to x86_32/x86_64 so moved them to a separate gdbstub.c file in the arch/x86/x86_32/64 directories.

-- Added SMP routines to pause/resume CPUs.

xen/arch/x86/gdbstub.c Mon Dec 17 11:02:36 2007 -0800 renamed from xen/arch/x86/gdbstub.c Fri Dec 14 11:50:24 2007 +0000 +15 -6
diff -r 966a6d3b7408 xen/arch/x86/gdbstub.c
@@ -20,55 +20,26 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 #include <asm/debugger.h>
+#include <xen/delay.h>
+
+
+struct gdb_cpu_info
+{
+    atomic_t paused;
+    atomic_t ack;
+};
+
+static struct gdb_cpu_info gdb_cpu[NR_CPUS];
+static atomic_t gdb_smp_paused_count;
+
+static void gdb_smp_pause(void);
+static void gdb_smp_resume(void);
+
 
 u16
 gdb_arch_signal_num(struct cpu_user_regs *regs, unsigned long cookie)
 {
-    /* XXX */
-    return 1;
-}
-
-void
-gdb_arch_read_reg_array(struct cpu_user_regs *regs, struct gdb_context

*ctx)
-{
-#define GDB_REG(r) gdb_write_to_packet_hex(r, sizeof(r), ctx);

-    GDB_REG(regs->eax);
-    GDB_REG(regs->ecx);
-    GDB_REG(regs->edx);
-    GDB_REG(regs->ebx);
-    GDB_REG(regs->esp);
-    GDB_REG(regs->ebp);
-    GDB_REG(regs->esi);
-    GDB_REG(regs->edi);
-    GDB_REG(regs->eip);
-    GDB_REG(regs->eflags);
-#undef GDB_REG
-#define GDB_SEG_REG(s)  gdb_write_to_packet_hex(s, sizeof(u32), ctx);
-    /* sizeof(segment) = 16bit */
-    /* but gdb requires its return value as 32bit value */
-    GDB_SEG_REG(regs->cs);
-    GDB_SEG_REG(regs->ss);
-    GDB_SEG_REG(regs->ds);
-    GDB_SEG_REG(regs->es);
-    GDB_SEG_REG(regs->fs);
-    GDB_SEG_REG(regs->gs);

-#undef GDB_SEG_REG
- gdb_send_packet(ctx);
-}
-
-void
-gdb_arch_write_reg_array(struct cpu_user_regs *regs, const char* buf,
- struct gdb_context *ctx)
-{
- /* XXX TODO */
- gdb_send_reply("E02", ctx);
-}
-
-void
-gdb_arch_read_reg(unsigned long regnum, struct cpu_user_regs *regs,
- struct gdb_context *ctx)
-{
- gdb_send_reply("", ctx);
+ return 5; /* TRAP signal. see include/gdb/signals.h */
}

/*
@@ -87,17 +58,6 @@ gdb_arch_copy_to_user(void *dest, const

     return __copy_to_user(dest, src, len);

}

-void
-gdb_arch_resume(struct cpu_user_regs *regs,
- unsigned long addr, unsigned long type,
- struct gdb_context *ctx)
-{
- /* XXX */
- if (type == GDB_STEP) {
- gdb_send_reply("S01", ctx);
- }
-}
-
void
gdb_arch_print_state(struct cpu_user_regs *regs)
{
@@ -107,13 +67,107 @@ void
void
gdb_arch_enter(struct cpu_user_regs *regs)
{
- /* nothing */
+ /*
+ * We must pause all other CPUs.
+ */
+ gdb_smp_pause();
}

void
gdb_arch_exit(struct cpu_user_regs *regs)
{
- /* nothing */
+ /*
+ * Resume all CPUs.
+ */
+ gdb_smp_resume();
+}
+
+static void gdb_pause_this_cpu(void *unused)
+{
+ unsigned long flags;
+
+ local_irq_save(flags);
+
+ atomic_set(&gdb_cpu[smp_processor_id()].ack, 1);
+ atomic_inc(&gdb_smp_paused_count);
+
+ while (atomic_read(&gdb_cpu[smp_processor_id()].paused))
+ mdelay(1);
+
+ atomic_dec(&gdb_smp_paused_count);
+ atomic_set(&gdb_cpu[smp_processor_id()].ack, 0);
+
+ /* Restore interrupts */
+ local_irq_restore(flags);
+}
+
+static void gdb_smp_pause(void)
+{
+ int timeout = 100;
+ int cpu;
+
+ for_each_online_cpu(cpu)
+ {
+ atomic_set(&gdb_cpu[cpu].ack, 0);
+ atomic_set(&gdb_cpu[cpu].paused, 1);
+ }
+
+ atomic_set(&gdb_smp_paused_count, 0);
+
+ smp_call_function(gdb_pause_this_cpu, NULL, /* dont wait! */0, 0);
+
+ /* Wait 100ms for all other CPUs to enter pause loop */
+ while ( (atomic_read(&gdb_smp_paused_count) < (num_online_cpus() - 1))
+ && (timeout-- > 0) )
+ mdelay(1);
+
+ if ( atomic_read(&gdb_smp_paused_count) < (num_online_cpus() - 1) )
+ {

+        printk("GDB: Not all CPUs have paused, missing CPUs ");
+        for_each_online_cpu(cpu)
+        {

+ if ( cpu == smp_processor_id() )
+ continue;
+
+ if ( !atomic_read(&gdb_cpu[cpu].ack) )
+ {
+ printk("%d ", cpu);
+ }
+ }
+ printk("\n");
+ }
+}
+
+static void gdb_smp_resume(void)
+{
+ int cpu;
+ int timeout = 100;
+
+ for_each_online_cpu(cpu)
+ {
+ atomic_set(&gdb_cpu[cpu].paused, 0);
+ }
+
+ /* Make sure all CPUs resume */
+ while ( (atomic_read(&gdb_smp_paused_count) > 0)
+ && (timeout-- > 0) )
+ mdelay(1);
+
+ if ( atomic_read(&gdb_smp_paused_count) > 0 )
+ {

+        printk("GDB: Not all CPUs have resumed execution, missing CPUs ");
+        for_each_online_cpu(cpu)
+        {

+ if ( cpu == smp_processor_id() )
+ continue;
+
+ if ( !atomic_read(&gdb_cpu[cpu].ack) )
+ {
+ printk("%d ", cpu);
+ }
+ }
+ printk("\n");
+ }
}

arch/x86/x86_64/gdbstub.c

-- new file to capture 64 bit specific gdb stubs.

/*
* x86_64 -specific gdb stub routines
*
* Copyright (C) 2007 Dan Doucette [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <asm/debugger.h>
#define GDB_REG64(r) gdb_write_to_packet_hex(r, sizeof(u64), ctx) #define GDB_REG32(r) gdb_write_to_packet_hex(r, sizeof(u32), ctx) void
gdb_arch_read_reg_array(struct cpu_user_regs *regs, struct gdb_context *ctx)

{
    GDB_REG64(regs->rax);
    GDB_REG64(regs->rbx);
    GDB_REG64(regs->rcx);
    GDB_REG64(regs->rdx);
    GDB_REG64(regs->rsi);
    GDB_REG64(regs->rdi);
    GDB_REG64(regs->rbp);
    GDB_REG64(regs->rsp);
    GDB_REG64(regs->r8);
    GDB_REG64(regs->r9);
    GDB_REG64(regs->r10);
    GDB_REG64(regs->r11);
    GDB_REG64(regs->r12);
    GDB_REG64(regs->r13);
    GDB_REG64(regs->r14);
    GDB_REG64(regs->r15);
    GDB_REG64(regs->rip);
    GDB_REG32(regs->eflags);
    GDB_REG32(regs->cs);
    GDB_REG32(regs->ss);
    GDB_REG32(regs->ds);
    GDB_REG32(regs->es);
    GDB_REG32(regs->fs);
    GDB_REG32(regs->gs);
    gdb_send_packet(ctx);
}
void
gdb_arch_write_reg_array(struct cpu_user_regs *regs, const char* buf,
                         struct gdb_context *ctx)
{
    gdb_send_reply("", ctx);
}
void
gdb_arch_read_reg(unsigned long regnum, struct cpu_user_regs *regs,
                  struct gdb_context *ctx)
{
    switch (regnum)
    {
        case 0: GDB_REG64(regs->rax); break;
        case 1: GDB_REG64(regs->rbx); break;
        case 2: GDB_REG64(regs->rcx); break;
        case 3: GDB_REG64(regs->rdx); break;
        case 4: GDB_REG64(regs->rsi); break;
        case 5: GDB_REG64(regs->rdi); break;
        case 6: GDB_REG64(regs->rbp); break;
        case 7: GDB_REG64(regs->rsp); break;
        case 8: GDB_REG64(regs->r8); break;
        case 9: GDB_REG64(regs->r9); break;
        case 10: GDB_REG64(regs->r10); break;
        case 11: GDB_REG64(regs->r11); break;
        case 12: GDB_REG64(regs->r12); break;
        case 13: GDB_REG64(regs->r13); break;
        case 14: GDB_REG64(regs->r14); break;
        case 15: GDB_REG64(regs->r15); break;
        case 16: GDB_REG64(regs->rip); break;
        case 17: GDB_REG32(regs->rflags); break;
        case 18: GDB_REG32(regs->cs); break;
        case 19: GDB_REG32(regs->ss); break;
        case 20: GDB_REG32(regs->ds); break;
        case 21: GDB_REG32(regs->es); break;
        case 22: GDB_REG32(regs->fs); break;
        case 23: GDB_REG32(regs->gs); break;
        default:
            GDB_REG64(0xbaadf00ddeadbeef);
            break;
    }
    gdb_send_packet(ctx);
}
void
gdb_arch_write_reg(unsigned long regnum, unsigned long val,
                    struct cpu_user_regs *regs, struct gdb_context *ctx)
{
    switch (regnum)
    {
        case 0: regs->rax = val; break;
        case 1: regs->rbx = val; break;
        case 2: regs->rcx = val; break;
        case 3: regs->rdx = val; break;
        case 4: regs->rsi = val; break;
        case 5: regs->rdi = val; break;
        case 6: regs->rbp = val; break;
        case 7: regs->rsp = val; break;
        case 8: regs->r8 = val; break;
        case 9: regs->r9 = val; break;
        case 10: regs->r10 = val; break;
        case 11: regs->r11 = val; break;
        case 12: regs->r12 = val; break;
        case 13: regs->r13 = val; break;
        case 14: regs->r14 = val; break;
        case 15: regs->r15 = val; break;
        case 16: regs->rip = val; break;
        case 17: regs->rflags = (u32)val; break;
        case 18: regs->cs = (u16)val; break;
        case 19: regs->ss = (u16)val; break;
        case 20: regs->ds = (u16)val; break;
        case 21: regs->es = (u16)val; break;
        case 22: regs->fs = (u16)val; break;
        case 23: regs->gs = (u16)val; break;
        default:
            break;
    }
    gdb_send_reply("OK", ctx);
}

void
gdb_arch_resume(struct cpu_user_regs *regs,
unsigned long addr, unsigned long type, struct gdb_context *ctx)

{
    if ( addr != -1UL )
    {
        regs->rip = addr;
    }
    regs->rflags &= ~X86_EFLAGS_TF;
    /*
     * Set eflags.RF to ensure we do not re-enter.
     */
    regs->rflags |= X86_EFLAGS_RF;
    /*
     * Set the trap flag if we are single stepping.
     */
    if ( type == GDB_STEP )
        regs->rflags |= X86_EFLAGS_TF;
}

xen/arch/x86/x86_64/Makefile

-- add in the newly created gdbstub.o object

xen/arch/x86/x86_64/Makefile Mon Dec 17 11:14:28 2007 -0800 renamed from xen/arch/x86/x86_64/Makefile Fri Dec 14 11:50:24 2007 +0000 +1 -0
diff -r 966a6d3b7408 xen/arch/x86/x86_64/Makefile
@@ -5,6 +5,7 @@ obj-y += mm.o
 obj-y += mm.o
 obj-y += traps.o
 
+obj-$(crash_debug)      += gdbstub.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_COMPAT) += compat_kexec.o
 obj-$(CONFIG_COMPAT) += domain.o

xen/arch/x86/nmi.c

-- externed the opt_watchdog flag to prevent gdb stubs from enabling the watchdog, even though we have selected not to use it via watchdog=0 flag.

xen/arch/x86/nmi.c Mon Dec 17 11:10:23 2007 -0800 renamed from xen/arch/x86/nmi.c Fri Dec 14 11:50:24 2007 +0000 +10 -1
diff -r 966a6d3b7408 xen/arch/x86/nmi.c
@@ -38,6 +38,8 @@ static unsigned int nmi_p4_cccr_val;
 static unsigned int nmi_p4_cccr_val;
 static DEFINE_PER_CPU(struct timer, nmi_timer);
 static DEFINE_PER_CPU(unsigned int, nmi_timer_ticks);
+
+extern int opt_watchdog;
 
 /*
  * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
@@ -169,6 +171,9 @@ static void disable_lapic_nmi_watchdog(v
 
 static void enable_lapic_nmi_watchdog(void)
 {
+    if ( !opt_watchdog )
+        return;
+
     if (nmi_active < 0) {
         nmi_watchdog = NMI_LOCAL_APIC;
         setup_apic_nmi_watchdog();
@@ -351,13 +356,17 @@ static atomic_t watchdog_disable_count =
 
 void watchdog_disable(void)
 {
-    atomic_inc(&watchdog_disable_count);
+    if ( opt_watchdog )
+        atomic_inc(&watchdog_disable_count);
 }
 
 void watchdog_enable(void)
 {
     static unsigned long heartbeat_initialised;
     unsigned int cpu;
+
+    if ( !opt_watchdog )
+        return;
 
     if ( !atomic_dec_and_test(&watchdog_disable_count) ||
          test_and_set_bit(0, &heartbeat_initialised) )

xen/arch/x86/setup.c

-- extern the opt_watchdog flag for use in nmi.c

xen/arch/x86/setup.c Mon Dec 17 11:12:08 2007 -0800 renamed from xen/arch/x86/setup.c Fri Dec 14 11:50:24 2007 +0000 +2 -3
diff -r 966a6d3b7408 xen/arch/x86/setup.c
@@ -72,7 +72,7 @@ integer_param("maxcpus", max_cpus);
 integer_param("maxcpus", max_cpus);
 
 /* opt_watchdog: If true, run a watchdog NMI on each processor. */
-static int opt_watchdog = 0;
+int opt_watchdog = 0;
 boolean_param("watchdog", opt_watchdog);
 
 /* **** Linux config option: propagated to domain0. */
@@ -952,8 +952,7 @@ void __init __start_xen(unsigned long mb
 
     do_initcalls();
 
-    if ( opt_watchdog )
-        watchdog_enable();
+    watchdog_enable();
 
     /* Create initial domain 0. */
     dom0 = domain_create(0, 0, DOM0_SSIDREF);

xen/arch/x86/traps.c

-- added handing for gdb stubs in do_int3
-- changed handler from a system gate to an interrupt gate to ensure interrupts are immediately disabled.

xen/arch/x86/traps.c Mon Dec 17 11:13:00 2007 -0800 renamed from xen/arch/x86/traps.c Fri Dec 14 11:50:24 2007 +0000 +2 -4
diff -r 966a6d3b7408 xen/arch/x86/traps.c
@@ -800,9 +800,8 @@ asmlinkage void do_int3(struct cpu_user_
 
     if ( !guest_mode(regs) )
     {
-        DEBUGGER_trap_fatal(TRAP_int3, regs);
-        show_execution_state(regs);
-        panic("FATAL TRAP: vector = 3 (Int3)\n");
+        __trap_to_gdb(regs, TRAP_int3);
+        return;
     }
 
     do_guest_trap(TRAP_int3, regs, 0);
@@ -2753,7 +2752,7 @@ void __init trap_init(void)
     set_intr_gate(TRAP_divide_error,&divide_error);
     set_intr_gate(TRAP_debug,&debug);
     set_intr_gate(TRAP_nmi,&nmi);
-    set_system_gate(TRAP_int3,&int3);         /* usable from all privileges

*/
+ set_intr_gate(TRAP_int3,&int3); /* usable from all privileges
*/
set_system_gate(TRAP_overflow,&overflow); /* usable from all privileges
*/

     set_intr_gate(TRAP_bounds,&bounds);
     set_intr_gate(TRAP_invalid_op,&invalid_op);

xen/common/gdbstub.c

-- remove calls to smp_send_stop. gdb_arch_enter/exit now implicitly handle SMP pause/resume.
-- add register writes and continue/single step commands to allow them to work.

xen/common/gdbstub.c Mon Dec 17 11:15:55 2007 -0800 renamed from xen/common/gdbstub.c Fri Dec 14 11:50:24 2007 +0000 +32 -18

80 lines not on this page · raw message

xen/common/keyhandler.c

-- modified to simply call into gdb stub, rather than use trap fatal.

xen/common/keyhandler.c Mon Dec 17 11:18:21 2007 -0800 renamed from xen/common/keyhandler.c Fri Dec 14 11:50:24 2007 +0000 +1 -1
diff -r 966a6d3b7408 xen/common/keyhandler.c
@@ -276,7 +276,7 @@ static void do_debug_key(unsigned char k
 static void do_debug_key(unsigned char key, struct cpu_user_regs *regs)
 {
     printk("'%c' pressed -> trapping into debugger\n", key);
-    (void)debugger_trap_fatal(0xf001, regs);
+    __trap_to_gdb(regs, TRAP_int3);
     nop(); /* Prevent the compiler doing tail call
                              optimisation, as that confuses xendbg a
                              bit. */

xen/drivers/char/serial.c

-- fixed 'cooked' mode insert of carriage returns to send them down the correct multiplexed character stream.

xen/drivers/char/serial.c Mon Dec 17 11:19:31 2007 -0800 renamed from xen/drivers/char/serial.c Fri Dec 14 11:50:24 2007 +0000 +14 -3
diff -r 966a6d3b7408 xen/drivers/char/serial.c
@@ -126,6 +126,7 @@ void serial_putc(int handle, char c)
 {
     struct serial_port *port;
     unsigned long flags;
+    char cr;
 
     if ( handle == -1 )
         return;
@@ -137,7 +138,12 @@ void serial_putc(int handle, char c)
     spin_lock_irqsave(&port->tx_lock, flags);
 
     if ( (c == '\n') && (handle & SERHND_COOKED) )
-        __serial_putc(port, '\r');
+    {
+        cr = '\r';
+        if ( handle & SERHND_HI )
+            cr |= 0x80;
+        __serial_putc(port, cr);
+    }
 
     if ( handle & SERHND_HI )
         c |= 0x80;
@@ -153,7 +159,7 @@ void serial_puts(int handle, const char
 {
     struct serial_port *port;
     unsigned long flags;
-    char c;
+    char c, cr;
 
     if ( handle == -1 )
         return;
@@ -167,7 +173,12 @@ void serial_puts(int handle, const char
     while ( (c = *s++) != '\0' )
     {
         if ( (c == '\n') && (handle & SERHND_COOKED) )
-            __serial_putc(port, '\r');
+        {
+            cr = '\r';
+            if ( handle & SERHND_HI )
+                cr |= 0x80;
+            __serial_putc(port, cr);
+        }
 
         if ( handle & SERHND_HI )
             c |= 0x80;

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

/*
* ssplitd.c
*
* Mux/demux incoming and outgoing bytes from a serial port which has MSB
* byte stream multiplexing enabled. Streams are sent to local tcp sockets. *
* High bit set stream is sent/received to tcp port set with -h option.
* High bit unset stream is sent/received to tcp port set with -l option. *
* % ssplitd -t /dev/ttyS0 -b 115200 -h 4014 -l 4015
*
*/

#define MAXNAMLEN 64
#define BUFSZ 64

#define ERROR(fmt, ...)    \
            fprintf(stderr, "%s:%d ", __FILE__, __LINE__);  \
            fprintf(stderr, fmt, ##__VA_ARGS__)

#define MAX(a,b) ((a)>(b)?(a):(b))

/*
* Private Data Types.
*/
struct options

{
    int         h_port;
    int         l_port;
    int         baud;
    char        tty_name[MAXNAMLEN];
};

enum stream
{
STREAM_LO,
STREAM_HI,
STREAM_NONE
};

enum stream_direction
{
STREAM_TO_HOST,
STREAM_FROM_HOST,
STREAM_DIR_NONE
};

/*
* Global Data
*/

static int h_sock = -1;         /* high bit set tcp socket */
static int h_data_sock = -1;    /* high bit set data socket */
static int l_sock = -1;         /* high bit unset tcp socket */
static int l_data_sock = -1;    /* high bit unset data socket */
static int tty_fd = -1;         /* tty file descriptor */

static struct options opts;
static int gdb_print = 0;
static int gdb_print_stream = STREAM_NONE;

static void print_usage(char *prog)

{
    ERROR("Usage: %s <-t /dev/ttySX> <-b baud> <-h high bit set stream port> <-l high bit unset stream port>\n", prog);
}

static void debug_print(char ch, enum stream_direction dir, enum stream str)

{
    static enum stream_direction last_dir = STREAM_DIR_NONE;

    if ( !gdb_print || str != gdb_print_stream )
        return;

    if ( dir != last_dir )
    {
        /* 
         * GDB packet communication has changed direction, 
         * print a message header. 
         */
        switch (dir)
        {
            case STREAM_TO_HOST:
                printf("\ngdb ==> host: ");
                break;
            case STREAM_FROM_HOST:
                printf("\nhost ==> gdb: ");
                break;
            default:
                break;
        }
        last_dir = dir;
    }
    printf("%c", ch);
}

static int setup_socket(int port, int *sfd)

{
    int                 sockfd;
    int                 sopt = 1;
    struct sockaddr_in  sin;

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        ERROR("Cannot open socket on port '%d'\n", port);
        perror("Error: ");
        return -1;
    }

    if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sopt, sizeof (int)) < 0 )
    {
        ERROR("Cannot set socket option (SO_REUSEADDR)\n");
    }

    memset(&sin, 0, sizeof(sin));
    sin.sin_family      = AF_INET;
    sin.sin_port        = htons(port);
    sin.sin_addr.s_addr = INADDR_ANY;

    if ( bind(sockfd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0 )
    {
        ERROR("Cannot bind to port '%d'\n", port);
        perror("Error: ");
        return -1;
    }

    if ( listen(sockfd, 1) < 0 )
    {
        ERROR("Cannot listen on port '%d'\n", port);
        return -1;
    }

    /* 
     * Set the socket as non-blocking so the later call to 'accept' 
     * wont block.
     */
#if 0
    if ( fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0 )
    {
        ERROR("Cannot set O_NONBLOCK flags\n");
        return -1;
    }
#endif
    *sfd = sockfd;
    return 0;
}

static int setup_tty(char *tty_name, int baud, int *nfd)

{
    struct termios  options;
    int             fd;
    speed_t         speed;

    /*
     * Ensure a valid baud rate has been provided.
     */
    switch (baud)
    {
        case 9600:
            speed = B9600;
            break;
        case 19200:
            speed = B19200;
            break;
        case 38400:
            speed = B38400;
            break;
        case 57600:
            speed = B57600;
            break;
        case 115200:
            speed = B115200;
            break;
        default:
            ERROR("Baud rate (%d) is not supported\n", baud);
            return -1;
    }

    fd = open(tty_name, O_RDWR | O_NDELAY);
    if ( fd < 0 )
    {
        ERROR("setup_tty: Unable to open '%s' - ", tty_name);
        perror("Error: ");
        return -1;
    }

    if ( fcntl(fd, F_SETFL, O_NONBLOCK) < 0 )
    {
        ERROR("Cannot set O_NONBLOCK flags\n");
        return -1;
    }

    /* 
     * Configure serial port 
     */
    tcgetattr(fd, &options);

    /*
     * Set the baud rates
     */
    cfsetispeed(&options, speed);
    cfsetospeed(&options, speed);

    /*
     * Enable the receiver and set local mode.
     */
    options.c_cflag |= (CLOCAL | CREAD);

    /* 8n1 is assumed */
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    /* Disable hardware and software flow control */
    options.c_cflag &= ~CRTSCTS;
    options.c_iflag &= ~(IXON | IXOFF | IXANY);

    /* Ensure we are in raw mode for input and output. */
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_oflag &= ~OPOST;

    /*
     * Set the new options for the port.
     */
    if ( tcsetattr(fd, TCSANOW, &options) )
    {
        perror("Failed to set serial port options: ");
        return -1;
    }

    *nfd = fd;
    return 0;
}

static int process_options(int argc, char **argv, struct options *opts)

{
    char    c;

    while ( (c = getopt(argc, argv, "t:b:h:l:g:")) != -1 )
    {
        switch (c)
        {
            case 't':
                strncpy(opts->tty_name, optarg, MAXNAMLEN);
                break;
            case 'b':
                opts->baud = atoi(optarg);
                break;
            case 'h':
                opts->h_port = atoi(optarg);
                break;
            case 'l':
                opts->l_port = atoi(optarg);
                break;
            case 'g':
                gdb_print_stream = atoi(optarg);
                gdb_print = 1;
                break;
            default:
                ERROR("Invalid option '%s'\n", optarg);
                return -1;
        }
    }
    return 0;
}

static int mux_stream(char *buf, int len, int highbit)

{
    int                 i;
    enum stream         str = STREAM_LO;
    unsigned char       chval = 0;
   
    if ( highbit )
    {
        chval = 0x80;
        str = STREAM_HI;
    }
 
    for ( i=0; i < len; i++ )
    {
        debug_print(buf[i], STREAM_TO_HOST, str);
        buf[i] |= chval;
    }

    if ( write(tty_fd, buf, len) < 0 )
    {
        ERROR("Cannot write to tty device\n");
        return -1;
    }
    return 0;
}

static int demux_stream(char *buf, int len)

{
    char    l_buf[BUFSZ];
    char    h_buf[BUFSZ];
    int     l_len = 0;
    int     h_len = 0;
    int     i;

    /*
     * Process each byte, sending bytes with the MSB set to the
     * h_data_sock and ones with the MSB not set to the
     * l_data_sock.
     */
    if ( h_data_sock < 0 && l_data_sock < 0 )
    {
        /* Neither socket is open, dont bother */
        return 0;
    }

    for ( i=0; i < len; i++ )
    {
        if ( buf[i] & 0x80 )
        {
            h_buf[h_len] = buf[i] & 0x7f;
            debug_print(h_buf[h_len], STREAM_FROM_HOST, STREAM_HI);
            h_len++;
        }
        else
        {
            l_buf[l_len] = buf[i];
            debug_print(l_buf[l_len], STREAM_FROM_HOST, STREAM_LO);
            l_len++;
        }
    }

    if ( h_len > 0 && h_data_sock > 0 )
    {
        if ( write(h_data_sock, h_buf, h_len) < 0 )
        {
            ERROR("Cannot write to high-bit-set socket\n");
        }
    }
    if ( l_len > 0 && l_data_sock > 0 )
    {
        if ( write(l_data_sock, l_buf, l_len) < 0 )
        {
            ERROR("Cannot write to high-bit-unset socket\n");
        }
    }

    return 0;
}

static int check_socket_connect(int sockfd, fd_set *rdset, int *newsock)

{
    struct sockaddr_in  from;
    socklen_t           from_len = sizeof(struct sockaddr_in);
    int                 new_socket, rv = -1;

    if ( FD_ISSET(sockfd, rdset) )
    {
        /* There is a connection ready to be accepted */
        if ( (new_socket = accept(sockfd, (struct sockaddr*)&from, &from_len)) < 0 )
        {
            ERROR("Cannot accept connection\n");
            return -1;
        }

        fprintf(stdout, "Accepted connection\n");

        *newsock = new_socket;
        rv = 0;
    }

    return rv;
}

static int check_socket_read(int *sfd, fd_set *rdset, int highbit)

{
    char    buf[BUFSZ];
    int     len = 0;
    int     sockfd = *sfd;

    if ( sockfd > 0 && FD_ISSET(sockfd, rdset) )
    {
        /* Characters available at socket */
        if ( (len = read(sockfd, buf, BUFSZ)) <= 0 )
        {
            ERROR("socket closed unexpectedly\n");
            /* Clear the bit in our I/O mask */
            FD_CLR(sockfd, rdset);
            close(sockfd);
            *sfd = -1;
            return 0;
        }

        if ( mux_stream(buf, len, highbit) )
        {
            ERROR("Cannot mux stream\n");
        }
        FD_SET(sockfd, rdset);
    }

    return len;
}

static int process_streams(void)

{
    fd_set              rdset, exset;
    int                 fd_count, len, nfds;
    char                buf[BUFSZ];

    /*
     * Initialize fd_sets
     */
    FD_ZERO(&rdset);
    FD_ZERO(&exset);

    nfds = MAX(nfds, tty_fd);
    nfds = MAX(nfds, h_sock);
    nfds = MAX(nfds, l_sock);
    nfds++;

    while (1)
    {
        if ( tty_fd > 0 )
            FD_SET(tty_fd, &rdset);
        if ( h_sock > 0 )
            FD_SET(h_sock, &rdset);
        if ( l_sock > 0 )
            FD_SET(l_sock, &rdset);
        if ( h_data_sock > 0 )
            FD_SET(h_data_sock, &rdset);
        if ( l_data_sock > 0 )
            FD_SET(l_data_sock, &rdset);

        fd_count = select(nfds, &rdset, NULL, NULL, NULL);
        if ( fd_count < 0 )
        {
            perror("select: ");
            return -1;
        }
   
        if ( FD_ISSET(tty_fd, &rdset) )
        {
            /* Characters available at tty device for reading */
            if ( (len = read(tty_fd, buf, BUFSZ)) <= 0 )
            {
                /* tty device has closed */
                ERROR("tty device (%s) closed unexpectedly\n", opts.tty_name);
                return -1;
            }
    
            if ( demux_stream(buf, len) )
            {
                ERROR("Cannot demux stream\n");
                return -1;
            }
        }

        check_socket_read(&h_data_sock, &rdset, 1);
        check_socket_read(&l_data_sock, &rdset, 0);

        if ( !check_socket_connect(h_sock, &rdset, &h_data_sock) )
        {
            /* Add the new data socket to our read fd_set */
            FD_SET(h_data_sock, &rdset);
            /* re-compute our nfds value for 'select' */
            nfds = MAX(nfds, h_data_sock) + 1;
        }
    
        if ( !check_socket_connect(l_sock, &rdset, &l_data_sock) )
        {
            /* Add the new data socket to our read fd_set */
            FD_SET(l_data_sock, &rdset);
            /* re-compute our nfds value for 'select' */
            nfds = MAX(nfds, l_data_sock) + 1;
        }
    }
    return 0;
}

int main(int argc, char **argv)

{
    /*
     * Set some default options.
     */
    strcpy(opts.tty_name, "/dev/ttyS0");
    opts.baud = 115200;
    opts.h_port = 4000;
    opts.l_port = 4001;

    if ( process_options(argc, argv, &opts) )
    {
        print_usage(argv[0]);
        return 1;
    }

    /*
     * Initialize and configure serial port device.
     */
    if ( setup_tty(opts.tty_name, opts.baud, &tty_fd) )
    {
        ERROR("Cannot initialize tty '%s'\n", opts.tty_name);
        return 1;
    }

    /*
     * Initialize high bit set socket.
     */
    if ( setup_socket(opts.h_port, &h_sock) )
    {
        ERROR("Cannot initialize high bit set socket on port (%d)\n", opts.h_port);
        return 1;
    }

    /*
     * Initialize high bit clear socket.
     */
    if ( setup_socket(opts.l_port, &l_sock) )
    {
        ERROR("Cannot initialize high bit clear socket on port (%d)\n", opts.l_port);
        return 1;
    }

    //daemonize();

    process_streams();
   
    return 0;
}

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

If you package this up as one or more proper patches, with a signed-off-by line, then they can probably go in the tree with not much work. I took the serial fix immediately.

-- Keir

Quoted 7 lines · Dan Doucette

If you are interested in debugging the gdb stubs, you can use the '-g X' option to ssplitd. This will cause it to output the gdb protocol strings. I found this a bit helpful when getting things working. You simply specify which serial port stream gdb is on. For the above example you would specify '-g 0' because gdb is using the 'high-bit-unset' serial stream.

The changes required are:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel