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 /** Return a string describing important configuration information.
67 * @return Pointer to a static buffer.
69 const char* debug_serveropts(void)
73 #define AddC(c) serveropts[i++] = (c)
75 bp = feature_int(FEAT_BUFFERPOOL);
79 AddC((char)('0' + (bp / 100000)));
81 AddC((char)('0' + (bp / 10000) % 10));
82 AddC((char)('0' + (bp / 1000) % 10));
86 AddC((char)('0' + (bp / 100000000)));
88 AddC((char)('0' + (bp / 10000000) % 10));
89 AddC((char)('0' + (bp / 1000000) % 10));
99 if (feature_bool(FEAT_HUB))
102 if (feature_bool(FEAT_IDLE_FROM_MSG))
105 if (feature_bool(FEAT_RELIABLE_CLOCK))
108 #if defined(USE_POLL) && defined(HAVE_POLL_H)
112 serveropts[i] = '\0';
117 /** Initialize debugging.
118 * If the -t option is not given on the command line when the server is
119 * started, all debugging output is sent to the file set by LPATH in config.h
120 * Here we just open that file and make sure it is opened to fd 2 so that
121 * any fprintf's to stderr also goto the logfile. If the debuglevel is not
122 * set from the command line by -x, use /dev/null as the dummy logfile as long
123 * as DEBUGMODE has been defined, else dont waste the fd.
124 * @param use_tty Passed to log_debug_init().
126 void debug_init(int use_tty)
129 if (debuglevel >= 0) {
130 printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
131 log_debug_init(use_tty);
137 /** Log a debug message using a va_list.
138 * If the current #debuglevel is less than \a level, do not display.
139 * @param level Debug level for message.
140 * @param form Format string, passed to log_vwrite().
141 * @param vl Varargs argument list for format string.
143 void vdebug(int level, const char *form, va_list vl)
148 if (!loop && (debuglevel >= 0) && (level <= debuglevel))
151 log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
157 /** Log a debug message using a variable number of arguments.
158 * This is a simple wrapper around debug(\a level, \a form, vl).
159 * @param level Debug level for message.
160 * @param form Format string of message.
162 void debug(int level, const char *form, ...)
166 vdebug(level, form, vl);
170 /** Send a literal RPL_STATSDEBUG message to a user.
171 * @param cptr Client to receive the message.
172 * @param msg Text message to send to user.
174 static void debug_enumerator(struct Client* cptr, const char* msg)
177 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
180 /** Send resource usage statistics to a client.
181 * @param cptr Client to send data to.
182 * @param sd StatDesc that generated the stats request (ignored).
183 * @param param Extra parameter from user (ignored).
185 void send_usage(struct Client *cptr, const struct StatDesc *sd,
188 os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
190 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
191 DBufAllocCount, DBufUsedCount);
193 #endif /* DEBUGMODE */
195 /** Report memory usage statistics to a client.
196 * @param cptr Client to send data to.
197 * @param sd StatDesc that generated the stats request (ignored).
198 * @param param Extra parameter from user (ignored).
200 void count_memory(struct Client *cptr, const struct StatDesc *sd,
203 struct Client *acptr;
205 struct Channel *chptr;
206 struct ConfItem *aconf;
207 const struct ConnectionClass* cltmp;
208 struct Membership* member;
210 int acc = 0, /* accounts */
212 cn = 0, /* connections */
213 ch = 0, /* channels */
214 lcc = 0, /* local client conf links */
215 us = 0, /* user structs */
216 chi = 0, /* channel invites */
217 chb = 0, /* channel bans */
218 wwu = 0, /* whowas users */
219 cl = 0, /* classes */
220 co = 0, /* conf lines */
221 memberships = 0; /* channel memberships */
223 int usi = 0, /* users invited */
224 aw = 0, /* aways set */
225 wwa = 0, /* whowas aways */
229 size_t chm = 0, /* memory used by channels */
230 chbm = 0, /* memory used by channel bans */
231 cm = 0, /* memory used by clients */
232 cnm = 0, /* memory used by connections */
233 awm = 0, /* memory used by aways */
234 wwam = 0, /* whowas away memory used */
235 wwm = 0, /* whowas array memory used */
236 glm = 0, /* memory used by glines */
237 jum = 0, /* memory used by jupes */
238 com = 0, /* memory used by conf lines */
239 dbufs_allocated = 0, /* memory used by dbufs */
240 dbufs_used = 0, /* memory used by dbufs */
241 msg_allocated = 0, /* memory used by struct Msg */
242 msgbuf_allocated = 0, /* memory used by struct MsgBuf */
243 rm = 0, /* res memory used */
244 totcl = 0, totch = 0, totww = 0, tot = 0;
246 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
247 wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
248 wwm += sizeof(struct Whowas *) * WW_MAX;
250 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
253 if (MyConnect(acptr))
256 for (link = cli_confs(acptr); link; link = link->next)
262 for (link = cli_user(acptr)->invited; link; link = link->next)
264 for (member = cli_user(acptr)->channel; member; member = member->next_channel)
266 if (cli_user(acptr)->away)
269 awm += (strlen(cli_user(acptr)->away) + 1);
273 if (IsAccount(acptr))
276 cm = c * sizeof(struct Client);
277 cnm = cn * sizeof(struct Connection);
279 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
282 chm += (strlen(chptr->chname) + sizeof(struct Channel));
283 for (link = chptr->invites; link; link = link->next)
285 for (link = chptr->banlist; link; link = link->next)
288 chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
292 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
295 com += aconf->host ? strlen(aconf->host) + 1 : 0;
296 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
297 com += aconf->name ? strlen(aconf->name) + 1 : 0;
298 com += sizeof(struct ConfItem);
301 for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
304 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
305 ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
306 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
307 ":Users %d(%zu) Accounts %d(%zu) Invites %d(%zu)",
308 us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
309 usi, usi * sizeof(struct SLink));
310 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
311 ":User channels %d(%zu) Aways %d(%zu)", memberships,
312 memberships * sizeof(struct Membership), aw, awm);
313 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
314 lcc, lcc * sizeof(struct SLink));
316 totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
317 totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
319 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
322 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
323 cl * sizeof(struct ConnectionClass));
325 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
326 ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
327 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
328 ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
329 memberships * sizeof(struct Membership), chi,
330 chi * sizeof(struct SLink));
332 totch = chm + chbm + chi * sizeof(struct SLink);
334 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
335 ":Whowas users %d(%zu) away %d(%zu)", wwu,
336 wwu * sizeof(struct User), wwa, wwam);
337 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
338 feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
340 totww = wwu * sizeof(struct User) + wwam + wwm;
342 motd_memory_count(cptr);
344 gl = gline_memory_count(&glm);
345 ju = jupe_memory_count(&jum);
346 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
347 ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
349 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
350 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
351 sizeof(void *) * HASHSIZE);
354 * NOTE: this count will be accurate only for the exact instant that this
355 * message is being sent, so the count is affected by the dbufs that
356 * are being used to send this message out. If this is not desired, move
357 * the dbuf_count_memory call to a place before we start sending messages
358 * and cache DBufAllocCount and DBufUsedCount in variables until they
361 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
362 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
363 ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
364 dbufs_allocated, DBufUsedCount, dbufs_used);
366 /* The DBuf caveats now count for this, but this routine now sends
367 * replies all on its own.
369 msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
374 totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
375 dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
376 tot += sizeof(void *) * HASHSIZE * 3;
379 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
380 fda_get_block_count(), fda_get_byte_count());
383 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
384 ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
385 totww, totch, totcl, com, dbufs_allocated, msg_allocated,