fix gdb debugging of hypervisor
PATCH 1/1 fix gdb debugging of hypervisor
There are (at least) two things which prevent one from debugging the hypervisor with gdb by setting crash_debug=y in xen/Rules.mk.
arch/x86/gdbstub.c uses copy_{from,to}_user to access hypervisor memory. This is a bit strange, as the comments nearby suggest: it does this so that it can reuse the fixup facility which allows pagefaults to be turned into error returns.
However, evidently since this code was last used by anyone an additional check was added to copy_{from,to}_user: access_ok is called to prevent copy_{from,to}_user from being tricked (by an errant guest) into reading or writing hypervisor memory. This is not appropriate for the gdbstubs. The effect is that all attempts to read and write hypervisor memory from the gdb session fail.
So in the patch below I have changed the calls to use __copy_{from,to}_user, bypassing access_ok.
gdb attempts to read inaccessible memory at startup, at least in my tests. This ought not to be a problem as the aforementioned page fault fixup magic ought just to turn these into errors, which gdb copes with.
However __spurious_page_fault is called fairly early on by the page fault handler - before looking for a fixup. And __spurious_page_fault in turn calls map_domain_page. map_domain_page asserts !in_irq(), which assertion can fail when the page fault happens while debugging, for example if the debugger was entered via the `%' hypervisor console keystroke.
The best way to deal with this seemed to me to make __spurious_page_fault always return false when the debugger is attached (ie, when we are running the debugging stub). The comments surrounding __spurious_page_fault seem to suggest that these only occur when page tables are changed. If that's true then this change is correct since the debugging stubs don't change page tables.
However, if general speculative memory accesses may generate spurious page faults then this change is wrong. I'm sure someone more familiar with the contents of the CPU architecture manuals can answer this question with less effort than me :-). In that case it may be necessary to put the debugger check in the very-widely-used map_domain_page. (Luckily you only bear the cost if you compile with -DCRASH_DEBUG.)
With these two changes, I can get gdb hypervisor debugging to work - at least, in my test I was able to trap into the debugging mode with `%', attach with gdb, and examine some hypervisor static variables.
I haven't spent any effort on getting it to be able to resume normal operation after such a %-interrupt, and that didn't work for me even though it's apparently intended to be supported.
The attached patch also changes the error message printed when debugging is requested with %' but no gdb=... was provided on the command line from dbg_printk (generally a noop) to a printk, and provides a confirmation that %' was pressed. This makes debugger entry a little more positive and the latter may help diagnosis when debugger entry fails for some other reason.
For the record, to get to a working gdb prompt I did these things:
* Set debug=y in Config.mk
* Set crash_debug=y in xen/Rules.mk
* Make the changes in the attached patch, and build.
* Pass gdb=com1 as a hypervisor command line argument
(I already have com1=38400,8n1 console=com1,vga sync_console)
* Boot the system with minicom connected from my workstation via
a null modem cable in the usual way
* In minicom, give the escape character (^A by default) three times
to talk to Xen (Xen prints `(XEN) *** Serial input -> Xen...').
* Press % and observe the messages
(XEN) '%' pressed -> trapping into debugger
(XEN) GDB connection activated.
(XEN) Waiting for GDB to attach...
* Disconnect from minicom without allowing minicom to send any
modem control sequences.
* Start gdb with gdb /path/to/build/tree/xen/xen-syms and then
(gdb) set remotebaud 38400
(gdb) target remote /dev/ttyS0
0x614d12ff in ?? ()
(gdb)As you can see at least the EIP seen by gdb doesn't seem accurate, but that's not important to me right now.
Ian.
xen/common/gdbstub.c +5 -1
@@ -512,7 +512,7 @@ __trap_to_gdb(struct cpu_user_regs *regsif ( gdb_ctx->serhnd <0 ) { -dbg_printk ("Debugger not ready yet.\n" ); +printk ("Debugging connection not set up.\n" );return -EBUSY ; }
@@ -582,6 +582,10 @@ __trap_to_gdb(struct cpu_user_regs *regslocal_irq_restore (flags);return rc; +} + +int debugger_currently_attached (void ) { +return gdb_ctx->connected; }void __init
xen/common/keyhandler.c +1 -0
@@ -275,6 +275,7 @@ extern void perfc_reset(unsigned char kestatic 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);nop ();/* Prevent the compiler doing tail call optimisation, as that confuses xendbg a
xen/arch/x86/gdbstub.c +7 -3
@@ -72,17 +72,21 @@ gdb_arch_read_reg(unsigned long regnum, }/* Like copy_from_user, but safe to call with interrupts disabled. -Trust me, and don't look behind the curtain. */ +Trust me, and don't look behind the curtain. +We use the __ versions to skip the access_ok check, which +would otherwise prevent us from accessing hypervisor memory +(which is the main point, obviously). +*/ unsigned int gdb_arch_copy_from_user (void *dest,const void *src,unsigned len) { -return copy_from_user (dest, src, len); +return __copy_from_user (dest, src, len); }unsigned int gdb_arch_copy_to_user (void *dest,const void *src,unsigned len) { -return copy_to_user (dest, src, len); +return __copy_to_user (dest, src, len); }void
xen/arch/x86/traps.c +7 -0
@@ -913,6 +913,13 @@ static int __spurious_page_fault(l2_pgentry_t l2e, *l2t;l1_pgentry_t l1e, *l1t;unsigned int required_flags, disallowed_flags; + +/* Debugger doesn't change page tables and shouldn't generate +* spurious faults. The debugger's only faults are supposed to be +* from copy_{from,to_user}. +*/ +if (debugger_currently_attached ()) +return 0 ;/* Reserved bit violations are never spurious faults. */ if ( regs->error_code & PFEC_reserved_bit )
xen/include/asm-x86/debugger.h +2 -0
@@ -52,11 +52,13 @@ static inline int debugger_trap_fatal(/* Int3 is a trivial way to gather cpu_user_regs context. */ #define debugger_trap_immediate () __asm__ __volatile__ ( "int3" ); +extern int debugger_currently_attached (void );#else #define debugger_trap_fatal (v, r) (0)#define debugger_trap_immediate () ((void)0) +#define debugger_currently_attached (0)#endif
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 6 lines · Ian Jackson
The best way to deal with this seemed to me to make
__spurious_page_faultalways return false when the debugger is attached (ie, when we are running the debugging stub). The comments surrounding__spurious_page_faultseem to suggest that these only occur when page tables are changed. If that's true then this change is correct since the debugging stubs don't change page tables.
I think it's more obvious to check for in_irq(). That predicate already exists, and pagetables are never updated in IRQ context. And it's exactly the case that __spurious_page_fault() cannot handle. Apart from that the patch is good.
-- Keir
Keir Fraser writes ("Re: [Xen-devel] [PATCH] fix gdb debugging of hypervisor"):
Quoted 4 lines
I think it's more obvious to check for
in_irq(). That predicate already exists, and pagetables are never updated in IRQ context. And it's exactly the case that__spurious_page_fault()cannot handle. Apart from that the patch is good.
I'll resubmit it.
Also, the problem with the register values was an endianness error which will be easy to fix.
Ian.
PATCH 1/1 fix gdb debugging of hypervisor (revised)
This is the revised version of the patch in my message:
Subject: [PATCH] fix gdb debugging of hypervisor
Date: Tue, 11 Dec 2007 17:41:52 +0000
Message-ID: <[email protected]>
This patch:
* enables the gdbstubs to properly access hypervisor memory;
* prevents an assertion failure in __spurious_page_fault's call
to map_domain_page if such accesses fail, by testing in_irq();
* prints some additional helpful messages;
* fixes the endianness of register transfers from the gdbstubs so that gdb is much less confused.
* fixes the documentation in docs/misc/crashdb.txt
Ian.
docs/misc/crashdb.txt +37 -24
@@ -5,31 +5,46 @@ you've crashed it, you get to poke aroun you've crashed it, you get to poke around and find out why. There's also a special key handler for making it crash, which is handy. -You need to have crash_debug=y set when compiling to enable the crash -debugger (so go ``export crash_debug=y; make'', or ``crash_debug=y -make'' or ``make crash_debug=y''), and you also need to enable it on -the Xen command line, by going e.g. cdb=com1. If you need to have a -serial port shared between cdb and the console, try cdb=com1H. CDB -will then set the high bit on every byte it sends, and only respond to -bytes with the high bit set. Similarly for com2. +You need to have crash_debug=y set when compiling , and you also need +to enable it on the Xen command line, eg by gdb=com1. -The next step depends on your individual setup. This is how to do -it for a normal test box in the SRG: +If you need to have a serial port shared between gdb and the console, +you can use gdb=com1H. CDB will then set the high bit on every byte +it sends, and only respond to bytes with the high bit set. Similarly +for com2. If you do this you will need a demultiplexing program on +the debugging workstation, such as perhaps tools/misc/nsplitd. --- Make your test machine crash. Either a normal panic or hitting - 'C-A C-A C-A %' on the serial console will do. --- Start gdb as ``gdb ./xen-syms'' --- Go ``target remote serial.srg:12331'', where 12331 is the second port - reported for that machine by xenuse. (In this case, the machine is - bombjack) --- Go ``add-symbol-file vmlinux'' --- Debug as if you had a core file --- When you're finished, go and reboot your test box. Hitting 'R' on the - serial console won't work. +The next step depends on your individual setup. This is how to do it +if you have a simple null modem connection between the test box and +the workstation, and aren't using a H/L split console: -At one stage, it was sometimes possible to resume after entering the -debugger from the serial console. This seems to have rotted, however, -and I'm not terribly interested in putting it back. + * Set debug=y in Config.mk + * Set crash_debug=y in xen/Rules.mk + * Make the changes in the attached patch, and build. + * Arrange to pass gdb=com1 as a hypervisor command line argument + (I already have com1=38400,8n1 console=com1,vga sync_console) + + * Boot the system with minicom (or your favourite terminal program) + connected from your workstation via a null modem cable in the + usual way. + * In minicom, give the escape character (^A by default) three times + to talk to Xen (Xen prints `(XEN) *** Serial input -> Xen...'). + * Press % and observe the messages + (XEN) '%' pressed -> trapping into debugger + (XEN) GDB connection activated. + (XEN) Waiting for GDB to attach... + * Disconnect from minicom without allowing minicom to send any + modem control sequences. + * Start gdb with gdb /path/to/build/tree/xen/xen-syms and then + (gdb) set remotebaud 38400 + Remote debugging using /dev/ttyS0 + 0xff124d61 in idle_loop () at domain.c:78 + 78 safe_halt(); + (gdb) + +There is code which was once intended to make it possible to resume +after entering the debugger. However this does not presently work; it +has been nonfunctional for quite some time. As soon as you reach the debugger, we disable interrupts, the watchdog, and every other CPU, so the state of the world shouldn't
@@ -44,7 +59,5 @@ Reasons why we might fail to reach the d you're screwed. -- If the page tables are wrong, you're screwed -- If the serial port setup is wrong, badness happens --- We acquire the console lock at one stage XXX this is unnecessary and - stupid -- Obviously, the low level processor state can be screwed in any number of wonderful ways
xen/common/gdbstub.c +16 -2
@@ -43,6 +43,7 @@#include <xen/smp.h> #include <xen/console.h> #include <xen/errno.h> +#include <asm/byteorder.h> /* Printk isn't particularly safe just after we've trapped to the debugger. so avoid it. */
@@ -215,7 +216,7 @@ gdb_write_to_packet_hex(unsigned long x, gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx) {char buf[sizeof (unsigned long ) *2 +1 ]; -int i =sizeof (unsigned long ) *2 ; +int i;int width = int_size *2 ; buf[sizeof (unsigned long ) *2 ] =0 ;
@@ -233,6 +234,8 @@ gdb_write_to_packet_hex(unsigned long x,break ; } +#ifdef __BIG_ENDIAN + i = sizeof(unsigned long) * 2 do { buf[--i] = hex2char(x & 15); x >>= 4;
@@ -242,6 +245,17 @@ gdb_write_to_packet_hex(unsigned long x, buf[--i] ='0' ;gdb_write_to_packet (&buf[i], width, ctx); +#elif defined(__LITTLE_ENDIAN) + i =0 ; +while (i < width) { + buf[i++] =hex2char (x>>4 ); + buf[i++] =hex2char (x); + x >>=8 ; + } +gdb_write_to_packet (buf, width, ctx); +#else +# error unknown endian +#endif } static int
@@ -512,7 +526,7 @@ __trap_to_gdb(struct cpu_user_regs *regsif ( gdb_ctx->serhnd <0 ) { -dbg_printk ("Debugger not ready yet.\n" ); +printk ("Debugging connection not set up.\n" );return -EBUSY ; }
xen/common/keyhandler.c +1 -0
@@ -275,6 +275,7 @@ extern void perfc_reset(unsigned char kestatic 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);nop ();/* Prevent the compiler doing tail call optimisation, as that confuses xendbg a
xen/include/xen/gdbstub.h +1 -0
@@ -53,6 +53,7 @@ void gdb_write_to_packet( const char *buf, int count, struct gdb_context *ctx);void gdb_write_to_packet_hex (unsigned long x,int int_size,struct gdb_context *ctx); +/* ... writes in target native byte order as required by gdb spec. */ void gdb_send_packet (struct gdb_context *ctx);void gdb_send_reply (const char *buf,struct gdb_context *ctx);
xen/arch/x86/gdbstub.c +7 -3
@@ -72,17 +72,21 @@ gdb_arch_read_reg(unsigned long regnum, }/* Like copy_from_user, but safe to call with interrupts disabled. -Trust me, and don't look behind the curtain. */ +Trust me, and don't look behind the curtain. +We use the __ versions to skip the access_ok check, which +would otherwise prevent us from accessing hypervisor memory +(which is the main point, obviously). +*/ unsigned int gdb_arch_copy_from_user (void *dest,const void *src,unsigned len) { -return copy_from_user (dest, src, len); +return __copy_from_user (dest, src, len); }unsigned int gdb_arch_copy_to_user (void *dest,const void *src,unsigned len) { -return copy_to_user (dest, src, len); +return __copy_to_user (dest, src, len); }void
xen/arch/x86/traps.c +6 -1
@@ -784,8 +784,8 @@ asmlinkage int do_invalid_op(struct cpu_ predicate =is_kernel (bug_str.str) ? (char *)bug_str.str :"<unknown>" ;printk ("Assertion '%s' failed at %.50s:%d\n" , predicate, filename, lineno); +show_execution_state (regs);DEBUGGER_trap_fatal (TRAP_invalid_op, regs); -show_execution_state (regs);panic ("Assertion '%s' failed at %.50s:%d\n" , predicate, filename, lineno);
@@ -913,6 +913,11 @@ static int __spurious_page_fault(l2_pgentry_t l2e, *l2t;l1_pgentry_t l1e, *l1t;unsigned int required_flags, disallowed_flags; + +/* We are not supposed to take any spurious page faults in IRQ +* handlers, and map_domain_page asserts !in_irq(), so just give up. */ +if (in_irq ()) +return 0 ;/* Reserved bit violations are never spurious faults. */ if ( regs->error_code & PFEC_reserved_bit )
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Re: [PATCH] fix gdb debugging of hypervisor (revised)
iwj writes ("[Xen-devel] [PATCH] fix gdb debugging of hypervisor (revised)"):
xen/arch/x86/traps.c asmlinkage int do_invalid_op(struct cpu_ · line 790
Quoted 12 lines
diff -r
38a45b7c6cb5xen/arch/x86/traps.cxen/arch/x86/traps.c +1
-1@@ -784,8 +784,8 @@ asmlinkage int do_invalid_op(struct cpu_ predicate =is_kernel (bug_str.str) ? (char *)bug_str.str :"<unknown>" ;printk ("Assertion '%s' failed at %.50s:%d\n" , predicate, filename, lineno); +show_execution_state (regs);DEBUGGER_trap_fatal (TRAP_invalid_op, regs); -show_execution_state (regs);panic ("Assertion '%s' failed at %.50s:%d\n" , predicate, filename, lineno);
Please don't apply that hunk, it was included by mistake.
Ian.