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