| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 #include "native_client/src/trusted/service_runtime/nacl_oop_debugger_hooks.h" |
| 7 #include "native_client/src/trusted/service_runtime/sel_ldr.h" |
| 8 |
| 9 /* |
| 10 * Note: this function does not do anything if program |
| 11 * is not running under debugger. |
| 12 * If program (sel_ldr) is running under debugger, calling this function |
| 13 * gives debugger a chance to modify state of the debuggee before |
| 14 * resuming debuggee execution. |
| 15 */ |
| 16 void SendMessageToDebuggerAndHalt(const char *fmt, ...) |
| 17 ATTRIBUTE_FORMAT_PRINTF(1, 2); |
| 18 |
| 19 void NaClOopDebuggerAppCreateHook(struct NaClApp *nap) { |
| 20 if (NULL == nap) |
| 21 return; |
| 22 SendMessageToDebuggerAndHalt( |
| 23 "-event AppCreate -nap %p -mem_start %p -user_entry_pt %p " |
| 24 "-initial_entry_pt %p", |
| 25 nap, |
| 26 (void *) nap->mem_start, |
| 27 (void *) nap->user_entry_pt, |
| 28 (void *) nap->initial_entry_pt); |
| 29 } |
| 30 |
| 31 void NaClOopDebuggerThreadCreateHook(struct NaClAppThread *natp) { |
| 32 SendMessageToDebuggerAndHalt("-event ThreadCreate -natp %p", natp); |
| 33 } |
| 34 |
| 35 void NaClOopDebuggerThreadExitHook(struct NaClAppThread *natp, int exit_code) { |
| 36 SendMessageToDebuggerAndHalt("-event ThreadExit -natp %p -exit_code %d", |
| 37 natp, |
| 38 exit_code); |
| 39 } |
| 40 |
| 41 void NaClOopDebuggerAppExitHook(int exit_code) { |
| 42 SendMessageToDebuggerAndHalt("-event AppExit -exit_code %d", exit_code); |
| 43 } |
| 44 |
| 45 void SendMessageToDebuggerAndHalt(const char *fmt, ...) { |
| 46 if (NULL == fmt) { |
| 47 NaClLog(LOG_FATAL, "SendMessageToDebuggerAndHalt: fmt == NULL\n"); |
| 48 return; |
| 49 } |
| 50 |
| 51 #define kVarMsgSize 512 |
| 52 /* |
| 53 * Determines whether the calling process is being debugged by a debugger. |
| 54 * IsDebuggerPresent() is a member of Windows Debugging API: |
| 55 * http://msdn.microsoft.com/en-us/library/ms680345%28v=VS.85%29.aspx |
| 56 */ |
| 57 if (IsDebuggerPresent()) { |
| 58 /* |
| 59 * Prefix has GUID string specific to our OOP debugger, so that it |
| 60 * can differentiate it from other uses of 'OutputDebugString'. |
| 61 */ |
| 62 char const prefix[] = "{7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 "; |
| 63 char msg[sizeof(prefix) - 1 + kVarMsgSize]; |
| 64 char *post_pref_msg = msg + sizeof(prefix) - 1; |
| 65 signed int res = 0; |
| 66 va_list marker; |
| 67 |
| 68 strcpy(msg, prefix); |
| 69 va_start(marker, fmt); |
| 70 res = _vsnprintf_s(post_pref_msg, kVarMsgSize, _TRUNCATE, fmt, marker); |
| 71 if (-1 != res) { |
| 72 /* |
| 73 * Sends a string to the debugger by raising OUTPUT_DEBUG_STRING_EVENT. |
| 74 * OutputDebugString() is a member of Windows Debugging API: |
| 75 * http://msdn.microsoft.com/en-us/library/aa363362%28v=VS.85%29.aspx |
| 76 * |
| 77 * It's conceptually eqvivalent to: |
| 78 * ::RaiseException(MY_OWN_EXCEPTION_CODE, 0, 1, &msg); |
| 79 * The difference is, raising MY_OWN_EXCEPTION_CODE can cause program |
| 80 * to crash (if there's no VEH handler and no debugger; or can cause |
| 81 * debugger to stop program (if debugger does not know about |
| 82 * MY_OWN_EXCEPTION_CODE). |
| 83 * |
| 84 * On the contrary, using OutputDebugString() is safe, cc from MSDN page: |
| 85 * "If the application has no debugger and the system debugger is not |
| 86 * active, OutputDebugString does nothing." |
| 87 */ |
| 88 OutputDebugStringA(msg); |
| 89 } else { |
| 90 NaClLog(LOG_FATAL, |
| 91 "SendMessageToDebuggerAndHalt: _vsnprintf_s returned -1\n"); |
| 92 } |
| 93 } |
| 94 #undef kVarMsgSize |
| 95 } |
| 96 /* Example of captured sel_ldr -> debugger messages: |
| 97 |
| 98 11.62454796 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ |
| 99 -event AppCreate -nap 00000000001CD3F0 -mem_start 0000000C00000000 \ |
| 100 -user_entry_pt 0000000000020080 -initial_entry_pt 0000000008000080 |
| 101 11.62480354 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ |
| 102 -event ThreadCreate -natp 0000000002304C50 |
| 103 ... |
| 104 23.96814728 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ |
| 105 -event ThreadExit -natp 0000000002304C50 -exit_code 1 |
| 106 23.96838570 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ |
| 107 -event AppExit -exit_code 1 |
| 108 */ |
| 109 |
| OLD | NEW |