User:Qiang0/Debugging

From WiiBrew
< User:Qiang0
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Debugging Homebrew Apps

Prerequisites

  • devkitPro-r19 with devkitPPC, Msys, and examples installed
  • Homebrew channel on your wii console
  • You must be able to compile and run/load the example code in
  /c/devkitPro/examples/wii/devices/network/sockettest


Definitions

In order to make my writing simple and accurate, let's denote the following environment variables, host-name strings, and other things will be mentioned in this document frequently:

  • Msys-window
To start a Msys window, Start-->Programs-->devkitPro-->Msys; all the file/folder path I am talking about in this document uses Msys path.
  • $DEVKITPRO
The location of devkitPro, default is "/c/devkitPro/"
  • $DEVKITPPC
The location of devkitPPC, default is "/c/devkitPro/devkitPPC"; make sure your $PATH contains $DEVKITPPC/bin directory.
  • $WII_DEV_DEUBUG
The full path where the wii_dev_debug.rar is extracted to.
  • $EXAMPLE
The full path to the example of "sockettest", should be "/c/devkitPro/examples/wii/devices/network/sockettest"
  • wii.home.net
The host-name of the wii-console that uses wireless DHCP connection; IP address is ok.
  • $WIILOAD
Required by "$DEVKITPPC/bin/wiiload.exe", should be something like "tcp:wii.home.net"

Use net_print Utilty to Trace Applications

Run the sockettest example

  • open a Msys window
  • "cd $EXAMPLE"
  • "make"
  • start Homebrew on your wii; make sure the Homebrew channel network-loaded service is running; if not, using your wii remote controller to point to the most right-bottom icon of the Homebrew screen, and press "A" button to active it.
  • "wiiload sockettest.elf"
  • the example should be running run your wii
  • use IE to browse the URL http://wii.home.net


