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