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