Chromium Code Reviews| 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 | |
| 7 #include "native_client/src/trusted/service_runtime/nacl_oop_debugger_hooks.h" | |
| 8 #include "native_client/src/trusted/service_runtime/sel_ldr.h" | |
| 9 | |
| 10 #ifdef NACL_WINDOWS | |
| 11 /* | |
| 12 * Note: this function does not do anything if program | |
| 13 * is not running under debugger. | |
| 14 * If program (sel_ldr) is running under debugger, calling this function | |
| 15 * gives debugger a chance to modify state of the debuggee before | |
| 16 * resuming debuggee execution. | |
| 17 */ | |
| 18 void SendMessageToDebuggerAndHalt(const char *fmt, ...) | |
| 19 ATTRIBUTE_FORMAT_PRINTF(1, 2); | |
| 20 | |
| 21 void NaClOopDebuggerAppCreateHook(struct NaClApp *nap) { | |
| 22 if (NULL == nap) | |
| 23 return; | |
| 24 SendMessageToDebuggerAndHalt( | |
| 25 "-event AppCreate -nap %p -mem_start %p -user_entry_pt %p " | |
| 26 "-initial_entry_pt %p", | |
| 27 nap, | |
| 28 (void *) nap->mem_start, | |
| 29 (void *) nap->user_entry_pt, | |
| 30 (void *) nap->initial_entry_pt); | |
| 31 } | |
| 32 | |
| 33 void NaClOopDebuggerThreadCreateHook(struct NaClAppThread *natp) { | |
| 34 SendMessageToDebuggerAndHalt("-event ThreadCreate -natp %p", natp); | |
| 35 } | |
| 36 | |
| 37 void NaClOopDebuggerThreadExitHook(struct NaClAppThread *natp, int exit_code) { | |
| 38 SendMessageToDebuggerAndHalt("-event ThreadExit -natp %p -exit_code %d", | |
| 39 natp, | |
| 40 exit_code); | |
| 41 } | |
| 42 | |
| 43 void NaClOopDebuggerAppExitHook(int exit_code) { | |
| 44 SendMessageToDebuggerAndHalt("-event AppExit -exit_code %d", exit_code); | |
| 45 } | |
| 46 | |
| 47 #else | |
| 48 /* placeholders for OOP debugger hooks on other platforms */ | |
| 49 void NaClOopDebuggerAppCreateHook(struct NaClApp *nap) { | |
| 50 UNREFERENCED_PARAMETER(nap); | |
| 51 } | |
| 52 | |
| 53 void NaClOopDebuggerThreadCreateHook(struct NaClAppThread *natp, | |
| 54 int exit_code) { | |
| 55 UNREFERENCED_PARAMETER(natp); | |
| 56 UNREFERENCED_PARAMETER(exit_code); | |
| 57 } | |
| 58 void NaClOopDebuggerThreadExitHook(struct NaClAppThread *natp, | |
| 59 int exit_code) { | |
| 60 UNREFERENCED_PARAMETER(natp); | |
| 61 UNREFERENCED_PARAMETER(exit_code); | |
| 62 } | |
| 63 | |
| 64 void NaClOopDebuggerAppExitHook(int exit_code) { | |
| 65 } | |
| 66 #endif | |
| 67 | |
| 68 #ifdef NACL_WINDOWS | |
| 69 void SendMessageToDebuggerAndHalt(const char *fmt, ...) { | |
| 70 #define kVarMsgSize 2048 | |
| 71 /* | |
| 72 * Determines whether the calling process is being debugged by a debugger. | |
| 73 * IsDebuggerPresent() is a member of Windows Debugging API: | |
| 74 * http://msdn.microsoft.com/en-us/library/ms680345%28v=VS.85%29.aspx | |
| 75 */ | |
| 76 if (IsDebuggerPresent()) { | |
| 77 /* | |
| 78 * Prefix has GUID string specific to our OOP debugger, so that it | |
| 79 * can differentiate it from other uses of 'OutputDebugString'. | |
| 80 */ | |
| 81 char prefix[] = "{7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 "; | |
|
bsy
2011/06/13 21:23:42
shouldn't this be char const prefix[] = "..."? sh
garianov1
2011/06/13 21:41:06
Done.
| |
| 82 char msg[sizeof(prefix) + kVarMsgSize]; | |
|
bsy
2011/06/13 21:23:42
note that trusted stack size used to be 32KB in si
garianov1
2011/06/13 21:41:06
Changed kVarMsgSize to 512, it should be plenty st
| |
| 83 char *post_pref_msg = msg + sizeof(prefix) - 1; | |
| 84 signed int res = 0; | |
| 85 va_list marker; | |
| 86 | |
| 87 strcpy(msg, prefix); | |
| 88 va_start(marker, fmt); | |
| 89 res = _vsnprintf_s(post_pref_msg, kVarMsgSize, _TRUNCATE, fmt, marker); | |
|
bsy
2011/06/13 21:23:42
msg is sizeof(prefix) + kVarMsgSize in size, but p
garianov1
2011/06/13 21:41:06
Done.
| |
| 90 if (-1 != res) { | |
| 91 /* | |
| 92 * Sends a string to the debugger by raising OUTPUT_DEBUG_STRING_EVENT. | |
| 93 * OutputDebugString() is a member of Windows Debugging API: | |
| 94 * http://msdn.microsoft.com/en-us/library/aa363362%28v=VS.85%29.aspx | |
| 95 * | |
| 96 * It's conceptually eqvivalent to: | |
| 97 * ::RaiseException(MY_OWN_EXCEPTION_CODE, 0, 1, &msg); | |
| 98 * The difference is, raising MY_OWN_EXCEPTION_CODE can cause program | |
| 99 * to crash (if there's no VEH handler and no debugger; or can cause | |
| 100 * debugger to stop program (if debugger does not know about | |
| 101 * MY_OWN_EXCEPTION_CODE). | |
| 102 * | |
| 103 * On the contrary, using OutputDebugString() is safe, cc from MSDN page: | |
| 104 * "If the application has no debugger and the system debugger is not | |
| 105 * active, OutputDebugString does nothing." | |
| 106 */ | |
| 107 OutputDebugStringA(msg); | |
| 108 } | |
|
bsy
2011/06/13 21:23:42
should this abort if the buffer couldn't hold the
garianov1
2011/06/13 21:41:06
Sending partial message is probably better than ab
| |
| 109 } | |
| 110 #undef kVarMsgSize | |
| 111 } | |
| 112 /* Example of captured sel_ldr -> debugger messages: | |
| 113 | |
| 114 11.62454796 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 115 -event AppCreate -nap 00000000001CD3F0 -mem_start 0000000C00000000 \ | |
| 116 -user_entry_pt 0000000000020080 -initial_entry_pt 0000000008000080 | |
| 117 11.62480354 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 118 -event ThreadCreate -natp 0000000002304C50 | |
| 119 ... | |
| 120 23.96814728 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 121 -event ThreadExit -natp 0000000002304C50 -exit_code 1 | |
| 122 23.96838570 [6060] {7AA7C9CF-89EC-4ed3-8DAD-6DC84302AB11} -version 1 \ | |
| 123 -event AppExit -exit_code 1 | |
| 124 */ | |
| 125 #endif | |
| 126 | |
| OLD | NEW |