Add '6' to server options when compiled with IPv6 support (and related
[ircu2.10.12-pk.git] / ircd / s_debug.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_debug.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
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)
9  * any later version.
10  *
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.
15  *
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.
19  */
20 /** @file
21  * @brief Debug support for the ircd.
22  * @version $Id$
23  */
24 #include "config.h"
25
26 #include "s_debug.h"
27 #include "channel.h"
28 #include "class.h"
29 #include "client.h"
30 #include "gline.h"
31 #include "hash.h"
32 #include "ircd_alloc.h"
33 #include "ircd_features.h"
34 #include "ircd_log.h"
35 #include "ircd_osdep.h"
36 #include "ircd_reply.h"
37 #include "ircd.h"
38 #include "jupe.h"
39 #include "list.h"
40 #include "motd.h"
41 #include "msgq.h"
42 #include "numeric.h"
43 #include "numnicks.h"
44 #include "res.h"
45 #include "s_bsd.h"
46 #include "s_conf.h"
47 #include "s_stats.h"
48 #include "send.h"
49 #include "struct.h"
50 #include "sys.h"
51 #include "whowas.h"
52
53 /* #include <assert.h> -- Now using assert in ircd_log.h */
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdarg.h>
57 #include <stddef.h>     /* offsetof */
58 #include <stdio.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 /*
63  * Option string.  Must be before #ifdef DEBUGMODE.
64  */
65 static char serveropts[256]; /* should be large enough for anything */
66
67 /** Return a string describing important configuration information.
68  * @return Pointer to a static buffer.
69  */
70 const char* debug_serveropts(void)
71 {
72   int bp;
73   int i = 0;
74 #define AddC(c) serveropts[i++] = (c)
75
76   bp = feature_int(FEAT_BUFFERPOOL);
77   if (bp < 1000000) {
78     AddC('b');
79     if (bp > 99999)
80       AddC((char)('0' + (bp / 100000)));
81     if (bp > 9999)
82       AddC((char)('0' + (bp / 10000) % 10));
83     AddC((char)('0' + (bp / 1000) % 10));
84   } else {
85     AddC('B');
86     if (bp > 99999999)
87       AddC((char)('0' + (bp / 100000000)));
88     if (bp > 9999999)
89       AddC((char)('0' + (bp / 10000000) % 10));
90     AddC((char)('0' + (bp / 1000000) % 10));
91   }
92
93 #ifndef NDEBUG
94   AddC('A');
95 #endif
96 #ifdef  DEBUGMODE
97   AddC('D');
98 #endif
99
100   if (feature_bool(FEAT_HUB))
101     AddC('H');
102
103   if (feature_bool(FEAT_IDLE_FROM_MSG))
104     AddC('M');
105
106   if (feature_bool(FEAT_RELIABLE_CLOCK))
107     AddC('R');
108
109 #if defined(USE_POLL) && defined(HAVE_POLL_H)
110   AddC('U');
111 #endif
112 #ifdef  IPV6
113   AddC('6');
114 #endif
115
116   serveropts[i] = '\0';
117
118   return serveropts;
119 }
120
121 /** Initialize debugging.
122  * If the -t option is not given on the command line when the server is
123  * started, all debugging output is sent to the file set by LPATH in config.h
124  * Here we just open that file and make sure it is opened to fd 2 so that
125  * any fprintf's to stderr also goto the logfile.  If the debuglevel is not
126  * set from the command line by -x, use /dev/null as the dummy logfile as long
127  * as DEBUGMODE has been defined, else dont waste the fd.
128  * @param use_tty Passed to log_debug_init().
129  */
130 void debug_init(int use_tty)
131 {
132 #ifdef  DEBUGMODE
133   if (debuglevel >= 0) {
134     printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
135     log_debug_init(use_tty);
136   }
137 #endif
138 }
139
140 #ifdef DEBUGMODE
141 /** Log a debug message using a va_list.
142  * If the current #debuglevel is less than \a level, do not display.
143  * @param level Debug level for message.
144  * @param form Format string, passed to log_vwrite().
145  * @param vl Varargs argument list for format string.
146  */
147 void vdebug(int level, const char *form, va_list vl)
148 {
149   static int loop = 0;
150   int err = errno;
151
152   if (!loop && (debuglevel >= 0) && (level <= debuglevel))
153   {
154     loop = 1;
155     log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
156     loop = 0;
157   }
158   errno = err;
159 }
160
161 /** Log a debug message using a variable number of arguments.
162  * This is a simple wrapper around debug(\a level, \a form, vl).
163  * @param level Debug level for message.
164  * @param form Format string of message.
165  */
166 void debug(int level, const char *form, ...)
167 {
168   va_list vl;
169   va_start(vl, form);
170   vdebug(level, form, vl);
171   va_end(vl);
172 }
173
174 /** Send a literal RPL_STATSDEBUG message to a user.
175  * @param cptr Client to receive the message.
176  * @param msg Text message to send to user.
177  */
178 static void debug_enumerator(struct Client* cptr, const char* msg)
179 {
180   assert(0 != cptr);
181   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
182 }
183
184 /** Send resource usage statistics to a client.
185  * @param cptr Client to send data to.
186  * @param sd StatDesc that generated the stats request (ignored).
187  * @param param Extra parameter from user (ignored).
188  */
189 void send_usage(struct Client *cptr, const struct StatDesc *sd,
190                 char *param)
191 {
192   os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
193
194   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
195              DBufAllocCount, DBufUsedCount);
196 }
197 #endif /* DEBUGMODE */
198
199 /** Report memory usage statistics to a client.
200  * @param cptr Client to send data to.
201  * @param sd StatDesc that generated the stats request (ignored).
202  * @param param Extra parameter from user (ignored).
203  */
204 void count_memory(struct Client *cptr, const struct StatDesc *sd,
205                   char *param)
206 {
207   struct Client *acptr;
208   struct SLink *link;
209   struct Ban *ban;
210   struct Channel *chptr;
211   struct ConfItem *aconf;
212   const struct ConnectionClass* cltmp;
213   struct Membership* member;
214
215   int acc = 0,                  /* accounts */
216       c = 0,                    /* clients */
217       cn = 0,                   /* connections */
218       ch = 0,                   /* channels */
219       lcc = 0,                  /* local client conf links */
220       us = 0,                   /* user structs */
221       chi = 0,                  /* channel invites */
222       chb = 0,                  /* channel bans */
223       wwu = 0,                  /* whowas users */
224       cl = 0,                   /* classes */
225       co = 0,                   /* conf lines */
226       memberships = 0;          /* channel memberships */
227
228   int usi = 0,                  /* users invited */
229       aw = 0,                   /* aways set */
230       wwa = 0,                  /* whowas aways */
231       gl = 0,                   /* glines */
232       ju = 0;                   /* jupes */
233
234   size_t chm = 0,               /* memory used by channels */
235       chbm = 0,                 /* memory used by channel bans */
236       cm = 0,                   /* memory used by clients */
237       cnm = 0,                  /* memory used by connections */
238       awm = 0,                  /* memory used by aways */
239       wwam = 0,                 /* whowas away memory used */
240       wwm = 0,                  /* whowas array memory used */
241       glm = 0,                  /* memory used by glines */
242       jum = 0,                  /* memory used by jupes */
243       com = 0,                  /* memory used by conf lines */
244       dbufs_allocated = 0,      /* memory used by dbufs */
245       dbufs_used = 0,           /* memory used by dbufs */
246       msg_allocated = 0,        /* memory used by struct Msg */
247       msgbuf_allocated = 0,     /* memory used by struct MsgBuf */
248       rm = 0,                   /* res memory used */
249       totcl = 0, totch = 0, totww = 0, tot = 0;
250
251   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
252   wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
253   wwm += sizeof(struct Whowas *) * WW_MAX;
254
255   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
256   {
257     c++;
258     if (MyConnect(acptr))
259     {
260       cn++;
261       for (link = cli_confs(acptr); link; link = link->next)
262         lcc++;
263     }
264     if (cli_user(acptr))
265     {
266       us++;
267       for (link = cli_user(acptr)->invited; link; link = link->next)
268         usi++;
269       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
270         ++memberships;
271       if (cli_user(acptr)->away)
272       {
273         aw++;
274         awm += (strlen(cli_user(acptr)->away) + 1);
275       }
276     }
277
278     if (IsAccount(acptr))
279       acc++;
280   }
281   cm = c * sizeof(struct Client);
282   cnm = cn * sizeof(struct Connection);
283
284   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
285   {
286     ch++;
287     chm += (strlen(chptr->chname) + sizeof(struct Channel));
288     for (link = chptr->invites; link; link = link->next)
289       chi++;
290     for (ban = chptr->banlist; link; ban = ban->next)
291     {
292       chb++;
293       chbm += strlen(ban->who) + strlen(ban->banstr) + 2 + sizeof(*ban);
294     }
295   }
296
297   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
298   {
299     co++;
300     com += aconf->host ? strlen(aconf->host) + 1 : 0;
301     com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
302     com += aconf->name ? strlen(aconf->name) + 1 : 0;
303     com += sizeof(struct ConfItem);
304   }
305
306   for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
307     cl++;
308
309   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
310              ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
311   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
312              ":Users %d(%zu) Accounts %d(%zu) Invites %d(%zu)",
313              us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
314              usi, usi * sizeof(struct SLink));
315   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
316              ":User channels %d(%zu) Aways %d(%zu)", memberships,
317              memberships * sizeof(struct Membership), aw, awm);
318   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
319              lcc, lcc * sizeof(struct SLink));
320
321   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
322   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
323
324   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
325              com);
326
327   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
328              cl * sizeof(struct ConnectionClass));
329
330   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
331              ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
332   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
333              ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
334              memberships * sizeof(struct Membership), chi,
335              chi * sizeof(struct SLink));
336
337   totch = chm + chbm + chi * sizeof(struct SLink);
338
339   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
340              ":Whowas users %d(%zu) away %d(%zu)", wwu,
341              wwu * sizeof(struct User), wwa, wwam);
342   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
343              feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
344
345   totww = wwu * sizeof(struct User) + wwam + wwm;
346
347   motd_memory_count(cptr);
348
349   gl = gline_memory_count(&glm);
350   ju = jupe_memory_count(&jum);
351   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
352              ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
353
354   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
355              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
356              sizeof(void *) * HASHSIZE);
357
358   /*
359    * NOTE: this count will be accurate only for the exact instant that this
360    * message is being sent, so the count is affected by the dbufs that
361    * are being used to send this message out. If this is not desired, move
362    * the dbuf_count_memory call to a place before we start sending messages
363    * and cache DBufAllocCount and DBufUsedCount in variables until they 
364    * are sent.
365    */
366   dbuf_count_memory(&dbufs_allocated, &dbufs_used);
367   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
368              ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
369              dbufs_allocated, DBufUsedCount, dbufs_used);
370
371   /* The DBuf caveats now count for this, but this routine now sends
372    * replies all on its own.
373    */
374   msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
375
376   rm = cres_mem(cptr);
377
378   tot =
379       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
380       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
381   tot += sizeof(void *) * HASHSIZE * 3;
382
383 #if defined(MDEBUG)
384   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
385              fda_get_block_count(), fda_get_byte_count());
386 #endif
387
388   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
389              ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
390              totww, totch, totcl, com, dbufs_allocated, msg_allocated,
391              msgbuf_allocated);
392 }
393