15ea2f547f28f8acdefb9ddd6cf1cf7f4a8334d1
[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
113   serveropts[i] = '\0';
114
115   return serveropts;
116 }
117
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().
126  */
127 void debug_init(int use_tty)
128 {
129 #ifdef  DEBUGMODE
130   if (debuglevel >= 0) {
131     printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
132     log_debug_init(use_tty);
133   }
134 #endif
135 }
136
137 #ifdef DEBUGMODE
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.
143  */
144 void vdebug(int level, const char *form, va_list vl)
145 {
146   static int loop = 0;
147   int err = errno;
148
149   if (!loop && (debuglevel >= 0) && (level <= debuglevel))
150   {
151     loop = 1;
152     log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
153     loop = 0;
154   }
155   errno = err;
156 }
157
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.
162  */
163 void debug(int level, const char *form, ...)
164 {
165   va_list vl;
166   va_start(vl, form);
167   vdebug(level, form, vl);
168   va_end(vl);
169 }
170
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.
174  */
175 static void debug_enumerator(struct Client* cptr, const char* msg)
176 {
177   assert(0 != cptr);
178   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
179 }
180
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).
185  */
186 void send_usage(struct Client *cptr, const struct StatDesc *sd,
187                 char *param)
188 {
189   os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
190
191   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
192              DBufAllocCount, DBufUsedCount);
193 }
194 #endif /* DEBUGMODE */
195
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).
200  */
201 void count_memory(struct Client *cptr, const struct StatDesc *sd,
202                   char *param)
203 {
204   struct Client *acptr;
205   struct SLink *link;
206   struct Ban *ban;
207   struct Channel *chptr;
208   struct ConfItem *aconf;
209   const struct ConnectionClass* cltmp;
210   struct Membership* member;
211
212   int acc = 0,                  /* accounts */
213       c = 0,                    /* clients */
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 */
224
225   int usi = 0,                  /* users invited */
226       aw = 0,                   /* aways set */
227       wwa = 0,                  /* whowas aways */
228       gl = 0,                   /* glines */
229       ju = 0;                   /* jupes */
230
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;
247
248   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
249   wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
250   wwm += sizeof(struct Whowas *) * WW_MAX;
251
252   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
253   {
254     c++;
255     if (MyConnect(acptr))
256     {
257       cn++;
258       for (link = cli_confs(acptr); link; link = link->next)
259         lcc++;
260     }
261     if (cli_user(acptr))
262     {
263       us++;
264       for (link = cli_user(acptr)->invited; link; link = link->next)
265         usi++;
266       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
267         ++memberships;
268       if (cli_user(acptr)->away)
269       {
270         aw++;
271         awm += (strlen(cli_user(acptr)->away) + 1);
272       }
273     }
274
275     if (IsAccount(acptr))
276       acc++;
277   }
278   cm = c * sizeof(struct Client);
279   cnm = cn * sizeof(struct Connection);
280
281   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
282   {
283     ch++;
284     chm += (strlen(chptr->chname) + sizeof(struct Channel));
285     for (link = chptr->invites; link; link = link->next)
286       chi++;
287     for (ban = chptr->banlist; link; ban = ban->next)
288     {
289       chb++;
290       chbm += strlen(ban->who) + strlen(ban->banstr) + 2 + sizeof(*ban);
291     }
292   }
293
294   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
295   {
296     co++;
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);
301   }
302
303   for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
304     cl++;
305
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));
317
318   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
319   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
320
321   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
322              com);
323
324   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
325              cl * sizeof(struct ConnectionClass));
326
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));
333
334   totch = chm + chbm + chi * sizeof(struct SLink);
335
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);
341
342   totww = wwu * sizeof(struct User) + wwam + wwm;
343
344   motd_memory_count(cptr);
345
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);
350
351   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
352              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
353              sizeof(void *) * HASHSIZE);
354
355   /*
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 
361    * are sent.
362    */
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);
367
368   /* The DBuf caveats now count for this, but this routine now sends
369    * replies all on its own.
370    */
371   msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
372
373   rm = cres_mem(cptr);
374
375   tot =
376       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
377       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
378   tot += sizeof(void *) * HASHSIZE * 3;
379
380 #if defined(MDEBUG)
381   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
382              fda_get_block_count(), fda_get_byte_count());
383 #endif
384
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,
388              msgbuf_allocated);
389 }
390