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"
29 #include "ircd_features.h"
31 #include "ircd_osdep.h"
32 #include "ircd_reply.h"
50 #include <stddef.h> /* offsetof */
56 * Option string. Must be before #ifdef DEBUGMODE.
58 static char serveropts[256]; /* should be large enough for anything */
60 const char* debug_serveropts(void)
63 #define AddC(c) serveropts[i++] = (c)
65 #if BUFFERPOOL < 1000000
67 #if BUFFERPOOL > 99999
68 AddC((char)('0' + (BUFFERPOOL/100000)));
71 AddC((char)('0' + (BUFFERPOOL/10000) % 10));
73 AddC((char)('0' + (BUFFERPOOL/1000) % 10));
76 #if BUFFERPOOL > 99999999
77 AddC((char)('0' + (BUFFERPOOL/100000000)));
79 #if BUFFERPOOL > 9999999
80 AddC((char)('0' + (BUFFERPOOL/10000000) % 10));
82 AddC((char)('0' + (BUFFERPOOL/1000000) % 10));
94 if (feature_bool(FEAT_LOCOP_REHASH))
97 if (feature_bool(FEAT_OPER_REHASH))
100 if (feature_bool(FEAT_OPER_NO_CHAN_LIMIT))
103 if (feature_bool(FEAT_OPER_MODE_LCHAN))
110 if (feature_bool(FEAT_SHOW_ALL_INVISIBLE_USERS))
112 else if (feature_bool(FEAT_SHOW_INVISIBLE_USERS))
115 if (feature_bool(FEAT_OPER_KILL)) {
116 if (feature_bool(FEAT_LOCAL_KILL_ONLY))
122 if (feature_bool(FEAT_OPER_WALK_THROUGH_LMODES))
132 if (feature_bool(FEAT_NO_OPER_DEOP_LCHAN))
135 #ifdef CRYPT_OPER_PASSWORD
138 #ifdef CRYPT_LINK_PASSWORD
141 #ifdef RELIABLE_CLOCK
145 if (feature_bool(FEAT_LOCOP_RESTART))
148 if (feature_bool(FEAT_OPER_RESTART))
151 #if defined(USE_POLL) && defined(HAVE_POLL_H)
158 serveropts[i] = '\0';
166 * If the -t option is not given on the command line when the server is
167 * started, all debugging output is sent to the file set by LPATH in config.h
168 * Here we just open that file and make sure it is opened to fd 2 so that
169 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
170 * set from the command line by -x, use /dev/null as the dummy logfile as long
171 * as DEBUGMODE has been defined, else dont waste the fd.
173 void debug_init(int use_tty)
176 if (debuglevel >= 0) {
177 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
178 log_debug_init(use_tty);
184 void vdebug(int level, const char *form, va_list vl)
189 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
192 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
198 void debug(int level, const char *form, ...)
202 vdebug(level, form, vl);
206 static void debug_enumerator(struct Client* cptr, const char* msg)
209 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
213 * This is part of the STATS replies. There is no offical numeric for this
214 * since this isnt an official command, in much the same way as HASH isnt.
215 * It is also possible that some systems wont support this call or have
216 * different field names for "struct rusage".
219 void send_usage(struct Client *cptr, char *nick)
221 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
223 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
224 DBufAllocCount, DBufUsedCount);
226 #endif /* DEBUGMODE */
228 void count_memory(struct Client *cptr, char *nick)
230 struct Client *acptr;
232 struct Channel *chptr;
233 struct ConfItem *aconf;
234 const struct ConnectionClass* cltmp;
235 struct Membership* member;
237 int c = 0, /* clients */
238 cn = 0, /* connections */
239 ch = 0, /* channels */
240 lcc = 0, /* local client conf links */
241 us = 0, /* user structs */
242 chi = 0, /* channel invites */
243 chb = 0, /* channel bans */
244 wwu = 0, /* whowas users */
245 cl = 0, /* classes */
246 co = 0, /* conf lines */
247 memberships = 0; /* channel memberships */
249 int usi = 0, /* users invited */
250 aw = 0, /* aways set */
251 wwa = 0; /* whowas aways */
253 size_t chm = 0, /* memory used by channels */
254 chbm = 0, /* memory used by channel bans */
255 cm = 0, /* memory used by clients */
256 cnm = 0, /* memory used by connections */
257 awm = 0, /* memory used by aways */
258 wwam = 0, /* whowas away memory used */
259 wwm = 0, /* whowas array memory used */
260 com = 0, /* memory used by conf lines */
261 dbufs_allocated = 0, /* memory used by dbufs */
262 dbufs_used = 0, /* memory used by dbufs */
263 msg_allocated = 0, /* memory used by struct Msg */
264 msg_used = 0, /* memory used by struct Msg */
265 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
266 msgbuf_used = 0, /* memory used by struct MsgBuf */
267 rm = 0, /* res memory used */
268 totcl = 0, totch = 0, totww = 0, tot = 0;
270 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
271 wwm += sizeof(struct Whowas) * NICKNAMEHISTORYLENGTH;
272 wwm += sizeof(struct Whowas *) * WW_MAX;
274 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
277 if (MyConnect(acptr))
280 for (link = cli_confs(acptr); link; link = link->next)
286 for (link = cli_user(acptr)->invited; link; link = link->next)
288 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
290 if (cli_user(acptr)->away)
293 awm += (strlen(cli_user(acptr)->away) + 1);
297 cm = c * sizeof(struct Client);
298 cnm = cn * sizeof(struct Connection);
300 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
303 chm += (strlen(chptr->chname) + sizeof(struct Channel));
306 * XXX - Members already counted in clients, don't count twice
308 for (member = chptr->members; member; member = member->next_member)
311 for (link = chptr->invites; link; link = link->next)
313 for (link = chptr->banlist; link; link = link->next)
316 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
320 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
323 com += aconf->host ? strlen(aconf->host) + 1 : 0;
324 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
325 com += aconf->name ? strlen(aconf->name) + 1 : 0;
326 com += sizeof(struct ConfItem);
329 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
332 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
333 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
334 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
335 ":Users %d(%zu) Invites %d(%zu)", us, us * sizeof(struct User),
336 usi, usi * sizeof(struct SLink));
337 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
338 ":User channels %d(%zu) Aways %d(%zu)", memberships,
339 memberships * sizeof(struct Membership), aw, awm);
340 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
341 lcc, lcc * sizeof(struct SLink));
343 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
344 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
346 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
349 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
350 cl * sizeof(struct ConnectionClass));
352 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
353 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
354 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
355 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
356 memberships * sizeof(struct Membership), chi,
357 chi * sizeof(struct SLink));
359 totch = chm + chbm + chi * sizeof(struct SLink);
361 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
362 ":Whowas users %d(%zu) away %d(%zu)", wwu,
363 wwu * sizeof(struct User), wwa, wwam);
364 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
365 NICKNAMEHISTORYLENGTH, wwm);
367 totww = wwu * sizeof(struct User) + wwam + wwm;
369 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
370 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
371 sizeof(void *) * HASHSIZE);
374 * NOTE: this count will be accurate only for the exact instant that this
375 * message is being sent, so the count is affected by the dbufs that
376 * are being used to send this message out. If this is not desired, move
377 * the dbuf_count_memory call to a place before we start sending messages
378 * and cache DBufAllocCount and DBufUsedCount in variables until they
381 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
382 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
383 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
384 dbufs_allocated, DBufUsedCount, dbufs_used);
386 msgq_count_memory(&msg_allocated, &msg_used, &msgbuf_allocated,
388 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
389 ":Msgs allocated %d(%zu) used %d(%zu)", msgCounts.alloc,
390 msg_allocated, msgCounts.used, msg_used);
391 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
392 ":MsgBufs allocated %d(%zu) used %d(%zu)", msgBufCounts.alloc,
393 msgbuf_allocated, msgBufCounts.used, msgbuf_used);
398 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
399 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
400 tot += sizeof(void *) * HASHSIZE * 3;
403 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
404 fda_get_block_count(), fda_get_byte_count());
407 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
408 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
409 totww, totch, totcl, com, dbufs_allocated, msg_allocated,