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));
90 if (feature_bool(FEAT_LOCOP_REHASH))
93 if (feature_bool(FEAT_OPER_REHASH))
96 if (feature_bool(FEAT_OPER_NO_CHAN_LIMIT))
99 if (feature_bool(FEAT_OPER_MODE_LCHAN))
102 if (feature_bool(FEAT_HUB))
105 if (feature_bool(FEAT_SHOW_ALL_INVISIBLE_USERS))
107 else if (feature_bool(FEAT_SHOW_INVISIBLE_USERS))
110 if (feature_bool(FEAT_OPER_KILL)) {
111 if (feature_bool(FEAT_LOCAL_KILL_ONLY))
117 if (feature_bool(FEAT_OPER_WALK_THROUGH_LMODES))
120 if (feature_bool(FEAT_IDLE_FROM_MSG))
123 if (feature_bool(FEAT_NO_OPER_DEOP_LCHAN))
126 if (feature_bool(FEAT_CRYPT_OPER_PASSWORD))
129 if (feature_bool(FEAT_RELIABLE_CLOCK))
132 if (feature_bool(FEAT_LOCOP_RESTART))
135 if (feature_bool(FEAT_OPER_RESTART))
138 #if defined(USE_POLL) && defined(HAVE_POLL_H)
142 if (feature_bool(FEAT_VIRTUAL_HOST))
145 serveropts[i] = '\0';
153 * If the -t option is not given on the command line when the server is
154 * started, all debugging output is sent to the file set by LPATH in config.h
155 * Here we just open that file and make sure it is opened to fd 2 so that
156 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
157 * set from the command line by -x, use /dev/null as the dummy logfile as long
158 * as DEBUGMODE has been defined, else dont waste the fd.
160 void debug_init(int use_tty)
163 if (debuglevel >= 0) {
164 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
165 log_debug_init(use_tty);
171 void vdebug(int level, const char *form, va_list vl)
176 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
179 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
185 void debug(int level, const char *form, ...)
189 vdebug(level, form, vl);
193 static void debug_enumerator(struct Client* cptr, const char* msg)
196 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
200 * This is part of the STATS replies. There is no offical numeric for this
201 * since this isnt an official command, in much the same way as HASH isnt.
202 * It is also possible that some systems wont support this call or have
203 * different field names for "struct rusage".
206 void send_usage(struct Client *cptr, char *nick)
208 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
210 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
211 DBufAllocCount, DBufUsedCount);
213 #endif /* DEBUGMODE */
215 void count_memory(struct Client *cptr, char *nick)
217 struct Client *acptr;
219 struct Channel *chptr;
220 struct ConfItem *aconf;
221 const struct ConnectionClass* cltmp;
222 struct Membership* member;
224 int c = 0, /* clients */
225 cn = 0, /* connections */
226 ch = 0, /* channels */
227 lcc = 0, /* local client conf links */
228 us = 0, /* user structs */
229 chi = 0, /* channel invites */
230 chb = 0, /* channel bans */
231 wwu = 0, /* whowas users */
232 cl = 0, /* classes */
233 co = 0, /* conf lines */
234 memberships = 0; /* channel memberships */
236 int usi = 0, /* users invited */
237 aw = 0, /* aways set */
238 wwa = 0; /* whowas aways */
240 size_t chm = 0, /* memory used by channels */
241 chbm = 0, /* memory used by channel bans */
242 cm = 0, /* memory used by clients */
243 cnm = 0, /* memory used by connections */
244 awm = 0, /* memory used by aways */
245 wwam = 0, /* whowas away memory used */
246 wwm = 0, /* whowas array memory used */
247 com = 0, /* memory used by conf lines */
248 dbufs_allocated = 0, /* memory used by dbufs */
249 dbufs_used = 0, /* memory used by dbufs */
250 msg_allocated = 0, /* memory used by struct Msg */
251 msg_used = 0, /* memory used by struct Msg */
252 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
253 msgbuf_used = 0, /* memory used by struct MsgBuf */
254 rm = 0, /* res memory used */
255 totcl = 0, totch = 0, totww = 0, tot = 0;
257 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
258 wwm += sizeof(struct Whowas) * NICKNAMEHISTORYLENGTH;
259 wwm += sizeof(struct Whowas *) * WW_MAX;
261 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
264 if (MyConnect(acptr))
267 for (link = cli_confs(acptr); link; link = link->next)
273 for (link = cli_user(acptr)->invited; link; link = link->next)
275 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
277 if (cli_user(acptr)->away)
280 awm += (strlen(cli_user(acptr)->away) + 1);
284 cm = c * sizeof(struct Client);
285 cnm = cn * sizeof(struct Connection);
287 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
290 chm += (strlen(chptr->chname) + sizeof(struct Channel));
293 * XXX - Members already counted in clients, don't count twice
295 for (member = chptr->members; member; member = member->next_member)
298 for (link = chptr->invites; link; link = link->next)
300 for (link = chptr->banlist; link; link = link->next)
303 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
307 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
310 com += aconf->host ? strlen(aconf->host) + 1 : 0;
311 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
312 com += aconf->name ? strlen(aconf->name) + 1 : 0;
313 com += sizeof(struct ConfItem);
316 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
319 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
320 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
321 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
322 ":Users %d(%zu) Invites %d(%zu)", us, us * sizeof(struct User),
323 usi, usi * sizeof(struct SLink));
324 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
325 ":User channels %d(%zu) Aways %d(%zu)", memberships,
326 memberships * sizeof(struct Membership), aw, awm);
327 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
328 lcc, lcc * sizeof(struct SLink));
330 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
331 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
333 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
336 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
337 cl * sizeof(struct ConnectionClass));
339 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
340 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
341 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
342 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
343 memberships * sizeof(struct Membership), chi,
344 chi * sizeof(struct SLink));
346 totch = chm + chbm + chi * sizeof(struct SLink);
348 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
349 ":Whowas users %d(%zu) away %d(%zu)", wwu,
350 wwu * sizeof(struct User), wwa, wwam);
351 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
352 NICKNAMEHISTORYLENGTH, wwm);
354 totww = wwu * sizeof(struct User) + wwam + wwm;
356 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
357 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
358 sizeof(void *) * HASHSIZE);
361 * NOTE: this count will be accurate only for the exact instant that this
362 * message is being sent, so the count is affected by the dbufs that
363 * are being used to send this message out. If this is not desired, move
364 * the dbuf_count_memory call to a place before we start sending messages
365 * and cache DBufAllocCount and DBufUsedCount in variables until they
368 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
369 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
370 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
371 dbufs_allocated, DBufUsedCount, dbufs_used);
373 msgq_count_memory(&msg_allocated, &msg_used, &msgbuf_allocated,
375 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
376 ":Msgs allocated %d(%zu) used %d(%zu)", msgCounts.alloc,
377 msg_allocated, msgCounts.used, msg_used);
378 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
379 ":MsgBufs allocated %d(%zu) used %d(%zu)", msgBufCounts.alloc,
380 msgbuf_allocated, msgBufCounts.used, msgbuf_used);
385 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
386 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
387 tot += sizeof(void *) * HASHSIZE * 3;
390 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
391 fda_get_block_count(), fda_get_byte_count());
394 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
395 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
396 totww, totch, totcl, com, dbufs_allocated, msg_allocated,