1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // The memory_watcher.dll is hooked by simply linking it. When we get the
6 // windows notification that this DLL is loaded, we do a few things:
7 // 1) Register a Hot Key.
8 // Only one process can hook the Hot Key, so one will get it, and the
9 // others will silently fail.
10 // 2) Create a thread to wait on an event.
11 // Since only one process will get the HotKey, it will be responsible for
12 // notifying all process when it's time to do something. Each process
13 // will have a thread waiting for communication from the master to dump
18 #include "base/at_exit.h"
19 #include "tools/memory_watcher/memory_watcher.h"
20 #include "tools/memory_watcher/hotkey.h"
22 class MemoryWatcherDumpKey
; // Defined below.
24 static wchar_t* kDumpEvent
= L
"MemWatcher.DumpEvent";
25 static base::AtExitManager
* g_memory_watcher_exit_manager
= NULL
;
26 static MemoryWatcher
* g_memory_watcher
= NULL
;
27 static MemoryWatcherDumpKey
* g_hotkey_handler
= NULL
;
28 static HANDLE g_dump_event
= INVALID_HANDLE_VALUE
;
29 static HANDLE g_quit_event
= INVALID_HANDLE_VALUE
;
30 static HANDLE g_watcher_thread
= INVALID_HANDLE_VALUE
;
32 // A HotKey to dump the memory statistics.
33 class MemoryWatcherDumpKey
: public HotKeyHandler
{
35 MemoryWatcherDumpKey(UINT modifiers
, UINT vkey
)
36 : HotKeyHandler(modifiers
, vkey
) {}
38 virtual void OnHotKey(UINT
, WPARAM
, LPARAM
) {
39 SetEvent(g_dump_event
);
43 // Creates the global memory watcher.
44 void CreateMemoryWatcher() {
45 g_memory_watcher_exit_manager
= new base::AtExitManager();
46 g_memory_watcher
= new MemoryWatcher();
47 // Register ALT-CONTROL-D to Dump Memory stats.
48 g_hotkey_handler
= new MemoryWatcherDumpKey(MOD_ALT
|MOD_CONTROL
, 0x44);
51 // Deletes the global memory watcher.
52 void DeleteMemoryWatcher() {
54 delete g_hotkey_handler
;
55 g_hotkey_handler
= NULL
;
57 delete g_memory_watcher
;
58 g_memory_watcher
= NULL
;
60 // Intentionly leak g_memory_watcher_exit_manager.
63 // Thread for watching for key events.
64 DWORD WINAPI
ThreadMain(LPVOID
) {
65 bool stopping
= false;
66 HANDLE events
[2] = { g_dump_event
, g_quit_event
};
68 DWORD rv
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
71 if (g_memory_watcher
) {
72 g_memory_watcher
->DumpLeaks();
76 case WAIT_OBJECT_0
+ 1:
87 // Creates the background thread
88 void CreateBackgroundThread() {
89 // Create a named event which can be used to notify
90 // all watched processes.
91 g_dump_event
= CreateEvent(0, TRUE
, FALSE
, kDumpEvent
);
92 DCHECK(g_dump_event
!= NULL
);
94 // Create a local event which can be used to kill our
96 g_quit_event
= CreateEvent(0, TRUE
, FALSE
, NULL
);
97 DCHECK(g_quit_event
!= NULL
);
99 // Create the background thread.
100 g_watcher_thread
= CreateThread(0,
106 DCHECK(g_watcher_thread
!= NULL
);
109 // Tell the background thread to stop.
110 void StopBackgroundThread() {
111 // Send notification to our background thread.
112 SetEvent(g_quit_event
);
114 // Wait for our background thread to die.
115 DWORD rv
= WaitForSingleObject(g_watcher_thread
, INFINITE
);
116 DCHECK(rv
== WAIT_OBJECT_0
);
118 // Cleanup our global handles.
119 CloseHandle(g_quit_event
);
120 CloseHandle(g_dump_event
);
121 CloseHandle(g_watcher_thread
);
125 return GetModuleHandleA("chrome.exe") != NULL
;
129 // DllMain is the windows entry point to this DLL.
130 // We use the entry point as the mechanism for starting and stopping
131 // the MemoryWatcher.
132 BOOL WINAPI
DllMain(HINSTANCE dll_instance
, DWORD reason
,
138 case DLL_PROCESS_ATTACH
:
139 CreateMemoryWatcher();
140 CreateBackgroundThread();
142 case DLL_PROCESS_DETACH
:
143 DeleteMemoryWatcher();
144 StopBackgroundThread();
150 __declspec(dllexport
) void __cdecl
SetLogName(char* name
) {
151 g_memory_watcher
->SetLogName(name
);