temporary added to source tree
[public/netxms.git] / src / server / core / debug.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: debug.cpp
20**
21**/
22
23#include "nxcore.h"
24
25#ifdef _WIN32
26#include <dbghelp.h>
27#endif
28
29
30//
31// Test mutex state and print to stdout
32//
33
34void DbgTestMutex(MUTEX hMutex, const TCHAR *szName, CONSOLE_CTX pCtx)
35{
36 ConsolePrintf(pCtx, _T(" %s: "), szName);
37 if (MutexLock(hMutex, 100))
38 {
39 ConsolePrintf(pCtx, _T("unlocked\n"));
40 MutexUnlock(hMutex);
41 }
42 else
43 {
44 ConsolePrintf(pCtx, _T("locked\n"));
45 }
46}
47
48
49//
50// Test read/write lock state and print to stdout
51//
52
53void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX pCtx)
54{
55 ConsolePrintf(pCtx, _T(" %s: "), szName);
56 if (RWLockWriteLock(hLock, 100))
57 {
58 ConsolePrintf(pCtx, _T("unlocked\n"));
59 RWLockUnlock(hLock);
60 }
61 else
62 {
63 if (RWLockReadLock(hLock, 100))
64 {
65 ConsolePrintf(pCtx, _T("locked for reading\n"));
66 RWLockUnlock(hLock);
67 }
68 else
69 {
70 ConsolePrintf(pCtx, _T("locked for writing\n"));
71 }
72 }
73}
74
75
76//
77// Print message to console, either local or remote
78//
79
35f836fe 80void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...)
5039dede
AK
81{
82 va_list args;
83
84 va_start(args, pszFormat);
200d662d 85 if ((pCtx->hSocket == -1) && (pCtx->session == NULL))
5039dede 86 {
35f836fe 87 _vtprintf(pszFormat, args);
5039dede
AK
88 }
89 else
90 {
5039dede
AK
91 TCHAR szBuffer[8192];
92
93 _vsntprintf(szBuffer, 8191, pszFormat, args);
94 szBuffer[8191] = 0;
95 pCtx->pMsg->SetVariable(VID_MESSAGE, szBuffer);
200d662d
VK
96 if (pCtx->session != NULL)
97 {
98 pCtx->session->sendMessage(pCtx->pMsg);
99 }
100 else
101 {
102 CSCP_MESSAGE *pRawMsg = pCtx->pMsg->CreateMessage();
103 SendEx(pCtx->hSocket, pRawMsg, ntohl(pRawMsg->dwSize), 0);
104 free(pRawMsg);
105 }
5039dede
AK
106 }
107 va_end(args);
108}
109
110
111//
112// Show server statistics
113//
114
115void ShowServerStats(CONSOLE_CTX pCtx)
116{
117 DWORD i, dwNumItems;
118
119 RWLockReadLock(g_rwlockNodeIndex, INFINITE);
120 for(i = 0, dwNumItems = 0; i < g_dwNodeAddrIndexSize; i++)
7c521895 121 dwNumItems += ((Node *)g_pNodeIndexByAddr[i].pObject)->getItemCount();
5039dede
AK
122 RWLockUnlock(g_rwlockNodeIndex);
123
35f836fe
VK
124 ConsolePrintf(pCtx, _T("Total number of objects: %d\n")
125 _T("Number of monitored nodes: %d\n")
126 _T("Number of collectable DCIs: %d\n\n"),
5039dede
AK
127 g_dwIdIndexSize, g_dwNodeAddrIndexSize, dwNumItems);
128}
129
130
131//
132// Show queue stats
133//
134
35f836fe 135void ShowQueueStats(CONSOLE_CTX pCtx, Queue *pQueue, const TCHAR *pszName)
5039dede
AK
136{
137 if (pQueue != NULL)
35f836fe 138 ConsolePrintf(pCtx, _T("%-32s : %d\n"), pszName, pQueue->Size());
5039dede
AK
139}
140
141
142//
143// Write process coredump
144//
145
146#ifdef _WIN32
147
148void DumpProcess(CONSOLE_CTX pCtx)
149{
35f836fe 150 STARTUPINFOA si;
5039dede 151 PROCESS_INFORMATION pi;
35f836fe 152 char cmdLine[64];
5039dede 153
35f836fe 154 ConsolePrintf(pCtx, _T("Dumping process to disk...\n"));
5039dede
AK
155
156 sprintf(cmdLine, "netxmsd.exe --dump %d", GetCurrentProcessId());
157 memset(&si, 0, sizeof(STARTUPINFO));
158 si.cb = sizeof(STARTUPINFO);
35f836fe
VK
159 if (CreateProcessA(NULL, cmdLine, NULL, NULL, FALSE,
160 (g_dwFlags & AF_DAEMON) ? CREATE_NO_WINDOW : 0, NULL, NULL, &si, &pi))
5039dede
AK
161 {
162 WaitForSingleObject(pi.hProcess, INFINITE);
163 CloseHandle(pi.hThread);
164 CloseHandle(pi.hProcess);
165
35f836fe 166 ConsolePrintf(pCtx, _T("Done.\n"));
5039dede
AK
167 }
168 else
169 {
35f836fe
VK
170 TCHAR buffer[256];
171 ConsolePrintf(pCtx, _T("Dump error: CreateProcess() failed (%s)\n"), GetSystemErrorText(GetLastError(), buffer, 256));
5039dede
AK
172 }
173}
174
175#else
176
177void DumpProcess(CONSOLE_CTX pCtx)
178{
179 ConsolePrintf(pCtx, "DUMP command is not supported for current operating system\n");
180}
181
182#endif