ntextsup.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*++
  2. Copyright (c) 2012 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. ntextsup.c
  9. Abstract:
  10. This module implements OS-specific support routines for using debugger
  11. extensions on Windows NT.
  12. Author:
  13. Evan Green 10-Sep-2012
  14. Environment:
  15. Debug Client
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <windows.h>
  21. #include <stdio.h>
  22. #include <assert.h>
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. #define HANDLE_TABLE_GROWTH 10
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // ----------------------------------------------- Internal Function Prototypes
  32. //
  33. //
  34. // -------------------------------------------------------------------- Globals
  35. //
  36. HMODULE *DbgHandleTable = NULL;
  37. ULONG DbgHandleTableSize = 0;
  38. ULONG DbgNextHandle = 1;
  39. //
  40. // ------------------------------------------------------------------ Functions
  41. //
  42. ULONG
  43. DbgLoadLibrary (
  44. PSTR BinaryName
  45. )
  46. /*++
  47. Routine Description:
  48. This routine loads a shared library.
  49. Arguments:
  50. BinaryName - Supplies the name of the binary to load.
  51. Return Value:
  52. Returns a non-zero handle on success.
  53. 0 on failure.
  54. --*/
  55. {
  56. HMODULE NewHandle;
  57. HMODULE *NewHandleTable;
  58. //
  59. // Attempt to load the library. Bail on failure.
  60. //
  61. NewHandle = LoadLibrary(BinaryName);
  62. if (NewHandle == NULL) {
  63. return 0;
  64. }
  65. //
  66. // Save the new handle in the table and return the index representing that
  67. // handle. If saving this new handle overruns the current buffer, allocate
  68. // a bigger buffer, copy the old contents in, and free the old buffer.
  69. //
  70. if (DbgNextHandle >= DbgHandleTableSize) {
  71. NewHandleTable = malloc((DbgHandleTableSize + HANDLE_TABLE_GROWTH) *
  72. sizeof(HMODULE));
  73. if (NewHandleTable == NULL) {
  74. FreeLibrary(NewHandle);
  75. return 0;
  76. }
  77. if (DbgHandleTable != NULL) {
  78. RtlCopyMemory(NewHandleTable,
  79. DbgHandleTable,
  80. DbgHandleTableSize * sizeof(HMODULE));
  81. free(DbgHandleTable);
  82. }
  83. DbgHandleTable = NewHandleTable;
  84. DbgHandleTableSize += HANDLE_TABLE_GROWTH;
  85. }
  86. assert(DbgNextHandle < DbgHandleTableSize);
  87. DbgHandleTable[DbgNextHandle] = NewHandle;
  88. DbgNextHandle += 1;
  89. return DbgNextHandle - 1;
  90. }
  91. VOID
  92. DbgFreeLibrary (
  93. ULONG Handle
  94. )
  95. /*++
  96. Routine Description:
  97. This routine unloads a shared library.
  98. Arguments:
  99. Handle - Supplies the handle to to the loaded library.
  100. Return Value:
  101. None.
  102. --*/
  103. {
  104. FreeLibrary(DbgHandleTable[Handle]);
  105. return;
  106. }
  107. PVOID
  108. DbgGetProcedureAddress (
  109. ULONG Handle,
  110. PSTR ProcedureName
  111. )
  112. /*++
  113. Routine Description:
  114. This routine gets the address of a routine in a loaded shared library (DLL).
  115. Arguments:
  116. Handle - Supplies the handle to to the loaded library.
  117. ProcedureName - Supplies the name of the procedure to look up.
  118. Return Value:
  119. Returns a pointer to the procedure on success.
  120. NULL on failure.
  121. --*/
  122. {
  123. return GetProcAddress(DbgHandleTable[Handle], ProcedureName);
  124. }
  125. //
  126. // --------------------------------------------------------- Internal Functions
  127. //