summaryrefslogtreecommitdiffabout
path: root/src/debug.c
blob: 00f568cd3c9a8c59981434717091ffbc9fda4b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* This file is part of GDBM, the GNU data base manager.
   Copyright 2016-2018 Free Software Foundation, Inc.

   GDBM is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GDBM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */

#include "autoconf.h"
#include "gdbmdefs.h"
#include <ctype.h>

gdbm_debug_printer_t gdbm_debug_printer;
int gdbm_debug_flags;

struct gdbm_debug_token_desc
{
  char const *name;
  int flag;
};

struct gdbm_debug_token_desc const gdbm_debug_token_tab[] = {
  { "err",    GDBM_DEBUG_ERR },
  { "open",   GDBM_DEBUG_OPEN },
  { "store",  GDBM_DEBUG_STORE },
  { "read",   GDBM_DEBUG_READ },
  { "lookup", GDBM_DEBUG_LOOKUP },
  { "all",    GDBM_DEBUG_ALL },
  { NULL, 0 }
};

int
gdbm_debug_token (char const *tok)
{
  int i;

  for (i = 0; gdbm_debug_token_tab[i].name; i++)
    if (strcmp (gdbm_debug_token_tab[i].name, tok) == 0)
      return gdbm_debug_token_tab[i].flag;

  return 0;
}

void
gdbm_debug_parse_state (int (*f) (void *, int, char const *), void *d)
{
  int i;
  
  for (i = 0; gdbm_debug_token_tab[i].name; i++)
    {
      if (gdbm_debug_token_tab[i].flag == GDBM_DEBUG_ALL)
	continue;
      if (gdbm_debug_flags & gdbm_debug_token_tab[i].flag)
	{
	  if (f (d, gdbm_debug_token_tab[i].flag, gdbm_debug_token_tab[i].name))
	    break;
	}
    }
}

#define DATBUFSIZE 69

static int
datbuf_format (char vbuf[DATBUFSIZE], const char *buf, size_t size)
{
  char *p = vbuf;
  char *q = vbuf + 51;
  int i;
  size_t j = 0;
  static char hexchar[] = "0123456789ABCDEF";
  
  for (i = 0; i < 16; i++)
    {
      unsigned c;
      if (j < size)
	{
	  c = *(const unsigned char*)buf++;
	  j++;

	  *p++ = hexchar[c >> 4];
	  *p++ = hexchar[c & 0xf];
	  *p++ = ' ';
	  
	  *q++ = isprint (c) ? c : '.';
	  if (i == 7)
	    {
	      *p++ = ' ';
	      *q++ = ' ';
	    }
	}
      else
	{
	  *p++ = ' ';
	  *p++ = ' ';
	  *p++ = ' ';
	  *q++ = ' ';
	}
    }
  *p++ = ' ';
  *p = ' ';
  *q = 0;
  return j;
}

void
gdbm_debug_datum (datum dat, char const *pfx)
{
  char const *buf = dat.dptr;
  size_t size = dat.dsize;
  unsigned off;
  char vbuf[DATBUFSIZE];

  if (!buf)
    {
      gdbm_debug_printer ("%s%s\n", pfx, "NULL");
      return;
    }

  gdbm_debug_printer ("size=%d\n", size);
  off = 0;
  while (size)
    {
      size_t rd = datbuf_format (vbuf, buf, size);
      gdbm_debug_printer ("%s%04x:  %s\n", pfx, off, vbuf);
      size -= rd;
      buf += rd;
      off += rd;
    }
}


struct hook_list
{
  struct hook_list *next;
  struct hook_list *prev;
  char *id;
  gdbm_debug_hook hook;
  void *data;
  int retval;
};

static struct hook_list *hook_head, *hook_tail;
static struct hook_list *hook_recent;

static struct hook_list *
hook_lookup_or_install (char const *id, int install)
{
  struct hook_list *p;

  for (p = hook_head; p; p = p->next)
    {
      int res = strcmp (p->id, id);
      if (res == 0)
	return p;
      if (res > 0)
	break;
    }

  if (install)
    {
      struct hook_list *elt = malloc (sizeof *elt);
      if (!elt)
	return NULL;
      elt->id = strdup (id);
      if (!elt->id)
	{
	  SAVE_ERRNO (free (elt));
	  return NULL;
	}
      elt->hook = NULL;
      elt->next = p;
      if (p)
	{
	  if (p->prev)
	    p->prev->next = elt;
	  else
	    hook_head = elt;
	  elt->prev = p->prev;
	}
      else
	{
	  elt->prev = hook_tail;
	  if (hook_tail)
	    hook_tail->next = elt;
	  else
	    hook_head = elt;
	  hook_tail = elt;
	}
      return elt;
    }
  
  return NULL;
}

static struct hook_list *
hook_lookup (char const *id)
{
  if (!(hook_recent && strcmp (hook_recent->id, id) == 0))
    hook_recent = hook_lookup_or_install (id, FALSE);
  return hook_recent;
}

static void
hook_remove (char const *id)
{
  struct hook_list *p;

  p = hook_lookup (id);
  if (!p)
    return;

  hook_recent = NULL;
  
  if (p->prev)
    p->prev->next = p->next;
  else
    hook_head = p->next;

  if (p->next)
    p->next->prev = p->prev;
  else
    hook_tail = p->prev;

  free (p->id);
  free (p);
}

static int
default_hook (char const *file, int line, char const *id, void *data)
{
  fprintf (stderr, "%s:%d: hit debug hook %s\n", file, line, id);
  return 1;
}
  
void
_gdbm_debug_hook_install (char const *id, gdbm_debug_hook hook, void *data)
{
  struct hook_list *p;

  p = hook_lookup_or_install (id, TRUE);
  p->hook = hook ? hook : default_hook;
  p->data = data;
}

void
_gdbm_debug_hook_remove (char const *id)
{
  hook_remove (id);
}

int
_gdbm_debug_hook_check (char const *file, int line, char const *id)
{
  struct hook_list *p = hook_lookup (id);
  if (p)
    return p->retval = p->hook (file, line, id, p->data);
  return 0;
}

int
_gdbm_debug_hook_val (char const *id)
{
  struct hook_list *p = hook_lookup (id);
  if (p)
    return p->retval;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.