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