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.
21 * @brief Debug support for the ircd.
32 #include "ircd_alloc.h"
33 #include "ircd_features.h"
35 #include "ircd_osdep.h"
36 #include "ircd_reply.h"
53 /* #include <assert.h> -- Now using assert in ircd_log.h */
57 #include <stddef.h> /* offsetof */
63 * Option string. Must be before #ifdef DEBUGMODE.
65 static char serveropts[256]; /* should be large enough for anything */
67 /** Return a string describing important configuration information.
68 * @return Pointer to a static buffer.
70 const char* debug_serveropts(void)
74 #define AddC(c) serveropts[i++] = (c)
76 bp = feature_int(FEAT_BUFFERPOOL);
80 AddC((char)('0' + (bp / 100000)));
82 AddC((char)('0' + (bp / 10000) % 10));
83 AddC((char)('0' + (bp / 1000) % 10));
87 AddC((char)('0' + (bp / 100000000)));
89 AddC((char)('0' + (bp / 10000000) % 10));
90 AddC((char)('0' + (bp / 1000000) % 10));
100 if (feature_bool(FEAT_HUB))
103 if (feature_bool(FEAT_IDLE_FROM_MSG))
106 if (feature_bool(FEAT_RELIABLE_CLOCK))
109 #if defined(USE_POLL) && defined(HAVE_POLL_H)
113 serveropts[i] = '\0';
118 /** Initialize debugging.
119 * If the -t option is not given on the command line when the server is
120 * started, all debugging output is sent to the file set by LPATH in config.h
121 * Here we just open that file and make sure it is opened to fd 2 so that
122 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
123 * set from the command line by -x, use /dev/null as the dummy logfile as long
124 * as DEBUGMODE has been defined, else dont waste the fd.
125 * @param use_tty Passed to log_debug_init().
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 /** Log a debug message using a va_list.
139 * If the current #debuglevel is less than \a level, do not display.
140 * @param level Debug level for message.
141 * @param form Format string, passed to log_vwrite().
142 * @param vl Varargs argument list for format string.
144 void vdebug(int level, const char *form, va_list vl)
149 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
152 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
158 /** Log a debug message using a variable number of arguments.
159 * This is a simple wrapper around debug(\a level, \a form, vl).
160 * @param level Debug level for message.
161 * @param form Format string of message.
163 void debug(int level, const char *form, ...)
167 vdebug(level, form, vl);
171 /** Send a literal RPL_STATSDEBUG message to a user.
172 * @param cptr Client to receive the message.
173 * @param msg Text message to send to user.
175 static void debug_enumerator(struct Client* cptr, const char* msg)
178 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
181 /** Send resource usage statistics to a client.
182 * @param cptr Client to send data to.
183 * @param sd StatDesc that generated the stats request (ignored).
184 * @param param Extra parameter from user (ignored).
186 void send_usage(struct Client *cptr, const struct StatDesc *sd,
189 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
191 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
192 DBufAllocCount, DBufUsedCount);
194 #endif /* DEBUGMODE */
196 /** Report memory usage statistics to a client.
197 * @param cptr Client to send data to.
198 * @param sd StatDesc that generated the stats request (ignored).
199 * @param param Extra parameter from user (ignored).
201 void count_memory(struct Client *cptr, const struct StatDesc *sd,
204 struct Client *acptr;
207 struct Channel *chptr;
208 struct ConfItem *aconf;
209 const struct ConnectionClass* cltmp;
210 struct Membership* member;
212 int acc = 0, /* accounts */
214 cn = 0, /* connections */
215 ch = 0, /* channels */
216 lcc = 0, /* local client conf links */
217 us = 0, /* user structs */
218 chi = 0, /* channel invites */
219 chb = 0, /* channel bans */
220 wwu = 0, /* whowas users */
221 cl = 0, /* classes */
222 co = 0, /* conf lines */
223 memberships = 0; /* channel memberships */
225 int usi = 0, /* users invited */
226 aw = 0, /* aways set */
227 wwa = 0, /* whowas aways */
231 size_t chm = 0, /* memory used by channels */
232 chbm = 0, /* memory used by channel bans */
233 cm = 0, /* memory used by clients */
234 cnm = 0, /* memory used by connections */
235 awm = 0, /* memory used by aways */
236 wwam = 0, /* whowas away memory used */
237 wwm = 0, /* whowas array memory used */
238 glm = 0, /* memory used by glines */
239 jum = 0, /* memory used by jupes */
240 com = 0, /* memory used by conf lines */
241 dbufs_allocated = 0, /* memory used by dbufs */
242 dbufs_used = 0, /* memory used by dbufs */
243 msg_allocated = 0, /* memory used by struct Msg */
244 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
245 rm = 0, /* res memory used */
246 totcl = 0, totch = 0, totww = 0, tot = 0;
248 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
249 wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
250 wwm += sizeof(struct Whowas *) * WW_MAX;
252 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
255 if (MyConnect(acptr))
258 for (link = cli_confs(acptr); link; link = link->next)
264 for (link = cli_user(acptr)->invited; link; link = link->next)
266 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
268 if (cli_user(acptr)->away)
271 awm += (strlen(cli_user(acptr)->away) + 1);
275 if (IsAccount(acptr))
278 cm = c * sizeof(struct Client);
279 cnm = cn * sizeof(struct Connection);
281 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
284 chm += (strlen(chptr->chname) + sizeof(struct Channel));
285 for (link = chptr->invites; link; link = link->next)
287 for (ban = chptr->banlist; link; ban = ban->next)
290 chbm += strlen(ban->who) + strlen(ban->banstr) + 2 + sizeof(*ban);
294 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
297 com += aconf->host ? strlen(aconf->host) + 1 : 0;
298 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
299 com += aconf->name ? strlen(aconf->name) + 1 : 0;
300 com += sizeof(struct ConfItem);
303 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
306 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
307 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
308 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
309 ":Users %d(%zu) Accounts %d(%zu) Invites %d(%zu)",
310 us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
311 usi, usi * sizeof(struct SLink));
312 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
313 ":User channels %d(%zu) Aways %d(%zu)", memberships,
314 memberships * sizeof(struct Membership), aw, awm);
315 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
316 lcc, lcc * sizeof(struct SLink));
318 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
319 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
321 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
324 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
325 cl * sizeof(struct ConnectionClass));
327 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
328 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
329 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
330 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
331 memberships * sizeof(struct Membership), chi,
332 chi * sizeof(struct SLink));
334 totch = chm + chbm + chi * sizeof(struct SLink);
336 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
337 ":Whowas users %d(%zu) away %d(%zu)", wwu,
338 wwu * sizeof(struct User), wwa, wwam);
339 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
340 feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
342 totww = wwu * sizeof(struct User) + wwam + wwm;
344 motd_memory_count(cptr);
346 gl = gline_memory_count(&glm);
347 ju = jupe_memory_count(&jum);
348 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
349 ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
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 /* The DBuf caveats now count for this, but this routine now sends
369 * replies all on its own.
371 msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
376 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
377 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
378 tot += sizeof(void *) * HASHSIZE * 3;
381 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
382 fda_get_block_count(), fda_get_byte_count());
385 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
386 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
387 totww, totch, totcl, com, dbufs_allocated, msg_allocated,