KSMachineContext.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // KSMachineContext.h
  3. //
  4. // Created by Karl Stenerud on 2016-12-02.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #ifndef HDR_KSMachineContext_h
  27. #define HDR_KSMachineContext_h
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "KSThread.h"
  32. #include <stdbool.h>
  33. #include <mach/mach.h>
  34. /** Suspend the runtime environment.
  35. */
  36. void ksmc_suspendEnvironment(thread_act_array_t *suspendedThreads, mach_msg_type_number_t *numSuspendedThreads);
  37. /** Resume the runtime environment.
  38. */
  39. void ksmc_resumeEnvironment(thread_act_array_t threads, mach_msg_type_number_t numThreads);
  40. /** Create a new machine context on the stack.
  41. * This macro creates a storage object on the stack, as well as a pointer of type
  42. * struct KSMachineContext* in the current scope, which points to the storage object.
  43. *
  44. * Example usage: KSMC_NEW_CONTEXT(a_context);
  45. * This creates a new pointer at the current scope that behaves as if:
  46. * struct KSMachineContext* a_context = some_storage_location;
  47. *
  48. * @param NAME The C identifier to give the pointer.
  49. */
  50. #define KSMC_NEW_CONTEXT(NAME) \
  51. char ksmc_##NAME##_storage[ksmc_contextSize()]; \
  52. struct KSMachineContext* NAME = (struct KSMachineContext*)ksmc_##NAME##_storage
  53. struct KSMachineContext;
  54. /** Get the internal size of a machine context.
  55. */
  56. int ksmc_contextSize(void);
  57. /** Fill in a machine context from a thread.
  58. *
  59. * @param thread The thread to get information from.
  60. * @param destinationContext The context to fill.
  61. * @param isCrashedContext Used to indicate that this is the thread that crashed,
  62. *
  63. * @return true if successful.
  64. */
  65. bool ksmc_getContextForThread(KSThread thread, struct KSMachineContext* destinationContext, bool isCrashedContext);
  66. /** Fill in a machine context from a signal handler.
  67. * A signal handler context is always assumed to be a crashed context.
  68. *
  69. * @param signalUserContext The signal context to get information from.
  70. * @param destinationContext The context to fill.
  71. *
  72. * @return true if successful.
  73. */
  74. bool ksmc_getContextForSignal(void* signalUserContext, struct KSMachineContext* destinationContext);
  75. /** Get the thread associated with a machine context.
  76. *
  77. * @param context The machine context.
  78. *
  79. * @return The associated thread.
  80. */
  81. KSThread ksmc_getThreadFromContext(const struct KSMachineContext* const context);
  82. /** Get the number of threads stored in a machine context.
  83. *
  84. * @param context The machine context.
  85. *
  86. * @return The number of threads.
  87. */
  88. int ksmc_getThreadCount(const struct KSMachineContext* const context);
  89. /** Get a thread from a machine context.
  90. *
  91. * @param context The machine context.
  92. * @param index The index of the thread to retrieve.
  93. *
  94. * @return The thread.
  95. */
  96. KSThread ksmc_getThreadAtIndex(const struct KSMachineContext* const context, int index);
  97. /** Get the index of a thread.
  98. *
  99. * @param context The machine context.
  100. * @param thread The thread.
  101. *
  102. * @return The thread's index, or -1 if it couldn't be determined.
  103. */
  104. int ksmc_indexOfThread(const struct KSMachineContext* const context, KSThread thread);
  105. /** Check if this is a crashed context.
  106. */
  107. bool ksmc_isCrashedContext(const struct KSMachineContext* const context);
  108. /** Check if this context can have stored CPU state.
  109. */
  110. bool ksmc_canHaveCPUState(const struct KSMachineContext* const context);
  111. /** Check if this context has valid exception registers.
  112. */
  113. bool ksmc_hasValidExceptionRegisters(const struct KSMachineContext* const context);
  114. /** Add a thread to the reserved threads list.
  115. *
  116. * @param thread The thread to add to the list.
  117. */
  118. void ksmc_addReservedThread(KSThread thread);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif // HDR_KSMachineContext_h