2 * IRC - Internet Relay Chat, ircd/s_debug.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 1, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "ircd_alloc.h"
30 #include "ircd_osdep.h"
31 #include "ircd_reply.h"
49 #include <stddef.h> /* offsetof */
57 * Option string. Must be before #ifdef DEBUGMODE.
60 #if BUFFERPOOL < 1000000
62 #if BUFFERPOOL > 99999
63 (char)('0' + (BUFFERPOOL/100000)),
66 (char)('0' + (BUFFERPOOL/10000) % 10),
68 (char)('0' + (BUFFERPOOL/1000) % 10),
71 #if BUFFERPOOL > 99999999
72 (char)('0' + (BUFFERPOOL/100000000)),
74 #if BUFFERPOOL > 9999999
75 (char)('0' + (BUFFERPOOL/10000000) % 10),
77 (char)('0' + (BUFFERPOOL/1000000) % 10),
97 #ifdef OPER_NO_CHAN_LIMIT
100 #ifdef OPER_MODE_LCHAN
106 #if defined(SHOW_INVISIBLE_USERS) || defined(SHOW_ALL_INVISIBLE_USERS)
107 #ifdef SHOW_ALL_INVISIBLE_USERS
114 #ifdef LOCAL_KILL_ONLY
123 #ifdef OPER_WALK_THROUGH_LMODES
132 #ifdef NO_OPER_DEOP_LCHAN
135 #ifdef CRYPT_OPER_PASSWORD
138 #ifdef CRYPT_LINK_PASSWORD
148 #ifdef RELIABLE_CLOCK
160 #if defined(USE_POLL) && defined(HAVE_POLL_H)
181 * If the -t option is not given on the command line when the server is
182 * started, all debugging output is sent to the file set by LPATH in config.h
183 * Here we just open that file and make sure it is opened to fd 2 so that
184 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
185 * set from the command line by -x, use /dev/null as the dummy logfile as long
186 * as DEBUGMODE has been defined, else dont waste the fd.
188 void debug_init(int use_tty)
191 if (debuglevel >= 0) {
192 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
193 log_debug_init(use_tty ? 0 : LOGFILE);
199 void vdebug(int level, const char *form, va_list vl)
204 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
207 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
213 void debug(int level, const char *form, ...)
217 vdebug(level, form, vl);
221 static void debug_enumerator(struct Client* cptr, const char* msg)
224 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
228 * This is part of the STATS replies. There is no offical numeric for this
229 * since this isnt an official command, in much the same way as HASH isnt.
230 * It is also possible that some systems wont support this call or have
231 * different field names for "struct rusage".
234 void send_usage(struct Client *cptr, char *nick)
236 os_get_rusage(cptr, CurrentTime - me.since, debug_enumerator);
238 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
239 DBufAllocCount, DBufUsedCount);
241 #endif /* DEBUGMODE */
243 void count_memory(struct Client *cptr, char *nick)
245 struct Client *acptr;
247 struct Channel *chptr;
248 struct ConfItem *aconf;
249 const struct ConnectionClass* cltmp;
250 struct Membership* member;
252 int lc = 0, /* local clients */
253 ch = 0, /* channels */
254 lcc = 0, /* local client conf links */
255 rc = 0, /* remote clients */
256 us = 0, /* user structs */
257 chi = 0, /* channel invites */
258 chb = 0, /* channel bans */
259 wwu = 0, /* whowas users */
260 cl = 0, /* classes */
261 co = 0, /* conf lines */
262 memberships = 0; /* channel memberships */
264 int usi = 0, /* users invited */
265 aw = 0, /* aways set */
266 wwa = 0; /* whowas aways */
268 size_t chm = 0, /* memory used by channels */
269 chbm = 0, /* memory used by channel bans */
270 lcm = 0, /* memory used by local clients */
271 rcm = 0, /* memory used by remote clients */
272 awm = 0, /* memory used by aways */
273 wwam = 0, /* whowas away memory used */
274 wwm = 0, /* whowas array memory used */
275 com = 0, /* memory used by conf lines */
276 dbufs_allocated = 0, /* memory used by dbufs */
277 dbufs_used = 0, /* memory used by dbufs */
278 msg_allocated = 0, /* memory used by struct Msg */
279 msg_used = 0, /* memory used by struct Msg */
280 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
281 msgbuf_used = 0, /* memory used by struct MsgBuf */
282 rm = 0, /* res memory used */
283 totcl = 0, totch = 0, totww = 0, tot = 0;
285 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
286 wwm += sizeof(struct Whowas) * NICKNAMEHISTORYLENGTH;
287 wwm += sizeof(struct Whowas *) * WW_MAX;
289 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
291 if (MyConnect(acptr))
294 for (link = acptr->confs; link; link = link->next)
302 for (link = acptr->user->invited; link; link = link->next)
304 for (member = acptr->user->channel; member; member = member->next_channel)
306 if (acptr->user->away)
309 awm += (strlen(acptr->user->away) + 1);
313 lcm = lc * CLIENT_LOCAL_SIZE;
314 rcm = rc * CLIENT_REMOTE_SIZE;
316 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
319 chm += (strlen(chptr->chname) + sizeof(struct Channel));
322 * XXX - Members already counted in clients, don't count twice
324 for (member = chptr->members; member; member = member->next_member)
327 for (link = chptr->invites; link; link = link->next)
329 for (link = chptr->banlist; link; link = link->next)
332 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
336 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
339 com += aconf->host ? strlen(aconf->host) + 1 : 0;
340 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
341 com += aconf->name ? strlen(aconf->name) + 1 : 0;
342 com += sizeof(struct ConfItem);
345 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
348 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
349 ":Client Local %d(%zu) Remote %d(%zu)", lc, lcm, rc, rcm);
350 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
351 ":Users %d(%zu) Invites %d(%zu)", us, us * sizeof(struct User),
352 usi, usi * sizeof(struct SLink));
353 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
354 ":User channels %d(%zu) Aways %d(%zu)", memberships,
355 memberships * sizeof(struct Membership), aw, awm);
356 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
357 lcc, lcc * sizeof(struct SLink));
359 totcl = lcm + rcm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
360 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
362 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
365 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
366 cl * sizeof(struct ConnectionClass));
368 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
369 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
370 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
371 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
372 memberships * sizeof(struct Membership), chi,
373 chi * sizeof(struct SLink));
375 totch = chm + chbm + chi * sizeof(struct SLink);
377 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
378 ":Whowas users %d(%zu) away %d(%zu)", wwu,
379 wwu * sizeof(struct User), wwa, wwam);
380 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
381 NICKNAMEHISTORYLENGTH, wwm);
383 totww = wwu * sizeof(struct User) + wwam + wwm;
385 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
386 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
387 sizeof(void *) * HASHSIZE);
390 * NOTE: this count will be accurate only for the exact instant that this
391 * message is being sent, so the count is affected by the dbufs that
392 * are being used to send this message out. If this is not desired, move
393 * the dbuf_count_memory call to a place before we start sending messages
394 * and cache DBufAllocCount and DBufUsedCount in variables until they
397 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
398 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
399 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
400 dbufs_allocated, DBufUsedCount, dbufs_used);
402 msgq_count_memory(&msg_allocated, &msg_used, &msgbuf_allocated,
404 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
405 ":Msgs allocated %d(%zu) used %d(%zu)", msgCounts.alloc,
406 msg_allocated, msgCounts.used, msg_used);
407 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
408 ":MsgBufs allocated %d(%zu) used %d(%zu)", msgBufCounts.alloc,
409 msgbuf_allocated, msgBufCounts.used, msgbuf_used);
414 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
415 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
416 tot += sizeof(void *) * HASHSIZE * 3;
419 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
420 fda_get_block_count(), fda_get_byte_count());
423 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
424 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
425 totww, totch, totcl, com, dbufs_allocated, msg_allocated,