Compile/Run the net_print Server

  • "cd $WII_DEV_DEBUG/net_print/server"
  • IF you have MinGW compiling environment, run "make -f Makefile.win"
  • IF you want to compile on Linux/Unix, copy the "server" folder to Linux/Unix, and run "make"
  • IF you trust the $WII_DEV_DEBUG/net_print/server/win32bin/*.exe", you can go server/win32bin folder.
  • Make sure that the "np_server" is working
    • Run "./np_server.exe" or "./np_server"
    • Open another Msys window, change the current directory to where you start the "np_server"; run "./np_client.exe" or "./np_client"; there should be some messages printing on your "np_server" screen; optionally, you can run the "np_client <server-host>" on another machine to make sure your computer's frewall settings are right.
  • Leave the "np_server" running, and open another Msys window, and change the directory to $WII_DEV_DEBUG/net_print/sockettest;
  • Change constant string NET_PRINT_DEFAULT_HOST defined in the header file "source/net_print.h" to point to the host where the "np_server" is running.
  • "make"
  • "wiiload.exe sockettest.elf" or "wiiload.exe sockettest.dol", and you should be seeing some messages printed to the screen of "np_server"


[ TIP: you can start the "np_server" by running "np_server | tee -a np_server.out", so that the output is also saved to the text file.]


Note:

  • The net_print_init() function must be called after the network is ready.
  • he net_print_init()/net_print_string()/net_print_binary() functions are not coded to thread safe; if you want to be thread-safe, you can add a mutex to those functions.


Using gdb to Debug over the network

You have to install another component on your computer: "com0com" and "com2tcp" ( http://com0com.sourceforge.net/ ); I highly recommend you read http://com0com.sourceforge.net/doc/UsingCom0com.pdf ;

I am using:

 com0com-2.2.1.0-i386-fre.zip
 com2tcp-1.3.0.0-368.zip

I assume you have installed com0com and com2tcp (com2tcp is just an executable) under "/c/Program Files/com0com" and created ports using "install COM132 -", so that the output from "Start-->All Programs-->com0com-->setup command prompt", looks like:

 command> list
      CNCA0 PortName=-
      CNCB0 PortName=-
      .......
      CNCA2 PortName=COM132
      CNCB2 PortName=-
 command>


After the creation of the ports, you may need to restart your computer; during the restart, windows will detect those new COM ports, and ask you to confirm the installation the drivers for those ports.


Steps

  • Copy my libdb.a to libogc directory
    • Save your $DEVKITPRO/libogc/lib/wii/libdb.a
    • "cp $WII_DEV_DEBUG/gdb/libdb.a $DEVKITPRO/libogc/lib/wii/libdb.a"
  • "cd $WII_DEV_DEBUG/gdb/sockettest"; in this folder the "makefile" is changed to using the "-g" flag and linking the libdb.a library.
  • Compile the program
    • study the code of "source/sockteset.c" ; the program creates two threads one is "httpd()" while the other one is "debug_thread()"; the "httpd()" calls the "DEBUG_Init()" and "_break()".
    • "make"
  • start the com2tcp
    • open another Msys window, change dirtory to "/c/Program Files/com0com", run:
 com2tcp \\\\.\\CNCB2 wii.home.net 5656
you should see some messages like:
 OpenC0C("\\.\CNCB2", baud=19200, data=8, parity=no, stop=1) - OK
  • run your application "wiiload.exe sockettest.elf" ( I tested you can run the sockettest.dol file too)
  • using a Msys window, "cd $WII_DEV_DEBUG/gdb/sockettest"
  • start gdb by running:
$DEVKITPPC/bin/powerpc-eabi-gdb.exe sockettest.elf
  • in the gdb, type in:
 target remote \\.\COM132
you should see that the program breaks, and the screen looks like:


 GNU gdb (GDB) 7.0
 Copyright (C) 2009 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
 and "show warranty" for details.
 This GDB was configured as "--host=i686-pc-mingw32 --target=powerpc-eabi".
 For bug reporting instructions, please see:
 <http://www.gnu.org/software/gdb/bugs/>...
 Reading symbols from c:\wii_dev_debug\gdb\sockettest/sockettest.elf...done.
 (gdb) target remote \\.\COM132
 Remote debugging using \\.\COM132
 0x8002d3a0 in _break ()
 (gdb) where
 #0  0x8002d3a0 in _break () 
 #1  0x80007194 in httpd (arg=0x800f1ce8)
     at c:/wii_dev_debug/gdb/sockettest/source/sockettest.c:148
 #2  0x80016fa0 in __lwp_thread_handler ()
 #3  0x80016f40 in __lwp_thread_exit ()
 (gdb) break my_func1
 Breakpoint 1 at 0x80006f20: file c:/wii_dev_debug/gdb/sockettest/source/sockett
 st.c, line 38.
 (gdb) break my_func2
 Breakpoint 2 at 0x80006f58: file c:/wii_dev_debug/gdb/sockettest/source/sockett
 st.c, line 45.
 (gdb) cont
 Continuing.
 [New Thread 4]
 [Switching to Thread 4]
 Breakpoint 1, my_func1 (p=4)
 at c:/wii_dev_debug/gdb/sockettest/source/sockettest.c:38
 38              p++;
 (gdb)

Note

  1. the current version of DEBUT_Init(100, port) must be called after your network is ready.
  2. don't try to suspend all the threads, as one of the them is the helper thread for the gdb.
  3. If you know that how to make DEBUG_Init(GDBSTUB_DEVICE_TCP,port) working, please let me know. I did some esearches for the libogc/libdb source files, but never get the DEBUG_Init(GDBSTUB_DEVICE_TCP,port) working; may be we need the USB-ethernet device ?
  4. you can load the .dol file and gdb the .elf file.
  5. the network-gdb works slow for me; if you have a gecko-usb, please let me know the performance comparision.
  6. I have not used this to debug a real application; let me know if you tried so.


Why I am doing this?

About two weeks ago, I installed Homebrew channel and two media player MPlayerCE and GeeXBox. I like both of them very much, especially MPlayerCE. But there are some thing wrong with my Chinese language file/folder name display/play/browse. So I decided to download the source code of MPlayerCE to have a look, after struggling several days of the compiling, I finally get my build up and running with the guidance of MPlayerCE project member .

The next thing I need is a means to debug the application conveniently. So I wrote the small but effective net_print utility, tracing why Chinese chars are not correctly handled by MPlayerCE (0.76 early source), fixed the problems in my environment.

While doing the tracing for the MPlayerCE, I also tried to find a way that I can remotely gdb the Homebrew applications without using the Gecko USB device. After reading the libogc/libdb source code, and some experimental tests. I finally modified the libdb.a to support gdb across network.


Comments and suggestions?

Please email qiang0@gmail.com