fixed event storm on object delete; changed behaviour of SYS_SUBNET_ADDED
[public/netxms.git] / src / server / core / debug.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
534e1b83 3** Copyright (C) 2003-2012 Victor Kirhenshtein
5039dede
AK
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
534e1b83
VK
29/**
30 * Test read/write lock state and print to stdout
31 */
5039dede
AK
32void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX pCtx)
33{
34 ConsolePrintf(pCtx, _T(" %s: "), szName);
35 if (RWLockWriteLock(hLock, 100))
36 {
37 ConsolePrintf(pCtx, _T("unlocked\n"));
38 RWLockUnlock(hLock);
39 }
40 else
41 {
42 if (RWLockReadLock(hLock, 100))
43 {
44 ConsolePrintf(pCtx, _T("locked for reading\n"));
45 RWLockUnlock(hLock);
46 }
47 else
48 {
49 ConsolePrintf(pCtx, _T("locked for writing\n"));
50 }
51 }
52}
53
534e1b83
VK
54/**
55 * Print message to console, either local or remote
56 */
35f836fe 57void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...)
5039dede
AK
58{
59 va_list args;
f669df41 60 TCHAR szBuffer[8192];
5039dede
AK
61
62 va_start(args, pszFormat);
f669df41
VK
63 _vsntprintf(szBuffer, 8191, pszFormat, args);
64 szBuffer[8191] = 0;
65 va_end(args);
66
200d662d 67 if ((pCtx->hSocket == -1) && (pCtx->session == NULL))
5039dede 68 {
f669df41 69 WriteToTerminal(szBuffer);
5039dede
AK
70 }
71 else
72 {
5039dede 73 pCtx->pMsg->SetVariable(VID_MESSAGE, szBuffer);
200d662d
VK
74 if (pCtx->session != NULL)
75 {
d3a7cf4c 76 pCtx->session->postMessage(pCtx->pMsg);
200d662d
VK
77 }
78 else
79 {
80 CSCP_MESSAGE *pRawMsg = pCtx->pMsg->CreateMessage();
d3a7cf4c 81 SendEx(pCtx->hSocket, pRawMsg, ntohl(pRawMsg->dwSize), 0, pCtx->socketMutex);
200d662d
VK
82 free(pRawMsg);
83 }
5039dede 84 }
5039dede
AK
85}
86
534e1b83
VK
87/**
88 * Show server statistics
89 */
9d605305 90static void DciCountCallback(NetObj *object, void *data)
5039dede 91{
9d605305
VK
92 *((int *)data) += (int)((Node *)object)->getItemCount();
93}
5039dede 94
9d605305
VK
95void ShowServerStats(CONSOLE_CTX pCtx)
96{
97 int dciCount = 0;
98 g_idxNodeById.forEach(DciCountCallback, &dciCount);
35f836fe
VK
99 ConsolePrintf(pCtx, _T("Total number of objects: %d\n")
100 _T("Number of monitored nodes: %d\n")
101 _T("Number of collectable DCIs: %d\n\n"),
9d605305 102 g_idxObjectById.getSize(), g_idxNodeById.getSize(), dciCount);
5039dede
AK
103}
104
534e1b83
VK
105/**
106 * Show queue stats
107 */
35f836fe 108void ShowQueueStats(CONSOLE_CTX pCtx, Queue *pQueue, const TCHAR *pszName)
5039dede
AK
109{
110 if (pQueue != NULL)
35f836fe 111 ConsolePrintf(pCtx, _T("%-32s : %d\n"), pszName, pQueue->Size());
5039dede
AK
112}
113
534e1b83
VK
114/**
115 * Write process coredump
116 */
5039dede
AK
117#ifdef _WIN32
118
119void DumpProcess(CONSOLE_CTX pCtx)
120{
35f836fe 121 STARTUPINFOA si;
5039dede 122 PROCESS_INFORMATION pi;
35f836fe 123 char cmdLine[64];
5039dede 124
35f836fe 125 ConsolePrintf(pCtx, _T("Dumping process to disk...\n"));
5039dede
AK
126
127 sprintf(cmdLine, "netxmsd.exe --dump %d", GetCurrentProcessId());
128 memset(&si, 0, sizeof(STARTUPINFO));
129 si.cb = sizeof(STARTUPINFO);
35f836fe
VK
130 if (CreateProcessA(NULL, cmdLine, NULL, NULL, FALSE,
131 (g_dwFlags & AF_DAEMON) ? CREATE_NO_WINDOW : 0, NULL, NULL, &si, &pi))
5039dede
AK
132 {
133 WaitForSingleObject(pi.hProcess, INFINITE);
134 CloseHandle(pi.hThread);
135 CloseHandle(pi.hProcess);
136
35f836fe 137 ConsolePrintf(pCtx, _T("Done.\n"));
5039dede
AK
138 }
139 else
140 {
35f836fe
VK
141 TCHAR buffer[256];
142 ConsolePrintf(pCtx, _T("Dump error: CreateProcess() failed (%s)\n"), GetSystemErrorText(GetLastError(), buffer, 256));
5039dede
AK
143 }
144}
145
146#else
147
148void DumpProcess(CONSOLE_CTX pCtx)
149{
b07c50cc 150 ConsolePrintf(pCtx, _T("DUMP command is not supported for current operating system\n"));
5039dede
AK
151}
152
153#endif