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