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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <mdb/mdb_modapi.h>
#include <mdb/mdb_ctf.h>
#include <configd.h>
mdb_ctf_id_t request_enum;
mdb_ctf_id_t response_enum;
mdb_ctf_id_t ptr_type_enum;
mdb_ctf_id_t thread_state_enum;
hrtime_t max_time_seen;
static void
enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val,
const char *prefix, const char *prefix2)
{
const char *cp;
size_t len = strlen(prefix);
size_t len2 = strlen(prefix2);
if ((cp = mdb_ctf_enum_name(id, val)) != NULL) {
if (strncmp(cp, prefix, len) == 0)
cp += len;
if (strncmp(cp, prefix2, len2) == 0)
cp += len2;
(void) strlcpy(out, cp, size);
} else {
mdb_snprintf(out, size, "? (%d)", val);
}
}
static void
make_lower(char *out, size_t sz)
{
while (*out != 0 && sz > 0) {
if (*out >= 'A' && *out <= 'Z')
*out += 'a' - 'A';
out++;
sz--;
}
}
/*ARGSUSED*/
static int
configd_status(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
int num_servers;
int num_started;
if (argc != 0)
return (DCMD_USAGE);
if (mdb_readvar(&num_servers, "num_servers") == -1) {
mdb_warn("unable to read num_servers");
return (DCMD_ERR);
}
if (mdb_readvar(&num_started, "num_started") == -1) {
mdb_warn("unable to read num_started");
return (DCMD_ERR);
}
mdb_printf(
"\nserver threads:\t%d running, %d starting\n\n", num_servers,
num_started - num_servers);
if (mdb_walk_dcmd("configd_threads", "configd_thread", argc,
argv) == -1) {
mdb_warn("can't walk 'configd_threads'");
return (DCMD_ERR);
}
return (DCMD_OK);
}
/*ARGSUSED*/
static int
configd_thread(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
thread_info_t t;
char state[20];
char oldstate[20];
if (!(flags & DCMD_ADDRSPEC)) {
if (mdb_walk_dcmd("configd_threads", "configd_thread", argc,
argv) == -1) {
mdb_warn("can't walk 'configd_threads'");
return (DCMD_ERR);
}
return (DCMD_OK);
}
if (argc != 0)
return (DCMD_USAGE);
if (DCMD_HDRSPEC(flags)) {
mdb_printf("%<u>%-?s %5s %-12s %-12s %-?s %-?s %-?s%</u>\n",
"ADDR", "TID", "STATE", "PREV_STATE", "CLIENT", "CLIENTRQ",
"MAINREQ");
}
if (mdb_vread(&t, sizeof (t), addr) == -1) {
mdb_warn("failed to read thread_info_t at %p", addr);
return (DCMD_ERR);
}
enum_lookup(state, sizeof (state), thread_state_enum,
t.ti_state, "TI_", "");
make_lower(state, sizeof (state));
enum_lookup(oldstate, sizeof (oldstate), thread_state_enum,
t.ti_prev_state, "TI_", "");
make_lower(oldstate, sizeof (oldstate));
mdb_printf("%0?p %5d %-12s %-12s %?p %?p %?p\n",
(void *)addr, t.ti_thread, state, oldstate,
t.ti_active_client, t.ti_client_request, t.ti_main_door_request);
return (DCMD_OK);
}
static int
walk_thread_info_init(mdb_walk_state_t *wsp)
{
if (mdb_readvar(&wsp->walk_addr, "thread_list") == -1) {
mdb_warn("unable to read thread_list");
return (WALK_ERR);
}
if (mdb_layered_walk("uu_list_node", wsp) == -1) {
mdb_warn("couldn't walk 'uu_list_node'");
return (WALK_ERR);
}
return (WALK_NEXT);
}
static int
walk_thread_info_step(mdb_walk_state_t *wsp)
{
uintptr_t addr = wsp->walk_addr;
thread_info_t ti;
if (mdb_vread(&ti, sizeof (ti), addr) == -1) {
mdb_warn("unable to read thread_info_t at %p", addr);
return (WALK_ERR);
}
return (wsp->walk_callback(addr, &ti, wsp->walk_cbdata));
}
static int
request_log(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
request_log_entry_t cur;
hrtime_t dur;
hrtime_t dursec;
hrtime_t durnsec;
char durstr[20];
char stampstr[20];
char requstr[30];
char respstr[30];
char typestr[30];
uintptr_t node = 0;
uintptr_t client = 0;
uint64_t clientid = 0;
int idx;
int opt_v = FALSE; /* verbose */
if (!(flags & DCMD_ADDRSPEC)) {
if (mdb_walk_dcmd("configd_log", "configd_log", argc,
argv) == -1) {
mdb_warn("can't walk 'configd_log'");
return (DCMD_ERR);
}
return (DCMD_OK);
}
if (mdb_getopts(argc, argv,
'c', MDB_OPT_UINTPTR, &client,
'i', MDB_OPT_UINT64, &clientid,
'n', MDB_OPT_UINTPTR, &node,
'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL) != argc)
return (DCMD_USAGE);
if (DCMD_HDRSPEC(flags)) {
mdb_printf("%<u>%-?s %-4s %-14s %9s %-22s %-17s\n%</u>",
"ADDR", "THRD", "START", "DURATION", "REQUEST",
"RESPONSE");
}
if (mdb_vread(&cur, sizeof (cur), addr) == -1) {
mdb_warn("couldn't read log entry at %p", addr);
return (DCMD_ERR);
}
/*
* apply filters, if any.
*/
if (clientid != 0 && clientid != cur.rl_clientid)
return (DCMD_OK);
if (client != 0 && client != (uintptr_t)cur.rl_client)
return (DCMD_OK);
if (node != 0) {
for (idx = 0; idx < MIN(MAX_PTRS, cur.rl_num_ptrs); idx++) {
if ((uintptr_t)cur.rl_ptrs[idx].rlp_data == node) {
node = 0; /* found it */
break;
}
}
if (node != 0)
return (DCMD_OK);
}
enum_lookup(requstr, sizeof (requstr), request_enum, cur.rl_request,
"REP_PROTOCOL_", "");
if (cur.rl_end != 0) {
enum_lookup(respstr, sizeof (respstr), response_enum,
cur.rl_response, "REP_PROTOCOL_", "FAIL_");
dur = cur.rl_end - cur.rl_start;
dursec = dur / NANOSEC;
durnsec = dur % NANOSEC;
if (dursec <= 9)
mdb_snprintf(durstr, sizeof (durstr),
"%lld.%06lld",
dursec, durnsec / (NANOSEC / MICROSEC));
else if (dursec <= 9999)
mdb_snprintf(durstr, sizeof (durstr),
"%lld.%03lld",
dursec, NSEC2MSEC(durnsec));
else
mdb_snprintf(durstr, sizeof (durstr),
"%lld", dursec);
} else {
(void) strcpy(durstr, "-");
(void) strcpy(respstr, "-");
}
if (max_time_seen != 0 && max_time_seen >= cur.rl_start) {
dur = max_time_seen - cur.rl_start;
dursec = dur / NANOSEC;
durnsec = dur % NANOSEC;
if (dursec <= 99ULL)
mdb_snprintf(stampstr, sizeof (stampstr),
"-%lld.%09lld", dursec, durnsec);
else if (dursec <= 99999ULL)
mdb_snprintf(stampstr, sizeof (stampstr),
"-%lld.%06lld",
dursec, durnsec / (NANOSEC / MICROSEC));
else if (dursec <= 99999999ULL)
mdb_snprintf(stampstr, sizeof (stampstr),
"-%lld.%03lld",
dursec, NSEC2MSEC(durnsec));
else
mdb_snprintf(stampstr, sizeof (stampstr),
"-%lld", dursec);
} else {
(void) strcpy(stampstr, "-");
}
mdb_printf("%0?x %4d T%13s %9s %-22s %-17s\n",
addr, cur.rl_tid, stampstr, durstr, requstr, respstr);
if (opt_v) {
mdb_printf("\tclient: %?p (%d)\tptrs: %d\tstamp: %llx\n",
cur.rl_client, cur.rl_clientid, cur.rl_num_ptrs,
cur.rl_start);
for (idx = 0; idx < MIN(MAX_PTRS, cur.rl_num_ptrs); idx++) {
enum_lookup(typestr, sizeof (typestr), ptr_type_enum,
cur.rl_ptrs[idx].rlp_type, "RC_PTR_TYPE_", "");
mdb_printf("\t\t%-7s %5d %?p %?p\n", typestr,
cur.rl_ptrs[idx].rlp_id, cur.rl_ptrs[idx].rlp_ptr,
cur.rl_ptrs[idx].rlp_data);
}
mdb_printf("\n");
}
return (DCMD_OK);
}
struct request_log_walk {
size_t rlw_max;
size_t rlw_count;
size_t rlw_cur;
struct request_entry {
hrtime_t timestamp;
uintptr_t addr;
} *rlw_list;
};
/*
* we want newer items at the top
*/
static int
request_entry_compare(const void *l, const void *r)
{
const struct request_entry *lp = l;
const struct request_entry *rp = r;
if (rp->timestamp == lp->timestamp)
return (0);
/*
* 0 timestamps go first.
*/
if (rp->timestamp == 0)
return (1);
if (lp->timestamp == 0)
return (-1);
if (lp->timestamp < rp->timestamp)
return (1);
return (-1);
}
/*ARGSUSED*/
static int
request_log_count_thread(uintptr_t addr, thread_info_t *tip, uint_t *arg)
{
(*arg)++;
return (WALK_NEXT);
}
static int
request_log_add_thread(uintptr_t addr, thread_info_t *tip,
struct request_entry **arg)
{
if (max_time_seen < tip->ti_log.rl_start)
max_time_seen = tip->ti_log.rl_start;
if (max_time_seen < tip->ti_log.rl_end)
max_time_seen = tip->ti_log.rl_end;
if (tip->ti_log.rl_start != 0) {
if (tip->ti_log.rl_end)
(*arg)->timestamp = tip->ti_log.rl_start;
else
(*arg)->timestamp = 0; /* sort to the top */
(*arg)->addr = addr + offsetof(thread_info_t, ti_log);
++*arg;
}
return (WALK_NEXT);
}
static int
request_log_walk_init(mdb_walk_state_t *wsp)
{
struct request_log_walk *rlw;
struct request_entry *list, *listp;
uint_t log_size;
uint_t size;
uint_t idx;
uint_t pos;
request_log_entry_t *base;
request_log_entry_t cur;
if (mdb_readvar(&base, "request_log") == -1) {
mdb_warn("couldn't read 'request_log'");
return (WALK_ERR);
}
if (mdb_readvar(&log_size, "request_log_size") == -1) {
mdb_warn("couldn't read 'request_log_size'");
return (WALK_ERR);
}
size = log_size;
if (mdb_walk("configd_threads", (mdb_walk_cb_t)request_log_count_thread,
&size) == -1) {
mdb_warn("couldn't walk 'configd_threads'");
return (WALK_ERR);
}
list = mdb_zalloc(sizeof (*list) * size, UM_SLEEP);
listp = list;
if (mdb_walk("configd_threads", (mdb_walk_cb_t)request_log_add_thread,
&listp) == -1) {
mdb_warn("couldn't walk 'configd_threads'");
mdb_free(list, sizeof (*list) * size);
return (WALK_ERR);
}
pos = listp - list;
for (idx = 0; idx < log_size; idx++) {
uintptr_t addr = (uintptr_t)&base[idx];
if (mdb_vread(&cur, sizeof (cur), addr) == -1) {
mdb_warn("couldn't read log entry at %p", addr);
mdb_free(list, sizeof (*list) * size);
return (WALK_ERR);
}
if (max_time_seen < cur.rl_start)
max_time_seen = cur.rl_start;
if (max_time_seen < cur.rl_end)
max_time_seen = cur.rl_end;
if (cur.rl_start != 0) {
list[pos].timestamp = cur.rl_start;
list[pos].addr = addr;
pos++;
}
}
qsort(list, pos, sizeof (*list), &request_entry_compare);
rlw = mdb_zalloc(sizeof (*rlw), UM_SLEEP);
rlw->rlw_max = size;
rlw->rlw_count = pos;
rlw->rlw_cur = 0;
rlw->rlw_list = list;
wsp->walk_data = rlw;
return (WALK_NEXT);
}
static int
request_log_walk_step(mdb_walk_state_t *wsp)
{
struct request_log_walk *rlw = wsp->walk_data;
uintptr_t addr;
request_log_entry_t cur;
if (rlw->rlw_cur >= rlw->rlw_count)
return (WALK_DONE);
addr = rlw->rlw_list[rlw->rlw_cur++].addr;
if (mdb_vread(&cur, sizeof (cur), addr) == -1) {
mdb_warn("couldn't read log entry at %p", addr);
return (WALK_ERR);
}
return (wsp->walk_callback(addr, &cur, wsp->walk_cbdata));
}
static void
request_log_walk_fini(mdb_walk_state_t *wsp)
{
struct request_log_walk *rlw = wsp->walk_data;
mdb_free(rlw->rlw_list, sizeof (*rlw->rlw_list) * rlw->rlw_max);
mdb_free(rlw, sizeof (*rlw));
}
static const mdb_dcmd_t dcmds[] = {
{ "configd_status", NULL, "svc.configd status summary",
configd_status },
{ "configd_thread", "?", "Print a thread_info_t tabularly",
configd_thread },
{ "configd_log", "?[-v] [-c clientptr] [-i clientid]",
"Print the request log, or a single entry", request_log },
{ NULL }
};
static const mdb_walker_t walkers[] = {
{ "configd_threads", "walks the thread_info_ts for all "
"threads", walk_thread_info_init, walk_thread_info_step },
{ "configd_log", "walks the request_log_entry_ts",
request_log_walk_init, request_log_walk_step,
request_log_walk_fini},
{ NULL }
};
static const mdb_modinfo_t modinfo = {
MDB_API_VERSION, dcmds, walkers
};
const mdb_modinfo_t *
_mdb_init(void)
{
if (mdb_ctf_lookup_by_name("enum rep_protocol_requestid",
&request_enum) == -1) {
mdb_warn("enum rep_protocol_requestid not found");
}
if (mdb_ctf_lookup_by_name("enum rep_protocol_responseid",
&response_enum) == -1) {
mdb_warn("enum rep_protocol_responseid not found");
}
if (mdb_ctf_lookup_by_name("enum rc_ptr_type",
&ptr_type_enum) == -1) {
mdb_warn("enum rc_ptr_type not found");
}
if (mdb_ctf_lookup_by_name("enum thread_state",
&thread_state_enum) == -1) {
mdb_warn("enum thread_state not found");
}
return (&modinfo);
}
|