1
0

consio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. consio.c
  5. Abstract:
  6. This module implements standard input and output functionality for the
  7. debugger.
  8. Author:
  9. Evan Green 30-Dec-2013
  10. Environment:
  11. Debug
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include "dbgrtl.h"
  17. #include <minoca/debug/spproto.h>
  18. #include <minoca/lib/im.h>
  19. #include <minoca/debug/dbgext.h>
  20. #include "disasm.h"
  21. #include "dbgapi.h"
  22. #include "dbgrprof.h"
  23. #include "console.h"
  24. #include "symbols.h"
  25. #include "dbgrcomm.h"
  26. #include "dbgsym.h"
  27. #include "extsp.h"
  28. #include "consio.h"
  29. #include "remsrv.h"
  30. #include <assert.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. //
  38. // ---------------------------------------------------------------- Definitions
  39. //
  40. #define DBGR_IO_BUFFER_SIZE 1024
  41. //
  42. // ------------------------------------------------------ Data Type Definitions
  43. //
  44. //
  45. // ----------------------------------------------- Internal Function Prototypes
  46. //
  47. BOOL
  48. DbgrpFormatWriteCharacter (
  49. CHAR Character,
  50. PPRINT_FORMAT_CONTEXT Context
  51. );
  52. //
  53. // -------------------------------------------------------------------- Globals
  54. //
  55. //
  56. // Store a pointer to the global context. Currently only one debugger context
  57. // is supported for output functions.
  58. //
  59. PDEBUGGER_CONTEXT DbgConsoleContext;
  60. //
  61. // ------------------------------------------------------------------ Functions
  62. //
  63. INT
  64. DbgrInitializeConsoleIo (
  65. PDEBUGGER_CONTEXT Context
  66. )
  67. /*++
  68. Routine Description:
  69. This routine initializes console I/O for the debugger.
  70. Arguments:
  71. Context - Supplies a pointer to the debugger context.
  72. Return Value:
  73. 0 on success.
  74. Returns an error code on failure.
  75. --*/
  76. {
  77. BOOL EchoCommands;
  78. INT Result;
  79. assert(DbgConsoleContext == NULL);
  80. DbgConsoleContext = Context;
  81. EchoCommands = FALSE;
  82. Result = DbgrOsInitializeConsole(&EchoCommands);
  83. if (Result == FALSE) {
  84. Result = EINVAL;
  85. goto InitializeConsoleIoEnd;
  86. }
  87. if (EchoCommands != FALSE) {
  88. Context->Flags |= DEBUGGER_FLAG_ECHO_COMMANDS;
  89. }
  90. assert(Context->StandardOut.ConsoleBuffer == NULL);
  91. Context->StandardOut.ConsoleBuffer = malloc(DBGR_IO_BUFFER_SIZE);
  92. if (Context->StandardOut.ConsoleBuffer == NULL) {
  93. Result = ENOMEM;
  94. goto InitializeConsoleIoEnd;
  95. }
  96. Context->StandardOut.ConsoleBufferCapacity = DBGR_IO_BUFFER_SIZE;
  97. Context->StandardOut.ConsoleBufferSize = 0;
  98. Context->StandardOut.Lock = CreateDebuggerLock();
  99. if (Context->StandardOut.Lock == NULL) {
  100. Result = ENOMEM;
  101. goto InitializeConsoleIoEnd;
  102. }
  103. Context->StandardIn.Lock = CreateDebuggerLock();
  104. if (Context->StandardIn.Lock == NULL) {
  105. Result = ENOMEM;
  106. goto InitializeConsoleIoEnd;
  107. }
  108. Result = 0;
  109. InitializeConsoleIoEnd:
  110. return Result;
  111. }
  112. VOID
  113. DbgrDestroyConsoleIo (
  114. PDEBUGGER_CONTEXT Context
  115. )
  116. /*++
  117. Routine Description:
  118. This routine destroys console I/O for the debugger.
  119. Arguments:
  120. Context - Supplies a pointer to the debugger context.
  121. Return Value:
  122. None.
  123. --*/
  124. {
  125. DbgrOsDestroyConsole();
  126. if (Context->StandardOut.ConsoleBuffer != NULL) {
  127. free(Context->StandardOut.ConsoleBuffer);
  128. }
  129. if (Context->StandardOut.Prompt != NULL) {
  130. free(Context->StandardOut.Prompt);
  131. }
  132. if (Context->StandardOut.Lock != NULL) {
  133. DestroyDebuggerLock(Context->StandardOut.Lock);
  134. }
  135. if (Context->StandardIn.Lock != NULL) {
  136. DestroyDebuggerLock(Context->StandardIn.Lock);
  137. }
  138. DbgConsoleContext = NULL;
  139. return;
  140. }
  141. INT
  142. DbgOut (
  143. const char *Format,
  144. ...
  145. )
  146. /*++
  147. Routine Description:
  148. This routine prints a formatted string to the debugger console.
  149. Arguments:
  150. Format - Supplies the printf format string.
  151. ... - Supplies a variable number of arguments, as required by the printf
  152. format string argument.
  153. Return Value:
  154. Returns the number of bytes successfully converted, not including the null
  155. terminator.
  156. Returns a negative number if an error was encountered.
  157. --*/
  158. {
  159. va_list Arguments;
  160. int Result;
  161. assert(DbgConsoleContext != NULL);
  162. va_start(Arguments, Format);
  163. Result = DbgOutVaList(DbgConsoleContext, Format, Arguments);
  164. va_end(Arguments);
  165. return Result;
  166. }
  167. INT
  168. DbgOutVaList (
  169. PDEBUGGER_CONTEXT Context,
  170. const char *Format,
  171. va_list Arguments
  172. )
  173. /*++
  174. Routine Description:
  175. This routine prints a formatted string to the given debugger console.
  176. Arguments:
  177. Context - Supplies a pointer to the debugger context to output to.
  178. Format - Supplies the printf format string.
  179. Arguments - Supplies the argument list to the format string. The va_end
  180. macro is not invoked on this list.
  181. Return Value:
  182. Returns the number of bytes successfully converted. A null terminator is
  183. not written.
  184. Returns a negative number if an error was encountered.
  185. --*/
  186. {
  187. PRINT_FORMAT_CONTEXT PrintContext;
  188. if (Context == NULL) {
  189. Context = DbgConsoleContext;
  190. }
  191. memset(&PrintContext, 0, sizeof(PRINT_FORMAT_CONTEXT));
  192. PrintContext.Context = Context;
  193. PrintContext.U.WriteCharacter = DbgrpFormatWriteCharacter;
  194. RtlInitializeMultibyteState(&(PrintContext.State),
  195. CharacterEncodingDefault);
  196. AcquireDebuggerLock(Context->StandardOut.Lock);
  197. RtlFormat(&PrintContext, (PSTR)Format, Arguments);
  198. //
  199. // If something was written poke all the clients to send the data along to
  200. // them too.
  201. //
  202. if (PrintContext.CharactersWritten != 0) {
  203. DbgrpServerNotifyClients(Context);
  204. }
  205. ReleaseDebuggerLock(Context->StandardOut.Lock);
  206. return PrintContext.CharactersWritten;
  207. }
  208. //
  209. // --------------------------------------------------------- Internal Functions
  210. //
  211. BOOL
  212. DbgrpFormatWriteCharacter (
  213. CHAR Character,
  214. PPRINT_FORMAT_CONTEXT Context
  215. )
  216. /*++
  217. Routine Description:
  218. This routine writes a character to the output during a printf-style
  219. formatting operation.
  220. Arguments:
  221. Character - Supplies the character to be written.
  222. Context - Supplies a pointer to the printf-context.
  223. Return Value:
  224. TRUE on success.
  225. FALSE on failure.
  226. --*/
  227. {
  228. PDEBUGGER_CONTEXT DebuggerContext;
  229. PVOID NewBuffer;
  230. ULONGLONG NewCapacity;
  231. PDEBUGGER_STANDARD_OUT Out;
  232. DebuggerContext = Context->Context;
  233. Out = &(DebuggerContext->StandardOut);
  234. //
  235. // Reallocate the console buffer if needed.
  236. //
  237. if (Out->ConsoleBufferSize + 2 > Out->ConsoleBufferCapacity) {
  238. NewCapacity = Out->ConsoleBufferCapacity * 2;
  239. assert(NewCapacity > Out->ConsoleBufferSize + 2);
  240. NewBuffer = realloc(Out->ConsoleBuffer, NewCapacity);
  241. if (NewBuffer == NULL) {
  242. return FALSE;
  243. }
  244. Out->ConsoleBuffer = NewBuffer;
  245. Out->ConsoleBufferCapacity = NewCapacity;
  246. }
  247. //
  248. // Add the character to the console buffer.
  249. //
  250. Out->ConsoleBuffer[Out->ConsoleBufferSize] = Character;
  251. Out->ConsoleBufferSize += 1;
  252. if (fputc(Character, stdout) == -1) {
  253. return FALSE;
  254. }
  255. return TRUE;
  256. }