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.
31 #include "ircd_alloc.h"
32 #include "ircd_features.h"
34 #include "ircd_osdep.h"
35 #include "ircd_reply.h"
56 #include <stddef.h> /* offsetof */
62 * Option string. Must be before #ifdef DEBUGMODE.
64 static char serveropts[256]; /* should be large enough for anything */
66 const char* debug_serveropts(void)
70 #define AddC(c) serveropts[i++] = (c)
72 bp = feature_int(FEAT_BUFFERPOOL);
76 AddC((char)('0' + (bp / 100000)));
78 AddC((char)('0' + (bp / 10000) % 10));
79 AddC((char)('0' + (bp / 1000) % 10));
83 AddC((char)('0' + (bp / 100000000)));
85 AddC((char)('0' + (bp / 10000000) % 10));
86 AddC((char)('0' + (bp / 1000000) % 10));
96 if (feature_bool(FEAT_HUB))
99 if (feature_bool(FEAT_IDLE_FROM_MSG))
102 if (feature_bool(FEAT_RELIABLE_CLOCK))
105 #if defined(USE_POLL) && defined(HAVE_POLL_H)
109 if (feature_bool(FEAT_VIRTUAL_HOST))
112 serveropts[i] = '\0';
120 * If the -t option is not given on the command line when the server is
121 * started, all debugging output is sent to the file set by LPATH in config.h
122 * Here we just open that file and make sure it is opened to fd 2 so that
123 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
124 * set from the command line by -x, use /dev/null as the dummy logfile as long
125 * as DEBUGMODE has been defined, else dont waste the fd.
127 void debug_init(int use_tty)
130 if (debuglevel >= 0) {
131 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
132 log_debug_init(use_tty);
138 void vdebug(int level, const char *form, va_list vl)
143 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
146 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
152 void debug(int level, const char *form, ...)
156 vdebug(level, form, vl);
160 static void debug_enumerator(struct Client* cptr, const char* msg)
163 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
167 * This is part of the STATS replies. There is no offical numeric for this
168 * since this isnt an official command, in much the same way as HASH isnt.
169 * It is also possible that some systems wont support this call or have
170 * different field names for "struct rusage".
173 void send_usage(struct Client *cptr, struct StatDesc *sd, int stat,
176 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
178 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
179 DBufAllocCount, DBufUsedCount);
181 #endif /* DEBUGMODE */
183 void count_memory(struct Client *cptr, struct StatDesc *sd, int stat,
186 struct Client *acptr;
188 struct Channel *chptr;
189 struct ConfItem *aconf;
190 const struct ConnectionClass* cltmp;
191 struct Membership* member;
193 int acc = 0, /* accounts */
195 cn = 0, /* connections */
196 ch = 0, /* channels */
197 lcc = 0, /* local client conf links */
198 us = 0, /* user structs */
199 chi = 0, /* channel invites */
200 chb = 0, /* channel bans */
201 wwu = 0, /* whowas users */
202 cl = 0, /* classes */
203 co = 0, /* conf lines */
204 memberships = 0; /* channel memberships */
206 int usi = 0, /* users invited */
207 aw = 0, /* aways set */
208 wwa = 0, /* whowas aways */
212 size_t chm = 0, /* memory used by channels */
213 chbm = 0, /* memory used by channel bans */
214 cm = 0, /* memory used by clients */
215 cnm = 0, /* memory used by connections */
216 awm = 0, /* memory used by aways */
217 wwam = 0, /* whowas away memory used */
218 wwm = 0, /* whowas array memory used */
219 glm = 0, /* memory used by glines */
220 jum = 0, /* memory used by jupes */
221 com = 0, /* memory used by conf lines */
222 dbufs_allocated = 0, /* memory used by dbufs */
223 dbufs_used = 0, /* memory used by dbufs */
224 msg_allocated = 0, /* memory used by struct Msg */
225 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
226 rm = 0, /* res memory used */
227 totcl = 0, totch = 0, totww = 0, tot = 0;
229 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
230 wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
231 wwm += sizeof(struct Whowas *) * WW_MAX;
233 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
236 if (MyConnect(acptr))
239 for (link = cli_confs(acptr); link; link = link->next)
245 for (link = cli_user(acptr)->invited; link; link = link->next)
247 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
249 if (cli_user(acptr)->away)
252 awm += (strlen(cli_user(acptr)->away) + 1);
256 if (IsAccount(acptr))
259 cm = c * sizeof(struct Client);
260 cnm = cn * sizeof(struct Connection);
262 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
265 chm += (strlen(chptr->chname) + sizeof(struct Channel));
266 for (link = chptr->invites; link; link = link->next)
268 for (link = chptr->banlist; link; link = link->next)
271 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
275 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
278 com += aconf->host ? strlen(aconf->host) + 1 : 0;
279 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
280 com += aconf->name ? strlen(aconf->name) + 1 : 0;
281 com += sizeof(struct ConfItem);
284 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
287 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
288 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
289 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
290 ":Users %d(%zu) Accounts %d(%zu) Invites %d(%zu)",
291 us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
292 usi, usi * sizeof(struct SLink));
293 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
294 ":User channels %d(%zu) Aways %d(%zu)", memberships,
295 memberships * sizeof(struct Membership), aw, awm);
296 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
297 lcc, lcc * sizeof(struct SLink));
299 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
300 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
302 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
305 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
306 cl * sizeof(struct ConnectionClass));
308 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
309 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
310 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
311 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
312 memberships * sizeof(struct Membership), chi,
313 chi * sizeof(struct SLink));
315 totch = chm + chbm + chi * sizeof(struct SLink);
317 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
318 ":Whowas users %d(%zu) away %d(%zu)", wwu,
319 wwu * sizeof(struct User), wwa, wwam);
320 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
321 feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
323 totww = wwu * sizeof(struct User) + wwam + wwm;
325 motd_memory_count(cptr);
327 gl = gline_memory_count(&glm);
328 ju = jupe_memory_count(&jum);
329 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
330 ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
332 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
333 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
334 sizeof(void *) * HASHSIZE);
337 * NOTE: this count will be accurate only for the exact instant that this
338 * message is being sent, so the count is affected by the dbufs that
339 * are being used to send this message out. If this is not desired, move
340 * the dbuf_count_memory call to a place before we start sending messages
341 * and cache DBufAllocCount and DBufUsedCount in variables until they
344 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
345 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
346 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
347 dbufs_allocated, DBufUsedCount, dbufs_used);
349 /* The DBuf caveats now count for this, but this routine now sends
350 * replies all on its own.
352 msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
357 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
358 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
359 tot += sizeof(void *) * HASHSIZE * 3;
362 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
363 fda_get_block_count(), fda_get_byte_count());
366 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
367 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
368 totww, totch, totcl, com, dbufs_allocated, msg_allocated,