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, char *nick)
210 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
212 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
213 DBufAllocCount, DBufUsedCount);
215 #endif /* DEBUGMODE */
217 void count_memory(struct Client *cptr, char *nick)
219 struct Client *acptr;
221 struct Channel *chptr;
222 struct ConfItem *aconf;
223 const struct ConnectionClass* cltmp;
224 struct Membership* member;
226 int c = 0, /* clients */
227 cn = 0, /* connections */
228 ch = 0, /* channels */
229 lcc = 0, /* local client conf links */
230 us = 0, /* user structs */
231 chi = 0, /* channel invites */
232 chb = 0, /* channel bans */
233 wwu = 0, /* whowas users */
234 cl = 0, /* classes */
235 co = 0, /* conf lines */
236 memberships = 0; /* channel memberships */
238 int usi = 0, /* users invited */
239 aw = 0, /* aways set */
240 wwa = 0; /* whowas aways */
242 size_t chm = 0, /* memory used by channels */
243 chbm = 0, /* memory used by channel bans */
244 cm = 0, /* memory used by clients */
245 cnm = 0, /* memory used by connections */
246 awm = 0, /* memory used by aways */
247 wwam = 0, /* whowas away memory used */
248 wwm = 0, /* whowas array memory used */
249 com = 0, /* memory used by conf lines */
250 dbufs_allocated = 0, /* memory used by dbufs */
251 dbufs_used = 0, /* memory used by dbufs */
252 msg_allocated = 0, /* memory used by struct Msg */
253 msg_used = 0, /* memory used by struct Msg */
254 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
255 msgbuf_used = 0, /* memory used by struct MsgBuf */
256 rm = 0, /* res memory used */
257 totcl = 0, totch = 0, totww = 0, tot = 0;
259 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
260 wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
261 wwm += sizeof(struct Whowas *) * WW_MAX;
263 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
266 if (MyConnect(acptr))
269 for (link = cli_confs(acptr); link; link = link->next)
275 for (link = cli_user(acptr)->invited; link; link = link->next)
277 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
279 if (cli_user(acptr)->away)
282 awm += (strlen(cli_user(acptr)->away) + 1);
286 cm = c * sizeof(struct Client);
287 cnm = cn * sizeof(struct Connection);
289 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
292 chm += (strlen(chptr->chname) + sizeof(struct Channel));
293 for (link = chptr->invites; link; link = link->next)
295 for (link = chptr->banlist; link; link = link->next)
298 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
302 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
305 com += aconf->host ? strlen(aconf->host) + 1 : 0;
306 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
307 com += aconf->name ? strlen(aconf->name) + 1 : 0;
308 com += sizeof(struct ConfItem);
311 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
314 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
315 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
316 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
317 ":Users %d(%zu) Invites %d(%zu)", us, us * sizeof(struct User),
318 usi, usi * sizeof(struct SLink));
319 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
320 ":User channels %d(%zu) Aways %d(%zu)", memberships,
321 memberships * sizeof(struct Membership), aw, awm);
322 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
323 lcc, lcc * sizeof(struct SLink));
325 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
326 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
328 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
331 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
332 cl * sizeof(struct ConnectionClass));
334 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
335 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
336 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
337 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
338 memberships * sizeof(struct Membership), chi,
339 chi * sizeof(struct SLink));
341 totch = chm + chbm + chi * sizeof(struct SLink);
343 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
344 ":Whowas users %d(%zu) away %d(%zu)", wwu,
345 wwu * sizeof(struct User), wwa, wwam);
346 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
347 feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
349 totww = wwu * sizeof(struct User) + wwam + wwm;
351 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
352 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
353 sizeof(void *) * HASHSIZE);
356 * NOTE: this count will be accurate only for the exact instant that this
357 * message is being sent, so the count is affected by the dbufs that
358 * are being used to send this message out. If this is not desired, move
359 * the dbuf_count_memory call to a place before we start sending messages
360 * and cache DBufAllocCount and DBufUsedCount in variables until they
363 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
364 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
365 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
366 dbufs_allocated, DBufUsedCount, dbufs_used);
368 msgq_count_memory(&msg_allocated, &msg_used, &msgbuf_allocated,
370 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
371 ":Msgs allocated %d(%zu) used %d(%zu)", msgCounts.alloc,
372 msg_allocated, msgCounts.used, msg_used);
373 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
374 ":MsgBufs allocated %d(%zu) used %d(%zu)", msgBufCounts.alloc,
375 msgbuf_allocated, msgBufCounts.used, msgbuf_used);
380 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
381 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
382 tot += sizeof(void *) * HASHSIZE * 3;
385 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
386 fda_get_block_count(), fda_get_byte_count());
389 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
390 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
391 totww, totch, totcl, com, dbufs_allocated, msg_allocated,