User:Qiang0/Debugging
This is an old revision of this page, as edited by Lukegb (talk | contribs) at 15:11, 5 December 2009. It may differ significantly from the current revision. |
Debugging Homebrew apps without Gecko USB
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
- Optional: MinGW compiling environment
- Windows XP Pro 32-bit
- The wii_dev_debug.rar ( http://qianglin.dyndns.org/wii_dev/wii_dev_debug.rar )
- (the above linke is hosted on a linux platform in my home, it might not available all the time; if you have a better place, please let me know)
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_DEBUG
- The full path where the wii_dev_debug.rar is extracted to, such as "/c/wii_dev_debug".
- $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 apps
This utility provides the following functions:
int net_print_init(const char *rhost, unsigned short port); int net_print_string( const char* file, int line, const char* format, ...); int net_print_binary( int format, const void* binary, int len);
With those functions, you are able to print the trace messages over to the screen of a computer on the network.
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.
- retstart the "np_server", 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.]
Restrictions
- the net_print_init() function must be called after the network is ready.
- the 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 net_print_string(), if the size of a mesage is over 511, you may corrupt the C stack; also if you are running on a very tight stack size, you may want to reduce the constant in net_print.c.
Using gdb to debug over the network
Install com0com/tcp2com
Since the gdb tool only knows how to talk to the remote host through serial port. In order to bridge the serial-port and network together, we have to find a tool that can do the job. Searching the internet, I found the com0com/com2tcp tool works perfectly for this purpose.
To study/download/install "com0com" and "com2tcp", please visit 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 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.
The usage
//after your network is initialized using if_config() function, you can call DEBUG_Init(100, 5656); ... _break();
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 that 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)
Notes, restrictions, and helps needed
- this is just the initial version so don't expect too much.
- the current version of DEBUG_Init(100, port) must be called after your network is ready.
- please only call once of DEBUG_Init()
- don't try to suspend all the threads, as one of the them is the helper thread for the gdb.
- if you know that how to make DEBUG_Init(GDBSTUB_DEVICE_TCP,port) working, please let me know. I did some researches 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 ?
- you can load the .dol file and gdb the .elf file.
- the network-gdb works slow for me; if you have a gecko-usb, please let me know the performance comparision.
- I have not used this to debug a real application; let me know if you have tried so.
The source code
Source code of the changes to libogc/libdb are in $WII_DEV_DEBUG/gdb/libogc/libdb. If you need to rebuild the libdb.a, you need to merge the debug.c using the debug.c.orig as the base to your debug.c. Also you need to change the "makefile" to include the "helper_tcpip.c". Again, this is just the initial version, I prabably will change it very soon.
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.
Bye the way, if you need a mplace-ce version that fully working (I hope so!) for Chinese language, please contact me. My changes have been submitted to the mplayer-ce team for reviewing.
Comments and suggestions?
Please email qiang0@gmail.com
this is the page normally used to give comments or suggestions. can you please make use of the PREVIEW button so you dont keep flooding the recent changes. thanks. --:~Love the World~: 04:30, 5 December 2009 (UTC)
Using the Show Preview button
Hi there! Instead of making repeated small changes to a page, please use the "Show preview" button. Do not press the "Save page" button until you are completely done making changes. That way, you avoid flooding the Recent Changes log with irrelevant information. --Lukegb 13:11, 5 December 2009 (UTC)