dbgdwarf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. dbgdwarf.c
  9. Abstract:
  10. This module implements the glue functions that connect the rest of the
  11. debugger to the DWARF symbol library.
  12. Author:
  13. Evan Green 17-Dec-2015
  14. Environment:
  15. Debug
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <minoca/lib/types.h>
  21. #include <minoca/lib/status.h>
  22. #include <minoca/lib/im.h>
  23. #include <minoca/debug/spproto.h>
  24. #include <minoca/debug/dbgext.h>
  25. #include "dbgrprof.h"
  26. #include "dbgapi.h"
  27. #include "symbols.h"
  28. #include "dbgrcomm.h"
  29. #include "dwarf.h"
  30. #include <assert.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. //
  35. // ---------------------------------------------------------------- Definitions
  36. //
  37. //
  38. // ------------------------------------------------------ Data Type Definitions
  39. //
  40. //
  41. // ----------------------------------------------- Internal Function Prototypes
  42. //
  43. //
  44. // -------------------------------------------------------------------- Globals
  45. //
  46. //
  47. // ------------------------------------------------------------------ Functions
  48. //
  49. INT
  50. DwarfTargetRead (
  51. PDWARF_CONTEXT Context,
  52. ULONGLONG TargetAddress,
  53. ULONGLONG Size,
  54. ULONG AddressSpace,
  55. PVOID Buffer
  56. )
  57. /*++
  58. Routine Description:
  59. This routine performs a read from target memory.
  60. Arguments:
  61. Context - Supplies a pointer to the DWARF context.
  62. TargetAddress - Supplies the address to read from.
  63. Size - Supplies the number of bytes to read.
  64. AddressSpace - Supplies the address space identifier. Supply 0 for normal
  65. memory.
  66. Buffer - Supplies a pointer where the read data will be returned on success.
  67. Return Value:
  68. 0 on success.
  69. Returns an error number on failure.
  70. --*/
  71. {
  72. ULONG BytesRead;
  73. PDEBUGGER_CONTEXT DebuggerContext;
  74. INT Status;
  75. PDEBUG_SYMBOLS Symbols;
  76. Symbols = (((PDEBUG_SYMBOLS)Context) - 1);
  77. DebuggerContext = Symbols->HostContext;
  78. assert(DebuggerContext != NULL);
  79. assert(AddressSpace == 0);
  80. Status = DbgReadMemory(DebuggerContext,
  81. TRUE,
  82. TargetAddress,
  83. Size,
  84. Buffer,
  85. &BytesRead);
  86. if (Status != 0) {
  87. return Status;
  88. }
  89. if (BytesRead != Size) {
  90. return EFAULT;
  91. }
  92. return 0;
  93. }
  94. INT
  95. DwarfTargetReadRegister (
  96. PDWARF_CONTEXT Context,
  97. ULONG Register,
  98. PULONGLONG Value
  99. )
  100. /*++
  101. Routine Description:
  102. This routine reads a register value.
  103. Arguments:
  104. Context - Supplies a pointer to the DWARF context.
  105. Register - Supplies the register to read.
  106. Value - Supplies a pointer where the value will be returned on success.
  107. Return Value:
  108. 0 on success.
  109. Returns an error number on failure.
  110. --*/
  111. {
  112. PDEBUGGER_CONTEXT DebuggerContext;
  113. PREGISTERS_UNION Registers;
  114. INT Status;
  115. PDEBUG_SYMBOLS Symbols;
  116. Symbols = (((PDEBUG_SYMBOLS)Context) - 1);
  117. DebuggerContext = Symbols->HostContext;
  118. assert(DebuggerContext != NULL);
  119. Registers = Symbols->RegistersContext;
  120. if (Registers == NULL) {
  121. Registers = &(DebuggerContext->FrameRegisters);
  122. }
  123. Status = DbgGetRegister(DebuggerContext, Registers, Register, Value);
  124. return Status;
  125. }
  126. INT
  127. DwarfTargetWriteRegister (
  128. PDWARF_CONTEXT Context,
  129. ULONG Register,
  130. ULONGLONG Value
  131. )
  132. /*++
  133. Routine Description:
  134. This routine writes a register value.
  135. Arguments:
  136. Context - Supplies a pointer to the DWARF context.
  137. Register - Supplies the register to write.
  138. Value - Supplies the new value of the register.
  139. Return Value:
  140. 0 on success.
  141. Returns an error number on failure.
  142. --*/
  143. {
  144. PDEBUGGER_CONTEXT DebuggerContext;
  145. PREGISTERS_UNION Registers;
  146. INT Status;
  147. PDEBUG_SYMBOLS Symbols;
  148. Symbols = (((PDEBUG_SYMBOLS)Context) - 1);
  149. DebuggerContext = Symbols->HostContext;
  150. assert(DebuggerContext != NULL);
  151. Registers = Symbols->RegistersContext;
  152. if (Registers == NULL) {
  153. Registers = &(DebuggerContext->FrameRegisters);
  154. }
  155. Status = DbgSetRegister(DebuggerContext, Registers, Register, Value);
  156. return Status;
  157. }
  158. INT
  159. DwarfTargetWritePc (
  160. PDWARF_CONTEXT Context,
  161. ULONGLONG Value
  162. )
  163. /*++
  164. Routine Description:
  165. This routine writes a the instruction pointer register, presumably with the
  166. return address.
  167. Arguments:
  168. Context - Supplies a pointer to the DWARF context.
  169. Value - Supplies the new value of the register.
  170. Return Value:
  171. 0 on success.
  172. Returns an error number on failure.
  173. --*/
  174. {
  175. PDEBUGGER_CONTEXT DebuggerContext;
  176. PREGISTERS_UNION Registers;
  177. PDEBUG_SYMBOLS Symbols;
  178. Symbols = (((PDEBUG_SYMBOLS)Context) - 1);
  179. DebuggerContext = Symbols->HostContext;
  180. assert(DebuggerContext != NULL);
  181. Registers = Symbols->RegistersContext;
  182. if (Registers == NULL) {
  183. Registers = &(DebuggerContext->FrameRegisters);
  184. }
  185. DbgSetPc(DebuggerContext, Registers, Value);
  186. return 0;
  187. }
  188. PSTR
  189. DwarfGetRegisterName (
  190. PDWARF_CONTEXT Context,
  191. ULONG Register
  192. )
  193. /*++
  194. Routine Description:
  195. This routine returns a string containing the name of the given register.
  196. Arguments:
  197. Context - Supplies a pointer to the application context.
  198. Register - Supplies the register number.
  199. Return Value:
  200. Returns a pointer to a constant string containing the name of the register.
  201. --*/
  202. {
  203. PDEBUG_SYMBOLS Symbols;
  204. Symbols = (((PDEBUG_SYMBOLS)Context) - 1);
  205. return DbgGetRegisterName(Symbols->Machine, Register);
  206. }
  207. ULONG
  208. DwarfGetNativeSize (
  209. PDWARF_CONTEXT Context
  210. )
  211. /*++
  212. Routine Description:
  213. This routine returns the native machine word size.
  214. Arguments:
  215. Context - Supplies a pointer to the application context.
  216. Return Value:
  217. Returns the number of bytes in a machine word: usually 4 for 32-bit
  218. machines and 8 for 64-bit machines.
  219. --*/
  220. {
  221. PDEBUG_SYMBOLS Symbols;
  222. Symbols = (((PDEBUG_SYMBOLS)Context) - 1);
  223. switch (Symbols->Machine) {
  224. case ImageMachineTypeX86:
  225. case ImageMachineTypeArm32:
  226. return 4;
  227. case ImageMachineTypeX64:
  228. case ImageMachineTypeArm64:
  229. return 8;
  230. default:
  231. assert(FALSE);
  232. break;
  233. }
  234. return 0;
  235. }
  236. //
  237. // --------------------------------------------------------- Internal Functions
  238. //