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