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.
30 #include "ircd_alloc.h"
31 #include "ircd_features.h"
33 #include "ircd_osdep.h"
34 #include "ircd_reply.h"
52 #include <stddef.h> /* offsetof */
58 * Option string. Must be before #ifdef DEBUGMODE.
60 static char serveropts[256]; /* should be large enough for anything */
62 const char* debug_serveropts(void)
66 #define AddC(c) serveropts[i++] = (c)
68 bp = feature_int(FEAT_BUFFERPOOL);
72 AddC((char)('0' + (bp / 100000)));
74 AddC((char)('0' + (bp / 10000) % 10));
75 AddC((char)('0' + (bp / 1000) % 10));
79 AddC((char)('0' + (bp / 100000000)));
81 AddC((char)('0' + (bp / 10000000) % 10));
82 AddC((char)('0' + (bp / 1000000) % 10));
92 if (feature_bool(FEAT_LOCOP_REHASH))
95 if (feature_bool(FEAT_OPER_REHASH))
98 if (feature_bool(FEAT_OPER_NO_CHAN_LIMIT))
101 if (feature_bool(FEAT_OPER_MODE_LCHAN))
104 if (feature_bool(FEAT_HUB))
107 if (feature_bool(FEAT_SHOW_ALL_INVISIBLE_USERS))
109 else if (feature_bool(FEAT_SHOW_INVISIBLE_USERS))
112 if (feature_bool(FEAT_OPER_KILL)) {
113 if (feature_bool(FEAT_LOCAL_KILL_ONLY))
119 if (feature_bool(FEAT_OPER_WALK_THROUGH_LMODES))
122 if (feature_bool(FEAT_IDLE_FROM_MSG))
125 if (feature_bool(FEAT_NO_OPER_DEOP_LCHAN))
128 if (feature_bool(FEAT_CRYPT_OPER_PASSWORD))
131 if (feature_bool(FEAT_RELIABLE_CLOCK))
134 if (feature_bool(FEAT_LOCOP_RESTART))
137 if (feature_bool(FEAT_OPER_RESTART))
140 #if defined(USE_POLL) && defined(HAVE_POLL_H)
144 if (feature_bool(FEAT_VIRTUAL_HOST))
147 serveropts[i] = '\0';
155 * If the -t option is not given on the command line when the server is
156 * started, all debugging output is sent to the file set by LPATH in config.h
157 * Here we just open that file and make sure it is opened to fd 2 so that
158 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
159 * set from the command line by -x, use /dev/null as the dummy logfile as long
160 * as DEBUGMODE has been defined, else dont waste the fd.
162 void debug_init(int use_tty)
165 if (debuglevel >= 0) {
166 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
167 log_debug_init(use_tty);
173 void vdebug(int level, const char *form, va_list vl)
178 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
181 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
187 void debug(int level, const char *form, ...)
191 vdebug(level, form, vl);
195 static void debug_enumerator(struct Client* cptr, const char* msg)
198 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
202 * This is part of the STATS replies. There is no offical numeric for this
203 * since this isnt an official command, in much the same way as HASH isnt.
204 * It is also possible that some systems wont support this call or have
205 * different field names for "struct rusage".
208 void send_usage(struct Client *cptr, struct StatDesc *sd, int stat,
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, struct StatDesc *sd, int stat,
221 struct Client *acptr;
223 struct Channel *chptr;
224 struct ConfItem *aconf;
225 const struct ConnectionClass* cltmp;
226 struct Membership* member;
228 int acc = 0, /* accounts */
230 cn = 0, /* connections */
231 ch = 0, /* channels */
232 lcc = 0, /* local client conf links */
233 us = 0, /* user structs */
234 chi = 0, /* channel invites */
235 chb = 0, /* channel bans */
236 wwu = 0, /* whowas users */
237 cl = 0, /* classes */
238 co = 0, /* conf lines */
239 memberships = 0; /* channel memberships */
241 int usi = 0, /* users invited */
242 aw = 0, /* aways set */
243 wwa = 0, /* whowas aways */
247 size_t chm = 0, /* memory used by channels */
248 chbm = 0, /* memory used by channel bans */
249 cm = 0, /* memory used by clients */
250 cnm = 0, /* memory used by connections */
251 awm = 0, /* memory used by aways */
252 wwam = 0, /* whowas away memory used */
253 wwm = 0, /* whowas array memory used */
254 glm = 0, /* memory used by glines */
255 jum = 0, /* memory used by jupes */
256 com = 0, /* memory used by conf lines */
257 dbufs_allocated = 0, /* memory used by dbufs */
258 dbufs_used = 0, /* memory used by dbufs */
259 msg_allocated = 0, /* memory used by struct Msg */
260 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
261 rm = 0, /* res memory used */
262 totcl = 0, totch = 0, totww = 0, tot = 0;
264 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
265 wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
266 wwm += sizeof(struct Whowas *) * WW_MAX;
268 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
271 if (MyConnect(acptr))
274 for (link = cli_confs(acptr); link; link = link->next)
280 for (link = cli_user(acptr)->invited; link; link = link->next)
282 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
284 if (cli_user(acptr)->away)
287 awm += (strlen(cli_user(acptr)->away) + 1);
291 if (IsAccount(acptr))
294 cm = c * sizeof(struct Client);
295 cnm = cn * sizeof(struct Connection);
297 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
300 chm += (strlen(chptr->chname) + sizeof(struct Channel));
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) Accounts %d(%zu) Invites %d(%zu)",
326 us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
327 usi, usi * sizeof(struct SLink));
328 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
329 ":User channels %d(%zu) Aways %d(%zu)", memberships,
330 memberships * sizeof(struct Membership), aw, awm);
331 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
332 lcc, lcc * sizeof(struct SLink));
334 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
335 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
337 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
340 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
341 cl * sizeof(struct ConnectionClass));
343 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
344 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
345 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
346 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
347 memberships * sizeof(struct Membership), chi,
348 chi * sizeof(struct SLink));
350 totch = chm + chbm + chi * sizeof(struct SLink);
352 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
353 ":Whowas users %d(%zu) away %d(%zu)", wwu,
354 wwu * sizeof(struct User), wwa, wwam);
355 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
356 feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
358 totww = wwu * sizeof(struct User) + wwam + wwm;
360 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
361 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
362 sizeof(void *) * HASHSIZE);
365 * NOTE: this count will be accurate only for the exact instant that this
366 * message is being sent, so the count is affected by the dbufs that
367 * are being used to send this message out. If this is not desired, move
368 * the dbuf_count_memory call to a place before we start sending messages
369 * and cache DBufAllocCount and DBufUsedCount in variables until they
372 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
373 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
374 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
375 dbufs_allocated, DBufUsedCount, dbufs_used);
377 msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
382 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
383 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
384 tot += sizeof(void *) * HASHSIZE * 3;
387 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
388 fda_get_block_count(), fda_get_byte_count());
391 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
392 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
393 totww, totch, totcl, com, dbufs_allocated, msg_allocated,