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)
64 #define AddC(c) serveropts[i++] = (c)
66 bp = feature_int(FEAT_BUFFERPOOL);
70 AddC((char)('0' + (bp / 100000)));
72 AddC((char)('0' + (bp / 10000) % 10));
73 AddC((char)('0' + (bp / 1000) % 10));
77 AddC((char)('0' + (bp / 100000000)));
79 AddC((char)('0' + (bp / 10000000) % 10));
80 AddC((char)('0' + (bp / 1000000) % 10));
93 if (feature_bool(FEAT_LOCOP_REHASH))
96 if (feature_bool(FEAT_OPER_REHASH))
99 if (feature_bool(FEAT_OPER_NO_CHAN_LIMIT))
102 if (feature_bool(FEAT_OPER_MODE_LCHAN))
105 if (feature_bool(FEAT_HUB))
108 if (feature_bool(FEAT_SHOW_ALL_INVISIBLE_USERS))
110 else if (feature_bool(FEAT_SHOW_INVISIBLE_USERS))
113 if (feature_bool(FEAT_OPER_KILL)) {
114 if (feature_bool(FEAT_LOCAL_KILL_ONLY))
120 if (feature_bool(FEAT_OPER_WALK_THROUGH_LMODES))
123 if (feature_bool(FEAT_IDLE_FROM_MSG))
126 if (feature_bool(FEAT_NO_OPER_DEOP_LCHAN))
129 if (feature_bool(FEAT_CRYPT_OPER_PASSWORD))
132 if (feature_bool(FEAT_RELIABLE_CLOCK))
135 if (feature_bool(FEAT_LOCOP_RESTART))
138 if (feature_bool(FEAT_OPER_RESTART))
141 #if defined(USE_POLL) && defined(HAVE_POLL_H)
145 if (feature_bool(FEAT_VIRTUAL_HOST))
148 serveropts[i] = '\0';
156 * If the -t option is not given on the command line when the server is
157 * started, all debugging output is sent to the file set by LPATH in config.h
158 * Here we just open that file and make sure it is opened to fd 2 so that
159 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
160 * set from the command line by -x, use /dev/null as the dummy logfile as long
161 * as DEBUGMODE has been defined, else dont waste the fd.
163 void debug_init(int use_tty)
166 if (debuglevel >= 0) {
167 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
168 log_debug_init(use_tty);
174 void vdebug(int level, const char *form, va_list vl)
179 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
182 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
188 void debug(int level, const char *form, ...)
192 vdebug(level, form, vl);
196 static void debug_enumerator(struct Client* cptr, const char* msg)
199 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
203 * This is part of the STATS replies. There is no offical numeric for this
204 * since this isnt an official command, in much the same way as HASH isnt.
205 * It is also possible that some systems wont support this call or have
206 * different field names for "struct rusage".
209 void send_usage(struct Client *cptr, char *nick)
211 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
213 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
214 DBufAllocCount, DBufUsedCount);
216 #endif /* DEBUGMODE */
218 void count_memory(struct Client *cptr, char *nick)
220 struct Client *acptr;
222 struct Channel *chptr;
223 struct ConfItem *aconf;
224 const struct ConnectionClass* cltmp;
225 struct Membership* member;
227 int c = 0, /* clients */
228 cn = 0, /* connections */
229 ch = 0, /* channels */
230 lcc = 0, /* local client conf links */
231 us = 0, /* user structs */
232 chi = 0, /* channel invites */
233 chb = 0, /* channel bans */
234 wwu = 0, /* whowas users */
235 cl = 0, /* classes */
236 co = 0, /* conf lines */
237 memberships = 0; /* channel memberships */
239 int usi = 0, /* users invited */
240 aw = 0, /* aways set */
241 wwa = 0; /* whowas aways */
243 size_t chm = 0, /* memory used by channels */
244 chbm = 0, /* memory used by channel bans */
245 cm = 0, /* memory used by clients */
246 cnm = 0, /* memory used by connections */
247 awm = 0, /* memory used by aways */
248 wwam = 0, /* whowas away memory used */
249 wwm = 0, /* whowas array memory used */
250 com = 0, /* memory used by conf lines */
251 dbufs_allocated = 0, /* memory used by dbufs */
252 dbufs_used = 0, /* memory used by dbufs */
253 msg_allocated = 0, /* memory used by struct Msg */
254 msg_used = 0, /* memory used by struct Msg */
255 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
256 msgbuf_used = 0, /* memory used by struct MsgBuf */
257 rm = 0, /* res memory used */
258 totcl = 0, totch = 0, totww = 0, tot = 0;
260 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
261 wwm += sizeof(struct Whowas) * NICKNAMEHISTORYLENGTH;
262 wwm += sizeof(struct Whowas *) * WW_MAX;
264 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
267 if (MyConnect(acptr))
270 for (link = cli_confs(acptr); link; link = link->next)
276 for (link = cli_user(acptr)->invited; link; link = link->next)
278 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
280 if (cli_user(acptr)->away)
283 awm += (strlen(cli_user(acptr)->away) + 1);
287 cm = c * sizeof(struct Client);
288 cnm = cn * sizeof(struct Connection);
290 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
293 chm += (strlen(chptr->chname) + sizeof(struct Channel));
296 * XXX - Members already counted in clients, don't count twice
298 for (member = chptr->members; member; member = member->next_member)
301 for (link = chptr->invites; link; link = link->next)
303 for (link = chptr->banlist; link; link = link->next)
306 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
310 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
313 com += aconf->host ? strlen(aconf->host) + 1 : 0;
314 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
315 com += aconf->name ? strlen(aconf->name) + 1 : 0;
316 com += sizeof(struct ConfItem);
319 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
322 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
323 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
324 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
325 ":Users %d(%zu) Invites %d(%zu)", us, us * sizeof(struct User),
326 usi, usi * sizeof(struct SLink));
327 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
328 ":User channels %d(%zu) Aways %d(%zu)", memberships,
329 memberships * sizeof(struct Membership), aw, awm);
330 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
331 lcc, lcc * sizeof(struct SLink));
333 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
334 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
336 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
339 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
340 cl * sizeof(struct ConnectionClass));
342 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
343 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
344 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
345 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
346 memberships * sizeof(struct Membership), chi,
347 chi * sizeof(struct SLink));
349 totch = chm + chbm + chi * sizeof(struct SLink);
351 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
352 ":Whowas users %d(%zu) away %d(%zu)", wwu,
353 wwu * sizeof(struct User), wwa, wwam);
354 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
355 NICKNAMEHISTORYLENGTH, wwm);
357 totww = wwu * sizeof(struct User) + wwam + wwm;
359 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
360 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
361 sizeof(void *) * HASHSIZE);
364 * NOTE: this count will be accurate only for the exact instant that this
365 * message is being sent, so the count is affected by the dbufs that
366 * are being used to send this message out. If this is not desired, move
367 * the dbuf_count_memory call to a place before we start sending messages
368 * and cache DBufAllocCount and DBufUsedCount in variables until they
371 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
372 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
373 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
374 dbufs_allocated, DBufUsedCount, dbufs_used);
376 msgq_count_memory(&msg_allocated, &msg_used, &msgbuf_allocated,
378 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
379 ":Msgs allocated %d(%zu) used %d(%zu)", msgCounts.alloc,
380 msg_allocated, msgCounts.used, msg_used);
381 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
382 ":MsgBufs allocated %d(%zu) used %d(%zu)", msgBufCounts.alloc,
383 msgbuf_allocated, msgBufCounts.used, msgbuf_used);
388 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
389 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
390 tot += sizeof(void *) * HASHSIZE * 3;
393 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
394 fda_get_block_count(), fda_get_byte_count());
397 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
398 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
399 totww, totch, totcl, com, dbufs_allocated, msg_allocated,