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