Author: Kev <klmitch@mit.edu>
[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  * $Id$
21  *
22  */
23 #include "s_debug.h"
24 #include "channel.h"
25 #include "class.h"
26 #include "client.h"
27 #include "hash.h"
28 #include "ircd_alloc.h"
29 #include "ircd_features.h"
30 #include "ircd_log.h"
31 #include "ircd_osdep.h"
32 #include "ircd_reply.h"
33 #include "ircd.h"
34 #include "list.h"
35 #include "msgq.h"
36 #include "numeric.h"
37 #include "numnicks.h"
38 #include "res.h"
39 #include "s_bsd.h"
40 #include "s_conf.h"
41 #include "send.h"
42 #include "struct.h"
43 #include "sys.h"
44 #include "whowas.h"
45
46 #include <assert.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stddef.h>     /* offsetof */
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 /*
56  * Option string.  Must be before #ifdef DEBUGMODE.
57  */
58 static char serveropts[256]; /* should be large enough for anything */
59
60 const char* debug_serveropts(void)
61 {
62   int bp;
63   int i = 0;
64 #define AddC(c) serveropts[i++] = (c)
65
66   bp = feature_int(FEAT_BUFFERPOOL);
67   if (bp < 1000000) {
68     AddC('b');
69     if (bp > 99999)
70       AddC((char)('0' + (bp / 100000)));
71     if (bp > 9999)
72       AddC((char)('0' + (bp / 10000) % 10));
73     AddC((char)('0' + (bp / 1000) % 10));
74   } else {
75     AddC('B');
76     if (bp > 99999999)
77       AddC((char)('0' + (bp / 100000000)));
78     if (bp > 9999999)
79       AddC((char)('0' + (bp / 10000000) % 10));
80     AddC((char)('0' + (bp / 1000000) % 10));
81   }
82
83 #ifdef  CHROOTDIR
84   AddC('c');
85 #endif
86 #ifdef  CMDLINE_CONFIG
87   AddC('C');
88 #endif
89 #ifdef  DEBUGMODE
90   AddC('D');
91 #endif
92
93   if (feature_bool(FEAT_LOCOP_REHASH))
94     AddC('e');
95
96   if (feature_bool(FEAT_OPER_REHASH))
97     AddC('E');
98
99   if (feature_bool(FEAT_OPER_NO_CHAN_LIMIT))
100     AddC('F');
101
102   if (feature_bool(FEAT_OPER_MODE_LCHAN))
103     AddC('f');
104
105   if (feature_bool(FEAT_HUB))
106     AddC('H');
107
108   if (feature_bool(FEAT_SHOW_ALL_INVISIBLE_USERS))
109     AddC('I');
110   else if (feature_bool(FEAT_SHOW_INVISIBLE_USERS))
111     AddC('i');
112
113   if (feature_bool(FEAT_OPER_KILL)) {
114     if (feature_bool(FEAT_LOCAL_KILL_ONLY))
115       AddC('k');
116     else
117       AddC('K');
118   }
119
120   if (feature_bool(FEAT_OPER_WALK_THROUGH_LMODES))
121     AddC('l');
122
123   if (feature_bool(FEAT_IDLE_FROM_MSG))
124     AddC('M');
125
126   if (feature_bool(FEAT_NO_OPER_DEOP_LCHAN))
127     AddC('o');
128
129   if (feature_bool(FEAT_CRYPT_OPER_PASSWORD))
130     AddC('p');
131
132   if (feature_bool(FEAT_RELIABLE_CLOCK))
133     AddC('R');
134
135   if (feature_bool(FEAT_LOCOP_RESTART))
136     AddC('s');
137
138   if (feature_bool(FEAT_OPER_RESTART))
139     AddC('S');
140
141 #if defined(USE_POLL) && defined(HAVE_POLL_H)
142   AddC('U');
143 #endif
144
145   if (feature_bool(FEAT_VIRTUAL_HOST))
146     AddC('v');
147
148   serveropts[i] = '\0';
149
150   return serveropts;
151 }
152
153 /*
154  * debug_init
155  *
156  * If the -t option is not given on the command line when the server is
157  * started, all debugging output is sent to the file set by LPATH in config.h
158  * Here we just open that file and make sure it is opened to fd 2 so that
159  * any fprintf's to stderr also goto the logfile.  If the debuglevel is not
160  * set from the command line by -x, use /dev/null as the dummy logfile as long
161  * as DEBUGMODE has been defined, else dont waste the fd.
162  */
163 void debug_init(int use_tty)
164 {
165 #ifdef  DEBUGMODE
166   if (debuglevel >= 0) {
167     printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
168     log_debug_init(use_tty);
169   }
170 #endif
171 }
172
173 #ifdef DEBUGMODE
174 void vdebug(int level, const char *form, va_list vl)
175 {
176   static int loop = 0;
177   int err = errno;
178
179   if (!loop && (debuglevel >= 0) && (level <= debuglevel))
180   {
181     loop = 1;
182     log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
183     loop = 0;
184   }
185   errno = err;
186 }
187
188 void debug(int level, const char *form, ...)
189 {
190   va_list vl;
191   va_start(vl, form);
192   vdebug(level, form, vl);
193   va_end(vl);
194 }
195
196 static void debug_enumerator(struct Client* cptr, const char* msg)
197 {
198   assert(0 != cptr);
199   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
200 }
201
202 /*
203  * This is part of the STATS replies. There is no offical numeric for this
204  * since this isnt an official command, in much the same way as HASH isnt.
205  * It is also possible that some systems wont support this call or have
206  * different field names for "struct rusage".
207  * -avalon
208  */
209 void send_usage(struct Client *cptr, char *nick)
210 {
211   os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
212
213   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
214              DBufAllocCount, DBufUsedCount);
215 }
216 #endif /* DEBUGMODE */
217
218 void count_memory(struct Client *cptr, char *nick)
219 {
220   struct Client *acptr;
221   struct SLink *link;
222   struct Channel *chptr;
223   struct ConfItem *aconf;
224   const struct ConnectionClass* cltmp;
225   struct Membership* member;
226
227   int c = 0,                    /* clients */
228       cn = 0,                   /* connections */
229       ch = 0,                   /* channels */
230       lcc = 0,                  /* local client conf links */
231       us = 0,                   /* user structs */
232       chi = 0,                  /* channel invites */
233       chb = 0,                  /* channel bans */
234       wwu = 0,                  /* whowas users */
235       cl = 0,                   /* classes */
236       co = 0,                   /* conf lines */
237       memberships = 0;          /* channel memberships */
238
239   int usi = 0,                  /* users invited */
240       aw = 0,                   /* aways set */
241       wwa = 0;                  /* whowas aways */
242
243   size_t chm = 0,               /* memory used by channels */
244       chbm = 0,                 /* memory used by channel bans */
245       cm = 0,                   /* memory used by clients */
246       cnm = 0,                  /* memory used by connections */
247       awm = 0,                  /* memory used by aways */
248       wwam = 0,                 /* whowas away memory used */
249       wwm = 0,                  /* whowas array memory used */
250       com = 0,                  /* memory used by conf lines */
251       dbufs_allocated = 0,      /* memory used by dbufs */
252       dbufs_used = 0,           /* memory used by dbufs */
253       msg_allocated = 0,        /* memory used by struct Msg */
254       msg_used = 0,             /* memory used by struct Msg */
255       msgbuf_allocated = 0,     /* memory used by struct MsgBuf */
256       msgbuf_used = 0,          /* memory used by struct MsgBuf */
257       rm = 0,                   /* res memory used */
258       totcl = 0, totch = 0, totww = 0, tot = 0;
259
260   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
261   wwm += sizeof(struct Whowas) * NICKNAMEHISTORYLENGTH;
262   wwm += sizeof(struct Whowas *) * WW_MAX;
263
264   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
265   {
266     c++;
267     if (MyConnect(acptr))
268     {
269       cn++;
270       for (link = cli_confs(acptr); link; link = link->next)
271         lcc++;
272     }
273     if (cli_user(acptr))
274     {
275       us++;
276       for (link = cli_user(acptr)->invited; link; link = link->next)
277         usi++;
278       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
279         ++memberships;
280       if (cli_user(acptr)->away)
281       {
282         aw++;
283         awm += (strlen(cli_user(acptr)->away) + 1);
284       }
285     }
286   }
287   cm = c * sizeof(struct Client);
288   cnm = cn * sizeof(struct Connection);
289
290   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
291   {
292     ch++;
293     chm += (strlen(chptr->chname) + sizeof(struct Channel));
294 #if 0
295     /*
296      * XXX - Members already counted in clients, don't count twice
297      */
298     for (member = chptr->members; member; member = member->next_member)
299       chu++;
300 #endif
301     for (link = chptr->invites; link; link = link->next)
302       chi++;
303     for (link = chptr->banlist; link; link = link->next)
304     {
305       chb++;
306       chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
307     }
308   }
309
310   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
311   {
312     co++;
313     com += aconf->host ? strlen(aconf->host) + 1 : 0;
314     com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
315     com += aconf->name ? strlen(aconf->name) + 1 : 0;
316     com += sizeof(struct ConfItem);
317   }
318
319   for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
320     cl++;
321
322   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
323              ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
324   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
325              ":Users %d(%zu) Invites %d(%zu)", us, us * sizeof(struct User),
326              usi, usi * sizeof(struct SLink));
327   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
328              ":User channels %d(%zu) Aways %d(%zu)", memberships,
329              memberships * sizeof(struct Membership), aw, awm);
330   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
331              lcc, lcc * sizeof(struct SLink));
332
333   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
334   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
335
336   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
337              com);
338
339   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
340              cl * sizeof(struct ConnectionClass));
341
342   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
343              ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
344   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
345              ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
346              memberships * sizeof(struct Membership), chi,
347              chi * sizeof(struct SLink));
348
349   totch = chm + chbm + chi * sizeof(struct SLink);
350
351   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
352              ":Whowas users %d(%zu) away %d(%zu)", wwu,
353              wwu * sizeof(struct User), wwa, wwam);
354   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
355              NICKNAMEHISTORYLENGTH, wwm);
356
357   totww = wwu * sizeof(struct User) + wwam + wwm;
358
359   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
360              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
361              sizeof(void *) * HASHSIZE);
362
363   /*
364    * NOTE: this count will be accurate only for the exact instant that this
365    * message is being sent, so the count is affected by the dbufs that
366    * are being used to send this message out. If this is not desired, move
367    * the dbuf_count_memory call to a place before we start sending messages
368    * and cache DBufAllocCount and DBufUsedCount in variables until they 
369    * are sent.
370    */
371   dbuf_count_memory(&dbufs_allocated, &dbufs_used);
372   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
373              ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
374              dbufs_allocated, DBufUsedCount, dbufs_used);
375
376   msgq_count_memory(&msg_allocated, &msg_used, &msgbuf_allocated,
377                     &msgbuf_used);
378   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
379              ":Msgs allocated %d(%zu) used %d(%zu)", msgCounts.alloc,
380              msg_allocated, msgCounts.used, msg_used);
381   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
382              ":MsgBufs allocated %d(%zu) used %d(%zu)", msgBufCounts.alloc,
383              msgbuf_allocated, msgBufCounts.used, msgbuf_used);
384
385   rm = cres_mem(cptr);
386
387   tot =
388       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
389       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
390   tot += sizeof(void *) * HASHSIZE * 3;
391
392 #if !defined(NDEBUG)
393   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
394              fda_get_block_count(), fda_get_byte_count());
395 #endif
396
397   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
398              ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
399              totww, totch, totcl, com, dbufs_allocated, msg_allocated,
400              msgbuf_allocated);
401 }
402