|
| 1 | +--- |
| 2 | +title: Debugging |
| 3 | +layout: home |
| 4 | +nav_order: 4 |
| 5 | +--- |
| 6 | + |
| 7 | +# Debugging |
| 8 | +{: .fs-8 .fw-700 .text-center } |
| 9 | + |
| 10 | +When developing for the Playstation Portable (PSP), you may run into crashes or code that does not quite work like expected. Figuring out what is going in is called debugging. This page will cover how to do that. |
| 11 | + |
| 12 | +## PSPLINK |
| 13 | + |
| 14 | +PSPLINK is THE tool to use for debugging on the PSP. It is an application which allows you to run and debug programs for the PSP from your PC through USB. |
| 15 | + |
| 16 | +### Setting up PSPLINK |
| 17 | + |
| 18 | +Each system involved in the use of PSPLINK requires a bit of setup for it to work. This includes both PSP and PC. Below are instructions for both. |
| 19 | + |
| 20 | +#### PSP |
| 21 | + |
| 22 | +Download the latest version of PSPLINK for the PSP [here](https://github.com/pspdev/psplinkusb/releases/download/latest/psplink.zip) and extract it in ``ms0:/PSP/GAME`` on the PSP memory card. |
| 23 | + |
| 24 | +#### PC |
| 25 | + |
| 26 | +Depending on the operating system used the setup on PC is different. Follow the on below which is relevant to your system. |
| 27 | + |
| 28 | +##### Windows |
| 29 | + |
| 30 | +On Windows a driver needs to be installed before PSPLINK can be used. To do this take the following steps: |
| 31 | + |
| 32 | +1. Make sure the programs `usbhostfs_pc` and `pspsh` are available in cmd. Otherwise download them [here](https://github.com/pspdev/psplinkusb/releases/download/latest/pspsh-windows.zip). |
| 33 | +2. Start PSPLINK on the Playstation Portable and connect it to the computer through USB. |
| 34 | +3. Download [Zadig](https://zadig.akeo.ie/) and start it. It will ask if you want to run it as administrator, click yes. |
| 35 | +4. In Zadig, click on `options` -> `List All Devices`. |
| 36 | +5. Select the entry `"PSP" type B` from the dropdown list. |
| 37 | +6. Left of driver, select the `libusb-win32` driver. Then click install. |
| 38 | +7. Wait for the installation to finish, then disconnect the USB cable from the PSP. |
| 39 | + |
| 40 | +Now PSPLINK can be used with Windows. See below how to do that. |
| 41 | + |
| 42 | +##### Linux |
| 43 | + |
| 44 | +With Linux PSPLINK will work without making any changes, but it will require using sudo for the `usbhostfs_pc` command. To make it work without sudo, a udev rule can be added. |
| 45 | + |
| 46 | +To make using PSPLINK without sudo create file called `/etc/udev/rules.d/50-psplink.rules` (for example with `sudo nano /etc/udev/rules.d/50-psplink.rules`) and add the following content: |
| 47 | + |
| 48 | +``` |
| 49 | +SUBSYSTEM=="usb", ATTR{idVendor}=="054c", ATTR{idProduct}=="01c9", SYMLINK+="psp", MODE="0666" |
| 50 | +``` |
| 51 | + |
| 52 | +Save this, in Nano this can be done with Ctrl+O and pressing enter. The run the following command: |
| 53 | + |
| 54 | +``` |
| 55 | +sudo udevadm control --reload |
| 56 | +``` |
| 57 | + |
| 58 | +Now PSPLINK can be used without sudo. See below how to do that. |
| 59 | + |
| 60 | +### Using PSPLINK |
| 61 | + |
| 62 | +To be able to use PSPLINK with Playstation Portable homebrew, the homebrew will need to be build into an unencrypted ``.prx`` file. This can be done by running CMake like `psp-cmake -DBUILD_PRX=1 .` or if you're using a Makefile by adding `BUILD_PRX=1` to it. Then build the homebrew. |
| 63 | + |
| 64 | +In the build directory, open a terminal and run the following program: |
| 65 | + |
| 66 | +``` |
| 67 | +usbhostfs_pc |
| 68 | +``` |
| 69 | + |
| 70 | +Keep this running! |
| 71 | + |
| 72 | +Then open another terminal window and run the following there: |
| 73 | + |
| 74 | +``` |
| 75 | +pspsh |
| 76 | +``` |
| 77 | + |
| 78 | +Now we can simply start our homebrew on the PSP by running the following command in the pspsh window: |
| 79 | + |
| 80 | +``` |
| 81 | +./myhomebrew.prx |
| 82 | +``` |
| 83 | + |
| 84 | +Replace myhomebrew with the name of the `.prx` file which was generated. |
| 85 | + |
| 86 | +When you're done with the current build, just run `reset`, rebuild the homebrew and try again. |
| 87 | + |
| 88 | +Options available can be found when using the `help` command, but here are some notable ones: |
| 89 | + |
| 90 | +- `scrshot screenshotname.bmp` for taking a screenshot. |
| 91 | +- `exit` for closing PSPLINK on the PSP. |
| 92 | +- `poweroff` for shutting down the PSP. |
| 93 | + |
| 94 | +### Getting Basic Crash Information |
| 95 | + |
| 96 | +When a crash happens a crash log will be shown with a hint of what might have happened at the top and some additional info. If you wish to figure out where the crash happened, only the address is needed. |
| 97 | + |
| 98 | +To figure out where the crash happened, open another terminal in the build directory and use the address shown by PSPLINK in the following command: |
| 99 | + |
| 100 | +``` |
| 101 | +psp-addr2line -e myhomebrew address |
| 102 | +``` |
| 103 | + |
| 104 | +Replace `address` with the actual adress and replace myhomebrew with the name of the elf file. This is **NOT** the `.prx` file and either has no extension or `.elf` depending on the build system used. |
| 105 | + |
| 106 | +If no result is returned, make sure to build with the `-g` or `-g3` option and without the `-O` option to make sure `psp-addr2line` knowns the function names and locations. |
| 107 | + |
| 108 | +The information received from `psp-addr2line` will be limited and not always useful, for more information you'll have to use a debugger as described below. |
| 109 | + |
| 110 | +### Using a Debugger |
| 111 | + |
| 112 | +When using `psp-addr2line` is not enough to figure out what is going on, the best way to debug will be by using an actual debugger called GDB. PSPLINK allows you to enable access to `psp-gdb` which comes bundles with the PSPDEV toolchain. |
| 113 | + |
| 114 | +#### Preparation |
| 115 | + |
| 116 | +Prepare a separate terminal for `usbhostfs_pc`, `pspsh` and `psp-gdb`. Open all of them in the directory in which your compiled `.prx` and the `elf` (PSP binary) files are located. |
| 117 | + |
| 118 | +##### 1. usbhostfs_pc |
| 119 | + |
| 120 | +Run `usbhostfs_pc` on your terminal dedicated for `usbhostfs_pc` and you will see the `waiting for device...` status. |
| 121 | + |
| 122 | +Now start the PSPLINK app on your PSP and connect the USB cable. You should see the `connected to device` status in the terminal, which means success. |
| 123 | + |
| 124 | +**Do not close this terminal after that.** |
| 125 | + |
| 126 | +##### 2. pspsh |
| 127 | + |
| 128 | +Run `pspsh` on your terminal dedicated for `pspsh` and you will see the `host0:/>`. Now run `debug file.prx`, and it will display something like this: |
| 129 | + |
| 130 | +```sh |
| 131 | +PSPLink USB GDBServer (c) 2k7 TyRaNiD |
| 132 | +host0:/> Loaded host0:/<file.prx> - UID 0x0408A763, Entry 0x088040AC |
| 133 | +``` |
| 134 | + |
| 135 | +> You need to replace `file.prx` with the file you need to debug. This is a `.prx` file. |
| 136 | +
|
| 137 | +It means the debugger is succesfully loaded. You can type `reset` if there's something wrong with your GDBServer. |
| 138 | + |
| 139 | +##### 3. psp-gdb |
| 140 | + |
| 141 | +In a new terminal run `psp-gdb file -q` and you will see something like this: |
| 142 | + |
| 143 | +> You need to replace `file` with the elf file of the program you're trying to debug. It has the same name as the file loaded in pspsh, but without the `.prx` ending. |
| 144 | +
|
| 145 | +> Check if your binary has enabled the debug symbols required for debugging by using `objdump --syms` command and should produce an output but if it says `no symbols` then it is disabled(You can enable it by adding `-g` option to gcc). |
| 146 | +
|
| 147 | +```sh |
| 148 | +Reading symbols from <file>... |
| 149 | +(gdb) |
| 150 | +``` |
| 151 | + |
| 152 | +> `<file>` is the name of the binary you are debugging. |
| 153 | +
|
| 154 | +then type the `target remote :10001` to connect to your GDBServer and you will see the gdb output something like this: |
| 155 | + |
| 156 | +```sh |
| 157 | +Remote debugging using :10001 |
| 158 | +_start (args=0, argp=0x0) at crt0_prx.c:103 |
| 159 | +103 if (&sce_newlib_nocreate_thread_in_start != NULL) { |
| 160 | +(gdb) |
| 161 | +``` |
| 162 | +This will display the `_start` routine, it means you succesfully connected and ready to the debug your app! |
| 163 | +
|
| 164 | +Here are a few useful commands for getting around in psp-gdb: |
| 165 | +- `b` or `break` - for setting breakpoints |
| 166 | +- `c` or `continue` - for resuming program execution until the next breakpoint or program completion |
| 167 | +- `s` or `step` - for executing the current line and, if it contains a function call, step into that function |
| 168 | +- `n` or `next` - for executing the current line, but if it contains a function call, step over it without diving into the function |
| 169 | +- `f` or `finish` - for executing the remaining lines of the current function and return to the caller |
| 170 | +- `bt` or `backtrace` - for getting stacktrace |
| 171 | +- `p $var` or `print $var` - for displaying the value of specific variable |
| 172 | +- `i r` or `info registers` - for displaying the contents of CPU registers |
| 173 | +- `d` or `delete` - for deleting all breakpoints |
| 174 | +- `q` or `quit` - for exiting from psp-gdb |
| 175 | +
|
| 176 | +You can type `help` for more information about the psp-gdb commands. |
| 177 | +
|
| 178 | +## Done |
| 179 | +
|
| 180 | +Now you know how to debug your code. Make sure to check out the [Tips and Tricks](tips_tricks.html) section to get most out of your PSP development experience. |
0 commit comments