a22745406dc7426ff3d9aea03bb6a1b4f55530e0
[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 #ifndef NDEBUG
86   AddC('A');
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, struct StatDesc *sd, int stat,
209                 char *param)
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, struct StatDesc *sd, int stat,
219                   char *param)
220 {
221   struct Client *acptr;
222   struct SLink *link;
223   struct Channel *chptr;
224   struct ConfItem *aconf;
225   const struct ConnectionClass* cltmp;
226   struct Membership* member;
227
228   int acc = 0,                  /* accounts */
229       c = 0,                    /* clients */
230       cn = 0,                   /* connections */
231       ch = 0,                   /* channels */
232       lcc = 0,                  /* local client conf links */
233       us = 0,                   /* user structs */
234       chi = 0,                  /* channel invites */
235       chb = 0,                  /* channel bans */
236       wwu = 0,                  /* whowas users */
237       cl = 0,                   /* classes */
238       co = 0,                   /* conf lines */
239       memberships = 0;          /* channel memberships */
240
241   int usi = 0,                  /* users invited */
242       aw = 0,                   /* aways set */
243       wwa = 0,                  /* whowas aways */
244       gl = 0,                   /* glines */
245       ju = 0;                   /* jupes */
246
247   size_t chm = 0,               /* memory used by channels */
248       chbm = 0,                 /* memory used by channel bans */
249       cm = 0,                   /* memory used by clients */
250       cnm = 0,                  /* memory used by connections */
251       awm = 0,                  /* memory used by aways */
252       wwam = 0,                 /* whowas away memory used */
253       wwm = 0,                  /* whowas array memory used */
254       glm = 0,                  /* memory used by glines */
255       jum = 0,                  /* memory used by jupes */
256       com = 0,                  /* memory used by conf lines */
257       dbufs_allocated = 0,      /* memory used by dbufs */
258       dbufs_used = 0,           /* memory used by dbufs */
259       msg_allocated = 0,        /* memory used by struct Msg */
260       msgbuf_allocated = 0,     /* memory used by struct MsgBuf */
261       rm = 0,                   /* res memory used */
262       totcl = 0, totch = 0, totww = 0, tot = 0;
263
264   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
265   wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
266   wwm += sizeof(struct Whowas *) * WW_MAX;
267
268   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
269   {
270     c++;
271     if (MyConnect(acptr))
272     {
273       cn++;
274       for (link = cli_confs(acptr); link; link = link->next)
275         lcc++;
276     }
277     if (cli_user(acptr))
278     {
279       us++;
280       for (link = cli_user(acptr)->invited; link; link = link->next)
281         usi++;
282       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
283         ++memberships;
284       if (cli_user(acptr)->away)
285       {
286         aw++;
287         awm += (strlen(cli_user(acptr)->away) + 1);
288       }
289     }
290
291     if (IsAccount(acptr))
292       acc++;
293   }
294   cm = c * sizeof(struct Client);
295   cnm = cn * sizeof(struct Connection);
296
297   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
298   {
299     ch++;
300     chm += (strlen(chptr->chname) + sizeof(struct Channel));
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) Accounts %d(%zu) Invites %d(%zu)",
326              us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
327              usi, usi * sizeof(struct SLink));
328   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
329              ":User channels %d(%zu) Aways %d(%zu)", memberships,
330              memberships * sizeof(struct Membership), aw, awm);
331   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
332              lcc, lcc * sizeof(struct SLink));
333
334   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
335   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
336
337   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
338              com);
339
340   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
341              cl * sizeof(struct ConnectionClass));
342
343   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
344              ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
345   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
346              ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
347              memberships * sizeof(struct Membership), chi,
348              chi * sizeof(struct SLink));
349
350   totch = chm + chbm + chi * sizeof(struct SLink);
351
352   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
353              ":Whowas users %d(%zu) away %d(%zu)", wwu,
354              wwu * sizeof(struct User), wwa, wwam);
355   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
356              feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
357
358   totww = wwu * sizeof(struct User) + wwam + wwm;
359
360   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
361              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
362              sizeof(void *) * HASHSIZE);
363
364   /*
365    * NOTE: this count will be accurate only for the exact instant that this
366    * message is being sent, so the count is affected by the dbufs that
367    * are being used to send this message out. If this is not desired, move
368    * the dbuf_count_memory call to a place before we start sending messages
369    * and cache DBufAllocCount and DBufUsedCount in variables until they 
370    * are sent.
371    */
372   dbuf_count_memory(&dbufs_allocated, &dbufs_used);
373   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
374              ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
375              dbufs_allocated, DBufUsedCount, dbufs_used);
376
377   msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
378
379   rm = cres_mem(cptr);
380
381   tot =
382       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
383       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
384   tot += sizeof(void *) * HASHSIZE * 3;
385
386 #if defined(MDEBUG)
387   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
388              fda_get_block_count(), fda_get_byte_count());
389 #endif
390
391   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
392              ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
393              totww, totch, totcl, com, dbufs_allocated, msg_allocated,
394              msgbuf_allocated);
395 }
396