├── .clang-format ├── .github ├── dependabot.yml └── workflows │ ├── ci.yml │ └── pr.yml ├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md └── src ├── global_vars.cpp ├── global_vars.h ├── imports.cpp ├── imports.h ├── main.cpp ├── main.h ├── patches.cpp ├── patches.h ├── stub ├── CoreAgent.cpp ├── CoreAgent.h ├── CoreAgent_GetSetPhys.h ├── CoreAgent_MemorySetGet.cpp ├── CoreAgent_MemorySetGet.h ├── EnumToStringUtil.cpp ├── EnumToStringUtil.h ├── GDBDParseUtils.h ├── GDBDefines.h ├── GDBThreadDefines.h ├── GDBUtils.h ├── GDB_Commands.cpp ├── GDB_Commands.h ├── GDB_IO.cpp ├── GDB_IO.h ├── MasterAgent_RegMemReadWrite.h ├── MasterAgent_ThreadUtils.cpp ├── MasterAgent_ThreadUtils.h ├── MasterAgent_Utils.cpp ├── MasterAgent_Utils.h └── helper.s ├── utils ├── IOUtils.cpp ├── IOUtils.h ├── kernel.cpp └── kernel.h └── version.h /.clang-format: -------------------------------------------------------------------------------- 1 | # Generated from CLion C/C++ Code Style settings 2 | BasedOnStyle: LLVM 3 | AccessModifierOffset: -4 4 | AlignAfterOpenBracket: Align 5 | AlignConsecutiveAssignments: Consecutive 6 | AlignConsecutiveMacros: AcrossEmptyLinesAndComments 7 | AlignOperands: Align 8 | AllowAllArgumentsOnNextLine: false 9 | AllowAllConstructorInitializersOnNextLine: false 10 | AllowAllParametersOfDeclarationOnNextLine: false 11 | AllowShortBlocksOnASingleLine: Always 12 | AllowShortCaseLabelsOnASingleLine: false 13 | AllowShortFunctionsOnASingleLine: All 14 | AllowShortIfStatementsOnASingleLine: Always 15 | AllowShortLambdasOnASingleLine: All 16 | AllowShortLoopsOnASingleLine: true 17 | AlwaysBreakAfterReturnType: None 18 | AlwaysBreakTemplateDeclarations: Yes 19 | BreakBeforeBraces: Custom 20 | BraceWrapping: 21 | AfterCaseLabel: false 22 | AfterClass: false 23 | AfterControlStatement: Never 24 | AfterEnum: false 25 | AfterFunction: false 26 | AfterNamespace: false 27 | AfterUnion: false 28 | BeforeCatch: false 29 | BeforeElse: false 30 | IndentBraces: false 31 | SplitEmptyFunction: false 32 | SplitEmptyRecord: true 33 | BreakBeforeBinaryOperators: None 34 | BreakBeforeTernaryOperators: true 35 | BreakConstructorInitializers: BeforeColon 36 | BreakInheritanceList: BeforeColon 37 | ColumnLimit: 0 38 | CompactNamespaces: false 39 | ContinuationIndentWidth: 8 40 | IndentCaseLabels: true 41 | IndentPPDirectives: None 42 | IndentWidth: 4 43 | KeepEmptyLinesAtTheStartOfBlocks: true 44 | MaxEmptyLinesToKeep: 2 45 | NamespaceIndentation: All 46 | ObjCSpaceAfterProperty: false 47 | ObjCSpaceBeforeProtocolList: true 48 | PointerAlignment: Right 49 | ReflowComments: false 50 | SpaceAfterCStyleCast: true 51 | SpaceAfterLogicalNot: false 52 | SpaceAfterTemplateKeyword: false 53 | SpaceBeforeAssignmentOperators: true 54 | SpaceBeforeCpp11BracedList: false 55 | SpaceBeforeCtorInitializerColon: true 56 | SpaceBeforeInheritanceColon: true 57 | SpaceBeforeParens: ControlStatements 58 | SpaceBeforeRangeBasedForLoopColon: true 59 | SpaceInEmptyParentheses: false 60 | SpacesBeforeTrailingComments: 1 61 | SpacesInAngles: false 62 | SpacesInCStyleCastParentheses: false 63 | SpacesInContainerLiterals: false 64 | SpacesInParentheses: false 65 | SpacesInSquareBrackets: false 66 | TabWidth: 4 67 | UseTab: Never 68 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "docker" 4 | directory: "/" 5 | schedule: 6 | interval: "daily" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "daily" 11 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI-Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | clang-format: 10 | runs-on: ubuntu-22.04 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: clang-format 14 | run: | 15 | docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./src 16 | build-binary: 17 | runs-on: ubuntu-22.04 18 | needs: clang-format 19 | steps: 20 | - uses: actions/checkout@v3 21 | - name: create version.h 22 | run: | 23 | git_hash=$(git rev-parse --short "$GITHUB_SHA") 24 | cat < ./src/version.h 25 | #pragma once 26 | #define PLUGIN_VERSION_EXTRA " (nightly-$git_hash)" 27 | EOF 28 | - name: build binary 29 | run: | 30 | docker build . -t builder 31 | docker run --rm -v ${PWD}:/project builder make 32 | - uses: actions/upload-artifact@master 33 | with: 34 | name: binary 35 | path: "*.wps" 36 | deploy-binary: 37 | needs: build-binary 38 | runs-on: ubuntu-22.04 39 | steps: 40 | - name: Get environment variables 41 | id: get_repository_name 42 | run: | 43 | echo REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//") >> $GITHUB_ENV 44 | echo DATETIME=$(echo $(date '+%Y%m%d-%H%M%S')) >> $GITHUB_ENV 45 | - uses: actions/download-artifact@master 46 | with: 47 | name: binary 48 | - name: zip artifact 49 | run: zip -r ${{ env.REPOSITORY_NAME }}_${{ env.DATETIME }}.zip *.wps 50 | - name: Create Release 51 | uses: "softprops/action-gh-release@v1" 52 | with: 53 | tag_name: ${{ env.REPOSITORY_NAME }}-${{ env.DATETIME }} 54 | draft: false 55 | prerelease: true 56 | generate_release_notes: true 57 | name: Nightly-${{ env.REPOSITORY_NAME }}-${{ env.DATETIME }} 58 | files: | 59 | ./${{ env.REPOSITORY_NAME }}_${{ env.DATETIME }}.zip -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | name: CI-PR 2 | 3 | on: [ pull_request ] 4 | 5 | jobs: 6 | clang-format: 7 | runs-on: ubuntu-22.04 8 | steps: 9 | - uses: actions/checkout@v3 10 | - name: clang-format 11 | run: | 12 | docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./src 13 | build-binary: 14 | runs-on: ubuntu-22.04 15 | needs: clang-format 16 | steps: 17 | - uses: actions/checkout@v3 18 | - name: create version.h 19 | run: | 20 | git_hash=$(git rev-parse --short "${{ github.event.pull_request.head.sha }}") 21 | cat < ./src/version.h 22 | #pragma once 23 | #define PLUGIN_VERSION_EXTRA " (nightly-$git_hash)" 24 | EOF 25 | - name: build binary 26 | run: | 27 | docker build . -t builder 28 | docker run --rm -v ${PWD}:/project builder make 29 | - uses: actions/upload-artifact@master 30 | with: 31 | name: binary 32 | path: "*.wps" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | *.elf 3 | *.wps 4 | .idea/ 5 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ghcr.io/wiiu-env/devkitppc:20230621 2 | 3 | COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:20230622 /artifacts $DEVKITPRO 4 | COPY --from=ghcr.io/wiiu-env/libkernel:20230621 /artifacts $DEVKITPRO 5 | COPY --from=ghcr.io/wiiu-env/libwupsbackend:20230621 /artifacts $DEVKITPRO 6 | 7 | WORKDIR project -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #------------------------------------------------------------------------------- 4 | 5 | ifeq ($(strip $(DEVKITPRO)),) 6 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") 7 | endif 8 | 9 | TOPDIR ?= $(CURDIR) 10 | 11 | include $(DEVKITPRO)/wups/share/wups_rules 12 | 13 | WUT_ROOT := $(DEVKITPRO)/wut 14 | WUMS_ROOT := $(DEVKITPRO)/wums 15 | 16 | #------------------------------------------------------------------------------- 17 | # TARGET is the name of the output 18 | # BUILD is the directory where object files & intermediate files will be placed 19 | # SOURCES is a list of directories containing source code 20 | # DATA is a list of directories containing data files 21 | # INCLUDES is a list of directories containing header files 22 | #------------------------------------------------------------------------------- 23 | TARGET := gdbstub 24 | BUILD := build 25 | SOURCES := src src/utils src/stub 26 | DATA := data 27 | INCLUDES := src 28 | 29 | #------------------------------------------------------------------------------- 30 | # options for code generation 31 | #------------------------------------------------------------------------------- 32 | CFLAGS := -Wall -O2 -ffunction-sections \ 33 | $(MACHDEP) 34 | 35 | CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ -D__WUPS__ 36 | 37 | CXXFLAGS := $(CFLAGS) -std=c++20 38 | 39 | ASFLAGS := -g $(ARCH) 40 | LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libkernel.ld -T$(WUPS_ROOT)/share/libwupsbackend.ld $(WUPSSPECS) 41 | 42 | LIBS := -lwups -lwupsbackend -lwut -lkernel 43 | 44 | #------------------------------------------------------------------------------- 45 | # list of directories containing libraries, this must be the top level 46 | # containing include and lib 47 | #------------------------------------------------------------------------------- 48 | LIBDIRS := $(PORTLIBS) $(WUPS_ROOT) $(WUMS_ROOT) $(WUT_ROOT) $(WUT_ROOT)/usr 49 | 50 | #------------------------------------------------------------------------------- 51 | # no real need to edit anything past this point unless you need to add additional 52 | # rules for different file extensions 53 | #------------------------------------------------------------------------------- 54 | ifneq ($(BUILD),$(notdir $(CURDIR))) 55 | #------------------------------------------------------------------------------- 56 | 57 | export OUTPUT := $(CURDIR)/$(TARGET) 58 | export TOPDIR := $(CURDIR) 59 | 60 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 61 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 62 | 63 | export DEPSDIR := $(CURDIR)/$(BUILD) 64 | 65 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 66 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 67 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 68 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 69 | 70 | #------------------------------------------------------------------------------- 71 | # use CXX for linking C++ projects, CC for standard C 72 | #------------------------------------------------------------------------------- 73 | ifeq ($(strip $(CPPFILES)),) 74 | #------------------------------------------------------------------------------- 75 | export LD := $(CC) 76 | #------------------------------------------------------------------------------- 77 | else 78 | #------------------------------------------------------------------------------- 79 | export LD := $(CXX) 80 | #------------------------------------------------------------------------------- 81 | endif 82 | #------------------------------------------------------------------------------- 83 | 84 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) 85 | export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 86 | export OFILES := $(OFILES_BIN) $(OFILES_SRC) 87 | export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) 88 | 89 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 90 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 91 | -I$(CURDIR)/$(BUILD) 92 | 93 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 94 | 95 | .PHONY: $(BUILD) clean all 96 | 97 | #------------------------------------------------------------------------------- 98 | all: $(BUILD) 99 | 100 | $(BUILD): 101 | @$(shell [ ! -d $(BUILD) ] && mkdir -p $(BUILD)) 102 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 103 | 104 | #------------------------------------------------------------------------------- 105 | clean: 106 | @echo clean ... 107 | @rm -fr $(BUILD) $(TARGET).wps $(TARGET).elf 108 | 109 | #------------------------------------------------------------------------------- 110 | else 111 | .PHONY: all 112 | 113 | DEPENDS := $(OFILES:.o=.d) 114 | 115 | #------------------------------------------------------------------------------- 116 | # main targets 117 | #------------------------------------------------------------------------------- 118 | all : $(OUTPUT).wps 119 | 120 | $(OUTPUT).wps : $(OUTPUT).elf 121 | $(OUTPUT).elf : $(OFILES) 122 | 123 | $(OFILES_SRC) : $(HFILES_BIN) 124 | 125 | #------------------------------------------------------------------------------- 126 | # you need a rule like this for each extension you use as binary data 127 | #------------------------------------------------------------------------------- 128 | %.bin.o %_bin.h : %.bin 129 | #------------------------------------------------------------------------------- 130 | @echo $(notdir $<) 131 | @$(bin2o) 132 | 133 | #--------------------------------------------------------------------------------- 134 | %.o: %.s 135 | @echo $(notdir $<) 136 | @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) 137 | 138 | 139 | -include $(DEPENDS) 140 | 141 | #------------------------------------------------------------------------------- 142 | endif 143 | #------------------------------------------------------------------------------- 144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![CI-Release](https://github.com/wiiu-env/gdbstub_plugin/actions/workflows/ci.yml/badge.svg)](https://github.com/wiiu-env/gdbstub_plugin/actions/workflows/ci.yml) 2 | 3 | # GDBStub for the Wii U 4 | 5 | This is a plugin which provides a gdbstub for debugging Wii U software, including homebrew. 6 | 7 | It's based on the gdbstub that's inside the `coreinit.rpl` and present on any Wii U. This plugin patches several functions to allow usage of the stub on retail consoles. 8 | 9 | With these patches some bugs have been fixed (e.g. basic `vCont` support and escape packet contents) and support for multiple queries have been added. 10 | Memory access is now done completely via the KernelModule which bypasses the MMU, which allow us to write/read anywhere. 11 | 12 | For research purposes it's planned to reimplement (the important parts of) the `coreinit.rpl` gdbstub step by step. 13 | 14 | ## Features 15 | - Hardware breakpoint support (maximum one hw breakpoint is supported at once). 16 | - Support up to 512 software breakpoints at the same time. 17 | - Hardware watchpoints for read/write access of data with 8 byte accuracy (maximum one hw watchpoint is supported at once). 18 | - Stepping to the next instruction (only the for currently active threads) 19 | - Memory/register reading/writing. 20 | - Implemented the optional `qXfer:features:read` and `qXfer:threads:read` query. 21 | - Interrupting the execution at any time via `CTRL+C` 22 | - See the `main.h` for further configuration options via macros. 23 | 24 | ## Requirements / Limitations: 25 | - Using [MochaPayload](https://github.com/wiiu-env/MochaPayload) v0.2 or higher or something else that supports the DK_PChar API. If not using mocha, you probably need to adjust the `makeOpenPath` replacement. 26 | - Any homebrew you want to debug needs to be built with [wut](https://github.com/devkitPro/wut) 1.2.0-2 or higher. Official software works too, but you probably won't have any symbols. 27 | - When the gdbstub is loaded, it'll directly start to wait for a connection via TCP. It's recommended to load the plugin when needed via the network instead of placing it permanently on the sd card. For loading plugins at runtime you can use the [wiiload plugin](https://github.com/wiiu-env/wiiload_plugin). 28 | - The console will **not** respond to any other input while waiting for a connection. Please connect to the stub or force-shutdown the console (hold the power button of the **console** for at least 4 seconds) 29 | - The gdbstub will stay loaded and active until the console is shut down. It's possible to change or reload the application. 30 | 31 | ## Recommended: 32 | - Using the [USBSerialLogger](https://github.com/wiiu-env/USBSerialLogger) module to have output via serial while debugging. For this you need a USB serial cable with certain chipset. The USBSerialLogger repo for more information. 33 | - Build the homebrew apps with -O0, make sure to add the `-g` compiler flag for creating debug builds with DWARF information. 34 | 35 | ## Usage 36 | - Boot your console into a [WiiUPluginLoaderBackend](https://github.com/wiiu-env/WiiUPluginLoaderBackend) compatible environment (e.g. [Aroma](https://github.com/wiiu-env/Aroma)) 37 | - Start the homebrew app you want to debug. 38 | - Load the gdbstub_plugin via wiiload. 39 | - Now the homebrew app will restart and wait for a connection. (When using the usb serial logging it should print `[+-*WAITING FOR DEBUGGER*-+]`) 40 | - Connect to the gdbstub via TCP at port 3000. 41 | - When you don't load any symbol file, you might have to force the endianness via `set endian big` 42 | 43 | Basic settings for gdb: 44 | ``` 45 | set arch powerpc:750 46 | set remotetimeout 30 47 | set tcp connect-timeout 60 48 | set print thread-events on 49 | set disassemble-next-line on 50 | set remote hardware-watchpoint-limit 1 51 | set remote hardware-breakpoint-limit 1 52 | target remote tcp:192.168.178.123:3000 53 | ``` 54 | 55 | ## Building 56 | In order to be able to compile this, you need to have devkitPPC installed 57 | [devkitPPC](https://devkitpro.org/wiki/Getting_Started) with the following 58 | pacman packages installed. 59 | 60 | ``` 61 | (sudo) (dkp-)pacman -Syu --needed wiiu-dev 62 | ``` 63 | 64 | Make sure the following environment variables are set: 65 | 66 | ``` 67 | DEVKITPRO=/opt/devkitpro 68 | DEVKITPPC=/opt/devkitpro/devkitPPC 69 | ``` 70 | 71 | Also make sure to 72 | install [wut](https://github.com/wiiu-env/wut), [WiiUPluginSystem](https://github.com/wiiu-env/WiiUPluginSystem), [libkernel](https://github.com/wiiu-env/libkernel) 73 | and [libwupsbackend](https://github.com/wiiu-env/libwupsbackend). 74 | 75 | ## Building using the Dockerfile 76 | 77 | It's possible to use a docker image for building. This way you don't need anything installed on your host system. 78 | 79 | ``` 80 | # Build docker image (only needed once) 81 | docker build . -t gbdstubplugin-builder 82 | 83 | # make 84 | docker run -it --rm -v ${PWD}:/project gbdstubplugin-builder make 85 | 86 | # make clean 87 | docker run -it --rm -v ${PWD}:/project gbdstubplugin-builder make clean 88 | ``` 89 | 90 | ## Format the code via docker 91 | 92 | `docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./src -i` -------------------------------------------------------------------------------- /src/global_vars.cpp: -------------------------------------------------------------------------------- 1 | #include "global_vars.h" 2 | 3 | bool gHeartbeatDisabled = false; 4 | bool gInitStubs = false; -------------------------------------------------------------------------------- /src/global_vars.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | extern bool gHeartbeatDisabled; 4 | extern bool gInitStubs; -------------------------------------------------------------------------------- /src/imports.cpp: -------------------------------------------------------------------------------- 1 | #include "stub/GDBDefines.h" 2 | #include 3 | 4 | auto *gGDBGHSShowDrvrThreadsPtr = (uint32_t *) 0x1004f9bc; 5 | auto *gAppflagsPtr = (uint32_t *) 0x10013c2c; 6 | auto *gIsDebuggerPresentPtr = (uint32_t *) 0x1003aff8; 7 | auto *DAT_100d1378Ptr = (uint32_t *) 0x100d1378; 8 | auto *g100msInTicksPtr = (uint32_t *) 0x100523d8; 9 | auto *gTimerKernelCallbackLastRunPtr = (uint32_t *) 0x100523d0; 10 | auto *DAT_100d1374Ptr = (uint32_t *) 0x100d1374; 11 | auto *gCoreCountPtr = (uint32_t *) 0x1004f9b8; 12 | auto *gGDBStub_main_statePtr = (GDBMainState *) 0x1004f9b4; 13 | auto *gIsDebuggerInitializedOnCore = (uint32_t *) 0x1004f974; 14 | auto *gCoreData = (GDBCoreInfo *) 0x1004fb38; 15 | auto *gCurrentThreadOnCore = (OSThread **) 0x10057c00; 16 | GDBCoreInfo **gGDBCurCoreInfoPtr = (GDBCoreInfo **) 0x1004f9e4; 17 | GDBCoreInfo **gGDBStoppedCoreInfoPtr = (GDBCoreInfo **) 0x1004f9e8; -------------------------------------------------------------------------------- /src/imports.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "stub/GDBDefines.h" 3 | #include 4 | #include 5 | 6 | extern uint32_t *gGDBGHSShowDrvrThreadsPtr; 7 | extern uint32_t *gAppflagsPtr; 8 | extern uint32_t *gIsDebuggerPresentPtr; 9 | extern uint32_t *DAT_100d1378Ptr; 10 | extern uint32_t *DAT_100d1374Ptr; 11 | extern uint32_t *gCoreCountPtr; 12 | extern GDBMainState *gGDBStub_main_statePtr; 13 | extern uint32_t *g100msInTicksPtr; 14 | extern uint32_t *gTimerKernelCallbackLastRunPtr; 15 | extern GDBCoreInfo **gGDBCurCoreInfoPtr; 16 | extern GDBCoreInfo **gGDBStoppedCoreInfoPtr; 17 | extern OSThread **gCurrentThreadOnCore; 18 | 19 | #define gGDBGHSShowDrvrThreads (*gGDBGHSShowDrvrThreadsPtr) 20 | #define gAppflags (*gAppflagsPtr) 21 | #define gIsDebuggerPresent (*gIsDebuggerPresentPtr) 22 | #define DAT_100d1378 (*DAT_100d1378Ptr) 23 | #define DAT_100d1374 (*DAT_100d1374Ptr) 24 | #define gGDBStub_main_state (*gGDBStub_main_statePtr) 25 | #define gCoreCount (*gCoreCountPtr) 26 | #define g100msInTicks (*g100msInTicksPtr) 27 | #define gTimerKernelCallbackLastRun (*gTimerKernelCallbackLastRunPtr) 28 | 29 | #define gGDBCurCoreInfo (*gGDBCurCoreInfoPtr) 30 | #define gGDBStoppedCoreInfo (*gGDBStoppedCoreInfoPtr) 31 | 32 | extern uint32_t *gIsDebuggerInitializedOnCore; 33 | extern GDBCoreInfo *gCoreData; 34 | 35 | extern "C" uint32_t __OSGetAppFlags(); 36 | 37 | #define OSDisableUserHeartbeat ((void (*)(void))(0x101C400 + 0x155b0)) 38 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "global_vars.h" 3 | #include "stub/GDB_IO.h" 4 | #include "utils/kernel.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | WUPS_PLUGIN_NAME("GDBStub"); 11 | WUPS_PLUGIN_DESCRIPTION("GDBStub"); 12 | WUPS_PLUGIN_VERSION(PLUGIN_VERSION_FULL); 13 | WUPS_PLUGIN_AUTHOR("Maschell, GaryOderNichts"); 14 | WUPS_PLUGIN_LICENSE("GPL"); 15 | 16 | INITIALIZE_PLUGIN() { 17 | gInitStubs = true; 18 | EnableIABRandDABRSupport(); 19 | PluginBackendApiErrorType res; 20 | if ((res = WUPSBackend_InitLibrary()) != PLUGIN_BACKEND_API_ERROR_NONE) { 21 | OSReport("WUPSBackend_InitLibrary failed: %s", WUPSBackend_GetStatusStr(res)); 22 | } 23 | } 24 | 25 | DEINITIALIZE_PLUGIN() { 26 | // The plugin is not loaded the syscalls would point to invalid memory. 27 | // So we have to restore the syscalls before unloading 28 | DisableIABRandDABRSupport(); 29 | } 30 | 31 | ON_APPLICATION_START() { 32 | ResetIOOffsets(); 33 | if (!gHeartbeatDisabled) { 34 | OSReport("Heartbeat not disabled!\n"); 35 | OSRestartGame(0, nullptr); 36 | } 37 | } 38 | 39 | ON_APPLICATION_ENDS() { 40 | gHeartbeatDisabled = false; 41 | bool stubNeedsToBeDisabled = false; 42 | PluginBackendApiErrorType res; 43 | if ((res = WUPSBackend_WillReloadPluginsOnNextLaunch(&stubNeedsToBeDisabled)) == PLUGIN_BACKEND_API_ERROR_NONE) { 44 | if (stubNeedsToBeDisabled) { 45 | gInitStubs = false; 46 | } 47 | } else { 48 | OSReport("gdbstub: WUPSBackend_WillReloadPluginsOnNextLaunch failed: %s", WUPSBackend_GetStatusStr(res)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "version.h" 3 | 4 | #define PLUGIN_VERSION "v0.1" 5 | #define PLUGIN_VERSION_FULL PLUGIN_VERSION PLUGIN_VERSION_EXTRA 6 | 7 | // Uncomment to log incoming and outgoing packet 8 | // #define PACKET_LOGGING 9 | 10 | // Uncomment to add driver threads to the thread list 11 | // #define SHOW_DRIVER_THREADS -------------------------------------------------------------------------------- /src/patches.cpp: -------------------------------------------------------------------------------- 1 | #include "global_vars.h" 2 | #include "imports.h" 3 | #include 4 | #include 5 | 6 | DECL_FUNCTION(void, OSPerGameInits) { 7 | real_OSPerGameInits(); 8 | if (gInitStubs && !gHeartbeatDisabled) { 9 | OSDisableUserHeartbeat(); 10 | gHeartbeatDisabled = true; 11 | } 12 | } 13 | 14 | // Hooks into an empty function after the appflags were already set. 15 | DECL_FUNCTION(void, EntryHook) { 16 | if (gInitStubs) { 17 | gAppflags |= 0x01; // Enable the GDBStub! 18 | gAppflags |= 0x80; // We want to use PChar instead of EXI 19 | } 20 | } 21 | 22 | // We have to "hide" the "GDBStub enabled" flag for everything that's using __OSGetAppFlags 23 | DECL_FUNCTION(uint32_t, __OSGetAppFlags) { 24 | auto res = real___OSGetAppFlags(); 25 | if (gInitStubs) { 26 | res = res & ~0x01; 27 | } 28 | return res; 29 | } 30 | 31 | DECL_FUNCTION(int32_t, makeOpenPath, const char *path, char *out) { 32 | strncpy(out, "/dev/iosuhax", 0x20); 33 | return 0; 34 | } 35 | 36 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(makeOpenPath, (0x3201C400 + (0x02040390 - 0x02000000)), (0x101C400 + (0x02040390 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 37 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(OSPerGameInits, (0x3001C400 + 0x020031ec), (0x020031ec - 0xFE3C00), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 38 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(EntryHook, (0x3001C400 + 0x020080a8), (0x020080a8 - 0xFE3C00), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 39 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(__OSGetAppFlags, (0x3001C400 + 0x02003308), (0x02003308 - 0xFE3C00), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); -------------------------------------------------------------------------------- /src/patches.h: -------------------------------------------------------------------------------- 1 | #pragma once -------------------------------------------------------------------------------- /src/stub/CoreAgent.cpp: -------------------------------------------------------------------------------- 1 | #include "CoreAgent.h" 2 | #include "CoreAgent_GetSetPhys.h" 3 | #include "EnumToStringUtil.h" 4 | #include "GDBDefines.h" 5 | #include "GDBThreadDefines.h" 6 | #include "GDBUtils.h" 7 | #include "imports.h" 8 | #include 9 | #include 10 | #include 11 | 12 | static const char exception_print_formats[18][45] = { 13 | "Exception type %d occurred!\n", // 0 14 | "GPR00 %08X GPR08 %08X GPR16 %08X GPR24 %08X\n", // 1 15 | "GPR01 %08X GPR09 %08X GPR17 %08X GPR25 %08X\n", // 2 16 | "GPR02 %08X GPR10 %08X GPR18 %08X GPR26 %08X\n", // 3 17 | "GPR03 %08X GPR11 %08X GPR19 %08X GPR27 %08X\n", // 4 18 | "GPR04 %08X GPR12 %08X GPR20 %08X GPR28 %08X\n", // 5 19 | "GPR05 %08X GPR13 %08X GPR21 %08X GPR29 %08X\n", // 6 20 | "GPR06 %08X GPR14 %08X GPR22 %08X GPR30 %08X\n", // 7 21 | "GPR07 %08X GPR15 %08X GPR23 %08X GPR31 %08X\n", // 8 22 | "LR %08X SRR0 %08x SRR1 %08x\n", // 9 23 | "DAR %08X DSISR %08X\n", // 10 24 | "\nSTACK DUMP:", // 11 25 | " --> ", // 12 26 | " -->\n", // 13 27 | "\n", // 14 28 | "%p", // 15 29 | "\nCODE DUMP:\n", // 16 30 | "%p: %08X %08X %08X %08X\n", // 17 31 | }; 32 | 33 | DECL_FUNCTION(void, __CoreAgent_SaveState, OSExceptionType exceptionType, OSContext *interruptedContext, OSContext *cbContext) { 34 | auto *currentCoreInfo = &gCoreData[OSGetCoreId()]; 35 | currentCoreInfo->exception_level++; 36 | if (currentCoreInfo->exception_level != 1) { 37 | gIsDebuggerPresent = 0; 38 | 39 | CoreAgent_SetCoreState(currentCoreInfo, GDBSTUB_STATE_STOPPING); 40 | 41 | const char *FPAsString = "OFF"; 42 | const char *PRAsString = ""; 43 | if ((interruptedContext->srr1 & MSR_BIT_FP) != 0) { 44 | FPAsString = "ON"; 45 | } 46 | if ((interruptedContext->srr1 & MSR_BIT_PR) != 0) { 47 | PRAsString = " USER"; 48 | } 49 | auto *exceptionAsStr = GDBStub_OSExceptionToString(exceptionType); 50 | OSReport("PANIC %s context:$%08x SRR0:$%08x SRR1:$%08x FP:%s%s\n", 51 | exceptionAsStr, interruptedContext, interruptedContext->srr0, 52 | interruptedContext->srr1, FPAsString, PRAsString); 53 | 54 | OSReport( 55 | "DOUBLE FAULT: __CoreAgent_SaveState(%d %08x) core->exception_level (%d) != 1\n", 56 | exceptionType, 57 | interruptedContext, 58 | currentCoreInfo->exception_level); 59 | 60 | OSReport(exception_print_formats[1], interruptedContext->gpr[0], interruptedContext->gpr[8], interruptedContext->gpr[16], interruptedContext->gpr[24]); 61 | OSReport(exception_print_formats[2], interruptedContext->gpr[1], interruptedContext->gpr[9], interruptedContext->gpr[17], interruptedContext->gpr[25]); 62 | OSReport(exception_print_formats[3], interruptedContext->gpr[2], interruptedContext->gpr[10], interruptedContext->gpr[18], interruptedContext->gpr[26]); 63 | OSReport(exception_print_formats[4], interruptedContext->gpr[3], interruptedContext->gpr[11], interruptedContext->gpr[19], interruptedContext->gpr[27]); 64 | OSReport(exception_print_formats[5], interruptedContext->gpr[4], interruptedContext->gpr[12], interruptedContext->gpr[20], interruptedContext->gpr[28]); 65 | OSReport(exception_print_formats[6], interruptedContext->gpr[5], interruptedContext->gpr[13], interruptedContext->gpr[21], interruptedContext->gpr[29]); 66 | OSReport(exception_print_formats[7], interruptedContext->gpr[6], interruptedContext->gpr[14], interruptedContext->gpr[22], interruptedContext->gpr[30]); 67 | OSReport(exception_print_formats[8], interruptedContext->gpr[7], interruptedContext->gpr[15], interruptedContext->gpr[23], interruptedContext->gpr[31]); 68 | OSReport(exception_print_formats[9], interruptedContext->lr, interruptedContext->srr0, interruptedContext->srr1); 69 | if (exceptionType == OS_EXCEPTION_TYPE_DSI) { 70 | OSReport(exception_print_formats[10], interruptedContext->dsisr, interruptedContext->dar); // this freezes 71 | } 72 | 73 | do { 74 | CoreAgent_SetCoreState(currentCoreInfo, GDBSTUB_STATE_HUNG); 75 | } while (true); 76 | } 77 | CoreAgent_SetCoreState(currentCoreInfo, GDBSTUB_STATE_STOPPING); 78 | currentCoreInfo->stoppedContext = interruptedContext; 79 | GDBStub_MasterCore(); 80 | 81 | currentCoreInfo->stoppedThread = gCurrentThreadOnCore[currentCoreInfo->core]; 82 | if (currentCoreInfo->stoppedThread == (OSThread *) nullptr) { 83 | auto curCore = currentCoreInfo->core; 84 | if (curCore == 0) { 85 | currentCoreInfo->stoppedThread = IDLE_THREAD_CORE_0; 86 | } else if (curCore == 1) { 87 | currentCoreInfo->stoppedThread = IDLE_THREAD_CORE_1; 88 | } else if (curCore == 2) { 89 | currentCoreInfo->stoppedThread = IDLE_THREAD_CORE_2; 90 | } 91 | } else { 92 | if (currentCoreInfo->stoppedThread != (OSThread *) interruptedContext) { 93 | // Check if the interruptedContext is on the stack of the stopped thread. 94 | if (((uint32_t) interruptedContext >= (uint32_t) currentCoreInfo->stoppedThread->stackStart) && 95 | ((uint32_t) interruptedContext < (uint32_t) currentCoreInfo->stoppedThread->stackEnd)) { 96 | } 97 | } 98 | } 99 | currentCoreInfo->currentContext = currentCoreInfo->stoppedContext; 100 | currentCoreInfo->currentThread = currentCoreInfo->stoppedThread; 101 | 102 | FlushFPUContext(); 103 | currentCoreInfo->signal = GDBSIGNAL_SIGTRAP; 104 | currentCoreInfo->OSException = (char) exceptionType; 105 | if (currentCoreInfo->dabr != 0) { 106 | CoreAgent_SetPhyReg32(GDB_REGISTER_DABR, 0); 107 | } 108 | if (currentCoreInfo->iabr != 0) { 109 | CoreAgent_SetPhyReg32(GDB_REGISTER_IABR, 0); 110 | } 111 | bool skipSetCoreData = false; 112 | switch (exceptionType) { 113 | case OS_EXCEPTION_TYPE_DSI: { 114 | auto DSISR = CoreAgent_GetPhyReg32(GDB_REGISTER_DSISR); 115 | if ((currentCoreInfo->dabr != 0) && (DSISR & DSISR_FLAG_DABRMatch) == DSISR_FLAG_DABRMatch) { 116 | currentCoreInfo->signal = GDBSIGNAL_SIGTRAP; 117 | currentCoreInfo->dar = CoreAgent_GetPhyReg32(GDB_REGISTER_DAR); 118 | } else { 119 | currentCoreInfo->signal = GDBSIGNAL_SIGSEGV; 120 | } 121 | break; 122 | } 123 | case OS_EXCEPTION_TYPE_ALIGNMENT: { 124 | currentCoreInfo->signal = GDBSIGNAL_SIGBUS; 125 | break; 126 | } 127 | case OS_EXCEPTION_TYPE_SYSTEM_RESET: 128 | case OS_EXCEPTION_TYPE_MACHINE_CHECK: 129 | case OS_EXCEPTION_TYPE_DECREMENTER: { 130 | currentCoreInfo->signal = GDBSIGNAL_SIGINT; 131 | break; 132 | } 133 | case OS_EXCEPTION_TYPE_TRACE: 134 | case OS_EXCEPTION_TYPE_BREAKPOINT: { 135 | currentCoreInfo->signal = GDBSIGNAL_SIGTRAP; 136 | break; 137 | } 138 | case OS_EXCEPTION_TYPE_ICI: { 139 | currentCoreInfo->signal = GDBSIGNAL_RESUME; 140 | break; 141 | } 142 | case OS_EXCEPTION_TYPE_PROGRAM: { 143 | auto curPC = CoreAgent_PeekWord32((void *) interruptedContext->srr0); 144 | if (curPC == TRAP_INSTRUCTION) { 145 | currentCoreInfo->signal = GDBSIGNAL_BREAKPT; 146 | } else if (curPC == DBGSTR_INSTRUCTION) { 147 | currentCoreInfo->signal = GDBSIGNAL_DBGSTR; 148 | } else if (curPC == DBGMSG_INSTRUCTION) { 149 | currentCoreInfo->signal = GDBSIGNAL_DBGMSG; 150 | } else if (curPC == DBGCTL_INSTRUCTION) { 151 | currentCoreInfo->signal = GDBSIGNAL_DBGCTL; 152 | } else { 153 | currentCoreInfo->signal = GDBSIGNAL_SIGILL; 154 | } 155 | break; 156 | } 157 | case OS_EXCEPTION_TYPE_ISI: 158 | case OS_EXCEPTION_TYPE_EXTERNAL_INTERRUPT: 159 | case OS_EXCEPTION_TYPE_FLOATING_POINT: 160 | case OS_EXCEPTION_TYPE_SYSTEM_CALL: 161 | case OS_EXCEPTION_TYPE_PERFORMANCE_MONITOR: 162 | case OS_EXCEPTION_TYPE_SYSTEM_INTERRUPT: 163 | default: 164 | currentCoreInfo->signal = GDBSIGNAL_SIG0; 165 | skipSetCoreData = true; 166 | break; 167 | } 168 | 169 | if (!skipSetCoreData) { 170 | currentCoreInfo->cachedMSR = interruptedContext->srr1 & 0x87c0ffff; 171 | currentCoreInfo->cachedHID2 = 0; 172 | currentCoreInfo->cachedPC = (currentCoreInfo->currentContext)->srr0; 173 | (currentCoreInfo->currentContext)->srr1 = (currentCoreInfo->currentContext)->srr1 & ~MSR_BIT_SE; 174 | CoreAgent_SetMasterReq(currentCoreInfo, GDBSTUB_REQ_STOPPED); 175 | CoreAgent_SetCoreState(currentCoreInfo, GDBSTUB_STATE_STOPPED); 176 | } 177 | 178 | if (currentCoreInfo->cmd.cmd == GDBSTUB_CMD_SINGLESTEP) { 179 | (currentCoreInfo->currentContext)->srr1 = (currentCoreInfo->currentContext)->srr1 | (uint32_t) currentCoreInfo->stoppedContextExternalIinterruptBit; 180 | CoreAgent_SetCmdDone(currentCoreInfo); 181 | } 182 | CoreAgent_MasterAgentLoop(currentCoreInfo); 183 | do { 184 | CoreAgent_SetCoreState(currentCoreInfo, GDBSTUB_STATE_HUNG); 185 | } while (true); 186 | } 187 | 188 | DECL_FUNCTION(void, CoreAgent_ResumeCore, GDBCoreInfo *curCoreData) { 189 | auto stoppedContext = curCoreData->stoppedContext; 190 | 191 | if (curCoreData->req.ack != curCoreData->reqAckValue) { 192 | return; 193 | } 194 | 195 | CoreAgent_SetCoreState(curCoreData, GDBSTUB_STATE_PROCESSING); 196 | 197 | uint32_t curPCInstruction = CoreAgent_PeekWord32((void *) stoppedContext->srr0); 198 | if (curPCInstruction == TRAP_INSTRUCTION || 199 | curPCInstruction == DBGSTR_INSTRUCTION || 200 | curPCInstruction == DBGMSG_INSTRUCTION || 201 | curPCInstruction == DBGCTL_INSTRUCTION) { 202 | stoppedContext->srr0 = stoppedContext->srr0 + 4; 203 | } 204 | 205 | if (curCoreData->cmd.cmd == GDBSTUB_CMD_CONTINUE) { 206 | CoreAgent_SetCoreState(curCoreData, GDBSTUB_STATE_RUNNING); 207 | curCoreData->stoppedContextExternalIinterruptBit = 0; 208 | stoppedContext->srr1 = stoppedContext->srr1 & ~MSR_BIT_SE; 209 | } else if (curCoreData->cmd.cmd == GDBSTUB_CMD_SINGLESTEP) { 210 | CoreAgent_SetCoreState(curCoreData, GDBSTUB_STATE_SINGLESTEP); 211 | curCoreData->stoppedContextExternalIinterruptBit = ((uint16_t) stoppedContext->srr1) | MSR_BIT_EE; 212 | stoppedContext->srr1 = (stoppedContext->srr1 & ~MSR_BIT_EE) | MSR_BIT_SE; 213 | } 214 | 215 | if (curCoreData->cmd.cmd == GDBSTUB_CMD_CONTINUE) { 216 | CoreAgent_SetCmdDone(curCoreData); 217 | } 218 | curCoreData->exception_level--; 219 | gTimerKernelCallbackLastRun = in_TBLr(); 220 | if (curCoreData->dabr != 0) { 221 | CoreAgent_SetPhyReg32(GDB_REGISTER_DABR, curCoreData->dabr); 222 | } 223 | if (curCoreData->iabr != 0) { 224 | CoreAgent_SetPhyReg32(GDB_REGISTER_IABR, curCoreData->iabr); 225 | } 226 | if (curCoreData->cmd.cmd == GDBSTUB_CMD_CONTINUE) { 227 | __OSSetAndLoadContext(stoppedContext); 228 | return; 229 | } 230 | __OSSetAndLoadContextDebugger(stoppedContext); 231 | } 232 | 233 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(__CoreAgent_SaveState, (0x3201C400 + (0x02068408 - 0x02000000)), (0x101C400 + (0x02068408 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 234 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(CoreAgent_ResumeCore, (0x3201C400 + (0x02067f60 - 0x02000000)), (0x101C400 + (0x02067f60 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 235 | -------------------------------------------------------------------------------- /src/stub/CoreAgent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define __CoreAgent_SaveState ((void (*)(OSExceptionType exceptionType, OSContext * interruptedContext, OSContext * cbContext))(0x101C400 + 0x02068408 - 0x02000000)) 4 | #define CoreAgent_ResumeCore ((void (*)(GDBCoreInfo *))(0x101C400 + 0x02067f60 - 0x02000000)) 5 | #define CoreAgent_SetMasterReq ((void (*)(GDBCoreInfo *, GDBStub_Req))(0x101C400 + 0x02067ac8 - 0x02000000)) 6 | #define CoreAgent_MasterAgentLoop ((void (*)(GDBCoreInfo *))(0x101C400 + 0x020683e0 - 0x02000000)) 7 | #define CoreAgent_SetCoreState ((void (*)(GDBCoreInfo *, GDBStubState))(0x101C400 + 0x020679c8 - 0x02000000)) 8 | #define CoreAgent_SetCmdDone ((void (*)(GDBCoreInfo *))(0x101C400 + 0x02067ab0 - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/CoreAgent_GetSetPhys.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define CoreAgent_GetPhyReg32 ((uint32_t(*)(uint32_t))(0x101C400 + 0x02067b20 - 0x02000000)) 4 | #define CoreAgent_SetPhyReg32 ((uint32_t(*)(uint32_t, uint32_t))(0x101C400 + 0x02067b98 - 0x02000000)) 5 | #define CoreAgent_PeekWord32 ((uint32_t(*)(void *))(0x101C400 + 0x02067cc0 - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/CoreAgent_MemorySetGet.cpp: -------------------------------------------------------------------------------- 1 | #include "CoreAgent_MemorySetGet.h" 2 | #include "utils/kernel.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | DECL_FUNCTION(uint32_t, CoreAgent_GetCoreMem, void *addr, uint32_t size, void *outRes) { 10 | if (OSIsAddressValid((uint32_t) addr)) { 11 | KernelWrite((uint32_t) outRes, addr, size); 12 | } else { 13 | memset(outRes, 0, size); 14 | OSReport("CoreAgent_GetCoreMem invalid addr: %08X\n", addr); 15 | } 16 | 17 | return 0; 18 | } 19 | 20 | DECL_FUNCTION(uint32_t, CoreAgent_SetCoreMem, void *addr, uint32_t size, void *srcBuf) { 21 | KernelWrite((uint32_t) addr, srcBuf, size); 22 | ICInvalidateRange(addr, size); 23 | DCFlushRange(addr, size); 24 | 25 | return 0; 26 | } 27 | 28 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(CoreAgent_GetCoreMem, (0x3201C400 + (0x02067bd4 - 0x02000000)), (0x101C400 + (0x02067bd4 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 29 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(CoreAgent_SetCoreMem, (0x3201C400 + (0x02067cf8 - 0x02000000)), (0x101C400 + (0x02067cf8 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 30 | -------------------------------------------------------------------------------- /src/stub/CoreAgent_MemorySetGet.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #define CoreAgent_GetCoreMem ((uint32_t(*)(void *addr, uint32_t size, void *outRes))(0x101C400 + 0x02067bd4 - 0x02000000)) 5 | #define CoreAgent_SetCoreMem ((uint32_t(*)(void *addr, uint32_t size, void *srcBuf))(0x101C400 + 0x02067cf8 - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/EnumToStringUtil.cpp: -------------------------------------------------------------------------------- 1 | #include "EnumToStringUtil.h" 2 | 3 | const char *GDBStub_OSExceptionToString(OSExceptionType type) { 4 | switch (type) { 5 | case OS_EXCEPTION_TYPE_SYSTEM_RESET: 6 | return "SYSTEM_RESET"; 7 | case OS_EXCEPTION_TYPE_MACHINE_CHECK: 8 | return "MACHINE_CHECK"; 9 | case OS_EXCEPTION_TYPE_DSI: 10 | return "DSI "; 11 | case OS_EXCEPTION_TYPE_ISI: 12 | return "ISI "; 13 | case OS_EXCEPTION_TYPE_EXTERNAL_INTERRUPT: 14 | return "EXTERNAL "; 15 | case OS_EXCEPTION_TYPE_ALIGNMENT: 16 | return "ALIGNMENT"; 17 | case OS_EXCEPTION_TYPE_PROGRAM: 18 | return "PROGRAM "; 19 | case OS_EXCEPTION_TYPE_FLOATING_POINT: 20 | return "FP "; 21 | case OS_EXCEPTION_TYPE_DECREMENTER: 22 | return "DECREMENT"; 23 | case OS_EXCEPTION_TYPE_SYSTEM_CALL: 24 | return "SYS_CALL "; 25 | case OS_EXCEPTION_TYPE_TRACE: 26 | return "TRACE "; 27 | case OS_EXCEPTION_TYPE_PERFORMANCE_MONITOR: 28 | return "PERF_MON "; 29 | case OS_EXCEPTION_TYPE_BREAKPOINT: 30 | return "BREAKPTR "; 31 | case OS_EXCEPTION_TYPE_SYSTEM_INTERRUPT: 32 | return "SYS_INTER"; 33 | case OS_EXCEPTION_TYPE_ICI: 34 | return "ICI "; 35 | case 15: 36 | return "NONE*****"; 37 | } 38 | return "UNKNOWN EXCEPTION"; 39 | } 40 | 41 | // Checked 42 | const char *GDBStub_SigToString(GDB_SIGNAL signal) { 43 | switch (signal) { 44 | case GDBSIGNAL_SIG0: 45 | return "GDBSIGNAL_SIG0"; 46 | case GDBSIGNAL_SIGINT: 47 | return "GDBSIGNAL_SIGINT"; 48 | case GDBSIGNAL_SIGQUIT: 49 | return "GDBSIGNAL_SIGQUIT"; 50 | case GDBSIGNAL_SIGILL: 51 | return "GDBSIGNAL_SIGILL"; 52 | case GDBSIGNAL_SIGTRAP: 53 | return "GDBSIGNAL_SIGTRAP"; 54 | case GDBSIGNAL_SIGKILL: 55 | return "GDBSIGNAL_SIGKILL"; 56 | case GDBSIGNAL_SIGBUS: 57 | return "GDBSIGNAL_SIGBUS"; 58 | case GDBSIGNAL_SIGSEGV: 59 | return "GDBSIGNAL_SIGSEGV"; 60 | case GDBSIGNAL_SIGTERM: 61 | return "GDBSIGNAL_SIGTERM"; 62 | case GDBSIGNAL_SIGSTOP: 63 | return "GDBSIGNAL_SIGSTOP"; 64 | case GDBSIGNAL_RESUME: 65 | return "GDBSIGNAL_RESUME"; 66 | case GDBSIGNAL_DBGCTL: 67 | return "GDBSIGNAL_DBGCTL"; 68 | case GDBSIGNAL_DBGMSG: 69 | return "GDBSIGNAL_DBGMSG"; 70 | case GDBSIGNAL_BREAKPT: 71 | return "GDBSIGNAL_BREAKPT"; 72 | case GDBSIGNAL_DBGSTR: 73 | return "GDBSIGNAL_DBGSTR"; 74 | } 75 | return "**INVALID**"; 76 | } 77 | 78 | // Checked 79 | const char *GDBStub_StateToString(GDBStubState state) { 80 | switch (state) { 81 | case GDBSTUB_STATE_INACTIVE: 82 | return "INACTIVE"; 83 | case GDBSTUB_STATE_STOPPED: 84 | return "STOPPED"; 85 | case GDBSTUB_STATE_RUNNING: 86 | return "RUNNING"; 87 | case GDBSTUB_STATE_STOPPING: 88 | return "STOPPING"; 89 | case GDBSTUB_STATE_SINGLESTEP: 90 | return "SINGLESTEP"; 91 | case GDBSTUB_STATE_PROCESSING: 92 | return "PROCESSING"; 93 | case GDBSTUB_STATE_HUNG: 94 | return "HUNG"; 95 | } 96 | return "**INVALID**"; 97 | } 98 | 99 | const char *GDBStub_GDBFlagsToString(GDBStub_GDBFlags flag) { 100 | switch (flag) { 101 | case GDBFLAGS_ZERO: 102 | return " "; 103 | case GDBFLAGS_GDBSTOP: 104 | return "GDBSTOP"; 105 | case GDBFLAGS_GDBSTEP: 106 | return "GDBSTEP"; 107 | case GDBFLAGS_GDBCONT: 108 | return "GDBCONT"; 109 | default: 110 | break; 111 | } 112 | return ""; 113 | } 114 | 115 | const char *GDBStub_CmdToString(GDBStub_CMD cmd) { 116 | switch (cmd) { 117 | case GDBSTUB_CMD_IDLE: 118 | return "IDLE"; 119 | case GDBSTUB_CMD_STOP: 120 | return "STOP"; 121 | case GDBSTUB_CMD_CONTINUE: 122 | return "CONTINUE"; 123 | case GDBSTUB_CMD_SINGLESTEP: 124 | return "SINGLESTEP"; 125 | case GDBSTUB_CMD_GETSYSREG: 126 | return "GETSYSREG"; 127 | case GDBSTUB_CMD_PUTSYSREG: 128 | return "PUTSYSREG"; 129 | case GDBSTUB_CMD_ICINVALIDBLK: 130 | return "ICINVALIDBLK"; 131 | case GDBSTUB_CMD_DCINVALRNG: 132 | return "DCINVALRNG"; 133 | case GDBSTUB_CMD_GETCOREMEM: 134 | return "GETCOREMEM"; 135 | case GDBSTUB_CMD_PUTCOREMEM: 136 | return "PUTCOREMEM"; 137 | } 138 | return "**INVALID**"; 139 | } 140 | 141 | const char *GDBStub_ReqToString(GDBStub_Req req) { 142 | switch (req) { 143 | case GDBSTUB_REQ_STOPPED: 144 | return "STOPPED"; 145 | case GDBSTUB_REQ_FINISHED: 146 | return "FINISHED"; 147 | } 148 | return "**INVALID**"; 149 | } 150 | -------------------------------------------------------------------------------- /src/stub/EnumToStringUtil.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "GDBDefines.h" 4 | #include 5 | 6 | const char *GDBStub_OSExceptionToString(OSExceptionType type); 7 | const char *GDBStub_SigToString(GDB_SIGNAL signal); 8 | const char *GDBStub_StateToString(GDBStubState state); 9 | const char *GDBStub_GDBFlagsToString(GDBStub_GDBFlags flag); 10 | const char *GDBStub_CmdToString(GDBStub_CMD cmd); 11 | const char *GDBStub_ReqToString(GDBStub_Req req); 12 | -------------------------------------------------------------------------------- /src/stub/GDBDParseUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define gdb_getint ((uint32_t(*)(char **))(0x101C400 + 0x0203b628 - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/GDBDefines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef enum GDBStub_CMD : uint8_t { 10 | GDBSTUB_CMD_IDLE = 0, 11 | GDBSTUB_CMD_STOP = 1, 12 | GDBSTUB_CMD_CONTINUE = 2, 13 | GDBSTUB_CMD_SINGLESTEP = 3, 14 | GDBSTUB_CMD_GETSYSREG = 4, 15 | GDBSTUB_CMD_PUTSYSREG = 5, 16 | GDBSTUB_CMD_ICINVALIDBLK = 6, 17 | GDBSTUB_CMD_DCINVALRNG = 7, 18 | GDBSTUB_CMD_GETCOREMEM = 8, 19 | GDBSTUB_CMD_PUTCOREMEM = 9 20 | } GDBStub_CMD; 21 | 22 | typedef enum GDBMainState { 23 | GDBMAINSTATE_LOOP = 0, 24 | GDBMAINSTATE_STEP = 1, 25 | GDBMAINSTATE_RUN = 2, 26 | GDBMAINSTATE_VCONT = 3, 27 | GDBMAINSTATE_MAX_VALUE = 4 28 | } GDBMainState; 29 | 30 | typedef enum GDBStub_GDBFlags : uint8_t { 31 | GDBFLAGS_ZERO = 0, 32 | GDBFLAGS_GDBSTOP = 1, 33 | GDBFLAGS_GDBSTEP = 2, 34 | GDBFLAGS_INVAL = 3, 35 | GDBFLAGS_GDBCONT = 4 36 | } GDBStub_GDBFlags; 37 | 38 | typedef enum GDB_SIGNAL : uint16_t { 39 | GDBSIGNAL_SIG0 = 0, 40 | GDBSIGNAL_SIGINT = 2, 41 | GDBSIGNAL_SIGQUIT = 3, 42 | GDBSIGNAL_SIGILL = 4, 43 | GDBSIGNAL_SIGTRAP = 5, 44 | GDBSIGNAL_SIGKILL = 9, 45 | GDBSIGNAL_SIGBUS = 10, 46 | GDBSIGNAL_SIGSEGV = 11, 47 | GDBSIGNAL_SIGTERM = 15, 48 | GDBSIGNAL_SIGSTOP = 17, 49 | GDBSIGNAL_RESUME = 65437, 50 | GDBSIGNAL_DBGCTL = 65445, 51 | GDBSIGNAL_DBGMSG = 65446, 52 | GDBSIGNAL_BREAKPT = 65534, 53 | GDBSIGNAL_DBGSTR = 65535 54 | } GDB_SIGNAL; 55 | 56 | typedef enum GDBStub_ACCESS { 57 | GDBSTUB_ACCESS_NONE = 0, 58 | GDBSTUB_ACCESS_READ = 1, 59 | GDBSTUB_ACCESS_WRITE = 2 60 | } GDBStub_ACCESS; 61 | 62 | typedef enum GDBStubState { 63 | GDBSTUB_STATE_INACTIVE = 0, 64 | GDBSTUB_STATE_STOPPED = 1, 65 | GDBSTUB_STATE_RUNNING = 2, 66 | GDBSTUB_STATE_STOPPING = 3, 67 | GDBSTUB_STATE_SINGLESTEP = 4, 68 | GDBSTUB_STATE_PROCESSING = 5, 69 | GDBSTUB_STATE_HUNG = 6 70 | } GDBStubState; 71 | 72 | typedef enum GDBStub_Req : uint8_t { 73 | GDBSTUB_REQ_STOPPED = 0, 74 | GDBSTUB_REQ_FINISHED = 1 75 | } GDBStub_Req; 76 | 77 | typedef struct WUT_PACKED CMDStruct { 78 | GDBStub_CMD cmd; 79 | struct WUT_PACKED { 80 | unsigned int ack : 24; 81 | }; 82 | } CMDStruct; 83 | WUT_CHECK_OFFSET(CMDStruct, 0, cmd); 84 | WUT_CHECK_SIZE(CMDStruct, 0x4); 85 | 86 | typedef struct WUT_PACKED REQStruct { 87 | GDBStub_Req req; 88 | struct WUT_PACKED { 89 | unsigned int ack : 24; 90 | }; 91 | } REQStruct; 92 | WUT_CHECK_SIZE(REQStruct, 0x4); 93 | 94 | typedef struct WUT_PACKED GDBCoreInfo { 95 | uint8_t core; 96 | GDBStub_GDBFlags flag; 97 | uint16_t stoppedContextExternalIinterruptBit; 98 | GDB_SIGNAL nextSignal__; 99 | GDB_SIGNAL signal; 100 | GDBStub_ACCESS accessType; 101 | uint32_t accessLevel; 102 | char OSException; 103 | WUT_UNKNOWN_BYTES(3); 104 | int32_t exception_level; 105 | uint32_t numPrintedMessages; 106 | uint32_t totalMessagesInQueue; 107 | OSContext *exceptionContext; 108 | void *exceptionStack; 109 | GDBStubState state; 110 | uint32_t lastStateUpdateTime; 111 | REQStruct req; 112 | uint32_t reqAckValue; 113 | uint32_t req_arg; 114 | uint32_t lastReqSetTime; 115 | uint32_t reqDoneTime; 116 | CMDStruct cmd; 117 | uint32_t cmdAckValue; 118 | uint32_t cmdArg1; 119 | uint32_t cmdArg2; 120 | uint32_t cmdResult; 121 | uint32_t cmdDoneTime; 122 | uint32_t cmdSetAtTime; 123 | OSThread *stoppedThread; 124 | OSContext *stoppedContext; 125 | OSThread *currentThread; 126 | OSContext *currentContext; 127 | uint32_t virtual_ssr0; 128 | uint32_t virtual_ssr1; 129 | uint32_t cachedHID2; 130 | uint32_t cachedMSR; 131 | uint32_t cachedPC; 132 | uint32_t dabr; 133 | uint32_t iabr; 134 | uint32_t dar; 135 | } GDBCoreInfo; 136 | WUT_CHECK_OFFSET(GDBCoreInfo, 0, core); 137 | WUT_CHECK_OFFSET(GDBCoreInfo, 1, flag); 138 | WUT_CHECK_OFFSET(GDBCoreInfo, 2, stoppedContextExternalIinterruptBit); 139 | WUT_CHECK_OFFSET(GDBCoreInfo, 4, nextSignal__); 140 | WUT_CHECK_OFFSET(GDBCoreInfo, 6, signal); 141 | WUT_CHECK_OFFSET(GDBCoreInfo, 8, accessType); 142 | WUT_CHECK_OFFSET(GDBCoreInfo, 12, accessLevel); 143 | WUT_CHECK_OFFSET(GDBCoreInfo, 20, exception_level); 144 | WUT_CHECK_OFFSET(GDBCoreInfo, 24, numPrintedMessages); 145 | WUT_CHECK_OFFSET(GDBCoreInfo, 28, totalMessagesInQueue); 146 | WUT_CHECK_OFFSET(GDBCoreInfo, 32, exceptionContext); 147 | WUT_CHECK_OFFSET(GDBCoreInfo, 36, exceptionStack); 148 | WUT_CHECK_OFFSET(GDBCoreInfo, 40, state); 149 | WUT_CHECK_OFFSET(GDBCoreInfo, 44, lastStateUpdateTime); 150 | WUT_CHECK_OFFSET(GDBCoreInfo, 48, req); 151 | WUT_CHECK_OFFSET(GDBCoreInfo, 52, reqAckValue); 152 | WUT_CHECK_OFFSET(GDBCoreInfo, 56, req_arg); 153 | WUT_CHECK_OFFSET(GDBCoreInfo, 60, lastReqSetTime); 154 | WUT_CHECK_OFFSET(GDBCoreInfo, 64, reqDoneTime); 155 | WUT_CHECK_OFFSET(GDBCoreInfo, 68, cmd); 156 | WUT_CHECK_OFFSET(GDBCoreInfo, 72, cmdAckValue); 157 | WUT_CHECK_OFFSET(GDBCoreInfo, 76, cmdArg1); 158 | WUT_CHECK_OFFSET(GDBCoreInfo, 80, cmdArg2); 159 | WUT_CHECK_OFFSET(GDBCoreInfo, 84, cmdResult); 160 | WUT_CHECK_OFFSET(GDBCoreInfo, 88, cmdDoneTime); 161 | WUT_CHECK_OFFSET(GDBCoreInfo, 92, cmdSetAtTime); 162 | WUT_CHECK_OFFSET(GDBCoreInfo, 96, stoppedThread); 163 | WUT_CHECK_OFFSET(GDBCoreInfo, 100, stoppedContext); 164 | WUT_CHECK_OFFSET(GDBCoreInfo, 104, currentThread); 165 | WUT_CHECK_OFFSET(GDBCoreInfo, 108, currentContext); 166 | WUT_CHECK_OFFSET(GDBCoreInfo, 112, virtual_ssr0); 167 | WUT_CHECK_OFFSET(GDBCoreInfo, 116, virtual_ssr1); 168 | WUT_CHECK_OFFSET(GDBCoreInfo, 120, cachedHID2); 169 | WUT_CHECK_OFFSET(GDBCoreInfo, 124, cachedMSR); 170 | WUT_CHECK_OFFSET(GDBCoreInfo, 128, cachedPC); 171 | WUT_CHECK_OFFSET(GDBCoreInfo, 132, dabr); 172 | WUT_CHECK_OFFSET(GDBCoreInfo, 136, iabr); 173 | WUT_CHECK_OFFSET(GDBCoreInfo, 140, dar); 174 | WUT_CHECK_SIZE(GDBCoreInfo, 144); 175 | 176 | typedef enum DebugQueueType { 177 | STRING = 0, 178 | SET_CMD = 1, 179 | ACK_CMD = 2, 180 | SET_REQ = 3, 181 | ACK_REQ = 4 182 | } DebugQueueType; 183 | 184 | typedef struct GDBDebugMessageString { 185 | const char *format; 186 | void *args[9]; 187 | } GDBDebugMessageString; 188 | WUT_CHECK_OFFSET(GDBDebugMessageString, 0x0, format); 189 | WUT_CHECK_OFFSET(GDBDebugMessageString, 0x4, args); 190 | WUT_CHECK_SIZE(GDBDebugMessageString, 40); 191 | 192 | typedef struct GDBDebugMessageAckCmd { 193 | GDBStub_CMD cmd; 194 | uint32_t cmdAck; 195 | uint32_t cmdResult; 196 | } GDBDebugMessageAckCmd; 197 | WUT_CHECK_OFFSET(GDBDebugMessageAckCmd, 0x0, cmd); 198 | WUT_CHECK_OFFSET(GDBDebugMessageAckCmd, 0x4, cmdAck); 199 | WUT_CHECK_OFFSET(GDBDebugMessageAckCmd, 0x8, cmdResult); 200 | WUT_CHECK_SIZE(GDBDebugMessageAckCmd, 0xC); 201 | 202 | typedef struct GDBDebugMessageSetReq { 203 | GDBStub_Req req; 204 | uint32_t reqAck; 205 | uint32_t arg; 206 | } GDBDebugMessageSetReq; 207 | WUT_CHECK_OFFSET(GDBDebugMessageSetReq, 0x0, req); 208 | WUT_CHECK_OFFSET(GDBDebugMessageSetReq, 0x4, reqAck); 209 | WUT_CHECK_OFFSET(GDBDebugMessageSetReq, 0x8, arg); 210 | WUT_CHECK_SIZE(GDBDebugMessageSetReq, 0xC); 211 | 212 | typedef struct GDBDebugMessageSetCmd { 213 | uint32_t core; 214 | GDBStub_CMD cmd; 215 | uint32_t cmdCount; 216 | uint32_t arg1; 217 | uint32_t arg2; 218 | } GDBDebugMessageSetCmd; 219 | WUT_CHECK_OFFSET(GDBDebugMessageSetCmd, 0x0, core); 220 | WUT_CHECK_OFFSET(GDBDebugMessageSetCmd, 0x4, cmd); 221 | WUT_CHECK_OFFSET(GDBDebugMessageSetCmd, 0x8, cmdCount); 222 | WUT_CHECK_OFFSET(GDBDebugMessageSetCmd, 0xC, arg1); 223 | WUT_CHECK_OFFSET(GDBDebugMessageSetCmd, 0x10, arg2); 224 | WUT_CHECK_SIZE(GDBDebugMessageSetCmd, 0x14); 225 | 226 | typedef struct GDBDebugMessageAckReq { 227 | uint32_t core; 228 | GDBStub_Req req; 229 | uint32_t reqCount; 230 | } GDBDebugMessageAckReq; 231 | WUT_CHECK_OFFSET(GDBDebugMessageAckReq, 0x0, core); 232 | WUT_CHECK_OFFSET(GDBDebugMessageAckReq, 0x4, req); 233 | WUT_CHECK_OFFSET(GDBDebugMessageAckReq, 0x8, reqCount); 234 | WUT_CHECK_SIZE(GDBDebugMessageAckReq, 0x0C); 235 | 236 | typedef struct debug_queue_msg { 237 | uint64_t timeInTicks; 238 | uint32_t debugMask; 239 | DebugQueueType type; 240 | union { 241 | GDBDebugMessageString dbgStr; 242 | GDBDebugMessageAckCmd dbgAckCmd; 243 | GDBDebugMessageAckReq dbgAckReq; 244 | GDBDebugMessageSetCmd dbgSetCmd; 245 | GDBDebugMessageSetReq dbgSetReq; 246 | WUT_UNKNOWN_BYTES(0x20); 247 | }; 248 | WUT_PADDING_BYTES(8); 249 | } debug_queue_msg; 250 | WUT_CHECK_OFFSET(debug_queue_msg, 0x0, timeInTicks); 251 | WUT_CHECK_OFFSET(debug_queue_msg, 0x8, debugMask); 252 | WUT_CHECK_OFFSET(debug_queue_msg, 0xC, type); 253 | WUT_CHECK_OFFSET(debug_queue_msg, 0x10, dbgStr); 254 | WUT_CHECK_OFFSET(debug_queue_msg, 0x10, dbgAckCmd); 255 | WUT_CHECK_OFFSET(debug_queue_msg, 0x10, dbgAckReq); 256 | WUT_CHECK_OFFSET(debug_queue_msg, 0x10, dbgSetCmd); 257 | WUT_CHECK_OFFSET(debug_queue_msg, 0x10, dbgSetReq); 258 | WUT_CHECK_SIZE(debug_queue_msg, 64); 259 | 260 | typedef enum GDBWatchState : uint8_t { 261 | GDBWATCHSTATE_UNKNOWN = 0, 262 | GDBWATCHSTATE_RWATCH = 5, 263 | GDBWATCHSTATE_WATCH = 6, 264 | GDBWATCHSTATE_AWATCH = 7, 265 | 266 | } GDBWatchState; 267 | 268 | #define MSR_BIT_POW (1 << (31 - 13)) /*0x40000*/ 269 | #define MSR_BIT_RESERVED0 (1 << (31 - 14)) /*0x20000*/ 270 | #define MSR_BIT_ILE (1 << (31 - 15)) /*0x10000*/ 271 | #define MSR_BIT_EE (1 << (31 - 16)) /*0x8000*/ 272 | #define MSR_BIT_PR (1 << (31 - 17)) /*0x4000*/ 273 | #define MSR_BIT_FP (1 << (31 - 18)) /*0x2000*/ 274 | #define MSR_BIT_ME (1 << (31 - 19)) /*0x1000*/ 275 | #define MSR_BIT_FE0 (1 << (31 - 20)) /*0x800*/ 276 | #define MSR_BIT_SE (1 << (31 - 21)) /*0x400*/ 277 | #define MSR_BIT_BE (1 << (31 - 22)) /*0x200*/ 278 | #define MSR_BIT_FE1 (1 << (31 - 23)) /*0x100*/ 279 | #define MSR_BIT_RESERVED1 (1 << (31 - 24)) /*0x80*/ 280 | #define MSR_BIT_IP (1 << (31 - 25)) /*0x40*/ 281 | #define MSR_BIT_IR (1 << (31 - 26)) /*0x20*/ 282 | #define MSR_BIT_DR (1 << (31 - 27)) /*0x10*/ 283 | #define MSR_BIT_RESERVED2 (1 << (31 - 28)) /*8*/ 284 | #define MSR_BIT_PM (1 << (31 - 29)) /*4*/ 285 | #define MSR_BIT_RI (1 << (31 - 30)) /*2 */ 286 | #define MSR_BIT_LE (1 << (31 - 31)) /*1*/ 287 | 288 | #define TRAP_INSTRUCTION 0x7FE00008 289 | #define DBGSTR_INSTRUCTION 0x7ffff808 290 | #define DBGMSG_INSTRUCTION 0x7ffef008 291 | #define DBGCTL_INSTRUCTION 0x7ffe0008 292 | 293 | #define GDB_REGISTER_R0 0x00 294 | #define GDB_REGISTER_R1 0x01 295 | #define GDB_REGISTER_R2 0x02 296 | #define GDB_REGISTER_R3 0x03 297 | #define GDB_REGISTER_R4 0x04 298 | #define GDB_REGISTER_R5 0x05 299 | #define GDB_REGISTER_R6 0x06 300 | #define GDB_REGISTER_R7 0x07 301 | #define GDB_REGISTER_R31 0x1F 302 | #define GDB_REGISTER_F0 0x20 303 | #define GDB_REGISTER_F31 0x3F 304 | #define GDB_REGISTER_PC 0x40 305 | #define GDB_REGISTER_MSR 0x41 306 | #define GDB_REGISTER_CR 0x42 307 | #define GDB_REGISTER_LR 0x43 308 | #define GDB_REGISTER_CTR 0x44 309 | #define GDB_REGISTER_XER 0x45 310 | #define GDB_REGISTER_FPSCR 0x46 311 | 312 | #define GDB_REGISTER_DAR 0x6a 313 | #define GDB_REGISTER_DSISR 0x6b 314 | 315 | #define GDB_REGISTER_SRR0 0x70 316 | #define GDB_REGISTER_SRR1 0x71 317 | #define GDB_REGISTER_DABR 0x75 318 | #define GDB_REGISTER_IABR 0x79 319 | 320 | 321 | #define GDB_REGISTER_HID2 0x9F 322 | #define GDB_REGISTER_PIR 0xD6 323 | #define GDB_REGISTER_WPAR 0xA5 324 | #define GDB_REGISTER_PS0 0xA6 325 | #define GDB_REGISTER_PS31 0xC5 326 | #define GDB_REGISTER_GQR0 0xC6 327 | #define GDB_REGISTER_GQR1 0xC7 328 | #define GDB_REGISTER_GQR2 0xC8 329 | #define GDB_REGISTER_GQR3 0xC9 330 | #define GDB_REGISTER_GQR4 0xCA 331 | #define GDB_REGISTER_GQR5 0xCB 332 | #define GDB_REGISTER_GQR6 0xCC 333 | #define GDB_REGISTER_GQR7 0xCD 334 | 335 | #define DSISR_FLAG_ACCESS_RESTRICTED_BY_BAT_OR_PAGE 1 << (31 - 4) 336 | #define DSISR_FLAG_DABRMatch 1 << (31 - 9) -------------------------------------------------------------------------------- /src/stub/GDBThreadDefines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define IDLE_THREAD_ID_CORE_0 0x7001 7 | #define IDLE_THREAD_ID_CORE_1 0x7002 8 | #define IDLE_THREAD_ID_CORE_2 0x7003 9 | 10 | #define IDLE_THREAD_CORE_0 ((OSThread *) 0x100) 11 | #define IDLE_THREAD_CORE_1 ((OSThread *) 0x200) 12 | #define IDLE_THREAD_CORE_2 ((OSThread *) 0x300) 13 | #define IDLE_THREAD_LIST_END ((OSThread *) 0x400) 14 | #define END_OF_LIST_THREAD ((OSThread *) 0xFFFFFFFF) 15 | 16 | #define GET_CORE_FROM_IDLE_THREAD(thread) ((uint32_t) thread >> 8 & 3) - 1; 17 | 18 | #define DEFAULT_THREAD_ID 0x8000 19 | 20 | #define IS_IDLE_THREAD(thread) (((uint32_t) thread & ~0x300) == 0) 21 | #define GET_NEXT_IDLE_THREAD(thread) (OSThread *) (((uint32_t) thread & 0x300) + 0x100) 22 | #define GET_IDLE_THREAD_BY_CORE(coreOfThread) (OSThread *) ((coreOfThread * 0x100) + 0x100); 23 | -------------------------------------------------------------------------------- /src/stub/GDBUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #define GDBStub_MasterCore ((uint32_t(*)(void))(0x101C400 + 0x02036bc8 - 0x02000000)) 5 | #define FlushFPUContext ((void (*)(void))(0x101C400 + 0x0203ee5c - 0x02000000)) 6 | #define __OSSetAndLoadContextDebugger ((void (*)(OSContext *))(0x101C400 + 0x02008188 - 0x02000000)) 7 | 8 | extern "C" uint32_t in_TBLr(); -------------------------------------------------------------------------------- /src/stub/GDB_Commands.cpp: -------------------------------------------------------------------------------- 1 | #include "GDB_Commands.h" 2 | #include "GDBDParseUtils.h" 3 | #include "GDBThreadDefines.h" 4 | #include "GDBUtils.h" 5 | #include "GDB_IO.h" 6 | #include "MasterAgent_RegMemReadWrite.h" 7 | #include "MasterAgent_ThreadUtils.h" 8 | #include "imports.h" 9 | #include "utils/IOUtils.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | static char sGDBQueryStringBuffer[100] = {}; 20 | 21 | static char sOutPaginationBuffer[IO_BUFFER_CAPACITY - 4] = {}; 22 | 23 | void qSupported(const char *packet) { 24 | gIsDebuggerPresent = 1; 25 | #ifdef SHOW_DRIVER_THREADS 26 | gGDBGHSShowDrvrThreads = 1; 27 | #endif 28 | DAT_100d1378 = DAT_100d1378 + 1; 29 | gTimerKernelCallbackLastRun = in_TBLr() + g100msInTicks; 30 | MasterAgent_IOInit(); 31 | MasterAgent_IOPutString("PacketSize="); 32 | snprintf(sGDBQueryStringBuffer, sizeof(sGDBQueryStringBuffer), "%x", IO_BUFFER_CAPACITY); 33 | MasterAgent_IOPutString(sGDBQueryStringBuffer); 34 | MasterAgent_IOPutString(";qXfer:features:read+;qXfer:threads:read+;swbreak+;hwbreak+"); 35 | MasterAgent_IOPutString(";COSVer="); 36 | snprintf(sGDBQueryStringBuffer, sizeof(sGDBQueryStringBuffer), "%d", 21301); 37 | MasterAgent_IOPutString(sGDBQueryStringBuffer); 38 | MasterAgent_IOSendEx(false); 39 | } 40 | 41 | void qOffsets(const char *packet) { 42 | OSDynLoad_InternalData *rplIterator = *(reinterpret_cast(0x10081018)); 43 | OSDynLoad_NotifyData *rpl = nullptr; 44 | 45 | for (; rplIterator != nullptr; rplIterator = rplIterator->next) { 46 | if (!rplIterator->notifyData->name || std::string_view(rplIterator->notifyData->name).ends_with("rpx")) { 47 | rpl = rplIterator->notifyData; 48 | break; 49 | } 50 | } 51 | 52 | if (rpl != nullptr) { 53 | MasterAgent_IOInit(); 54 | 55 | snprintf(sGDBQueryStringBuffer, sizeof(sGDBQueryStringBuffer), "TextSeg=%08X;DataSeg=%08X", rpl->textAddr, rpl->dataAddr); 56 | MasterAgent_IOPutString(sGDBQueryStringBuffer); 57 | MasterAgent_IOSendEx(false); 58 | } else { 59 | MasterAgent_PutPacket("", false); 60 | } 61 | } 62 | 63 | static char sLibrariesReadBuffer[IO_BUFFER_CAPACITY] = {}; 64 | 65 | bool qXferLibrariesRead(const char *packet) { 66 | char *charPtr = (char *) packet; 67 | uint32_t read_offset = 0; 68 | uint32_t read_length = 0; 69 | if (charPtr[0] != '\0') { 70 | read_offset = gdb_getint(&charPtr); 71 | charPtr++; 72 | } else { 73 | OSReport("WARNING: qXferLibrariesRead failed to read offset.\n"); 74 | } 75 | if (charPtr[0] != '\0') { 76 | read_length = gdb_getint(&charPtr); 77 | } else { 78 | OSReport("WARNING: qXferLibrariesRead failed to read length.\n"); 79 | } 80 | 81 | if (((read_length * 2) + 1) > (IO_BUFFER_CAPACITY - 4)) { 82 | read_length = ((IO_BUFFER_CAPACITY - 4) / 2) - 1; 83 | } 84 | MasterAgent_IOInit(); 85 | 86 | if (GDBqXfer_libraries_read(read_offset, read_length + 1, sLibrariesReadBuffer) == 0) { 87 | MasterAgent_IOPutString("l"); 88 | DAT_100d1374 = 0; 89 | } else { 90 | MasterAgent_IOPutString("m"); 91 | } 92 | MasterAgent_IOPutStringAsHex(sLibrariesReadBuffer); 93 | MasterAgent_IOSendEx(false); 94 | return true; 95 | } 96 | 97 | bool qXferFeaturesRead(const char *packet) { 98 | auto *annexPtr = (char *) packet; 99 | char *annexEnd = annexPtr; 100 | while (annexEnd[0] != '\0' && annexEnd[0] != ':') { 101 | annexEnd++; 102 | } 103 | annexEnd[0] = '\0'; 104 | auto *charPtr = annexEnd + 1; 105 | if (strncmp(annexPtr, "target.xml", 10) == 0) { 106 | uint32_t read_offset = 0; 107 | uint32_t read_length = 0; 108 | if (charPtr[0] != '\0') { 109 | read_offset = gdb_getint(&charPtr); 110 | charPtr++; 111 | } else { 112 | OSReport("WARNING: qXferFeaturesRead failed to read offset.\n"); 113 | } 114 | if (charPtr[0] != '\0') { 115 | read_length = gdb_getint(&charPtr); 116 | } else { 117 | OSReport("WARNING: qXferFeaturesRead failed to read length.\n"); 118 | } 119 | if (read_length == 0 || read_offset >= GDBTargetXML.size()) { 120 | MasterAgent_PutPacket("l", false); 121 | } else { 122 | auto paginated_str = GDBTargetXML.substr(read_offset, read_length); 123 | memset(sOutPaginationBuffer, 0, sizeof(sOutPaginationBuffer)); 124 | if (paginated_str.size() < sizeof(sOutPaginationBuffer)) { 125 | memcpy(sOutPaginationBuffer, paginated_str.data(), paginated_str.size()); 126 | } else { 127 | OSReport("WARN: qXferFeaturesRead tried to overflow sOutPaginationBuffer!\n"); 128 | } 129 | MasterAgent_IOInit(); 130 | MasterAgent_IOPutString((paginated_str.size() == read_length) ? "m" : "l"); 131 | MasterAgent_IOPutString(sOutPaginationBuffer); 132 | MasterAgent_IOSendEx(false); 133 | } 134 | return true; 135 | } 136 | return false; 137 | } 138 | 139 | void qXferThreadRead(const char *packet) { 140 | auto *charPtr = (char *) packet; 141 | uint32_t read_offset = 0; 142 | uint32_t read_length = 0; 143 | if (charPtr[0] != '\0') { 144 | read_offset = gdb_getint(&charPtr); 145 | charPtr++; 146 | } else { 147 | OSReport("WARNING: qXferThreadRead failed to read offset.\n"); 148 | } 149 | if (charPtr[0] != '\0') { 150 | read_length = gdb_getint(&charPtr); 151 | } else { 152 | OSReport("WARNING: qXferThreadRead failed to read length.\n"); 153 | } 154 | if (read_length == 0) { 155 | MasterAgent_PutPacket("l", false); 156 | return; 157 | } 158 | 159 | int32_t index = 0; 160 | InitThreadInfoBuffer(); 161 | PutStringIntoThreadInfoBuffer(R"( )"); 162 | while (true) { 163 | auto *thread = MasterAgent_GetIndexedThreadID(index); 164 | if (thread == (OSThread *) 0xFFFFFFFF) { 165 | break; 166 | } 167 | 168 | auto threadID = MasterAgent_GetThreadID(thread); 169 | uint32_t core; 170 | const char *threadName; 171 | char threadNameOnStack[0x18]; 172 | 173 | if (thread == IDLE_THREAD_CORE_0) { 174 | core = 0; 175 | threadName = "Idle Core 0"; 176 | } else if (thread == IDLE_THREAD_CORE_1) { 177 | core = 1; 178 | threadName = "Idle Core 1"; 179 | } else if (thread == IDLE_THREAD_CORE_2) { 180 | core = 2; 181 | threadName = "Idle Core 2"; 182 | } else { 183 | core = thread->context.upir; 184 | threadName = thread->name; 185 | if (!threadName) { 186 | snprintf(threadNameOnStack, 0x18, "Thread 0x%8.8x", (uint32_t) thread); 187 | threadName = threadNameOnStack; 188 | } 189 | } 190 | snprintf(sGDBQueryStringBuffer, sizeof(sGDBQueryStringBuffer), R"("); 195 | 196 | PutXMLEscapedStringIntoThreadInfoBuffer(MasterAgent_ThreadExtraInfo(thread)); 197 | 198 | PutStringIntoThreadInfoBuffer(""); 199 | 200 | index++; 201 | } 202 | PutStringIntoThreadInfoBuffer(""); 203 | std::string_view resulBufAsView(GetThreadInfoBuffer()); 204 | if (read_offset >= resulBufAsView.size()) { 205 | MasterAgent_PutPacket("l", false); 206 | } else { 207 | auto paginated_str = resulBufAsView.substr(read_offset, read_length); 208 | memset(sOutPaginationBuffer, 0, sizeof(sOutPaginationBuffer)); 209 | if (paginated_str.size() < sizeof(sOutPaginationBuffer)) { 210 | memcpy(sOutPaginationBuffer, paginated_str.data(), paginated_str.size()); 211 | } else { 212 | OSReport("WARN: qXferThreadRead tried to overflow sOutPaginationBuffer!\n"); 213 | } 214 | MasterAgent_IOInit(); 215 | MasterAgent_IOPutString((paginated_str.size() == read_length) ? "m" : "l"); 216 | MasterAgent_IOPutString(sOutPaginationBuffer); 217 | MasterAgent_IOSendEx(false); 218 | } 219 | } 220 | 221 | void gdb_vpacket(const char *packet) { 222 | auto packetAsString = (char *) packet; 223 | 224 | if (strcmp(packetAsString, "vCont?") == 0) { 225 | MasterAgent_PutPacket("vCont;c;C;s;S;", false); 226 | return; 227 | } 228 | if (strcmp(packetAsString, "vCont;c") == 0) { 229 | gdb_continue("c"); 230 | return; 231 | } 232 | if (strncmp(packetAsString, "vGetRegs;", 9) == 0) { 233 | gdb_vgetregisters(&packetAsString[9]); 234 | return; 235 | } 236 | if (strncmp(packetAsString, "vCont;", 6) != 0) { 237 | MasterAgent_PutPacket("", false); 238 | return; 239 | } 240 | packetAsString = &packetAsString[6]; 241 | for (uint32_t i = 0; i < 3; i++) { 242 | gCoreData[i].flag = GDBFLAGS_GDBSTOP; 243 | gCoreData[i].nextSignal__ = GDBSIGNAL_SIG0; 244 | } 245 | 246 | bool stepDone = false; 247 | 248 | uint32_t result = 0; 249 | while (true) { 250 | if (packetAsString[0] == '\0') { 251 | if (result == 0) { 252 | gGDBStub_main_state = GDBMAINSTATE_VCONT; 253 | } 254 | MasterAgent_SendResult(result); 255 | return; 256 | } 257 | auto packetType = packetAsString[0]; 258 | if (packetType == 'C' || packetType == 'c' || packetType == 'S' || packetType == 's') { 259 | packetAsString++; 260 | GDB_SIGNAL signal = GDBSIGNAL_SIG0; 261 | GDBStub_GDBFlags flag; 262 | 263 | if (packetType == 'C' || packetType == 'S') { 264 | signal = (GDB_SIGNAL) gdb_getint(&packetAsString); 265 | } 266 | 267 | if (packetType == 'C' || packetType == 'c') { 268 | if (!stepDone) { 269 | flag = GDBFLAGS_GDBCONT; 270 | } else { 271 | flag = GDBFLAGS_GDBSTOP; 272 | } 273 | } else { 274 | flag = GDBFLAGS_GDBSTEP; 275 | } 276 | 277 | if (packetAsString[0] == '\0' || packetAsString[0] == ';') { 278 | for (uint32_t i = 0; i < gCoreCount; i++) { 279 | if (gCoreData[i].flag == GDBFLAGS_GDBSTOP) { 280 | gCoreData[i].signal = GDBSIGNAL_RESUME; 281 | if (flag == GDBFLAGS_GDBSTEP) { 282 | stepDone = true; 283 | } 284 | gCoreData[i].flag = flag; 285 | gCoreData[i].nextSignal__ = signal; 286 | } 287 | } 288 | } else if (packetAsString[0] == ':') { 289 | packetAsString++; 290 | auto *thread = gdb_get_pid_tid(&packetAsString); 291 | bool found = false; 292 | for (uint32_t i = 0; i < gCoreCount; i++) { 293 | auto *coreDataForI = &gCoreData[i]; 294 | if (coreDataForI->stoppedThread == thread) { 295 | coreDataForI->signal = GDBSIGNAL_RESUME; 296 | if (flag == GDBFLAGS_GDBSTEP) { 297 | stepDone = true; 298 | } 299 | coreDataForI->flag = flag; 300 | coreDataForI->nextSignal__ = signal; 301 | auto MSR = MasterAgent_GetRegister32(coreDataForI, coreDataForI->stoppedContext, GDB_REGISTER_MSR); 302 | if ((MSR & MSR_BIT_SE) != 0) { 303 | result = MasterAgent_SetRegister32(coreDataForI, coreDataForI->stoppedContext, GDB_REGISTER_MSR, MSR & ~MSR_BIT_SE); 304 | } 305 | found = true; 306 | } 307 | } 308 | 309 | if (!found) { 310 | OSReport("WARN: gdb_vpacket Core which runs thread %08x not found\n", thread); 311 | MasterAgent_SendResult(EPERM); 312 | return; 313 | } 314 | } 315 | } else { 316 | OSReport("WARN: gdb_vpacket invalid packet type. %s\n", packetAsString); 317 | MasterAgent_SendResult(EINVAL); 318 | return; 319 | } 320 | if (packetAsString[0] == ';') { 321 | packetAsString++; 322 | } 323 | } 324 | } 325 | 326 | static bool gdb_sendmemory(const char *packet) { 327 | auto *intPtr = (char *) packet; 328 | auto addr = gdb_getint(&intPtr); 329 | if (intPtr[0] != ',') { 330 | MasterAgent_SendResult(EINVAL); 331 | return true; 332 | } 333 | intPtr++; 334 | auto size = gdb_getint(&intPtr); 335 | MasterAgent_IOInit(); 336 | auto readResult = gdb_mem2hex(addr, size); 337 | if (readResult == 0) { 338 | MasterAgent_IOSendEx(false); 339 | } else { 340 | MasterAgent_SendResult(EIO); 341 | } 342 | 343 | return true; 344 | } 345 | 346 | static void gdb_sendregister(const char *packet) { 347 | auto *intPtr = (char *) (packet); 348 | auto regId = gdb_getint(&intPtr); 349 | if (regId > 0xdb) { 350 | MasterAgent_SendResult(EINVAL); 351 | return; 352 | } 353 | MasterAgent_IOInit(); 354 | if ((regId >= GDB_REGISTER_F0 && regId <= GDB_REGISTER_F31) || 355 | (regId >= GDB_REGISTER_PS0 && regId <= GDB_REGISTER_PS31)) { 356 | auto res = MasterAgent_GetRegister64(gGDBCurCoreInfo, gGDBCurCoreInfo->currentContext, regId); 357 | 358 | MasterAgent_IOPutHex64(res); 359 | } else { 360 | auto res = MasterAgent_GetRegister32(gGDBCurCoreInfo, gGDBCurCoreInfo->currentContext, regId); 361 | MasterAgent_IOPutHex32(res); 362 | } 363 | MasterAgent_IOSendEx(false); 364 | } 365 | 366 | DECL_FUNCTION(void, MasterAgent_ProcessPacket, const char *packet) { 367 | #ifdef PACKET_LOGGING 368 | OSReport("Process packet \"%s\"\n", packet); 369 | #endif 370 | auto prefix = packet[0]; 371 | if (prefix == 'v') { 372 | gdb_vpacket(packet); 373 | return; 374 | } 375 | if (prefix == 'm') { 376 | gdb_sendmemory(&packet[1]); 377 | return; 378 | } else if (prefix == 'p') { 379 | gdb_sendregister(&packet[1]); 380 | return; 381 | } else if (prefix == 'q') { 382 | auto *charPtr = (char *) (&packet[1]); 383 | if (strncmp(charPtr, "Supported:", 10) == 0) { 384 | qSupported(&charPtr[10]); 385 | return; 386 | } else if (strncmp(charPtr, "Xfer:", 5) == 0) { 387 | charPtr = (char *) (&charPtr[5]); 388 | if (strncmp(charPtr, "threads:read::", 14) == 0) { 389 | qXferThreadRead(&charPtr[14]); 390 | return; 391 | } else if (strncmp(charPtr, "features:read:", 14) == 0) { 392 | if (qXferFeaturesRead(&charPtr[14])) { 393 | return; 394 | } 395 | } else if (strncmp(charPtr, "libraries:read::", 16) == 0) { 396 | if (qXferLibrariesRead(&charPtr[16])) { 397 | return; 398 | } 399 | } 400 | } else if (strncmp(charPtr, "Offsets", 7) == 0) { 401 | qOffsets(&charPtr[7]); 402 | return; 403 | } 404 | } 405 | 406 | real_MasterAgent_ProcessPacket(packet); 407 | } 408 | 409 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_ProcessPacket, (0x3201C400 + (0x0203e4c4 - 0x02000000)), (0x101C400 + (0x0203e4c4 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); -------------------------------------------------------------------------------- /src/stub/GDB_Commands.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | static constexpr std::string_view GDBTargetXML = R"( 5 | 6 | 7 | powerpc:common 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | )"; 84 | 85 | void qSupported(const char *packet); 86 | void qXferThreadRead(const char *packet); 87 | bool qXferLibrariesRead(const char *packet); 88 | bool qXferFeaturesRead(const char *packet); 89 | void qOffsets(const char *packet); 90 | 91 | #define gdb_continue ((void (*)(const char *packet))(0x101C400 + 0x0203d11c - 0x02000000)) 92 | #define gdb_vgetregisters ((void (*)(const char *packet))(0x101C400 + 0x0203dfb4 - 0x02000000)) 93 | #define gdb_get_pid_tid ((OSThread * (*) (char **input))(0x101C400 + 0x0203d26c - 0x02000000)) 94 | #define GDBqXfer_libraries_read ((uint32_t(*)(uint32_t, int32_t, char *))(0x101C400 + 0x0203d3fc - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/GDB_IO.cpp: -------------------------------------------------------------------------------- 1 | #include "GDB_IO.h" 2 | #include "GDBUtils.h" 3 | #include "imports.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static const char *sHexChars = "0123456789abcdef"; 11 | static int32_t gIOOutputOffset = 0; 12 | int32_t gGDBCharsInIOBuffer = 0; 13 | int32_t gGDBCharsOfIOBufferConsumed = 0; 14 | char gIOBuffer[IO_BUFFER_CAPACITY] = {}; 15 | char gIOOutBuffer[IO_BUFFER_CAPACITY] = {}; 16 | 17 | static char sGDBCharUndoGetBuffer = '\0'; 18 | 19 | void ResetIOOffsets() { 20 | gIOOutputOffset = 0; 21 | gGDBCharsInIOBuffer = 0; 22 | gGDBCharsOfIOBufferConsumed = 0; 23 | memset(gIOBuffer, 0, sizeof(gIOBuffer)); 24 | memset(gIOOutBuffer, 0, sizeof(gIOOutBuffer)); 25 | sGDBCharUndoGetBuffer = '\0'; 26 | } 27 | 28 | // 0x02039158 29 | DECL_FUNCTION(void, MasterAgent_IOInit) { 30 | gIOOutputOffset = 0; 31 | } 32 | 33 | // 0x0203a76c 34 | DECL_FUNCTION(int32_t, MasterAgent_IOCurrentIndex) { 35 | return gIOOutputOffset; 36 | } 37 | 38 | // 0x0203a778 39 | DECL_FUNCTION(int32_t, MasterAgent_IORemaining) { 40 | return (IO_BUFFER_CAPACITY - 4) - gIOOutputOffset; 41 | } 42 | 43 | // 0x0203a788 44 | DECL_FUNCTION(void, MasterAgent_IORestoreIndex, int32_t index) { 45 | gIOOutputOffset = index; 46 | } 47 | 48 | // 0x0203a758 49 | DECL_FUNCTION(char *, MasterAgent_IOCurrentBufferPointer) { 50 | return &gIOOutBuffer[gIOOutputOffset]; 51 | } 52 | 53 | // 0x02039168 54 | DECL_FUNCTION(void, MasterAgent_IOPutChar, char curChar) { 55 | if ((IO_BUFFER_CAPACITY - 5) < gIOOutputOffset) { 56 | return; 57 | } 58 | 59 | gIOOutBuffer[gIOOutputOffset] = curChar; 60 | gIOOutputOffset++; 61 | } 62 | 63 | // 0x0203a7ec 64 | DECL_FUNCTION(void, MasterAgent_IOPutHex4, uint8_t val) { 65 | if ((IO_BUFFER_CAPACITY - 5) < gIOOutputOffset) { 66 | return; 67 | } 68 | gIOOutBuffer[gIOOutputOffset] = sHexChars[val & 0xf]; 69 | gIOOutputOffset++; 70 | } 71 | 72 | // 0x0203a794 73 | DECL_FUNCTION(void, MasterAgent_IOPutHex8, uint8_t val) { 74 | if (gIOOutputOffset < (IO_BUFFER_CAPACITY - 4 - 1)) { 75 | gIOOutBuffer[gIOOutputOffset] = sHexChars[(val >> 4) & 0xf]; 76 | gIOOutBuffer[gIOOutputOffset + 1] = sHexChars[val & 0xf]; 77 | gIOOutputOffset += 2; 78 | } else { 79 | if (gIOOutputOffset < (IO_BUFFER_CAPACITY - 4)) { 80 | gIOOutBuffer[gIOOutputOffset] = 'f'; 81 | } 82 | } 83 | } 84 | 85 | // 0x0203a884 86 | DECL_FUNCTION(void, MasterAgent_IOPutHex16, uint16_t value) { 87 | if (gIOOutputOffset < (IO_BUFFER_CAPACITY - 4 - 3)) { 88 | gIOOutBuffer[gIOOutputOffset] = sHexChars[(value >> 0xc) & 0xf]; 89 | gIOOutBuffer[gIOOutputOffset + 1] = sHexChars[(value >> 0x8) & 0xf]; 90 | gIOOutBuffer[gIOOutputOffset + 2] = sHexChars[(value >> 0x4) & 0xf]; 91 | gIOOutBuffer[gIOOutputOffset + 3] = sHexChars[(value >> 0x0) & 0xf]; 92 | gIOOutputOffset += 4; 93 | } else { 94 | auto offsetCpy = gIOOutputOffset; 95 | while (offsetCpy < (IO_BUFFER_CAPACITY - 4)) { 96 | gIOOutBuffer[offsetCpy] = 'f'; 97 | offsetCpy++; 98 | } 99 | } 100 | } 101 | 102 | // 0x02039190 103 | DECL_FUNCTION(void, MasterAgent_IOPutHex32, uint32_t value) { 104 | if (gIOOutputOffset < (IO_BUFFER_CAPACITY - 4 - 7)) { 105 | gIOOutBuffer[gIOOutputOffset] = sHexChars[value >> 0x1c & 0xf]; 106 | gIOOutBuffer[gIOOutputOffset + 1] = sHexChars[value >> 0x18 & 0xf]; 107 | gIOOutBuffer[gIOOutputOffset + 2] = sHexChars[value >> 0x14 & 0xf]; 108 | gIOOutBuffer[gIOOutputOffset + 3] = sHexChars[value >> 0x10 & 0xf]; 109 | gIOOutBuffer[gIOOutputOffset + 4] = sHexChars[value >> 0xc & 0xf]; 110 | gIOOutBuffer[gIOOutputOffset + 5] = sHexChars[value >> 0x8 & 0xf]; 111 | gIOOutBuffer[gIOOutputOffset + 6] = sHexChars[value >> 0x4 & 0xf]; 112 | gIOOutBuffer[gIOOutputOffset + 7] = sHexChars[value >> 0x0 & 0xf]; 113 | gIOOutputOffset += 8; 114 | } else { 115 | auto offsetCpy = gIOOutputOffset; 116 | while (offsetCpy < (IO_BUFFER_CAPACITY - 4)) { 117 | gIOOutBuffer[offsetCpy] = 'f'; 118 | offsetCpy++; 119 | } 120 | } 121 | } 122 | 123 | // 0x0203a81c 124 | DECL_FUNCTION(void, MasterAgent_IOPutStringAsHex, const char *str) { 125 | uint32_t offset = 0; 126 | char curChar = *str; 127 | while (curChar != 0) { 128 | if (gIOOutputOffset < (IO_BUFFER_CAPACITY - 4 - 1)) { 129 | gIOOutBuffer[gIOOutputOffset] = sHexChars[curChar >> 4]; 130 | gIOOutBuffer[gIOOutputOffset + 1] = sHexChars[curChar & 0xf]; 131 | gIOOutputOffset += 2; 132 | } 133 | offset++; 134 | curChar = str[offset]; 135 | } 136 | } 137 | 138 | void my_MasterAgent_PutPacket(const char *packetStr, bool checkPendingInputFirst); 139 | 140 | DECL_FUNCTION(void, MasterAgent_IOSendEx, bool unknwn1) { 141 | if ((IO_BUFFER_CAPACITY - 4) < gIOOutputOffset) { 142 | return; 143 | } 144 | gIOOutBuffer[gIOOutputOffset] = '\0'; 145 | gIOOutputOffset++; 146 | my_MasterAgent_PutPacket(gIOOutBuffer, unknwn1); 147 | } 148 | 149 | // Checked 150 | DECL_FUNCTION(void, MasterAgent_IOPutString, const char *str) { 151 | uint32_t offset = 0; 152 | auto curChar = str[0]; 153 | while (curChar != '\0') { 154 | auto uVar1 = gIOOutputOffset; 155 | if (gIOOutputOffset < (IO_BUFFER_CAPACITY - 4)) { 156 | uVar1 = gIOOutputOffset + 1; 157 | gIOOutBuffer[gIOOutputOffset] = curChar; 158 | } 159 | offset = offset + 1; 160 | gIOOutputOffset = uVar1; 161 | curChar = str[offset]; 162 | } 163 | } 164 | 165 | extern "C" int32_t DK_PCharReadAsync(uint32_t channel_guess, uint32_t bufferSize, char *buffer); 166 | 167 | // 0x02039250 168 | DECL_FUNCTION(bool, MasterAgent_IOPendingInput) { 169 | auto masterCore = GDBStub_MasterCore(); 170 | if (OSGetCoreId() == masterCore) { 171 | if (gIsDebuggerInitializedOnCore[masterCore] != 0) { 172 | if ((__OSGetAppFlags() & 0x80) == 0) { 173 | gGDBCharsInIOBuffer = Waikiki_Read(gIOBuffer, IO_BUFFER_CAPACITY); 174 | } else if (gGDBCharsInIOBuffer == gGDBCharsOfIOBufferConsumed) { 175 | gGDBCharsInIOBuffer = DK_PCharReadAsync(1, IO_BUFFER_CAPACITY, gIOBuffer); 176 | } 177 | gGDBCharsOfIOBufferConsumed = 0; 178 | if (gGDBCharsInIOBuffer < 1) { 179 | gGDBCharsInIOBuffer = 0; 180 | return false; 181 | } 182 | return true; 183 | } 184 | } 185 | return false; 186 | } 187 | 188 | // Checked 189 | DECL_FUNCTION(void, MasterAgent_IOUnGetChar, char charToUndo) { 190 | sGDBCharUndoGetBuffer = charToUndo; 191 | } 192 | 193 | // 0x0203936c 194 | DECL_FUNCTION(char, MasterAgent_IOGetChar) { 195 | char result; 196 | 197 | result = sGDBCharUndoGetBuffer; 198 | if (sGDBCharUndoGetBuffer != '\0') { 199 | sGDBCharUndoGetBuffer = '\0'; 200 | return result; 201 | } 202 | if (gGDBCharsInIOBuffer <= gGDBCharsOfIOBufferConsumed) { 203 | while (!my_MasterAgent_IOPendingInput()) { 204 | // wait for input... 205 | } 206 | if (gGDBCharsInIOBuffer <= gGDBCharsOfIOBufferConsumed) { 207 | return -1; 208 | } 209 | } 210 | auto offsetCpy = gGDBCharsOfIOBufferConsumed; 211 | gGDBCharsOfIOBufferConsumed++; 212 | return gIOBuffer[offsetCpy]; 213 | } 214 | 215 | // Doubled capacity to be able to escape every single char, add 4 for the '$' prefix and '#00' checksum suffix 216 | static char sWritePacketBuffer[(IO_BUFFER_CAPACITY * 2) + 4]; 217 | 218 | DECL_FUNCTION(void, MasterAgent_PutPacket, const char *packetStr, bool checkPendingInputFirst) { 219 | #ifdef PACKET_LOGGING 220 | OSReport("Send packet \"%s\"\n", packetStr); 221 | #endif 222 | uint32_t tryCount = 0; 223 | bool ctrl_c_seen = false; 224 | bool checkPendingInput = checkPendingInputFirst; 225 | while (true) { 226 | if (checkPendingInput) { 227 | while (my_MasterAgent_IOPendingInput()) { 228 | auto curChar = my_MasterAgent_IOGetChar(); 229 | if (curChar == '$') { 230 | my_MasterAgent_IOUnGetChar((char) curChar); 231 | return; 232 | } 233 | if (curChar == '\x03') { 234 | ctrl_c_seen = true; 235 | } 236 | } 237 | } 238 | 239 | if (packetStr == nullptr) { 240 | MasterAgent_IOWriteString("$#00"); 241 | } else { 242 | memset(sWritePacketBuffer, 0, sizeof(sWritePacketBuffer)); 243 | auto *outBufferPtr = sWritePacketBuffer; 244 | sWritePacketBuffer[0] = '$'; 245 | outBufferPtr++; 246 | uint32_t checksum = 0; 247 | uint32_t offset = 0; 248 | auto curChar = packetStr[offset]; 249 | while (curChar != '\0') { 250 | if ((curChar == '#' || curChar == '$' || curChar == '}' || curChar == '*')) { 251 | outBufferPtr[0] = '}'; 252 | checksum += '}'; 253 | outBufferPtr[1] = (char) (curChar ^ 0x20); 254 | checksum += (char) (curChar ^ 0x20); 255 | outBufferPtr++; 256 | } else { 257 | outBufferPtr[0] = curChar; 258 | checksum += curChar; 259 | } 260 | offset++; 261 | outBufferPtr++; 262 | curChar = packetStr[offset]; 263 | } 264 | 265 | outBufferPtr[0] = '#'; 266 | outBufferPtr[1] = sHexChars[(checksum & 0xff) >> 4]; 267 | outBufferPtr[2] = sHexChars[checksum & 0xf]; 268 | MasterAgent_IOWriteString(sWritePacketBuffer); 269 | } 270 | 271 | do { 272 | auto curChar = my_MasterAgent_IOGetChar(); 273 | if (((curChar == '+') || (curChar == '-')) || (curChar == '$')) { 274 | if (ctrl_c_seen) { 275 | gCoreData[GDBStub_MasterCore()].signal = GDBSIGNAL_SIGINT; 276 | } 277 | if (curChar == '+') { 278 | return; 279 | } 280 | if (curChar == '-') { 281 | tryCount++; 282 | if (tryCount > 9) { 283 | OSReport("MasterAgent_PutPacket: NACK received 10 times in a row\n"); 284 | return; 285 | } 286 | checkPendingInput = true; 287 | break; 288 | } else { 289 | my_MasterAgent_IOUnGetChar((char) curChar); 290 | return; 291 | } 292 | } 293 | if (curChar == '\x03') { 294 | ctrl_c_seen = true; 295 | } 296 | } while (!my_MasterAgent_IOPendingInput()); 297 | } 298 | } 299 | 300 | DECL_FUNCTION(void, MasterAgent_SendResult, uint32_t result) { 301 | if (result == 0) { 302 | MasterAgent_PutPacket("OK", false); 303 | return; 304 | } 305 | MasterAgent_IOInit(); 306 | my_MasterAgent_IOPutChar(0x45); 307 | my_MasterAgent_IOPutHex8((uint8_t) (result & 0xFF)); 308 | MasterAgent_IOSendEx(false); 309 | } 310 | 311 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOInit, (0x3201C400 + (0x02039158 - 0x02000000)), (0x101C400 + (0x02039158 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 312 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOCurrentIndex, (0x3201C400 + (0x0203a76c - 0x02000000)), (0x101C400 + (0x0203a76c - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 313 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IORemaining, (0x3201C400 + (0x0203a778 - 0x02000000)), (0x101C400 + (0x0203a778 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 314 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IORestoreIndex, (0x3201C400 + (0x0203a788 - 0x02000000)), (0x101C400 + (0x0203a788 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 315 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOCurrentBufferPointer, (0x3201C400 + (0x0203a758 - 0x02000000)), (0x101C400 + (0x0203a758 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 316 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutChar, (0x3201C400 + (0x02039168 - 0x02000000)), (0x101C400 + (0x02039168 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 317 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutHex4, (0x3201C400 + (0x0203a7ec - 0x02000000)), (0x101C400 + (0x0203a7ec - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 318 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutHex8, (0x3201C400 + (0x0203a794 - 0x02000000)), (0x101C400 + (0x0203a794 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 319 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutHex16, (0x3201C400 + (0x0203a884 - 0x02000000)), (0x101C400 + (0x0203a884 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 320 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutHex32, (0x3201C400 + (0x02039190 - 0x02000000)), (0x101C400 + (0x02039190 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 321 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutStringAsHex, (0x3201C400 + (0x0203a81c - 0x02000000)), (0x101C400 + (0x0203a81c - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 322 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOSendEx, (0x3201C400 + (0x02039a28 - 0x02000000)), (0x101C400 + (0x02039a28 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 323 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPutString, (0x3201C400 + (0x0203b6b4 - 0x02000000)), (0x101C400 + (0x0203b6b4 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 324 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOPendingInput, (0x3201C400 + (0x02039250 - 0x02000000)), (0x101C400 + (0x02039250 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 325 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOUnGetChar, (0x3201C400 + (0x0203942c - 0x02000000)), (0x101C400 + (0x0203942c - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 326 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_IOGetChar, (0x3201C400 + (0x0203936c - 0x02000000)), (0x101C400 + (0x0203936c - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 327 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_PutPacket, (0x3201C400 + (0x020394d8 - 0x02000000)), (0x101C400 + (0x020394d8 - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); 328 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_SendResult, (0x3201C400 + (0x0203bafc - 0x02000000)), (0x101C400 + (0x0203bafc - 0x02000000)), WUPS_FP_TARGET_PROCESS_GAME_AND_MENU); -------------------------------------------------------------------------------- /src/stub/GDB_IO.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "GDBDefines.h" 3 | #include 4 | #include 5 | 6 | void ResetIOOffsets(); 7 | 8 | // The MasterAgent_MainLoop still has a fixed buffer we need to replace before we 9 | // can increase this value 10 | #define IO_BUFFER_CAPACITY 0x800 11 | 12 | #define MasterAgent_IOInit ((void (*)(void))(0x101C400 + 0x02039158 - 0x02000000)) 13 | #define MasterAgent_IOPutString ((void (*)(const char *str))(0x101C400 + 0x0203b6b4 - 0x02000000)) 14 | #define MasterAgent_IOSendEx ((void (*)(bool))(0x101C400 + 0x02039a28 - 0x02000000)) 15 | #define MasterAgent_PutPacket ((void (*)(const char *packetStr, bool checkPendingInputFirst))(0x101C400 + 0x020394d8 - 0x02000000)) 16 | #define MasterAgent_IOPutStringAsHex ((void (*)(const char *str))(0x101C400 + 0x0203a81c - 0x02000000)) 17 | #define MasterAgent_SendResult ((void (*)(uint32_t))(0x101C400 + 0x0203bafc - 0x02000000)) 18 | #define MasterAgent_PutPacket ((void (*)(const char *packetStr, bool checkPendingInputFirst))(0x101C400 + 0x020394d8 - 0x02000000)) 19 | #define MasterAgent_IOInit ((void (*)(void))(0x101C400 + 0x02039158 - 0x02000000)) 20 | #define MasterAgent_IOPutString ((void (*)(const char *str))(0x101C400 + 0x0203b6b4 - 0x02000000)) 21 | #define MasterAgent_IOPutHex32 ((void (*)(uint32_t value))(0x101C400 + 0x02039190 - 0x02000000)) 22 | #define MasterAgent_IOWriteString ((void (*)(const char *))(0x101C400 + 0x020394a8 - 0x02000000)) 23 | #define MasterAgent_IOPutHex64 ((void (*)(uint64_t))(0x101C400 + 0x0203b6f8 - 0x02000000)) 24 | #define gdb_mem2hex ((uint32_t(*)(uint32_t addr, uint32_t size))(0x101C400 + 0x0203bcec - 0x02000000)) 25 | #define Waikiki_Read ((int32_t(*)(char *buffer, uint32_t bufferSize))(0x101C400 + 0x0203f2f0 - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/MasterAgent_RegMemReadWrite.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define MasterAgent_GetRegister64 ((uint64_t(*)(GDBCoreInfo * curInfo, OSContext * context, uint32_t regId))(0x101C400 + 0x02039e60 - 0x02000000)) 4 | #define MasterAgent_GetRegister32 ((uint32_t(*)(GDBCoreInfo * curInfo, OSContext * context, uint32_t regId))(0x101C400 + 0x02038cf0 - 0x02000000)) 5 | #define MasterAgent_SetRegister32 ((uint32_t(*)(GDBCoreInfo * coreInfo, OSContext * context, uint32_t regId, uint32_t value))(0x101C400 + 0x02039ed0 - 0x02000000)) 6 | -------------------------------------------------------------------------------- /src/stub/MasterAgent_ThreadUtils.cpp: -------------------------------------------------------------------------------- 1 | #include "GDBThreadDefines.h" 2 | #include "GDBUtils.h" 3 | #include "imports.h" 4 | #include 5 | #include 6 | #include 7 | static char sSomeStringBuffer[100] = {}; 8 | 9 | DECL_FUNCTION(const char *, MasterAgent_ThreadExtraInfo, OSThread *thread) { 10 | const char *result; 11 | if (thread == IDLE_THREAD_CORE_0 || thread == IDLE_THREAD_CORE_1 || thread == IDLE_THREAD_CORE_2) { 12 | if (thread == IDLE_THREAD_CORE_0) { 13 | result = "core 0 *running priority = (32:32) name = Idle Core 0"; 14 | } else if (thread == IDLE_THREAD_CORE_1) { 15 | result = "core 1 *running priority = (32:32) name = Idle Core 1"; 16 | } else { 17 | result = "core 2 *running priority = (32:32) name = Idle Core 2"; 18 | } 19 | } else { 20 | GDBStub_MasterCore(); 21 | uint32_t coreOfThread; 22 | 23 | if ((thread->attr & OS_THREAD_ATTRIB_AFFINITY_ANY) == OS_THREAD_ATTRIB_AFFINITY_CPU0) { 24 | coreOfThread = 0; 25 | } else if ((thread->attr & OS_THREAD_ATTRIB_AFFINITY_ANY) == OS_THREAD_ATTRIB_AFFINITY_CPU1) { 26 | coreOfThread = 1; 27 | } else if ((thread->attr & OS_THREAD_ATTRIB_AFFINITY_ANY) == OS_THREAD_ATTRIB_AFFINITY_CPU2) { 28 | coreOfThread = 2; 29 | } else { 30 | coreOfThread = thread->context.upir; 31 | } 32 | 33 | uint32_t threadState; 34 | threadState = thread->state; 35 | 36 | auto *threadStateAsStr = ""; 37 | if (gCoreData[coreOfThread].state == GDBSTUB_STATE_HUNG) { 38 | threadStateAsStr = "HUNG "; 39 | } 40 | if ((gGDBCurCoreInfo != nullptr) && 41 | (thread == gGDBStoppedCoreInfo->stoppedThread)) { 42 | threadStateAsStr = "*"; 43 | } 44 | 45 | auto threadReadyStr = ""; 46 | if ((threadState & OS_THREAD_STATE_READY) != 0) { 47 | threadReadyStr = "ready "; 48 | } 49 | auto threadRunningStr = ""; 50 | if ((threadState & OS_THREAD_STATE_RUNNING) != 0) { 51 | threadRunningStr = "running "; 52 | } 53 | auto threadWaitingStr = ""; 54 | if ((threadState & OS_THREAD_STATE_WAITING) != 0) { 55 | threadWaitingStr = "waiting "; 56 | } 57 | auto threadMoribundStr = ""; 58 | if ((threadState & OS_THREAD_STATE_MORIBUND) != 0) { 59 | threadMoribundStr = "moribund "; 60 | } 61 | auto threadDetachStr = ""; 62 | if ((thread->attr & OS_THREAD_ATTRIB_DETACHED) != 0) { 63 | threadDetachStr = "detach "; 64 | } 65 | char threadNameOnStack[0x18]; 66 | auto *threadName = thread->name; 67 | if (threadName == nullptr) { 68 | snprintf(threadNameOnStack, 0x18, "Thread 0x%8.8x", (uint32_t) thread); 69 | threadName = threadNameOnStack; 70 | } 71 | snprintf(sSomeStringBuffer, 100, "core %d %s%s%s%s%s%spriority = (%d:%d) name = %s", 72 | coreOfThread, 73 | threadStateAsStr, 74 | threadRunningStr, 75 | threadReadyStr, 76 | threadWaitingStr, 77 | threadMoribundStr, 78 | threadDetachStr, 79 | thread->priority, 80 | thread->basePriority, 81 | threadName); 82 | result = sSomeStringBuffer; 83 | } 84 | return result; 85 | } 86 | 87 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_ThreadExtraInfo, (0x3201C400 + (0x0203a548 - 0x02000000)), (0x101C400 + (0x0203a548 - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 88 | -------------------------------------------------------------------------------- /src/stub/MasterAgent_ThreadUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define MasterAgent_GetIndexedThreadID ((OSThread * (*) (int32_t))(0x101C400 + 0x0203a3e0 - 0x02000000)) 4 | #define MasterAgent_GetThreadID ((uint32_t(*)(OSThread *))(0x101C400 + 0x0203a290 - 0x02000000)) 5 | #define MasterAgent_ThreadExtraInfo ((const char *(*) (OSThread *) )(0x101C400 + 0x0203a548 - 0x02000000)) 6 | -------------------------------------------------------------------------------- /src/stub/MasterAgent_Utils.cpp: -------------------------------------------------------------------------------- 1 | #include "MasterAgent_Utils.h" 2 | #include "GDBDefines.h" 3 | #include "GDBUtils.h" 4 | #include "imports.h" 5 | #include "utils/kernel.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | DECL_FUNCTION(uint32_t, MasterAgent_PeekWord32, void *addr) { 12 | uint32_t value = 0xbaadf00d; 13 | if (OSIsAddressValid((uint32_t) &value)) { 14 | KernelWrite((uint32_t) &value, addr, 4); 15 | } else { 16 | OSReport("MasterAgent_PeekWord32 invalid addr: %08X\n", addr); 17 | } 18 | return value; 19 | } 20 | 21 | DECL_FUNCTION(uint32_t, MasterAgent_PokeInstr, void *addr, uint32_t newInstruction) { 22 | if (OSIsAddressValid((uint32_t) addr)) { 23 | KernelWriteU32((uint32_t) addr, newInstruction); 24 | for (uint32_t i = 0; i < 3; i++) { 25 | if (gCoreData[i].state == GDBSTUB_STATE_STOPPED) { 26 | if (i != GDBStub_MasterCore()) { 27 | MasterAgent_FlushICBlock(&gCoreData[i], (uint32_t) addr); 28 | } 29 | } 30 | } 31 | } else { 32 | OSReport("MasterAgent_PokeInstr invalid addr: %08X\n", addr); 33 | return EINVAL; 34 | } 35 | return 0; 36 | } 37 | 38 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_PeekWord32, (0x3201C400 + (0x0203881c - 0x02000000)), (0x101C400 + (0x0203881c - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 39 | WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(MasterAgent_PokeInstr, (0x3201C400 + (0x020389cc - 0x02000000)), (0x101C400 + (0x020389cc - 0x02000000)), WUPS_FP_TARGET_PROCESS_ALL); 40 | -------------------------------------------------------------------------------- /src/stub/MasterAgent_Utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define MasterAgent_FlushICBlock ((uint32_t(*)(GDBCoreInfo * curCoreInfo, uint32_t addr))(0x101C400 + 0x02038970 - 0x02000000)) -------------------------------------------------------------------------------- /src/stub/helper.s: -------------------------------------------------------------------------------- 1 | .globl in_TBLr 2 | in_TBLr: 3 | mftb %r3,268 4 | blr 5 | -------------------------------------------------------------------------------- /src/utils/IOUtils.cpp: -------------------------------------------------------------------------------- 1 | #include "IOUtils.h" 2 | #include 3 | #include 4 | 5 | #define THREAD_INFO_BUFFER_CAPACITY 0x10001 6 | static char sThreadInfoBuffer[THREAD_INFO_BUFFER_CAPACITY] = {}; 7 | static uint32_t sThreadInfoBufferOffset = 0; 8 | 9 | char *GetThreadInfoBuffer() { 10 | sThreadInfoBuffer[sThreadInfoBufferOffset] = '\0'; 11 | return sThreadInfoBuffer; 12 | } 13 | void InitThreadInfoBuffer() { 14 | memset(sThreadInfoBuffer, 0, sizeof(sThreadInfoBuffer)); 15 | sThreadInfoBufferOffset = 0; 16 | } 17 | 18 | bool PutStringIntoThreadInfoBuffer(const char *str) { 19 | uint32_t offset = 0; 20 | while (str[offset] != '\0') { 21 | if (sThreadInfoBufferOffset < (THREAD_INFO_BUFFER_CAPACITY - 2)) { 22 | sThreadInfoBuffer[sThreadInfoBufferOffset] = str[offset]; 23 | sThreadInfoBufferOffset++; 24 | } else { 25 | return false; 26 | } 27 | offset++; 28 | } 29 | return true; 30 | } 31 | 32 | bool PutCharIntoThreadInfoBuffer(char c) { 33 | // we want to keep a null byte at the end 34 | if (sThreadInfoBufferOffset < (THREAD_INFO_BUFFER_CAPACITY - 2)) { 35 | sThreadInfoBuffer[sThreadInfoBufferOffset] = c; 36 | sThreadInfoBufferOffset++; 37 | } else { 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | void PutXMLEscapedStringIntoThreadInfoBuffer(const char *str) { 44 | if (str != nullptr) { 45 | const char *curPtr = str; 46 | while (curPtr[0] != '\0') { 47 | switch (curPtr[0]) { 48 | case '<': 49 | PutStringIntoThreadInfoBuffer("<"); 50 | break; 51 | case '>': 52 | PutStringIntoThreadInfoBuffer(">"); 53 | break; 54 | case '&': 55 | PutStringIntoThreadInfoBuffer("&"); 56 | break; 57 | case '"': 58 | PutStringIntoThreadInfoBuffer("""); 59 | break; 60 | case '\'': 61 | PutStringIntoThreadInfoBuffer("'"); 62 | break; 63 | default: 64 | PutCharIntoThreadInfoBuffer(curPtr[0]); 65 | break; 66 | } 67 | curPtr++; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/utils/IOUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | char *GetThreadInfoBuffer(); 4 | void InitThreadInfoBuffer(); 5 | bool PutStringIntoThreadInfoBuffer(const char *str); 6 | bool PutCharIntoThreadInfoBuffer(char curChar); 7 | void PutXMLEscapedStringIntoThreadInfoBuffer(const char *str); -------------------------------------------------------------------------------- /src/utils/kernel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void KernelWrite(uint32_t addr, const void *data, uint32_t length) { 7 | uint32_t dst = OSEffectiveToPhysical(addr); 8 | uint32_t src = OSEffectiveToPhysical((uint32_t) data); 9 | if (dst == 0 || src == 0) { 10 | OSReport("Attempted to read/write from 0 (phys). dst %08X src %08X\n", dst, src); 11 | return; 12 | } 13 | KernelCopyData(dst, src, length); 14 | DCFlushRange((void *) addr, length); 15 | ICInvalidateRange((void *) addr, length); 16 | } 17 | 18 | void KernelWriteU32(uint32_t addr, uint32_t value) { 19 | uint32_t dst = OSEffectiveToPhysical(addr); 20 | uint32_t src = OSEffectiveToPhysical((uint32_t) &value); 21 | KernelCopyData(dst, src, 4); 22 | DCFlushRange((void *) addr, 4); 23 | ICInvalidateRange((void *) addr, 4); 24 | } 25 | 26 | 27 | #define KiInitRendezvous ((void (*)(void *)) 0xfff097c4) 28 | #define KiSendICIToOtherCores ((void (*)(uint32_t, uint32_t, uint32_t, uint32_t)) 0xfff052c8) 29 | #define KiWaitRendezvous ((void (*)(void *, uint32_t)) 0xfff097e0) 30 | 31 | void KeSetDABR(uint32_t addr, int32_t allCores, int32_t matchReads, int32_t matchWrite) { 32 | auto maskedAddr = (addr & 0xfffffff8) | 4; 33 | if (matchReads != 0) { 34 | maskedAddr = maskedAddr | 5; 35 | } 36 | if (matchWrite != 0) { 37 | maskedAddr = maskedAddr | 2; 38 | } 39 | 40 | asm volatile( 41 | " mtspr 1013, %0 \n" 42 | : 43 | : "r"(maskedAddr) 44 | :); 45 | 46 | if (allCores != 0) { 47 | register int r13 asm("r13"); 48 | KiInitRendezvous(reinterpret_cast(r13 + -0x72a0)); 49 | KiSendICIToOtherCores(5, maskedAddr, 0, 1); 50 | KiWaitRendezvous(reinterpret_cast(r13 + -0x72a0), 7); 51 | } 52 | } 53 | 54 | void KeSetIABR(uint32_t addr, int32_t allCores) { 55 | auto maskedAddr = (addr & 0xfffffffc) | 3; 56 | 57 | asm volatile( 58 | " mtspr 1010, %0 \n" 59 | : 60 | : "r"(maskedAddr) 61 | :); 62 | 63 | if (allCores != 0) { 64 | register int r13 asm("r13"); 65 | KiInitRendezvous(reinterpret_cast(r13 + -0x7290)); 66 | KiSendICIToOtherCores(10, maskedAddr, 0, 1); 67 | KiWaitRendezvous(reinterpret_cast(r13 + -0x7290), 7); 68 | } 69 | } 70 | 71 | void EnableIABRandDABRSupport() { 72 | KernelPatchSyscall(0x4B, reinterpret_cast(&KeSetDABR)); 73 | KernelPatchSyscall(0x4C, reinterpret_cast(&KeSetIABR)); 74 | } 75 | 76 | void DisableIABRandDABRSupport() { 77 | // Restore original syscalls 78 | KernelPatchSyscall(0x4B, 0xfff036a0); 79 | KernelPatchSyscall(0x4C, 0xfff0370c); 80 | } -------------------------------------------------------------------------------- /src/utils/kernel.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | void KernelWrite(uint32_t addr, const void *data, uint32_t length); 6 | void KernelWriteU32(uint32_t addr, uint32_t value); 7 | 8 | void EnableIABRandDABRSupport(); 9 | void DisableIABRandDABRSupport(); -------------------------------------------------------------------------------- /src/version.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #define PLUGIN_VERSION_EXTRA "" --------------------------------------------------------------------------------