Doxyfy s_debug.h and s_debug.c.
[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 /** Return a string describing important configuration information.
67  * @return Pointer to a static buffer.
68  */
69 const char* debug_serveropts(void)
70 {
71   int bp;
72   int i = 0;
73 #define AddC(c) serveropts[i++] = (c)
74
75   bp = feature_int(FEAT_BUFFERPOOL);
76   if (bp < 1000000) {
77     AddC('b');
78     if (bp > 99999)
79       AddC((char)('0' + (bp / 100000)));
80     if (bp > 9999)
81       AddC((char)('0' + (bp / 10000) % 10));
82     AddC((char)('0' + (bp / 1000) % 10));
83   } else {
84     AddC('B');
85     if (bp > 99999999)
86       AddC((char)('0' + (bp / 100000000)));
87     if (bp > 9999999)
88       AddC((char)('0' + (bp / 10000000) % 10));
89     AddC((char)('0' + (bp / 1000000) % 10));
90   }
91
92 #ifndef NDEBUG
93   AddC('A');
94 #endif
95 #ifdef  DEBUGMODE
96   AddC('D');
97 #endif
98
99   if (feature_bool(FEAT_HUB))
100     AddC('H');
101
102   if (feature_bool(FEAT_IDLE_FROM_MSG))
103     AddC('M');
104
105   if (feature_bool(FEAT_RELIABLE_CLOCK))
106     AddC('R');
107
108 #if defined(USE_POLL) && defined(HAVE_POLL_H)
109   AddC('U');
110 #endif
111
112   serveropts[i] = '\0';
113
114   return serveropts;
115 }
116
117 /** Initialize debugging.
118  * If the -t option is not given on the command line when the server is
119  * started, all debugging output is sent to the file set by LPATH in config.h
120  * Here we just open that file and make sure it is opened to fd 2 so that
121  * any fprintf's to stderr also goto the logfile.  If the debuglevel is not
122  * set from the command line by -x, use /dev/null as the dummy logfile as long
123  * as DEBUGMODE has been defined, else dont waste the fd.
124  * @param use_tty Passed to log_debug_init().
125  */
126 void debug_init(int use_tty)
127 {
128 #ifdef  DEBUGMODE
129   if (debuglevel >= 0) {
130     printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
131     log_debug_init(use_tty);
132   }
133 #endif
134 }
135
136 #ifdef DEBUGMODE
137 /** Log a debug message using a va_list.
138  * If the current #debuglevel is less than \a level, do not display.
139  * @param level Debug level for message.
140  * @param form Format string, passed to log_vwrite().
141  * @param vl Varargs argument list for format string.
142  */
143 void vdebug(int level, const char *form, va_list vl)
144 {
145   static int loop = 0;
146   int err = errno;
147
148   if (!loop && (debuglevel >= 0) && (level <= debuglevel))
149   {
150     loop = 1;
151     log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
152     loop = 0;
153   }
154   errno = err;
155 }
156
157 /** Log a debug message using a variable number of arguments.
158  * This is a simple wrapper around debug(\a level, \a form, vl).
159  * @param level Debug level for message.
160  * @param form Format string of message.
161  */
162 void debug(int level, const char *form, ...)
163 {
164   va_list vl;
165   va_start(vl, form);
166   vdebug(level, form, vl);
167   va_end(vl);
168 }
169
170 /** Send a literal RPL_STATSDEBUG message to a user.
171  * @param cptr Client to receive the message.
172  * @param msg Text message to send to user.
173  */
174 static void debug_enumerator(struct Client* cptr, const char* msg)
175 {
176   assert(0 != cptr);
177   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
178 }
179
180 /** Send resource usage statistics to a client.
181  * @param cptr Client to send data to.
182  * @param sd StatDesc that generated the stats request (ignored).
183  * @param param Extra parameter from user (ignored).
184  */
185 void send_usage(struct Client *cptr, const struct StatDesc *sd,
186                 char *param)
187 {
188   os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
189
190   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
191              DBufAllocCount, DBufUsedCount);
192 }
193 #endif /* DEBUGMODE */
194
195 /** Report memory usage statistics to a client.
196  * @param cptr Client to send data to.
197  * @param sd StatDesc that generated the stats request (ignored).
198  * @param param Extra parameter from user (ignored).
199  */
200 void count_memory(struct Client *cptr, const struct StatDesc *sd,
201                   char *param)
202 {
203   struct Client *acptr;
204   struct SLink *link;
205   struct Channel *chptr;
206   struct ConfItem *aconf;
207   const struct ConnectionClass* cltmp;
208   struct Membership* member;
209
210   int acc = 0,                  /* accounts */
211       c = 0,                    /* clients */
212       cn = 0,                   /* connections */
213       ch = 0,                   /* channels */
214       lcc = 0,                  /* local client conf links */
215       us = 0,                   /* user structs */
216       chi = 0,                  /* channel invites */
217       chb = 0,                  /* channel bans */
218       wwu = 0,                  /* whowas users */
219       cl = 0,                   /* classes */
220       co = 0,                   /* conf lines */
221       memberships = 0;          /* channel memberships */
222
223   int usi = 0,                  /* users invited */
224       aw = 0,                   /* aways set */
225       wwa = 0,                  /* whowas aways */
226       gl = 0,                   /* glines */
227       ju = 0;                   /* jupes */
228
229   size_t chm = 0,               /* memory used by channels */
230       chbm = 0,                 /* memory used by channel bans */
231       cm = 0,                   /* memory used by clients */
232       cnm = 0,                  /* memory used by connections */
233       awm = 0,                  /* memory used by aways */
234       wwam = 0,                 /* whowas away memory used */
235       wwm = 0,                  /* whowas array memory used */
236       glm = 0,                  /* memory used by glines */
237       jum = 0,                  /* memory used by jupes */
238       com = 0,                  /* memory used by conf lines */
239       dbufs_allocated = 0,      /* memory used by dbufs */
240       dbufs_used = 0,           /* memory used by dbufs */
241       msg_allocated = 0,        /* memory used by struct Msg */
242       msgbuf_allocated = 0,     /* memory used by struct MsgBuf */
243       rm = 0,                   /* res memory used */
244       totcl = 0, totch = 0, totww = 0, tot = 0;
245
246   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
247   wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
248   wwm += sizeof(struct Whowas *) * WW_MAX;
249
250   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
251   {
252     c++;
253     if (MyConnect(acptr))
254     {
255       cn++;
256       for (link = cli_confs(acptr); link; link = link->next)
257         lcc++;
258     }
259     if (cli_user(acptr))
260     {
261       us++;
262       for (link = cli_user(acptr)->invited; link; link = link->next)
263         usi++;
264       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
265         ++memberships;
266       if (cli_user(acptr)->away)
267       {
268         aw++;
269         awm += (strlen(cli_user(acptr)->away) + 1);
270       }
271     }
272
273     if (IsAccount(acptr))
274       acc++;
275   }
276   cm = c * sizeof(struct Client);
277   cnm = cn * sizeof(struct Connection);
278
279   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
280   {
281     ch++;
282     chm += (strlen(chptr->chname) + sizeof(struct Channel));
283     for (link = chptr->invites; link; link = link->next)
284       chi++;
285     for (link = chptr->banlist; link; link = link->next)
286     {
287       chb++;
288       chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
289     }
290   }
291
292   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
293   {
294     co++;
295     com += aconf->host ? strlen(aconf->host) + 1 : 0;
296     com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
297     com += aconf->name ? strlen(aconf->name) + 1 : 0;
298     com += sizeof(struct ConfItem);
299   }
300
301   for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
302     cl++;
303
304   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
305              ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
306   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
307              ":Users %d(%zu) Accounts %d(%zu) Invites %d(%zu)",
308              us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
309              usi, usi * sizeof(struct SLink));
310   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
311              ":User channels %d(%zu) Aways %d(%zu)", memberships,
312              memberships * sizeof(struct Membership), aw, awm);
313   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
314              lcc, lcc * sizeof(struct SLink));
315
316   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
317   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
318
319   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
320              com);
321
322   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
323              cl * sizeof(struct ConnectionClass));
324
325   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
326              ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
327   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
328              ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
329              memberships * sizeof(struct Membership), chi,
330              chi * sizeof(struct SLink));
331
332   totch = chm + chbm + chi * sizeof(struct SLink);
333
334   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
335              ":Whowas users %d(%zu) away %d(%zu)", wwu,
336              wwu * sizeof(struct User), wwa, wwam);
337   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
338              feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
339
340   totww = wwu * sizeof(struct User) + wwam + wwm;
341
342   motd_memory_count(cptr);
343
344   gl = gline_memory_count(&glm);
345   ju = jupe_memory_count(&jum);
346   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
347              ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
348
349   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
350              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
351              sizeof(void *) * HASHSIZE);
352
353   /*
354    * NOTE: this count will be accurate only for the exact instant that this
355    * message is being sent, so the count is affected by the dbufs that
356    * are being used to send this message out. If this is not desired, move
357    * the dbuf_count_memory call to a place before we start sending messages
358    * and cache DBufAllocCount and DBufUsedCount in variables until they 
359    * are sent.
360    */
361   dbuf_count_memory(&dbufs_allocated, &dbufs_used);
362   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
363              ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
364              dbufs_allocated, DBufUsedCount, dbufs_used);
365
366   /* The DBuf caveats now count for this, but this routine now sends
367    * replies all on its own.
368    */
369   msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
370
371   rm = cres_mem(cptr);
372
373   tot =
374       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
375       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
376   tot += sizeof(void *) * HASHSIZE * 3;
377
378 #if defined(MDEBUG)
379   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
380              fda_get_block_count(), fda_get_byte_count());
381 #endif
382
383   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
384              ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
385              totww, totch, totcl, com, dbufs_allocated, msg_allocated,
386              msgbuf_allocated);
387 }
388