├── .gitignore ├── .gitmodules ├── .ycm_extra_conf.py ├── AUTHORS.txt ├── CMakeLists.txt ├── Doxyfile ├── LICENSE.txt ├── README.md ├── build.sh ├── demos ├── xendbg-lldb1.png ├── xendbg-lldb2.png └── xendbg-repl.gif ├── include ├── Debugger │ ├── Debugger.hpp │ ├── DebuggerHVM.hpp │ ├── DebuggerPV.hpp │ ├── StopReason.hpp │ └── WatchpointType.hpp ├── GDBServer │ ├── GDBConnection.hpp │ ├── GDBPacket.hpp │ ├── GDBPacketQueue.hpp │ ├── GDBRequest │ │ ├── GDBBreakpointRequest.hpp │ │ ├── GDBMemoryRequest.hpp │ │ ├── GDBQueryRequest.hpp │ │ ├── GDBRegisterRequest.hpp │ │ ├── GDBRequest.hpp │ │ ├── GDBRequestBase.hpp │ │ └── GDBStepContinueRequest.hpp │ ├── GDBRequestHandler.hpp │ ├── GDBResponse │ │ ├── GDBMemoryResponse.hpp │ │ ├── GDBQueryResponse.hpp │ │ ├── GDBRegisterResponse.hpp │ │ ├── GDBResponse.hpp │ │ └── GDBResponseBase.hpp │ └── GDBServer.hpp ├── Globals.hpp ├── Registers │ ├── Register.hpp │ ├── RegisterContext.hpp │ ├── RegistersX86.hpp │ ├── RegistersX86Any.hpp │ ├── RegistersX86_32.hpp │ └── RegistersX86_64.hpp ├── Util │ ├── IndentHelper.hpp │ ├── choice.hpp │ ├── clear.hpp │ ├── overloaded.hpp │ ├── pop_ret.hpp │ └── string.hpp └── Xen │ ├── BridgeHeaders │ ├── domctl.h │ ├── hvm_save.h │ ├── libxl.h │ ├── privcmd.h │ ├── ring.h │ ├── vm_event.h │ ├── xencall.h │ ├── xenctrl.h │ ├── xendevicemodel.h │ ├── xenevtchn.h │ ├── xenforeignmemory.h │ ├── xenguest.h │ └── xenstore.h │ ├── Common.hpp │ ├── Domain.hpp │ ├── DomainHVM.hpp │ ├── DomainPV.hpp │ ├── HVMMonitor.hpp │ ├── PagePermissions.hpp │ ├── PageTableEntry.hpp │ ├── Xen.hpp │ ├── XenCall.hpp │ ├── XenCtrl.hpp │ ├── XenDeviceModel.hpp │ ├── XenEventChannel.hpp │ ├── XenException.hpp │ ├── XenForeignMemory.hpp │ └── XenStore.hpp ├── install.sh └── src ├── CommandLine.cpp ├── CommandLine.hpp ├── Constants.hpp ├── DebugSession.cpp ├── DebugSession.hpp ├── Debugger ├── Debugger.cpp ├── DebuggerHVM.cpp └── DebuggerPV.cpp ├── GDBServer ├── GDBConnection.cpp ├── GDBPacket.cpp ├── GDBPacketQueue.cpp ├── GDBRequest │ ├── GDBMemoryRequest.cpp │ ├── GDBQueryRequest.cpp │ └── GDBRegisterRequest.cpp ├── GDBRequestHandler.cpp ├── GDBResponse │ ├── GDBMemoryResponse.cpp │ ├── GDBQueryResponse.cpp │ ├── GDBRegisterResponse.cpp │ └── GDBResponse.cpp └── GDBServer.cpp ├── REPL ├── Command │ ├── Action.hpp │ ├── ArgsHandle.cpp │ ├── ArgsHandle.hpp │ ├── Argument.cpp │ ├── Argument.hpp │ ├── Command.cpp │ ├── Command.hpp │ ├── CommandBase.hpp │ ├── CommandVerb.cpp │ ├── CommandVerb.hpp │ ├── Flag.cpp │ ├── Flag.hpp │ ├── FlagsHandle.cpp │ ├── FlagsHandle.hpp │ ├── MakeCommand.cpp │ ├── MakeCommand.hpp │ ├── Match.cpp │ ├── Match.hpp │ ├── MatchHelper.hpp │ ├── Verb.cpp │ └── Verb.hpp ├── DebuggerREPL.cpp ├── DebuggerREPL.hpp ├── DebuggerWrapper.cpp ├── DebuggerWrapper.hpp ├── Parser │ ├── Expression │ │ ├── Expression.hpp │ │ ├── ExpressionGeneric.hpp │ │ └── Operator │ │ │ ├── BinaryOperator.hpp │ │ │ ├── Precedence.hpp │ │ │ └── UnaryOperator.hpp │ ├── Parser.cpp │ ├── Parser.hpp │ ├── ParserException.hpp │ ├── Predicate.hpp │ ├── Sentinel.hpp │ ├── Token │ │ ├── Constant.hpp │ │ ├── Label.hpp │ │ ├── Match.hpp │ │ ├── String.hpp │ │ ├── Symbol.hpp │ │ ├── TokenMatchResult.hpp │ │ └── Variable.hpp │ └── Tokenizer.hpp ├── REPL.cpp └── REPL.hpp ├── ServerModeController.cpp ├── ServerModeController.hpp ├── Util └── IndentHelper.cpp ├── Xen ├── Domain.cpp ├── DomainHVM.cpp ├── DomainPV.cpp ├── HVMMonitor.cpp ├── PageTableEntry.cpp ├── Xen.cpp ├── XenCall.cpp ├── XenCtrl.cpp ├── XenDeviceModel.cpp ├── XenEventChannel.cpp ├── XenForeignMemory.cpp └── XenStore.cpp └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 2 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 3 | 4 | .idea/ 5 | .ycm_extra_conf.py 6 | 7 | # User-specific stuff 8 | .idea/**/workspace.xml 9 | .idea/**/tasks.xml 10 | .idea/**/usage.statistics.xml 11 | .idea/**/dictionaries 12 | .idea/**/shelf 13 | 14 | # Generated files 15 | .idea/**/contentModel.xml 16 | 17 | # Sensitive or high-churn files 18 | .idea/**/dataSources/ 19 | .idea/**/dataSources.ids 20 | .idea/**/dataSources.local.xml 21 | .idea/**/sqlDataSources.xml 22 | .idea/**/dynamic.xml 23 | .idea/**/uiDesigner.xml 24 | .idea/**/dbnavigator.xml 25 | 26 | # Gradle 27 | .idea/**/gradle.xml 28 | .idea/**/libraries 29 | 30 | # Gradle and Maven with auto-import 31 | # When using Gradle or Maven with auto-import, you should exclude module files, 32 | # since they will be recreated, and may cause churn. Uncomment if using 33 | # auto-import. 34 | # .idea/modules.xml 35 | # .idea/*.iml 36 | # .idea/modules 37 | 38 | # CMake 39 | cmake-build-*/ 40 | 41 | # Mongo Explorer plugin 42 | .idea/**/mongoSettings.xml 43 | 44 | # File-based project format 45 | *.iws 46 | 47 | # IntelliJ 48 | out/ 49 | 50 | # mpeltonen/sbt-idea plugin 51 | .idea_modules/ 52 | 53 | # JIRA plugin 54 | atlassian-ide-plugin.xml 55 | 56 | # Cursive Clojure plugin 57 | .idea/replstate.xml 58 | 59 | # Crashlytics plugin (for Android Studio and IntelliJ) 60 | com_crashlytics_export_strings.xml 61 | crashlytics.properties 62 | crashlytics-build.properties 63 | fabric.properties 64 | 65 | # Editor-based Rest Client 66 | .idea/httpRequests 67 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/ELFIO"] 2 | path = third_party/ELFIO 3 | url = https://github.com/serge1/ELFIO.git 4 | [submodule "third_party/CLI11"] 5 | path = third_party/CLI11 6 | url = https://github.com/CLIUtils/CLI11.git 7 | [submodule "third_party/uvw"] 8 | path = third_party/uvw 9 | url = https://github.com/skypjack/uvw.git 10 | -------------------------------------------------------------------------------- /AUTHORS.txt: -------------------------------------------------------------------------------- 1 | Authors ordered by first contribution. 2 | 3 | Spencer Michaels 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.9) 2 | project(xendbg) 3 | 4 | if(NOT CMAKE_BUILD_TYPE) 5 | set(CMAKE_BUILD_TYPE Release ... FORCE) 6 | endif() 7 | 8 | # xendbg must be built with clang/libc++; it won't compile with gcc/libstdc++. 9 | set(CMAKE_C_COMPILER "clang") 10 | set(CMAKE_CXX_COMPILER "clang++") 11 | 12 | set(SEC_FLAGS "-fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE") 13 | set(SEC_LINK_FLAGS "-Wl,-z,relro,-z,now,-z,noexecstack -pie") 14 | 15 | set(WARNING_EXCEPTIONS "-Wno-unused-private-field -Wno-unused-parameter -Wno-unused-function") 16 | 17 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics -Wall -Wextra -pedantic ${WARNING_EXCEPTIONS} -O3 ${SEC_FLAGS}") 18 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3") # for Valgrind 19 | set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -g3 -fsanitize=address,undefined -Wno-macro-redefined") 20 | 21 | set(CMAKE_CXX_STANDARD 17) 22 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 23 | set(CMAKE_REQUIRED_FLAGS -std=c++17) 24 | set(CLANG_DEFAULT_CXX_STDLIB "libc++") 25 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") 26 | 27 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics -stdlib=libc++ -Wall -Wextra -pedantic ${WARNING_EXCEPTIONS} -O3 ${SEC_FLAGS}") 28 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3") #valgrind 29 | set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -g3 -fsanitize=address,undefined -Wno-macro-redefined") 30 | 31 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SEC_LINK_FLAGS}") 32 | 33 | include_directories( 34 | include 35 | third_party/CLI11/include 36 | third_party/ELFIO/include 37 | third_party/uvw/src) 38 | 39 | file(GLOB_RECURSE SRC_FILES src/*.cpp) 40 | file(GLOB_RECURSE INCLUDE_FILES include/*.hpp) 41 | 42 | add_executable(xendbg ${SRC_FILES} ${INCLUDE_FILES}) 43 | 44 | target_link_libraries(xendbg 45 | capstone 46 | pthread 47 | readline 48 | c++experimental 49 | uv 50 | xencall 51 | xenctrl 52 | xendevicemodel 53 | xenevtchn 54 | xenforeignmemory 55 | xenlight 56 | xenstore 57 | xlutil) 58 | 59 | install(TARGETS xendbg DESTINATION bin) 60 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-2019 NCC Group 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | git submodule update --init 5 | 6 | # Build CLI11 7 | echo "# Building third party dep: CLI11" 8 | cd third_party/CLI11 9 | git submodule update --init 10 | mkdir build && cd build 11 | cmake .. && make && sudo make install 12 | 13 | # Build ELFIO 14 | echo "# Building third party dep: ELFIO" 15 | cd ../ELFIO 16 | aclocal 17 | autoconf 18 | autoheader 19 | automake --add-missing 20 | ./configure && make && sudo make install 21 | 22 | # Build uvw 23 | echo "# Building third party dep: uvw" 24 | cd ../uvw/build 25 | cmake .. && make && sudo make install 26 | 27 | echo "# Building xendbg" 28 | mkdir build && cd build 29 | cmake .. 30 | make 31 | -------------------------------------------------------------------------------- /demos/xendbg-lldb1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nccgroup/xendbg/74ce0c1ad6398b14e5702d0f58832d40088cea23/demos/xendbg-lldb1.png -------------------------------------------------------------------------------- /demos/xendbg-lldb2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nccgroup/xendbg/74ce0c1ad6398b14e5702d0f58832d40088cea23/demos/xendbg-lldb2.png -------------------------------------------------------------------------------- /demos/xendbg-repl.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nccgroup/xendbg/74ce0c1ad6398b14e5702d0f58832d40088cea23/demos/xendbg-repl.gif -------------------------------------------------------------------------------- /include/Debugger/DebuggerHVM.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_DEBUGGERHVM_HPP 23 | #define XENDBG_DEBUGGERHVM_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include "Debugger.hpp" 36 | 37 | namespace xd::dbg { 38 | 39 | class DebuggerHVM : public Debugger { 40 | public: 41 | DebuggerHVM(uvw::Loop &loop, xen::DomainHVM domain, 42 | xen::XenDeviceModel &xendevicemodel, xen::XenEventChannel &xenevtchn, 43 | bool non_stop_mode); 44 | ~DebuggerHVM() override = default; 45 | 46 | void attach() override; 47 | void detach() override; 48 | 49 | void continue_() override; 50 | void single_step() override; 51 | 52 | void insert_watchpoint(xen::Address address, uint32_t bytes, WatchpointType type) override; 53 | void remove_watchpoint(xen::Address address, uint32_t bytes, WatchpointType type) override; 54 | 55 | private: 56 | xen::DomainHVM _domain; 57 | std::shared_ptr _monitor; 58 | 59 | std::optional _last_single_step_breakpoint_addr; 60 | bool _is_continuing; 61 | bool _non_stop_mode; 62 | 63 | void on_event(vm_event_st event); 64 | }; 65 | 66 | } 67 | 68 | 69 | #endif //XENDBG_DEBUGGERHVM_HPP 70 | -------------------------------------------------------------------------------- /include/Debugger/DebuggerPV.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_DEBUGGERPV_HPP 23 | #define XENDBG_DEBUGGERPV_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include "Debugger.hpp" 35 | 36 | namespace xd::dbg { 37 | 38 | class DebuggerPV : public Debugger { 39 | public: 40 | DebuggerPV(uvw::Loop &loop, xen::DomainPV domain); 41 | ~DebuggerPV() override = default; 42 | 43 | void attach() override; 44 | void detach() override; 45 | 46 | void continue_() override; 47 | void single_step() override; 48 | 49 | private: 50 | xen::DomainPV _domain; 51 | std::shared_ptr _timer; 52 | bool _is_in_pre_continue_singlestep, _is_continuing; 53 | 54 | xen::VCPU_ID _last_single_step_vcpu_id; 55 | std::optional _last_single_step_breakpoint_addr; 56 | }; 57 | 58 | } 59 | 60 | 61 | #endif //XENDBG_DEBUGGERPV_HPP 62 | -------------------------------------------------------------------------------- /include/Debugger/StopReason.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_STOP_REASON_HPP 23 | #define XENDBG_STOP_REASON_HPP 24 | 25 | #include 26 | 27 | #include 28 | 29 | #include "WatchpointType.hpp" 30 | 31 | namespace xd::dbg { 32 | 33 | struct StopReasonBase { 34 | StopReasonBase(int signal, xen::VCPU_ID vcpu_id) 35 | : signal(signal), vcpu_id(vcpu_id) 36 | {} 37 | 38 | int signal; 39 | xen::VCPU_ID vcpu_id; 40 | }; 41 | 42 | struct StopReasonBreakpoint : public StopReasonBase { 43 | StopReasonBreakpoint(int signal, xen::VCPU_ID vcpu_id) 44 | : StopReasonBase(signal, vcpu_id) 45 | {} 46 | }; 47 | 48 | struct StopReasonWatchpoint : public StopReasonBase { 49 | StopReasonWatchpoint(int signal, xen::VCPU_ID vcpu_id, xen::Address address, 50 | WatchpointType type) 51 | : StopReasonBase(signal, vcpu_id ), address(address), type(type) 52 | {} 53 | 54 | xen::Address address; 55 | WatchpointType type; 56 | }; 57 | 58 | using StopReason = std::variant< 59 | StopReasonBreakpoint, 60 | StopReasonWatchpoint 61 | >; 62 | 63 | } 64 | 65 | #endif //XENDBG_STOP_REASON_HPP 66 | -------------------------------------------------------------------------------- /include/Debugger/WatchpointType.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_WATCHPOINT_TYPE_HPP 23 | #define XENDBG_WATCHPOINT_TYPE_HPP 24 | 25 | namespace xd::dbg { 26 | 27 | enum class WatchpointType { 28 | Read, 29 | Write, 30 | Access, // RWX 31 | }; 32 | 33 | } 34 | 35 | #endif //XENDBG_WATCHPOINT_TYPE_HPP 36 | -------------------------------------------------------------------------------- /include/GDBServer/GDBConnection.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBCONNECTION_HPP 23 | #define XENDBG_GDBCONNECTION_HPP 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include "GDBPacketQueue.hpp" 31 | #include "GDBServer/GDBRequest/GDBRequest.hpp" 32 | #include "GDBServer/GDBResponse/GDBResponse.hpp" 33 | 34 | namespace xd::gdb { 35 | 36 | class UnknownPacketTypeException : public std::runtime_error { 37 | public: 38 | explicit UnknownPacketTypeException(const std::string &data) 39 | : std::runtime_error(data) {}; 40 | }; 41 | 42 | class GDBPacket; 43 | 44 | class GDBConnection : public std::enable_shared_from_this { 45 | public: 46 | using OnReceiveFn = std::function; 47 | using OnCloseFn = std::function; 48 | using OnErrorFn = std::function; 49 | 50 | explicit GDBConnection(std::shared_ptr tcp); 51 | ~GDBConnection(); 52 | 53 | void enable_error_strings() { _error_strings = true; }; 54 | void disable_ack_mode() { _ack_mode = false; }; 55 | 56 | void stop(); 57 | void read(OnReceiveFn on_receive, OnCloseFn on_close, OnErrorFn on_error); 58 | 59 | void send(const rsp::GDBResponse &packet); 60 | void send_error(uint8_t code, std::string message); 61 | 62 | private: 63 | std::shared_ptr _tcp; 64 | GDBPacketQueue _input_queue; 65 | bool _ack_mode, _is_initializing, _error_strings; 66 | OnCloseFn _on_close; 67 | OnErrorFn _on_error; 68 | OnReceiveFn _on_receive; 69 | 70 | static req::GDBRequest parse_packet(const GDBPacket &packet); 71 | }; 72 | 73 | } 74 | 75 | #endif //XENDBG_GDBCONNECTION_HPP 76 | -------------------------------------------------------------------------------- /include/GDBServer/GDBPacket.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBPACKET_HPP 23 | #define XENDBG_GDBPACKET_HPP 24 | 25 | #include 26 | #include 27 | 28 | namespace xd::gdb { 29 | 30 | class GDBPacket { 31 | public: 32 | explicit GDBPacket(std::string contents); 33 | GDBPacket(std::string contents, uint8_t checksum); 34 | 35 | const std::string &get_contents() const { return _contents; }; 36 | const uint8_t &get_checksum() const { return _checksum; }; 37 | 38 | std::string to_string() const; 39 | 40 | bool is_checksum_valid() const; 41 | bool starts_with(const std::string &s) const; 42 | 43 | private: 44 | std::string _contents; 45 | uint8_t _checksum; 46 | 47 | uint8_t calculate_checksum() const; 48 | }; 49 | 50 | } 51 | 52 | #endif //XENDBG_GDBPACKET_HPP 53 | -------------------------------------------------------------------------------- /include/GDBServer/GDBPacketQueue.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBPACKETQUEUE_HPP 23 | #define XENDBG_GDBPACKETQUEUE_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "GDBPacket.hpp" 31 | 32 | namespace xd::gdb { 33 | 34 | class NoPacketException : std::exception {}; 35 | 36 | class GDBPacketQueue { 37 | public: 38 | void append(const std::vector &data); 39 | GDBPacket pop(); 40 | 41 | bool empty() const { return _packets.empty(); }; 42 | 43 | private: 44 | std::queue _packets; 45 | std::vector _buffer; 46 | }; 47 | 48 | } 49 | 50 | #endif //XENDBG_GDBPACKETQUEUE_HPP 51 | -------------------------------------------------------------------------------- /include/GDBServer/GDBRequest/GDBBreakpointRequest.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBBREAKPOINTREQUEST_HPP 23 | #define XENDBG_GDBBREAKPOINTREQUEST_HPP 24 | 25 | #include "GDBRequestBase.hpp" 26 | 27 | #define DECLARE_BREAKPOINT_REQUEST(name, ch) \ 28 | class name : public GDBRequestBase { \ 29 | public: \ 30 | explicit name(const std::string &data) \ 31 | : GDBRequestBase(data, ch) \ 32 | { \ 33 | _type = read_hex_number(); \ 34 | expect_char(','); \ 35 | _address = read_hex_number(); \ 36 | expect_char(','); \ 37 | _kind = read_hex_number(); \ 38 | expect_end(); \ 39 | }; \ 40 | uint64_t get_address() const { return _address; }; \ 41 | uint8_t get_type() const { return _type; }; \ 42 | uint8_t get_kind() const { return _kind; }; \ 43 | private: \ 44 | uint64_t _address; \ 45 | uint8_t _type, _kind; \ 46 | } 47 | 48 | namespace xd::gdb::req { 49 | 50 | DECLARE_BREAKPOINT_REQUEST(BreakpointInsertRequest, 'Z'); 51 | DECLARE_BREAKPOINT_REQUEST(BreakpointRemoveRequest, 'z'); 52 | 53 | } 54 | 55 | #endif //XENDBG_GDBBREAKPOINTREQUEST_HPP 56 | -------------------------------------------------------------------------------- /include/GDBServer/GDBRequest/GDBMemoryRequest.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBMEMORYREQUEST_HPP 23 | #define XENDBG_GDBMEMORYREQUEST_HPP 24 | 25 | #include 26 | 27 | #include "GDBRequestBase.hpp" 28 | 29 | namespace xd::gdb::req { 30 | 31 | class MemoryReadRequest : public GDBRequestBase { 32 | public: 33 | explicit MemoryReadRequest(const std::string &data); 34 | 35 | uint64_t get_address() const { return _address; }; 36 | uint64_t get_length() const { return _length; }; 37 | 38 | private: 39 | uint64_t _address; 40 | uint64_t _length; 41 | }; 42 | 43 | class MemoryWriteRequest : public GDBRequestBase { 44 | public: 45 | explicit MemoryWriteRequest(const std::string &data); 46 | 47 | uint64_t get_address() const { return _address; }; 48 | 49 | uint64_t get_length() const { return _length; }; 50 | 51 | const std::vector &get_data() const { return _data; }; 52 | 53 | private: 54 | uint64_t _address; 55 | uint64_t _length; 56 | std::vector _data; 57 | }; 58 | 59 | } 60 | 61 | #endif //XENDBG_GDBMEMORYREQUEST_HPP 62 | -------------------------------------------------------------------------------- /include/GDBServer/GDBRequest/GDBQueryRequest.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBQUERYREQUEST_HPP 23 | #define XENDBG_GDBQUERYREQUEST_HPP 24 | 25 | #include 26 | 27 | #include "GDBRequestBase.hpp" 28 | 29 | namespace xd::gdb::req { 30 | 31 | DECLARE_SIMPLE_REQUEST(QueryEnableErrorStrings, "QEnableErrorStrings"); 32 | 33 | DECLARE_SIMPLE_REQUEST(QueryThreadSuffixSupportedRequest, "QThreadSuffixSupported"); 34 | 35 | DECLARE_SIMPLE_REQUEST(QueryListThreadsInStopReplySupportedRequest, "QListThreadsInStopReply"); 36 | 37 | DECLARE_SIMPLE_REQUEST(QueryHostInfoRequest, "qHostInfo"); 38 | 39 | DECLARE_SIMPLE_REQUEST(QueryProcessInfoRequest, "qProcessInfo"); 40 | 41 | DECLARE_SIMPLE_REQUEST(QueryCurrentThreadIDRequest, "qC"); 42 | 43 | DECLARE_SIMPLE_REQUEST(QueryThreadInfoStartRequest, "qfThreadInfo"); 44 | 45 | DECLARE_SIMPLE_REQUEST(QueryThreadInfoContinuingRequest, "qsThreadInfo"); 46 | 47 | class QueryWatchpointSupportInfo : public GDBRequestBase { 48 | public: 49 | explicit QueryWatchpointSupportInfo(const std::string &data); 50 | }; 51 | 52 | class QuerySupportedRequest : public GDBRequestBase { 53 | public: 54 | explicit QuerySupportedRequest(const std::string &data); 55 | 56 | const std::vector get_features() { return _features; }; 57 | 58 | private: 59 | std::vector _features; 60 | }; 61 | 62 | class QueryRegisterInfoRequest : public GDBRequestBase { 63 | public: 64 | explicit QueryRegisterInfoRequest(const std::string &data); 65 | 66 | uint16_t get_register_id() const { return _register_id; }; 67 | 68 | private: 69 | uint16_t _register_id; 70 | }; 71 | 72 | class QueryMemoryRegionInfoRequest : public GDBRequestBase { 73 | public: 74 | explicit QueryMemoryRegionInfoRequest(const std::string &data); 75 | 76 | uint64_t get_address() const { return _address; }; 77 | 78 | private: 79 | uint64_t _address; 80 | }; 81 | 82 | } 83 | 84 | #endif //XENDBG_GDBQUERYREQUEST_HPP 85 | -------------------------------------------------------------------------------- /include/GDBServer/GDBRequest/GDBStepContinueRequest.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBSTEPCONTINUEREQUEST_HPP 23 | #define XENDBG_GDBSTEPCONTINUEREQUEST_HPP 24 | 25 | #include "GDBRequestBase.hpp" 26 | 27 | #define DECLARE_SIGNAL_REQUESTS(name1, ch1, name2, ch2) \ 28 | DECLARE_SIMPLE_REQUEST(name1, ch1); \ 29 | class name2 : public GDBRequestBase { \ 30 | public: \ 31 | explicit name2(const std::string &data) \ 32 | : GDBRequestBase(data, ch2), _signal(0) \ 33 | { \ 34 | _signal = read_byte(); \ 35 | expect_end(); \ 36 | }; \ 37 | uint8_t get_signal() { return _signal; }; \ 38 | private: \ 39 | uint8_t _signal; \ 40 | } 41 | 42 | namespace xd::gdb::req { 43 | 44 | DECLARE_SIGNAL_REQUESTS(ContinueRequest, 'c', ContinueSignalRequest, 'C'); 45 | DECLARE_SIGNAL_REQUESTS(StepRequest, 's', StepSignalRequest, 'S'); 46 | 47 | } 48 | 49 | #endif //XENDBG_GDBSTEPCONTINUEREQUEST_HPP 50 | -------------------------------------------------------------------------------- /include/GDBServer/GDBResponse/GDBMemoryResponse.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBMEMORYRESPONSE_HPP 23 | #define XENDBG_GDBMEMORYRESPONSE_HPP 24 | 25 | #include 26 | #include 27 | 28 | #include "GDBResponseBase.hpp" 29 | 30 | namespace xd::gdb::rsp { 31 | 32 | class MemoryReadResponse : public GDBResponse { 33 | public: 34 | explicit MemoryReadResponse(unsigned char * data, size_t length) 35 | : _data(data, data + length) {}; 36 | 37 | std::string to_string() const override; 38 | 39 | private: 40 | std::vector _data; 41 | }; 42 | 43 | } 44 | 45 | #endif //XENDBG_GDBMEMORYRESPONSE_HPP 46 | -------------------------------------------------------------------------------- /include/GDBServer/GDBResponse/GDBRegisterResponse.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBREGISTERRESPONSE_HPP 23 | #define XENDBG_GDBREGISTERRESPONSE_HPP 24 | 25 | #include 26 | 27 | #include 28 | 29 | #include "GDBResponseBase.hpp" 30 | 31 | namespace xd::gdb::rsp { 32 | 33 | class RegisterReadResponse : public GDBResponse { 34 | public: 35 | explicit RegisterReadResponse(uint64_t value, int width = sizeof(uint64_t)) 36 | : _value(value), _width(width) {}; 37 | 38 | std::string to_string() const override; 39 | 40 | private: 41 | uint64_t _value; 42 | int _width; 43 | }; 44 | 45 | class GeneralRegistersBatchReadResponse : public GDBResponse { 46 | public: 47 | explicit GeneralRegistersBatchReadResponse(xd::reg::RegistersX86Any registers) 48 | : _registers(std::move(registers)) {} 49 | 50 | std::string to_string() const override; 51 | 52 | template 53 | static void write_register(std::stringstream &ss, const Reg_t®) { 54 | ss << std::setw(2*sizeof(typename Reg_t::Value)) << reg; 55 | } 56 | 57 | private: 58 | xd::reg::RegistersX86Any _registers; 59 | }; 60 | 61 | } 62 | 63 | #endif //XENDBG_GDBREGISTERRESPONSE_HPP 64 | -------------------------------------------------------------------------------- /include/GDBServer/GDBResponse/GDBResponseBase.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_GDBRESPONSEBASE_HPP 23 | #define XENDBG_GDBRESPONSEBASE_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace xd::gdb::rsp { 30 | 31 | namespace { 32 | // Writes the bytes of a value of arbitrary size in guest order 33 | template 34 | void write_bytes(std::stringstream &ss, Value_t value) { 35 | auto *p = (unsigned char*)&value; 36 | auto *end = p + sizeof(Value_t); 37 | 38 | ss << std::hex << std::setfill('0'); 39 | while (p != end) 40 | ss << std::setw(2) << (unsigned)(*p++); 41 | } 42 | 43 | void write_byte(std::stringstream &ss, uint8_t byte) { 44 | ss << std::hex << std::setfill('0'); 45 | ss << std::setw(2) << (unsigned)byte; 46 | } 47 | 48 | std::string hexify(const std::string& s) { 49 | std::stringstream ss; 50 | ss << std::hex << std::setfill('0'); 51 | for (const unsigned char c : s) 52 | ss << std::setw(2) << (unsigned)c; 53 | return ss.str(); 54 | } 55 | 56 | template 57 | void add_list_entry(std::stringstream &ss, Value_t value) { 58 | ss << value; 59 | ss << ","; 60 | } 61 | 62 | template 63 | void add_map_entry(std::stringstream &ss, Key_t key, Value_t value) { 64 | ss << key; 65 | ss << ":"; 66 | ss << value; 67 | ss << ";"; 68 | } 69 | } 70 | 71 | class GDBResponse { 72 | public: 73 | virtual ~GDBResponse() = default; 74 | virtual std::string to_string() const = 0; 75 | }; 76 | 77 | } 78 | 79 | #endif //XENDBG_GDBRESPONSEBASE_HPP 80 | -------------------------------------------------------------------------------- /include/GDBServer/GDBServer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | 23 | #ifndef XENDBG_GDBSERVER_HPP 24 | #define XENDBG_GDBSERVER_HPP 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | #include 32 | 33 | namespace xd::gdb { 34 | 35 | class GDBConnection; 36 | 37 | class GDBServer : public std::enable_shared_from_this { 38 | public: 39 | using OnAcceptFn = std::function)>; 40 | using OnErrorFn = std::function; 41 | 42 | explicit GDBServer(uvw::Loop &loop); 43 | ~GDBServer(); 44 | 45 | void stop(); 46 | void listen(const std::string& address_str, uint16_t port, OnAcceptFn on_accept, OnErrorFn on_error); 47 | 48 | private: 49 | std::shared_ptr _server; 50 | OnAcceptFn _on_accept; 51 | OnErrorFn _on_error; 52 | }; 53 | 54 | } 55 | 56 | #endif //XENDBG_GDBSERVER_HPP 57 | -------------------------------------------------------------------------------- /include/Globals.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef GLOBALS_HPP 23 | #define GLOBALS_HPP 24 | 25 | #define LOGNAME_CONSOLE "console" 26 | #define LOGNAME_ERROR "stderr" 27 | 28 | #endif //GLOBALS_HPP 29 | -------------------------------------------------------------------------------- /include/Registers/RegistersX86.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_REGISTERSX86_HPP 23 | #define XENDBG_REGISTERSX86_HPP 24 | 25 | #include "Register.hpp" 26 | 27 | namespace xd::reg::x86 { 28 | 29 | DECLARE_REGISTER(cr0, uint64_t, -1); 30 | DECLARE_REGISTER(cr3, uint64_t, -1); 31 | DECLARE_REGISTER(cr4, uint64_t, -1); 32 | DECLARE_REGISTER(msr_efer, uint64_t, -1); 33 | 34 | } 35 | 36 | #endif //XENDBG_REGISTERSX86_HPP 37 | -------------------------------------------------------------------------------- /include/Registers/RegistersX86Any.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_REGISTERSX86ANY_HPP 23 | #define XENDBG_REGISTERSX86ANY_HPP 24 | 25 | #include 26 | 27 | #include "RegistersX86_32.hpp" 28 | #include "RegistersX86_64.hpp" 29 | #include 30 | 31 | namespace xd::reg { 32 | 33 | using RegistersX86Any = std::variant< 34 | x86_32::RegistersX86_32, 35 | x86_64::RegistersX86_64>; 36 | 37 | template 38 | uint64_t read_register(const RegistersX86Any ®s) { 39 | return std::visit(util::overloaded { 40 | [](const reg::x86_32::RegistersX86_32 ®s) { 41 | return (uint64_t)regs.get(); 42 | }, 43 | [](const reg::x86_64::RegistersX86_64 ®s) { 44 | return (uint64_t)regs.get(); 45 | } 46 | }, regs); 47 | } 48 | 49 | } 50 | 51 | #endif //XENDBG_REGISTERS_HPP 52 | -------------------------------------------------------------------------------- /include/Registers/RegistersX86_32.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | 23 | #ifndef XENDBG_REGISTERS_X86_HPP 24 | #define XENDBG_REGISTERS_X86_HPP 25 | 26 | #include "Register.hpp" 27 | #include "RegisterContext.hpp" 28 | #include "RegistersX86.hpp" 29 | 30 | namespace xd::reg::x86_32 { 31 | 32 | DECLARE_REGISTER(eax, uint32_t, 0); 33 | DECLARE_REGISTER(ebx, uint32_t, 2); 34 | DECLARE_REGISTER(ecx, uint32_t, 2); 35 | DECLARE_REGISTER(edx, uint32_t, 1); 36 | DECLARE_REGISTER(esi, uint32_t, 4); 37 | DECLARE_REGISTER(edi, uint32_t, 5); 38 | DECLARE_REGISTER(ebp, uint32_t, 6); 39 | DECLARE_REGISTER(esp, uint32_t, 7); 40 | DECLARE_REGISTER(eip, uint32_t, 8); 41 | DECLARE_REGISTER(eflags, uint16_t, 9); 42 | DECLARE_REGISTER(ss, uint32_t, -1); 43 | DECLARE_REGISTER(cs, uint16_t, -1); 44 | DECLARE_REGISTER(ds, uint16_t, -1); 45 | DECLARE_REGISTER(es, uint16_t, -1); 46 | DECLARE_REGISTER(fs, uint16_t, -1); 47 | DECLARE_REGISTER(gs, uint16_t, -1); 48 | 49 | using RegistersX86_32 = RegisterContext< 50 | eax, ebx, ecx, edx, esp, ss, ebp, esi, edi, 51 | eip, eflags, cs, ds, es, fs, gs, 52 | x86::cr0, x86::cr3, x86::cr4, x86::msr_efer>; 53 | 54 | } 55 | 56 | #endif //XENDBG_REGISTERS_X86_HPP 57 | -------------------------------------------------------------------------------- /include/Registers/RegistersX86_64.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | 23 | #ifndef XENDBG_REGISTERS_X86_64_HPP 24 | #define XENDBG_REGISTERS_X86_64_HPP 25 | 26 | #include "Register.hpp" 27 | #include "RegisterContext.hpp" 28 | #include "RegistersX86.hpp" 29 | 30 | namespace xd::reg::x86_64 { 31 | 32 | DECLARE_REGISTER(rax, uint64_t, 0); 33 | DECLARE_REGISTER(rdx, uint64_t, 1); 34 | DECLARE_REGISTER(rcx, uint64_t, 2); 35 | DECLARE_REGISTER(rbx, uint64_t, 3); 36 | DECLARE_REGISTER(rsi, uint64_t, 4); 37 | DECLARE_REGISTER(rdi, uint64_t, 5); 38 | DECLARE_REGISTER(rbp, uint64_t, 6); 39 | DECLARE_REGISTER(rsp, uint64_t, 7); 40 | DECLARE_REGISTER(r8, uint64_t, 8); 41 | DECLARE_REGISTER(r9, uint64_t, 9); 42 | DECLARE_REGISTER(r10, uint64_t, 10); 43 | DECLARE_REGISTER(r11, uint64_t, 11); 44 | DECLARE_REGISTER(r12, uint64_t, 12); 45 | DECLARE_REGISTER(r13, uint64_t, 13); 46 | DECLARE_REGISTER(r14, uint64_t, 14); 47 | DECLARE_REGISTER(r15, uint64_t, 15); 48 | DECLARE_REGISTER(rip, uint64_t, 16); 49 | DECLARE_REGISTER(rflags, uint64_t, 17); 50 | DECLARE_REGISTER(fs, uint16_t, -1); 51 | DECLARE_REGISTER(gs, uint16_t, -1); 52 | DECLARE_REGISTER(cs, uint16_t, -1); 53 | DECLARE_REGISTER(ds, uint16_t, -1); 54 | DECLARE_REGISTER(ss, uint16_t, -1); 55 | 56 | using RegistersX86_64 = RegisterContext< 57 | rax, rbx, rcx, rdx, rsp, rbp, rsi, rdi, 58 | r8, r9, r10, r11, r12, r13, r14, r15, 59 | rip, rflags, cs, fs, gs, ds, ss, 60 | x86::cr0, x86::cr3, x86::cr4, x86::msr_efer>; 61 | 62 | } 63 | 64 | #endif //XENDBG_REGISTERS_X86_64_HPP 65 | -------------------------------------------------------------------------------- /include/Util/IndentHelper.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_INDENT_HELPER_HPP 23 | #define XENDBG_INDENT_HELPER_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace xd::util { 30 | 31 | class IndentHelper { 32 | public: 33 | IndentHelper(size_t indent_size = 2, bool use_tabs = false) 34 | : _indent_size(indent_size), _indent_level(0), _use_tabs(use_tabs) {}; 35 | 36 | void indent(size_t i = 1) { _indent_level += i; }; 37 | void unindent(size_t i = 1) { _indent_level -= i; }; 38 | 39 | std::string make_indent() const { 40 | const char c = _use_tabs ? '\t' : ' '; 41 | if (_indent_level == 0) 42 | return ""; 43 | return std::string(_indent_level * _indent_size, c); 44 | }; 45 | 46 | private: 47 | size_t _indent_size, _indent_level; 48 | bool _use_tabs; 49 | }; 50 | 51 | } 52 | 53 | std::ostream &operator<<(std::ostream &out, const xd::util::IndentHelper &indent); 54 | 55 | #endif //XENDBG_INDENT_HELPER_HPP 56 | -------------------------------------------------------------------------------- /include/Util/choice.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_UTIL_CHOICE_HPP 23 | #define XENDBG_UTIL_CHOICE_HPP 24 | 25 | namespace xd::util { 26 | 27 | template 28 | struct choice; 29 | 30 | template 31 | struct choice { 32 | using type = IfTrue_t; 33 | }; 34 | 35 | template 36 | struct choice { 37 | using type = IfFalse_t; 38 | }; 39 | 40 | } 41 | 42 | #endif //XENDBG_UTIL_CHOICE_HPP 43 | -------------------------------------------------------------------------------- /include/Util/clear.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_UTIL_CLEAR_HPP 23 | #define XENDBG_UTIL_CLEAR_HPP 24 | 25 | namespace xd::util { 26 | 27 | template 28 | void clear(Container &c) { 29 | //Container().swap(c); 30 | while (!c.empty()) 31 | c.pop(); 32 | } 33 | 34 | } 35 | 36 | #endif //XENDBG_UTIL_CLEAR_HPP 37 | -------------------------------------------------------------------------------- /include/Util/overloaded.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_UTIL_OVERLOADED_HPP 23 | #define XENDBG_UTIL_OVERLOADED_HPP 24 | 25 | namespace xd::util { 26 | 27 | template 28 | struct overloaded : Ts ... { using Ts::operator()...; }; 29 | template overloaded(Ts...) -> overloaded; 30 | 31 | } 32 | 33 | #endif //XENDBG_OVERLOADED_HPP 34 | -------------------------------------------------------------------------------- /include/Util/pop_ret.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_UTIL_POP_RET_HPP 23 | #define XENDBG_UTIL_POP_RET_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace xd::util { 31 | 32 | template 33 | struct _pop_ret_impl { 34 | static V pop_ret(C& c) { 35 | if (c.empty()) 36 | throw std::runtime_error("Can't pop_ret an empty cueue!"); 37 | auto ret = std::move(c.front()); 38 | c.pop(); 39 | return ret; 40 | } 41 | }; 42 | 43 | template 44 | struct _pop_ret_impl, V> { 45 | static V pop_ret(std::stack& c) { 46 | if (c.empty()) 47 | throw std::runtime_error("Can't pop_ret an empty cueue!"); 48 | auto ret = std::move(c.top()); 49 | c.pop(); 50 | return ret; 51 | } 52 | }; 53 | 54 | template 55 | typename C::value_type pop_ret(C& c) { 56 | return _pop_ret_impl::pop_ret(c); 57 | } 58 | 59 | } 60 | 61 | #endif //XENDBG_POP_HPP 62 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/domctl.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_DOMCTL_H 24 | #define XENDBG_BRIDGE_DOMCTL_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_DOMCTL_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/hvm_save.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_HVM_SAVE_H 24 | #define XENDBG_BRIDGE_HVM_SAVE_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_HVM_SAVE_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/libxl.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_LIBXL_H 24 | #define XENDBG_BRIDGE_LIBXL_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_LIBXL_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/privcmd.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_PRIVCMD_H 24 | #define XENDBG_BRIDGE_PRIVCMD_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_PRIVCMD_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/ring.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_RING_H 24 | #define XENDBG_BRIDGE_RING_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_RING_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/vm_event.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_VMEVENT_H 24 | #define XENDBG_BRIDGE_VMEVENT_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_VMEVENT_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xencall.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_XENCALL_H 24 | #define XENDBG_BRIDGE_XENCALL_H 25 | 26 | extern "C" { 27 | #include 28 | } 29 | 30 | #endif //XENDBG_BRIDGE_XENCALL_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xenctrl.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_XENCTRL_H 24 | #define XENDBG_BRIDGE_XENCTRL_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_XENCTRL_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xendevicemodel.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_XENDEVICEMODEL_H 24 | #define XENDBG_BRIDGE_XENDEVICEMODEL_H 25 | 26 | #define __XEN_TOOLS__ 1 27 | extern "C" { 28 | #include 29 | } 30 | 31 | #endif //XENDBG_BRIDGE_XENDEVICEMODEL_H 32 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xenevtchn.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_XENEVTCHN_H 24 | #define XENDBG_BRIDGE_XENEVTCHN_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_XENEVTCHN_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xenforeignmemory.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_XENFOREIGNMEMORY_H 24 | #define XENDBG_BRIDGE_XENFOREIGNMEMORY_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_BRIDGE_XENFOREIGNMEMORY_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xenguest.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_XENGUEST_H 24 | #define XENDBG_XENGUEST_H 25 | 26 | extern "C" { 27 | #include 28 | }; 29 | 30 | #endif //XENDBG_XENGUEST_H 31 | -------------------------------------------------------------------------------- /include/Xen/BridgeHeaders/xenstore.h: -------------------------------------------------------------------------------- 1 | /* * Copyright (C) 2018-2019 NCC Group 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | * this software and associated documentation files (the "Software"), to deal in 5 | * the Software without restriction, including without limitation the rights to 6 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | * the Software, and to permit persons to whom the Software is furnished to do so, 8 | * subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | 22 | 23 | #ifndef XENDBG_BRIDGE_XENSTORE_H 24 | #define XENDBG_BRIDGE_XENSTORE_H 25 | 26 | extern "C" { 27 | #include 28 | } 29 | 30 | #endif //XENDBG_BRIDGE_XENSTORE_H 31 | -------------------------------------------------------------------------------- /include/Xen/Common.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_COMMON_HPP 23 | #define XENDBG_COMMON_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "BridgeHeaders/xenctrl.h" 30 | #include "BridgeHeaders/xenguest.h" 31 | 32 | namespace xd::xen { 33 | using Address = uintptr_t; 34 | using DomID = domid_t; 35 | using DomInfo = xc_dominfo_t; 36 | using MemInfo = std::unique_ptr>; 37 | using VCPU_ID = uint32_t; 38 | using WordSize = unsigned int; 39 | 40 | } 41 | 42 | #endif //XENDBG_COMMON_HPP 43 | -------------------------------------------------------------------------------- /include/Xen/DomainHVM.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_DOMAINHVM_HPP 23 | #define XENDBG_DOMAINHVM_HPP 24 | 25 | #include "Domain.hpp" 26 | #include "XenEventChannel.hpp" 27 | 28 | namespace xd::xen { 29 | 30 | class DomainHVM : public Domain { 31 | public: 32 | DomainHVM(DomID domid, std::shared_ptr xen); 33 | 34 | reg::RegistersX86Any get_cpu_context(VCPU_ID vcpu_id) const override; 35 | void set_cpu_context(reg::RegistersX86Any regs, VCPU_ID vcpu_id) const override; 36 | 37 | void set_singlestep(bool enabled, VCPU_ID vcpu_id) const override; 38 | 39 | XenEventChannel::RingPageAndPort enable_monitor() const; 40 | void disable_monitor() const; 41 | 42 | struct MonitorCapabilities { 43 | bool mov_to_msr, singlestep, software_breakpoint, descriptor_access, 44 | guest_request, debug_exception, cpuid_privileged_call; 45 | }; 46 | 47 | MonitorCapabilities monitor_get_capabilities(); 48 | void monitor_mov_to_msr(uint32_t msr, bool enable); 49 | void monitor_singlestep(bool enable); 50 | void monitor_software_breakpoint(bool enable); 51 | void monitor_debug_exceptions(bool enable, bool sync); 52 | void monitor_cpuid(bool enable); 53 | void monitor_descriptor_access(bool enable); 54 | void monitor_privileged_call(bool enable); 55 | void monitor_guest_request(bool enable, bool sync); 56 | 57 | private: 58 | struct hvm_hw_cpu get_cpu_context_raw(VCPU_ID vcpu_id) const; 59 | void set_cpu_context_raw(struct hvm_hw_cpu context, VCPU_ID vcpu_id) const; 60 | 61 | static reg::RegistersX86Any convert_regs_from_hvm(const struct hvm_hw_cpu &hvm); 62 | static struct hvm_hw_cpu convert_regs_to_hvm(const reg::x86_64::RegistersX86_64 ®s, hvm_hw_cpu hvm); 63 | }; 64 | 65 | } 66 | 67 | #endif //XENDBG_DOMAINHVM_HPP 68 | -------------------------------------------------------------------------------- /include/Xen/DomainPV.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_DOMAINPV_HPP 23 | #define XENDBG_DOMAINPV_HPP 24 | 25 | #include "Domain.hpp" 26 | 27 | namespace xd::xen { 28 | 29 | class DomainPV : public Domain { 30 | public: 31 | DomainPV(DomID domid, std::shared_ptr xen); 32 | 33 | reg::RegistersX86Any get_cpu_context(VCPU_ID vcpu_id) const override; 34 | void set_cpu_context(reg::RegistersX86Any regs, VCPU_ID vcpu_id) const override; 35 | 36 | void set_singlestep(bool enabled, VCPU_ID vcpu_id) const override; 37 | 38 | private: 39 | vcpu_guest_context_any_t get_cpu_context_raw(VCPU_ID vcpu_id) const; 40 | void set_cpu_context_raw(vcpu_guest_context_any_t context, VCPU_ID vcpu_id) const; 41 | 42 | static reg::x86_64::RegistersX86_64 convert_regs_from_pv64( 43 | const vcpu_guest_context_any_t &pv); 44 | static vcpu_guest_context_any_t convert_regs_to_pv64( 45 | const reg::x86_64::RegistersX86_64 ®s, vcpu_guest_context_any_t pv); 46 | static reg::x86_32::RegistersX86_32 convert_regs_from_pv32( 47 | const vcpu_guest_context_any_t &pv); 48 | static vcpu_guest_context_any_t convert_regs_to_pv32( 49 | const reg::x86_32::RegistersX86_32 ®s, vcpu_guest_context_any_t pv); 50 | }; 51 | 52 | } 53 | 54 | #endif //XENDBG_DOMAINPV_HPP 55 | -------------------------------------------------------------------------------- /include/Xen/HVMMonitor.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_HVMMONITOR_HPP 23 | #define XENDBG_HVMMONITOR_HPP 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | 31 | #include "Common.hpp" 32 | #include "DomainHVM.hpp" 33 | #include "BridgeHeaders/ring.h" 34 | #include "BridgeHeaders/vm_event.h" 35 | #include "XenDeviceModel.hpp" 36 | #include "XenEventChannel.hpp" 37 | 38 | namespace xd::xen { 39 | 40 | class HVMMonitor : public std::enable_shared_from_this { 41 | public: 42 | using OnEventFn = std::function; 43 | 44 | HVMMonitor(xen::XenDeviceModel &xendevicemodel, xen::XenEventChannel &xenevtchn, 45 | uvw::Loop &loop, DomainHVM &domain); 46 | ~HVMMonitor(); 47 | 48 | void start(); 49 | void stop(); 50 | 51 | void on_event(OnEventFn callback) { 52 | _on_event = std::move(callback); 53 | }; 54 | 55 | private: 56 | static void unmap_ring_page(void *ring_page); 57 | 58 | xen::XenDeviceModel &_xendevicemodel; 59 | xen::XenEventChannel &_xenevtchn; 60 | DomainHVM &_domain; 61 | 62 | xen::DomID _domid; 63 | XenEventChannel::Port _port; 64 | std::unique_ptr _ring_page; 65 | vm_event_back_ring_t _back_ring; 66 | std::shared_ptr _poll; 67 | 68 | OnEventFn _on_event; 69 | 70 | private: 71 | vm_event_request_t get_request(); 72 | void put_response(vm_event_response_t rsp); 73 | void read_events(); 74 | }; 75 | } 76 | 77 | #endif //XENDBG_HVMMONITOR_HPP 78 | -------------------------------------------------------------------------------- /include/Xen/PagePermissions.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_PAGEPERMISSIONS_HPP 23 | #define XENDBG_PAGEPERMISSIONS_HPP 24 | 25 | #include "BridgeHeaders/xenctrl.h" 26 | #include "PageTableEntry.hpp" 27 | 28 | namespace xd::xen { 29 | 30 | struct PagePermissions { 31 | PagePermissions(bool read, bool write, bool execute) 32 | : read(read), write(write), execute(execute) 33 | {}; 34 | 35 | PagePermissions(const PageTableEntry &pte) 36 | : read(true), 37 | write(pte.is_rw()), 38 | execute(!pte.is_nx()) 39 | {} 40 | 41 | bool read, write, execute; 42 | }; 43 | 44 | } 45 | 46 | #endif //XENDBG_PAGEPERMISSIONS_HPP 47 | -------------------------------------------------------------------------------- /include/Xen/PageTableEntry.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_PAGETABLEENTRY_HPP 23 | #define XENDBG_PAGETABLEENTRY_HPP 24 | 25 | #include 26 | 27 | #include "Common.hpp" 28 | 29 | namespace xd::xen { 30 | 31 | class Domain; 32 | 33 | class PageTableEntry { 34 | public: 35 | using RawPTE = uint64_t; 36 | 37 | enum class Level { 38 | L1, L2, L3, L4 39 | }; 40 | 41 | static PageTableEntry read_level(const Domain &domain, Address virtual_address, 42 | Address mfn, Level level); 43 | 44 | public: 45 | PageTableEntry(uint64_t pte) 46 | : _pte(pte) {}; 47 | 48 | operator bool() const { return is_present(); }; 49 | 50 | Address get_mfn() const; 51 | uint64_t get_raw() const { return _pte; } 52 | 53 | bool is_present() const; 54 | bool is_rw() const; 55 | bool is_user() const; 56 | bool is_pwt() const; 57 | bool is_pcd() const; 58 | bool is_accessed() const; 59 | bool is_dirty() const; 60 | bool is_pat() const; 61 | bool is_pse() const; 62 | bool is_global() const; 63 | bool is_nx() const; 64 | bool is_grant_table() const; 65 | bool is_guest_kernel() const; 66 | 67 | private: 68 | uint64_t _pte; 69 | 70 | uint64_t get_flags() const; 71 | 72 | static unsigned get_pte_offset(Address address, Level level); 73 | }; 74 | 75 | } 76 | 77 | #endif //XENDBG_PAGETABLEENTRY_HPP 78 | -------------------------------------------------------------------------------- /include/Xen/Xen.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XEN_HPP 23 | #define XENDBG_XEN_HPP 24 | 25 | #include 26 | #include 27 | 28 | #include "DomainHVM.hpp" 29 | #include "DomainPV.hpp" 30 | #include "XenCall.hpp" 31 | #include "XenCtrl.hpp" 32 | #include "XenDeviceModel.hpp" 33 | #include "XenEventChannel.hpp" 34 | #include "XenForeignMemory.hpp" 35 | #include "XenStore.hpp" 36 | 37 | namespace xd::xen { 38 | 39 | using DomainAny = std::variant; 40 | 41 | class Xen : public std::enable_shared_from_this { 42 | private: 43 | struct ConstructorAccess {}; 44 | 45 | public: 46 | Xen() = default; 47 | explicit Xen(ConstructorAccess ca) : Xen() {}; 48 | 49 | static std::shared_ptr create() { 50 | return std::make_shared(ConstructorAccess{}); 51 | } 52 | 53 | DomainAny init_domain(DomID domid); 54 | std::vector get_domains(); 55 | 56 | XenCtrl xenctrl; 57 | XenDeviceModel xendevicemodel; 58 | XenEventChannel xenevtchn; 59 | XenForeignMemory xenforeignmemory; 60 | XenStore xenstore; 61 | 62 | static xen::DomID get_domid_any(const xen::DomainAny &domain_any); 63 | static std::string get_name_any(const xen::DomainAny &domain_any); 64 | 65 | std::optional get_domain_from_name(const std::string &name); 66 | std::optional get_domain_from_domid(DomID domid); 67 | }; 68 | 69 | } 70 | 71 | #endif //XENDBG_XEN_HPP 72 | -------------------------------------------------------------------------------- /include/Xen/XenCall.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XENCALL_HPP 23 | #define XENDBG_XENCALL_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | #include "Common.hpp" 37 | #include "XenException.hpp" 38 | 39 | #include "BridgeHeaders/domctl.h" 40 | #include "BridgeHeaders/xencall.h" 41 | 42 | namespace xd::xen { 43 | 44 | class Domain; 45 | 46 | class XenCall { 47 | private: 48 | static xen_domctl _dummy_domctl; 49 | 50 | public: 51 | using DomctlUnion = decltype(XenCall::_dummy_domctl.u); 52 | using InitFn = std::function; 53 | using CleanupFn = std::function; 54 | 55 | explicit XenCall(std::shared_ptr xenctrl); 56 | 57 | DomctlUnion do_domctl(const Domain &domain, uint32_t command, InitFn init = {}, CleanupFn cleanup = {}) const; 58 | 59 | private: 60 | std::shared_ptr _xenctrl; 61 | std::unique_ptr _xencall; 62 | }; 63 | 64 | } 65 | 66 | #endif //XENDBG_XENCALL_HPP 67 | -------------------------------------------------------------------------------- /include/Xen/XenCtrl.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XENCTRL_HPP 23 | #define XENDBG_XENCTRL_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #include "BridgeHeaders/xenctrl.h" 33 | #include "BridgeHeaders/xenguest.h" 34 | #include "Common.hpp" 35 | #include "XenCall.hpp" 36 | #include "XenEventChannel.hpp" 37 | 38 | namespace xd::xen { 39 | 40 | class Domain; 41 | 42 | class XenCtrl { 43 | private: 44 | struct XenVersion { 45 | int major, minor; 46 | }; 47 | static xen_domctl _dummy_domctl; 48 | 49 | public: 50 | using DomctlUnion = decltype(XenCtrl::_dummy_domctl.u); 51 | using InitFn = std::function; 52 | using CleanupFn = std::function; 53 | 54 | XenCtrl(); 55 | 56 | xc_interface *get() const { return _xenctrl.get(); }; 57 | XenVersion get_xen_version() const; 58 | 59 | DomInfo get_domain_info(DomID domid) const; 60 | 61 | private: 62 | std::shared_ptr _xenctrl; 63 | 64 | public: 65 | XenCall xencall; 66 | }; 67 | 68 | } 69 | 70 | #endif //XENDBG_XENCTRL_HPP 71 | -------------------------------------------------------------------------------- /include/Xen/XenDeviceModel.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XENDEVICEMODEL_HPP 23 | #define XENDBG_XENDEVICEMODEL_HPP 24 | 25 | #include "BridgeHeaders/xendevicemodel.h" 26 | #include "Common.hpp" 27 | 28 | namespace xd::xen { 29 | 30 | class Domain; 31 | 32 | class XenDeviceModel { 33 | public: 34 | XenDeviceModel(); 35 | 36 | xendevicemodel_handle *get() { return _xendevicemodel.get(); }; 37 | 38 | void inject_event(const Domain &domain, VCPU_ID vcpu_id, uint8_t vector, 39 | uint8_t type, uint32_t error_code, uint8_t insn_len, uint64_t cr2); 40 | 41 | private: 42 | std::unique_ptr _xendevicemodel; 44 | 45 | }; 46 | 47 | } 48 | 49 | #endif //XENDBG_XENDEVICEMODEL_HPP 50 | -------------------------------------------------------------------------------- /include/Xen/XenEventChannel.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XENEVENTCHANNEL_HPP 23 | #define XENDBG_XENEVENTCHANNEL_HPP 24 | 25 | #include 26 | 27 | #include "BridgeHeaders/xenevtchn.h" 28 | 29 | namespace xd::xen { 30 | 31 | class Domain; 32 | 33 | class XenEventChannel { 34 | public: 35 | using Port = uint32_t; 36 | struct RingPageAndPort { 37 | void *ring_page; 38 | Port port; 39 | }; 40 | 41 | XenEventChannel(); 42 | 43 | xenevtchn_handle *get() const { return _xenevtchn.get(); }; 44 | int get_fd(); 45 | 46 | Port get_next_pending_channel(); 47 | Port unmask_channel(Port port); 48 | 49 | Port bind_interdomain(const Domain &domain, Port remote_port); 50 | void unbind(Port port); 51 | 52 | void notify(Port port); 53 | 54 | private: 55 | std::unique_ptr _xenevtchn; 56 | 57 | }; 58 | } 59 | 60 | #endif //XENDBG_XENEVENTCHANNEL_HPP 61 | -------------------------------------------------------------------------------- /include/Xen/XenException.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XENEXCEPTION_HPP 23 | #define XENDBG_XENEXCEPTION_HPP 24 | 25 | #include 26 | #include 27 | 28 | namespace xd::xen { 29 | 30 | class XenException : public std::runtime_error { 31 | public: 32 | explicit XenException(const std::string& what) 33 | : std::runtime_error(what.c_str()), _err(0) {}; 34 | 35 | explicit XenException(const char* what) 36 | : std::runtime_error(what), _err(0) {}; 37 | 38 | explicit XenException(const std::string& what, int err) 39 | : std::runtime_error(what.c_str()), _err(err) {}; 40 | 41 | explicit XenException(const char* what, int err) 42 | : std::runtime_error(what), _err(err) {}; 43 | 44 | int get_err() const { return _err; }; 45 | 46 | private: 47 | int _err; 48 | }; 49 | 50 | } 51 | 52 | #endif //XENDBG_XENEXCEPTION_HPP 53 | -------------------------------------------------------------------------------- /include/Xen/XenForeignMemory.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_FOREIGNMEMORY_HPP 23 | #define XENDBG_FOREIGNMEMORY_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | // NOTE: This order is necessary. For some reason, including 30 | // xenforeignmemory.h before xenctrl.h will fail. 31 | #include "BridgeHeaders/xenctrl.h" 32 | #include "BridgeHeaders/xenforeignmemory.h" 33 | 34 | #include "Common.hpp" 35 | #include "XenException.hpp" 36 | 37 | struct xenforeignmemory_handle; 38 | 39 | namespace xd::xen { 40 | 41 | class Domain; 42 | 43 | class XenForeignMemory { 44 | public: 45 | template 46 | using MappedMemory = std::shared_ptr; 47 | 48 | XenForeignMemory(); 49 | 50 | xenforeignmemory_handle *get() { return _xen_foreign_memory.get(); }; 51 | 52 | template 53 | MappedMemory map_by_mfn(const Domain_t &domain, Address base_mfn, Address offset, size_t size, int prot) const { 54 | auto fmem = _xen_foreign_memory; 55 | auto mem = map_by_mfn_raw(domain, base_mfn, offset, size, prot); 56 | auto num_pages = size / XC_PAGE_SIZE; 57 | 58 | return std::shared_ptr((Memory_t*)mem, [fmem, mem, num_pages](void *memory) { 59 | if (memory) 60 | xenforeignmemory_unmap(fmem.get(), mem, num_pages); 61 | }); 62 | } 63 | 64 | private: 65 | std::shared_ptr _xen_foreign_memory; 66 | 67 | void *map_by_mfn_raw(const Domain &domain, Address base_mfn, Address offset, size_t size, int prot) const; 68 | }; 69 | 70 | } 71 | 72 | #endif //XENDBG_FOREIGNMEMORY_HPP 73 | -------------------------------------------------------------------------------- /include/Xen/XenStore.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_XENSTORE_HPP 23 | #define XENDBG_XENSTORE_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include "BridgeHeaders/xenstore.h" 34 | #include "Common.hpp" 35 | 36 | namespace xd::xen { 37 | 38 | class XenStore { 39 | public: 40 | class Watch; 41 | using Path = std::string; 42 | using Token = std::string; 43 | 44 | XenStore(); 45 | 46 | struct xs_handle *get() { return _xenstore.get(); }; 47 | int get_fileno() const; 48 | 49 | Watch &add_watch(); 50 | 51 | std::string read(const std::string& file) const; 52 | std::vector read_directory(const std::string& dir) const; 53 | 54 | public: 55 | class Watch { 56 | public: 57 | Watch(XenStore &xenstore, std::string token); 58 | ~Watch(); 59 | 60 | Watch(Watch&& other) = default; 61 | Watch(const Watch& other) = delete; 62 | Watch& operator=(Watch&& other) = default; 63 | Watch& operator=(const Watch& other) = delete; 64 | 65 | void add_path(Path path); 66 | std::optional check(); 67 | 68 | private: 69 | friend class XenStore; 70 | 71 | XenStore &_xenstore; 72 | Token _token; 73 | std::vector _paths; 74 | 75 | std::queue _events; 76 | }; 77 | 78 | private: 79 | std::unique_ptr _xenstore; 80 | std::unordered_map _watches; 81 | size_t _next_watch_id; 82 | 83 | void check_watches(); 84 | 85 | }; 86 | } 87 | 88 | #endif //XENDBG_XENSTORE_HPP 89 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | echo "# Installing dependencies" 5 | sudo apt-get install -y git cmake build-essential \ 6 | libcapstone-dev libspdlog-dev libxen-dev libreadline-dev \ 7 | libc++abi-dev libc++1 libc++-dev clang \ 8 | autoconf libboost-test-dev \ 9 | libuv1-dev 10 | 11 | ./build.sh 12 | sudo make install 13 | -------------------------------------------------------------------------------- /src/CommandLine.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_COMMANDLINE_HPP 23 | #define XENDBG_COMMANDLINE_HPP 24 | 25 | #include 26 | 27 | #include "../include/Xen/Common.hpp" 28 | 29 | namespace xd { 30 | 31 | namespace uv { 32 | class UVLoop; 33 | } 34 | 35 | class CommandLine { 36 | public: 37 | CommandLine(); 38 | 39 | int parse(int argc, char **argv); 40 | 41 | private: 42 | CLI::App _app; 43 | 44 | private: 45 | uint16_t _port; 46 | std::string _ip, _domain; 47 | }; 48 | 49 | } 50 | 51 | #endif //XENDBG_COMMANDLINE_HPP 52 | -------------------------------------------------------------------------------- /src/Constants.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_CONSTANTS_HPP 23 | #define XENDBG_CONSTANTS_HPP 24 | 25 | #define APP_NAME "xendbg" 26 | #define APP_VERSION "1.0.0" 27 | #define APP_NAME_AND_VERSION (APP_NAME " v" APP_VERSION) 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/DebugSession.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include "DebugSession.hpp" 23 | 24 | using xd::DebugSession; 25 | 26 | DebugSession::DebugSession(uvw::Loop &loop, std::shared_ptr debugger) 27 | : _debugger(std::move(debugger)), 28 | _gdb_server(std::make_shared(loop)) 29 | { 30 | }; 31 | 32 | DebugSession::~DebugSession() { 33 | stop(); 34 | } 35 | 36 | void DebugSession::stop() { 37 | if (_gdb_connection) 38 | _gdb_connection->stop(); 39 | if (_gdb_server) 40 | _gdb_server->stop(); 41 | } 42 | 43 | void DebugSession::run(const std::string& address_str, uint16_t port, OnErrorFn on_error) { 44 | _gdb_server->listen(address_str, port, 45 | [this, on_error](auto &server, auto connection) { 46 | _gdb_connection = connection; 47 | _request_handler.emplace(*_debugger, *_gdb_connection); 48 | 49 | _debugger->on_stop([this, connection](auto reason) { 50 | _request_handler->send_stop_reply(reason); 51 | }); 52 | _debugger->attach(); 53 | 54 | _gdb_connection->read([this](auto &connection, const auto &packet) { 55 | try { 56 | std::visit(*_request_handler, packet); 57 | } catch (const xen::XenException &e) { 58 | spdlog::get(LOGNAME_CONSOLE)->error("Error {0:d} ({1:s}): {2:s}", e.get_err(), std::strerror(e.get_err()), e.what()); 59 | connection.send_error(e.get_err(), e.what()); 60 | } catch (const dbg::FeatureNotSupportedException &e) { 61 | spdlog::get(LOGNAME_CONSOLE)->warn("Unsupported feature: {0:s}", e.what()); 62 | connection.send(gdb::rsp::NotSupportedResponse()); 63 | } 64 | }, [this]() { 65 | _debugger->detach(); 66 | _request_handler.reset(); 67 | }, on_error); 68 | }, on_error); 69 | } 70 | -------------------------------------------------------------------------------- /src/DebugSession.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_DEBUGSESSION_HPP 23 | #define XENDBG_DEBUGSESSION_HPP 24 | 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include "GDBServer/GDBRequestHandler.hpp" 34 | #include "GDBServer/GDBServer.hpp" 35 | 36 | namespace xd { 37 | 38 | class DebugSession : public std::enable_shared_from_this { 39 | public: 40 | using OnErrorFn = std::function; 41 | 42 | DebugSession(uvw::Loop &loop, std::shared_ptr debugger); 43 | ~DebugSession(); 44 | 45 | void stop(); 46 | void run(const std::string& address_str, uint16_t port, OnErrorFn on_error); 47 | 48 | private: 49 | std::shared_ptr _debugger; 50 | std::shared_ptr _gdb_server; 51 | std::shared_ptr _gdb_connection; 52 | std::optional _request_handler; 53 | }; 54 | 55 | } 56 | 57 | #endif //XENDBG_DEBUGSESSION_HPP 58 | -------------------------------------------------------------------------------- /src/GDBServer/GDBPacket.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | using xd::gdb::GDBPacket; 29 | 30 | GDBPacket::GDBPacket(std::string contents) 31 | : _contents(std::move(contents)), _checksum(calculate_checksum()) 32 | { 33 | } 34 | 35 | GDBPacket::GDBPacket(std::string contents, uint8_t checksum) 36 | : _contents(std::move(contents)), _checksum(checksum) 37 | { 38 | } 39 | 40 | std::string GDBPacket::to_string() const { 41 | std::stringstream ss; 42 | ss << "$" << _contents << "#"; 43 | ss << std::hex << std::setfill('0') << std::setw(2); 44 | ss << (unsigned)_checksum; 45 | return ss.str(); 46 | } 47 | 48 | bool GDBPacket::is_checksum_valid() const { 49 | return _checksum == calculate_checksum(); 50 | } 51 | 52 | uint8_t GDBPacket::calculate_checksum() const { 53 | return std::accumulate(_contents.begin(), _contents.end(), (uint8_t)0); 54 | } 55 | 56 | bool GDBPacket::starts_with(const std::string &s) const { 57 | return s.size() <= _contents.size() && 58 | std::equal(s.begin(), s.end(), _contents.begin(), _contents.begin() + s.size()); 59 | } 60 | -------------------------------------------------------------------------------- /src/GDBServer/GDBPacketQueue.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #define CHECKSUM_LENGTH 2 28 | 29 | using xd::gdb::GDBPacket; 30 | using xd::gdb::GDBPacketQueue; 31 | 32 | void GDBPacketQueue::append(const std::vector &data) { 33 | _buffer.insert(_buffer.end(), data.begin(), data.end()); 34 | 35 | /* 36 | * For some ungodly reason, GDB sends interrupt requests as a raw 0x03 byte, 37 | * not encapsulated in a packet. As such, we have to check the intermediate 38 | * space between packets for 0x03s and interpret them as interrupts. 39 | */ 40 | const auto find_checking_interrupts = [this](auto it, auto end, char target) { 41 | while (it != end && *it != target) 42 | if (*it++ == '\x03') { 43 | _packets.emplace(GDBPacket{"\x03", 0x03}); 44 | } 45 | return it; 46 | }; 47 | 48 | auto end = _buffer.begin(); 49 | while (end != _buffer.end()) { 50 | auto packet_start = find_checking_interrupts(end, _buffer.end(), '$'); 51 | auto checksum_start = std::find(packet_start, _buffer.end(), '#'); 52 | 53 | if (packet_start == _buffer.end() || 54 | checksum_start == _buffer.end() || 55 | _buffer.end() - checksum_start < (CHECKSUM_LENGTH + 1)) 56 | break; 57 | 58 | end = checksum_start + CHECKSUM_LENGTH + 1; 59 | _packets.emplace(GDBPacket{ 60 | std::string(packet_start+1, checksum_start), 61 | static_cast(std::stoul(std::string(checksum_start+1, end), 0, 16)) 62 | }); 63 | } 64 | 65 | end = find_checking_interrupts(end, _buffer.end(), '$'); 66 | _buffer.erase(_buffer.begin(), end); 67 | } 68 | 69 | GDBPacket GDBPacketQueue::pop() { 70 | if (_packets.empty()) 71 | throw NoPacketException(); 72 | 73 | return util::pop_ret(_packets); 74 | } 75 | -------------------------------------------------------------------------------- /src/GDBServer/GDBRequest/GDBMemoryRequest.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | using namespace xd::gdb::req; 25 | 26 | MemoryReadRequest::MemoryReadRequest(const std::string &data) 27 | : GDBRequestBase(data, 'm') 28 | { 29 | _address = read_hex_number(); 30 | expect_char(','); 31 | _length = read_hex_number(); 32 | expect_end(); 33 | }; 34 | 35 | MemoryWriteRequest::MemoryWriteRequest(const std::string &data) 36 | : GDBRequestBase(data, 'M') 37 | { 38 | _address = read_hex_number(); 39 | expect_char(','); 40 | _length = read_hex_number(); 41 | expect_char(':'); 42 | 43 | _data.reserve(_length); 44 | for (size_t i = 0; i < _length; ++i) 45 | _data.push_back(read_byte()); 46 | 47 | expect_end(); 48 | }; 49 | -------------------------------------------------------------------------------- /src/GDBServer/GDBRequest/GDBQueryRequest.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | using namespace xd::gdb::req; 25 | 26 | QueryWatchpointSupportInfo::QueryWatchpointSupportInfo(const std::string &data) 27 | : GDBRequestBase(data, "qWatchpointSupportInfo") 28 | { 29 | check_char(':'); 30 | expect_end(); 31 | }; 32 | 33 | QuerySupportedRequest::QuerySupportedRequest(const std::string &data) 34 | : GDBRequestBase(data, "qSupported") 35 | { 36 | expect_char(':'); 37 | while (has_more()) { 38 | const auto feature = read_until_char_or_end(';'); 39 | _features.push_back(feature); 40 | } 41 | expect_end(); 42 | }; 43 | 44 | QueryRegisterInfoRequest::QueryRegisterInfoRequest(const std::string &data) 45 | : GDBRequestBase(data, "qRegisterInfo") 46 | { 47 | _register_id = read_hex_number(); 48 | expect_end(); 49 | }; 50 | 51 | QueryMemoryRegionInfoRequest::QueryMemoryRegionInfoRequest(const std::string &data) 52 | : GDBRequestBase(data, "qMemoryRegionInfo") 53 | { 54 | expect_char(':'); 55 | _address = read_hex_number(); 56 | expect_end(); 57 | }; 58 | -------------------------------------------------------------------------------- /src/GDBServer/GDBResponse/GDBMemoryResponse.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | using namespace xd::gdb::rsp; 25 | 26 | std::string MemoryReadResponse::to_string() const { 27 | std::stringstream ss; 28 | 29 | ss << std::hex << std::setfill('0'); 30 | std::for_each(_data.begin(), _data.end(), 31 | [&ss](const unsigned char &ch) { 32 | ss << std::setw(2) << (unsigned)ch; 33 | }); 34 | 35 | return ss.str(); 36 | }; 37 | -------------------------------------------------------------------------------- /src/GDBServer/GDBResponse/GDBRegisterResponse.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | using namespace xd::gdb::rsp; 25 | 26 | std::string RegisterReadResponse::to_string() const { 27 | std::stringstream ss; 28 | write_bytes(ss, _value); 29 | return ss.str(); 30 | }; 31 | 32 | std::string GeneralRegistersBatchReadResponse::to_string() const { 33 | std::stringstream ss; 34 | 35 | ss << std::hex << std::setfill('0'); 36 | std::visit(util::overloaded { 37 | [&ss](const xd::reg::x86_64::RegistersX86_64& regs) { 38 | regs.for_each([&ss](const auto&, const auto ®) { 39 | write_register(ss, reg); 40 | }); 41 | }, 42 | [&ss](const xd::reg::x86_32::RegistersX86_32& regs) { 43 | regs.for_each([&ss](const auto&, const auto ®) { 44 | write_register(ss, reg); 45 | }); 46 | }, 47 | }, _registers); 48 | 49 | return ss.str(); 50 | } 51 | -------------------------------------------------------------------------------- /src/GDBServer/GDBResponse/GDBResponse.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | using namespace xd::gdb::rsp; 25 | 26 | std::string ErrorResponse::to_string() const { 27 | std::stringstream ss; 28 | ss << "E"; 29 | ss << std::hex << std::setfill('0') << std::setw(2) << (unsigned)_error_code; 30 | if (!_message.empty()) 31 | ss << ";" << _message; 32 | return ss.str(); 33 | } 34 | 35 | std::string StopReasonSignalResponse::to_string() const { 36 | std::stringstream ss; 37 | ss << "T"; 38 | ss << std::hex << std::setfill('0') << std::setw(2); 39 | ss << (unsigned)_signal; 40 | 41 | if (!_stop_reason_key.empty()) 42 | ss << _stop_reason_key << ":" << _stop_reason_value << ";"; 43 | 44 | ss << "thread:"; 45 | ss << _thread_id; 46 | ss << ";threads:"; 47 | if (_thread_ids.size() == 1) 48 | ss << _thread_ids.front(); 49 | else 50 | for (const auto thread_id : _thread_ids) 51 | add_list_entry(ss, thread_id); 52 | ss << ";reason:signal;"; 53 | return ss.str(); 54 | }; 55 | -------------------------------------------------------------------------------- /src/GDBServer/GDBServer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | #include 24 | 25 | using xd::gdb::GDBServer; 26 | 27 | GDBServer::GDBServer(uvw::Loop &loop) 28 | : _server(loop.resource()) 29 | { 30 | } 31 | 32 | GDBServer::~GDBServer() { 33 | stop(); 34 | } 35 | 36 | void GDBServer::stop() { 37 | if (!_server->closing()) 38 | _server->close(); 39 | } 40 | 41 | void GDBServer::listen(const std::string &address, uint16_t port, OnAcceptFn on_accept, OnErrorFn on_error) { 42 | _on_accept = std::move(on_accept); 43 | _on_error = std::move(on_error); 44 | 45 | _server->data(shared_from_this()); 46 | 47 | _server->once([](const auto &event, auto &tcp) { 48 | auto self = tcp.template data(); 49 | self->_on_error(event); 50 | }); 51 | 52 | // Only accept one connection; LLDB doesn't handle multiple clients attached to she same stub 53 | _server->once([](const auto &event, auto &tcp) { 54 | auto self = tcp.template data(); 55 | auto client = tcp.loop().template resource(); 56 | 57 | client->template on( 58 | [ptr = tcp.shared_from_this()](const auto&, auto&) { ptr->close(); }); 59 | client->template on( 60 | [](const auto&, auto &client) { client.close(); }); 61 | 62 | tcp.accept(*client); 63 | 64 | self->_on_accept(*self, std::make_shared(client)); 65 | }); 66 | 67 | _server->bind(address, port); 68 | _server->listen(); 69 | } 70 | -------------------------------------------------------------------------------- /src/REPL/Command/Action.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_ACTION_HPP 23 | #define XENDBG_ACTION_HPP 24 | 25 | #include 26 | 27 | namespace xd::repl { 28 | 29 | class REPL; 30 | 31 | namespace cmd { 32 | 33 | using Action = std::function; 34 | 35 | } 36 | 37 | } 38 | 39 | #endif //XENDBG_ACTION_HPP 40 | -------------------------------------------------------------------------------- /src/REPL/Command/ArgsHandle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include "ArgsHandle.hpp" 23 | -------------------------------------------------------------------------------- /src/REPL/Command/Argument.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | #include "Argument.hpp" 25 | #include 26 | #include 27 | 28 | using xd::repl::cmd::Argument; 29 | using xd::util::IndentHelper; 30 | 31 | void Argument::print(std::ostream& out, IndentHelper& /*indent*/) const { 32 | /* 33 | const auto print_completion_options = [this](std::ostream &out) { 34 | if (_completer) { 35 | const std::string dummy_input = ""; 36 | const auto options_opt = _completer(dummy_input.end(), dummy_input.end()); 37 | 38 | if (!options_opt || options_opt.value().empty()) 39 | return; 40 | const auto options = options_opt.value(); 41 | 42 | std::ostringstream ss; 43 | for (const auto &opt : options) { 44 | ss << opt << ","; 45 | } 46 | 47 | out 48 | << "={" 49 | << ss.str() 50 | << "}"; 51 | } 52 | }; 53 | */ 54 | 55 | const auto print_default_value = [this](std::ostream &out) { 56 | if (!_default_value.empty()) { 57 | out 58 | << "=" 59 | << _default_value; 60 | } 61 | }; 62 | 63 | if (_is_optional) { 64 | out 65 | << "[" 66 | << _name; 67 | //print_completion_options(out); 68 | print_default_value(out); 69 | out << "]"; 70 | } 71 | if (_completer) { 72 | out 73 | << "<" 74 | << _name; 75 | //print_completion_options(out); 76 | out << ">"; 77 | } else { 78 | out 79 | << "<" 80 | << _name 81 | << ">"; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/REPL/Command/Command.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include 23 | 24 | #include "Command.hpp" 25 | #include 26 | #include 27 | 28 | using xd::util::IndentHelper; 29 | using xd::util::string::expect; 30 | using xd::util::string::skip_whitespace; 31 | using xd::repl::cmd::Action; 32 | using xd::repl::cmd::Command; 33 | 34 | void Command::print(std::ostream& out, IndentHelper& indent) const { 35 | out << indent 36 | << get_name() 37 | << ": " 38 | << get_description() 39 | << std::endl; 40 | 41 | indent.indent(); 42 | for (const auto& verb : _verbs) { 43 | verb.print(out, indent); 44 | } 45 | indent.unindent(); 46 | } 47 | 48 | std::optional Command::match(std::string::const_iterator begin, std::string::const_iterator end) const { 49 | const auto name_end = expect(get_name(), begin, end); 50 | 51 | if (name_end == begin) 52 | return std::nullopt; 53 | 54 | for (const auto& verb : _verbs) { 55 | auto action = verb.match(name_end, end); 56 | if (action) 57 | return action; 58 | } 59 | 60 | return std::nullopt; 61 | } 62 | 63 | std::optional> Command::complete(std::string::const_iterator begin, std::string::const_iterator end) const { 64 | const auto name_end = expect(get_name(), begin, end); 65 | 66 | // If s doesn't have this command as a prefix, neither this command 67 | // nor its children have any relevant completion options to give 68 | if (name_end == begin) 69 | return std::nullopt; 70 | 71 | // If a verb has more specific completion options, return those instead 72 | for (const auto& verb : _verbs) { 73 | auto options = verb.complete(name_end, end); 74 | 75 | if (options) 76 | return options; 77 | } 78 | 79 | // Otherwise return the list of verbs 80 | std::vector options; 81 | options.reserve(_verbs.size()); 82 | std::transform(_verbs.begin(), _verbs.end(), std::back_inserter(options), 83 | [](auto& verb) { 84 | return verb.get_name(); 85 | }); 86 | 87 | return options; 88 | } 89 | -------------------------------------------------------------------------------- /src/REPL/Command/Command.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_COMMAND_HPP 23 | #define XENDBG_COMMAND_HPP 24 | 25 | #include "CommandBase.hpp" 26 | #include "Verb.hpp" 27 | 28 | namespace xd::util { 29 | 30 | class IndentHelper; 31 | 32 | } 33 | 34 | namespace xd::repl::cmd { 35 | 36 | class Command : public CommandBase { 37 | public: 38 | Command(std::string name, std::string description, std::vector verbs) 39 | : CommandBase(std::move(name), std::move(description)), 40 | _verbs(std::move(verbs)) {}; 41 | 42 | void print(std::ostream& out, xd::util::IndentHelper& indent) const override; 43 | 44 | std::optional match(std::string::const_iterator begin, std::string::const_iterator end) const override; 45 | std::optional> complete(std::string::const_iterator begin, std::string::const_iterator end) const override; 46 | 47 | void add_verb(const Verb& verb) { _verbs.push_back(verb); } 48 | 49 | private: 50 | std::string::const_iterator match_prefix_skipping_whitespace( 51 | std::string::const_iterator begin, std::string::const_iterator end) const; 52 | 53 | std::vector _verbs; 54 | }; 55 | 56 | } 57 | 58 | #endif //XENDBG_COMMAND_HPP 59 | -------------------------------------------------------------------------------- /src/REPL/Command/CommandBase.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_COMMANDBASE_HPP 23 | #define XENDBG_COMMANDBASE_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "Action.hpp" 30 | 31 | namespace xd::util { 32 | 33 | class IndentHelper; 34 | 35 | } 36 | 37 | namespace xd::repl::cmd { 38 | 39 | class CommandBase { 40 | public: 41 | CommandBase(std::string name, std::string description) 42 | : _name(std::move(name)), _description(std::move(description)) {}; 43 | virtual ~CommandBase() {}; 44 | 45 | std::string get_name() const { return _name; }; 46 | std::string get_description() const { return _description; }; 47 | 48 | virtual void print(std::ostream& out, xd::util::IndentHelper& indent) const = 0; 49 | 50 | virtual std::optional match(std::string::const_iterator begin, std::string::const_iterator end) const = 0; 51 | 52 | virtual std::optional> complete(std::string::const_iterator begin, std::string::const_iterator end) const = 0; 53 | 54 | private: 55 | const std::string _name; 56 | const std::string _description; 57 | }; 58 | 59 | } 60 | 61 | #endif //XENDBG_COMMANDBASE_HPP 62 | -------------------------------------------------------------------------------- /src/REPL/Command/CommandVerb.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include "CommandVerb.hpp" 23 | #include 24 | 25 | using xd::repl::cmd::Action; 26 | using xd::repl::cmd::CommandVerb; 27 | using xd::util::IndentHelper; 28 | 29 | void CommandVerb::print(std::ostream& out, IndentHelper& indent) const { 30 | _verb.print(out, indent); 31 | } 32 | 33 | std::optional CommandVerb::match(std::string::const_iterator begin, std::string::const_iterator end) const { 34 | return _verb.match(begin, end); 35 | } 36 | 37 | std::optional> CommandVerb::complete(std::string::const_iterator begin, std::string::const_iterator end) const 38 | { 39 | return _verb.complete(begin, end); 40 | } 41 | -------------------------------------------------------------------------------- /src/REPL/Command/CommandVerb.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_COMMANDVERB_HPP 23 | #define XENDBG_COMMANDVERB_HPP 24 | 25 | #include "CommandBase.hpp" 26 | #include "Verb.hpp" 27 | 28 | namespace xd::util { 29 | 30 | class IndentHelper; 31 | 32 | } 33 | 34 | namespace xd::repl::cmd { 35 | 36 | class CommandVerb : public CommandBase { 37 | public: 38 | explicit CommandVerb(Verb verb) 39 | : CommandBase(verb.get_name(), verb.get_description()), _verb(std::move(verb)) {}; 40 | 41 | void print(std::ostream& out, xd::util::IndentHelper& indent) const override; 42 | 43 | std::optional match(std::string::const_iterator begin, std::string::const_iterator end) const override; 44 | std::optional> complete(std::string::const_iterator begin, std::string::const_iterator end) const override; 45 | 46 | private: 47 | Verb _verb; 48 | }; 49 | 50 | } 51 | 52 | #endif //XENDBG_COMMANDVERB_HPP 53 | -------------------------------------------------------------------------------- /src/REPL/Command/FlagsHandle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include "FlagsHandle.hpp" 23 | -------------------------------------------------------------------------------- /src/REPL/Command/FlagsHandle.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_FLAGSHANDLE_HPP 23 | #define XENDBG_FLAGSHANDLE_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ArgsHandle.hpp" 31 | #include "Flag.hpp" 32 | 33 | namespace xd::repl::cmd { 34 | 35 | class FlagsHandle { 36 | private: 37 | using FlagHandle = std::optional; 38 | using FlagsList = std::vector, ArgsHandle>>; 39 | 40 | private: 41 | template 42 | FlagHandle get_predicate(F pred) const { 43 | auto found = std::find_if(_flags.begin(), _flags.end(), pred); 44 | 45 | if (found == _flags.end()) 46 | return std::nullopt; 47 | 48 | return found->second; 49 | } 50 | 51 | public: 52 | void put(const Flag& flag, ArgsHandle args) { 53 | auto flag_names = std::make_pair(flag.get_short_name(), flag.get_long_name()); 54 | _flags.push_back(std::make_pair(flag_names, args)); 55 | } 56 | 57 | bool has(char short_name) const { 58 | return get(short_name).has_value(); 59 | } 60 | bool has(const std::string& long_name) const { 61 | return get(long_name).has_value(); 62 | } 63 | 64 | FlagHandle get(char short_name) const { 65 | return get_predicate([short_name](const auto& f) { 66 | return f.first.first == short_name; 67 | }); 68 | } 69 | 70 | FlagHandle get(std::string long_name) const { 71 | return get_predicate([long_name](const auto& f) { 72 | return f.first.second == long_name; 73 | }); 74 | } 75 | 76 | private: 77 | FlagsList _flags; 78 | }; 79 | } 80 | 81 | #endif //XENDBG_FLAGSHANDLE_HPP 82 | -------------------------------------------------------------------------------- /src/REPL/Command/MakeCommand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #include "MakeCommand.hpp" 23 | 24 | using xd::repl::cmd::Command; 25 | using xd::repl::cmd::CommandBase; 26 | using xd::repl::cmd::CommandVerb; 27 | using xd::repl::cmd::Verb; 28 | 29 | std::unique_ptr xd::repl::cmd::make_command(Verb verb) { 30 | return std::make_unique(verb); 31 | } 32 | 33 | std::unique_ptr xd::repl::cmd::make_command( 34 | std::string name, std::string description, std::vector verbs) 35 | { 36 | return std::make_unique(name, description, verbs); 37 | } 38 | -------------------------------------------------------------------------------- /src/REPL/Command/MakeCommand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_MAKECOMMAND_HPP 23 | #define XENDBG_MAKECOMMAND_HPP 24 | 25 | #include 26 | 27 | #include "Command.hpp" 28 | #include "CommandBase.hpp" 29 | #include "CommandVerb.hpp" 30 | #include "Verb.hpp" 31 | 32 | namespace xd::repl::cmd { 33 | 34 | std::unique_ptr make_command(Verb verb); 35 | std::unique_ptr make_command( 36 | std::string name, std::string description, std::vector verbs); 37 | 38 | } 39 | 40 | #endif //XENDBG_MAKECOMMAND_HPP 41 | -------------------------------------------------------------------------------- /src/REPL/Command/Match.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_MATCH_HPP 23 | #define XENDBG_MATCH_HPP 24 | 25 | #include 26 | 27 | #include "ArgsHandle.hpp" 28 | #include "Argument.hpp" 29 | #include "Flag.hpp" 30 | #include "FlagsHandle.hpp" 31 | 32 | namespace xd::repl::cmd { 33 | 34 | class DefaultArgPositionException : public std::exception {}; 35 | 36 | class UnknownFlagException : public std::exception { 37 | public: 38 | UnknownFlagException(std::string::const_iterator pos) 39 | : _pos(pos) {}; 40 | 41 | std::string::const_iterator get_pos() const { return _pos; } 42 | 43 | private: 44 | std::string::const_iterator _pos; 45 | }; 46 | 47 | class ArgMatchFailedException : public std::exception { 48 | public: 49 | ArgMatchFailedException(std::string::const_iterator pos, Argument arg) 50 | : _pos(pos), _arg(arg) {}; 51 | 52 | std::string::const_iterator get_pos() const { return _pos; } 53 | const Argument &get_argument() const { return _arg; }; 54 | 55 | private: 56 | std::string::const_iterator _pos; 57 | Argument _arg; 58 | }; 59 | 60 | void validate_args(const std::vector &args); 61 | void validate_new_arg(const std::vector &args, 62 | const Argument &new_arg); 63 | 64 | std::pair match_args( 65 | std::string::const_iterator begin, std::string::const_iterator end, 66 | const std::vector &args); 67 | 68 | std::pair match_flags( 69 | std::string::const_iterator begin, std::string::const_iterator end, 70 | const std::vector &flags, bool ignore_unknown_flags = false); 71 | 72 | std::optional> get_next_arg( 73 | std::string::const_iterator begin, std::string::const_iterator end, 74 | const std::vector &args); 75 | } 76 | 77 | #endif //XENDBG_MATCH_HPP 78 | -------------------------------------------------------------------------------- /src/REPL/Command/MatchHelper.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_MATCHHELPER_HPP 23 | #define XENDBG_MATCHHELPER_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | namespace xd::repl::cmd::match { 32 | 33 | template 34 | using MatcherFn = std::function; 35 | 36 | template 37 | It_t match_everything(It_t /*begin*/, It_t end) { 38 | return end; 39 | } 40 | 41 | template 42 | It_t match_word(It_t begin, It_t end) { 43 | return util::string::next_whitespace(begin, end); 44 | } 45 | 46 | template 47 | It_t match_number_unsigned(It_t begin, It_t end) { 48 | const auto ws = util::string::next_whitespace(begin, end); 49 | 50 | try { 51 | std::stoul(std::string(begin, ws)); 52 | return ws; 53 | } catch (const std::invalid_argument &e) { 54 | return begin; 55 | } 56 | } 57 | 58 | template 59 | MatcherFn make_match_one_of(Container_t options) { 60 | return [options](It_t begin, It_t end) { 61 | size_t len = 0; 62 | std::find_if(options.begin(), options.end(), 63 | [begin, end, &len](const auto& opt) { 64 | if (util::string::is_prefix(opt.begin(), opt.end(), begin, end)) { 65 | len = opt.size(); 66 | return true; 67 | } 68 | return false; 69 | }); 70 | 71 | return begin + len; 72 | }; 73 | } 74 | 75 | } 76 | 77 | #endif //XENDBG_MATCHHELPER_HPP 78 | -------------------------------------------------------------------------------- /src/REPL/DebuggerREPL.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_DEBUGGERREPL_HPP 23 | #define XENDBG_DEBUGGERREPL_HPP 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | #include "DebuggerWrapper.hpp" 32 | #include "REPL.hpp" 33 | 34 | namespace xd::dbg { 35 | 36 | class NotSupportedException : public std::runtime_error { 37 | public: 38 | explicit NotSupportedException(const std::string &what) 39 | : std::runtime_error(what.c_str()) {}; 40 | }; 41 | 42 | class InvalidInputException : public std::runtime_error { 43 | public: 44 | explicit InvalidInputException(const std::string &what) 45 | : std::runtime_error(what.c_str()) {}; 46 | }; 47 | 48 | class NoSuchDomainException : public std::runtime_error { 49 | public: 50 | explicit NoSuchDomainException(const std::string &what) 51 | : std::runtime_error(what.c_str()) {}; 52 | }; 53 | 54 | class DebuggerREPL { 55 | public: 56 | DebuggerREPL(bool non_stop_mode); 57 | DebuggerREPL(const DebuggerREPL &other) = delete; 58 | DebuggerREPL& operator=(const DebuggerREPL &other) = delete; 59 | 60 | void run(); 61 | 62 | private: 63 | void setup_repl(); 64 | 65 | static void print_domain_info(const xen::Domain& domain); 66 | static void print_registers(const reg::RegistersX86Any& regs); 67 | static void print_xen_info(const xen::Xen& xen); 68 | void examine(uint64_t address, size_t word_size, size_t num_words); 69 | void disassemble(uint64_t address, size_t length, size_t max_instrs = 0); 70 | void stop(); 71 | 72 | private: 73 | repl::REPL _repl; 74 | std::shared_ptr _loop; 75 | std::shared_ptr _signal; 76 | repl::DebuggerWrapper _dwrap; 77 | size_t _vcpu_id, _max_vcpu_id; 78 | csh _capstone; 79 | }; 80 | 81 | } 82 | 83 | #endif //XENDBG_DEBUGGERREPL_HPP 84 | -------------------------------------------------------------------------------- /src/REPL/Parser/Expression/Expression.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_EXPRESSION_HPP 23 | #define XENDBG_EXPRESSION_HPP 24 | 25 | #include 26 | 27 | #include "ExpressionGeneric.hpp" 28 | 29 | namespace xd::parser::expr { 30 | 31 | struct Constant : public Unit {}; 32 | struct Label : public Unit {}; 33 | struct Variable : public Unit {}; 34 | 35 | using Expression = ExpressionGeneric; 36 | 37 | } 38 | 39 | #endif //XENDBG_EXPRESSION_HPP 40 | -------------------------------------------------------------------------------- /src/REPL/Parser/Expression/Operator/BinaryOperator.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_BINARYOPERATOR_HPP 23 | #define XENDBG_BINARYOPERATOR_HPP 24 | 25 | #include 26 | 27 | namespace xd::parser::expr::op { 28 | 29 | struct Add {}; 30 | struct Equals {}; 31 | struct Divide {}; 32 | struct Multiply {}; 33 | struct Subtract {}; 34 | 35 | 36 | using BinaryOperator = std::variant; 37 | 38 | } 39 | 40 | #endif //XENDBG_BINARYOPERATOR_HPP 41 | -------------------------------------------------------------------------------- /src/REPL/Parser/Expression/Operator/Precedence.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_PRECEDENCE_HPP 23 | #define XENDBG_PRECEDENCE_HPP 24 | 25 | #include 26 | 27 | #include "BinaryOperator.hpp" 28 | #include "UnaryOperator.hpp" 29 | 30 | namespace xd::parser::expr::op { 31 | using Precedence = int; 32 | 33 | template 34 | struct _precedence{}; 35 | 36 | template <> struct _precedence { static const Precedence p = 1; }; 37 | template <> struct _precedence { static const Precedence p = 10; }; 38 | template <> struct _precedence { static const Precedence p = 20; }; 39 | template <> struct _precedence { static const Precedence p = 20; }; 40 | template <> struct _precedence { static const Precedence p = 30; }; 41 | template <> struct _precedence { static const Precedence p = 30; }; 42 | template <> struct _precedence { static const Precedence p = 50; }; 43 | 44 | template 45 | Precedence precedence_of(const T&) { 46 | return _precedence::p; 47 | } 48 | 49 | template 50 | Precedence precedence_of(const std::variant& op) { 51 | return std::visit([](auto&& op) { 52 | return precedence_of(op); 53 | }, op); 54 | } 55 | } 56 | 57 | #endif //XENDBG_PRECEDENCE_HPP 58 | -------------------------------------------------------------------------------- /src/REPL/Parser/Expression/Operator/UnaryOperator.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_UNARYOPERATOR_HPP 23 | #define XENDBG_UNARYOPERATOR_HPP 24 | 25 | #include 26 | 27 | namespace xd::parser::expr::op { 28 | 29 | struct Dereference {}; 30 | struct Negate {}; 31 | 32 | using UnaryOperator = std::variant; 33 | 34 | } 35 | 36 | #endif //XENDBG_UNARYOPERATOR_HPP 37 | -------------------------------------------------------------------------------- /src/REPL/Parser/Sentinel.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_SENTINEL_HPP 23 | #define XENDBG_SENTINEL_HPP 24 | 25 | #include 26 | 27 | #include "Expression/Operator/Precedence.hpp" 28 | 29 | namespace xd::parser::expr::op { 30 | class Sentinel {}; 31 | 32 | template <> 33 | struct _precedence { 34 | static const op::Precedence p = std::numeric_limits::min(); 35 | }; 36 | } 37 | 38 | #endif //XENDBG_SENTINEL_HPP 39 | -------------------------------------------------------------------------------- /src/REPL/Parser/Token/Constant.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_TOKEN_CONSTANT_HPP 23 | #define XENDBG_TOKEN_CONSTANT_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "TokenMatchResult.hpp" 32 | 33 | namespace xd::parser::token { 34 | 35 | class Constant { 36 | public: 37 | using Value = uint64_t; 38 | 39 | public: 40 | explicit Constant(Value value) 41 | : _value(value) {} 42 | 43 | Value value() const { return _value; }; 44 | 45 | private: 46 | Value _value; 47 | 48 | public: 49 | static TokenMatchResult match(std::string::const_iterator begin, std::string::const_iterator end) { 50 | if (begin == end || *begin == '-' || *begin == '+') 51 | return std::make_pair(std::nullopt, begin); 52 | 53 | // stoi doesn't handle the 0b prefix, so we have to do this manually 54 | size_t base = 10; 55 | 56 | if ((end-begin) > 1 && *begin == '0') { 57 | const char base_ch = *(begin+1); 58 | switch (base_ch) { 59 | case 'b': 60 | base = 2; 61 | break; 62 | case 'x': 63 | base = 16; 64 | break; 65 | default: 66 | break; 67 | } 68 | } 69 | 70 | // Skip base the 0x/0b if found 71 | const size_t skip = (base != 10 ? 2 : 0); 72 | 73 | try { 74 | size_t pos; 75 | const auto s = std::string(begin + skip, end); 76 | const auto value = std::stoul(s, &pos, base); 77 | const auto new_begin = begin + skip + pos; 78 | 79 | return std::make_pair(Constant(value), new_begin); 80 | } catch (const std::invalid_argument &e) { 81 | return std::make_pair(std::nullopt, begin); 82 | } 83 | } 84 | }; 85 | } 86 | 87 | #endif //XENDBG_CONSTANT_HPP 88 | -------------------------------------------------------------------------------- /src/REPL/Parser/Token/Label.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2018-2019 NCC Group 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | // 21 | 22 | #ifndef XENDBG_TOKEN_LABEL_HPP 23 | #define XENDBG_TOKEN_LABEL_HPP 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "TokenMatchResult.hpp" 31 | 32 | namespace xd::parser::token { 33 | 34 | class Label { 35 | public: 36 | explicit Label(std::string name) 37 | : _name(std::move(name)) {} 38 | 39 | std::string name() const { return _name; }; 40 | 41 | private: 42 | std::string _name; 43 | 44 | public: 45 | static TokenMatchResult