-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMAGE.C
406 lines (355 loc) · 9.98 KB
/
IMAGE.C
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
/* image.c - build screen images from internal database
*
* Author:
* Benjamin W. Slivka
* (c) 1987,1988
* Microsoft Corporation
*
* History:
* 17-Aug-1987 bws Modified assertion usage
* 18-Aug-1987 bws Added more assertions
* 27-Feb-1988 bws Decode system semaphore flags
* 24-Aug-1988 bws Tag assertion stuff separately from DEBUG
* 01-Sep-1988 bws Use fmt instead of sprintf
*/
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include "asrt.h" /* assertion checking */
#include "vio.h"
#include "ps.h"
#include "vars.h"
#include "data.h"
#include "procstat.h"
#define NEST_WIDTH 4 /* Width of nesting column in process display */
#define LIB_INDENT 4 /* Library indentation tab width */
#define SEM_INDENT 21 /* Width of module,pid,tid area in Sem display */
extern dproc_t *findProc (word);
extern dproc_t *findSlot (word, dthrd_t **);
extern dsem_t *pSemHead;
extern dmem_t *pMemHead;
/*** allocLine - allocate a line_t and copy of string
*
* Create copy of string, allocate a line_t, and make line_t
* point to copy of string.
*/
line_t *allocLine (buf)
char *buf;
{
line_t *li;
char *s;
if ((li = (line_t *)malloc(sizeof(line_t))) == NULL)
Fatal("allocLine: could not allocate line node");
if ((s = malloc(strlen(buf)+1)) == NULL)
Fatal("allocLine: could not allocate string");
strcpy (s,buf); /* copy string */
li->li_next = NULL; /* assume end of list */
li->li_str = s; /* point to string */
#ifdef CheckAsserts
li->li_dbgSig = LI_SIG;
#endif
return li;
}
/*** appendLine - append a line to a linked list of lines
*
*/
appendLine (tail, cnt, buf)
line_t **tail;
int *cnt;
char *buf;
{
line_t *p;
AssertNotNull(tail);
AssertLI(*tail);
AssertNotNull(cnt);
AssertNotNull(buf);
p = allocLine(buf);
(*tail)->li_next = p;
*tail = p;
(*cnt)++;
}
/*** stateToString - map a thread state number to state name
*
*/
char *stateToString(state)
int state;
{
switch (state) {
case S_VOID: return "void ";
case S_ZOMBIE: return "zombie ";
case S_READY: return "ready ";
case S_BLOCK: return "block ";
case S_FROZE: return "froze ";
case S_CRITSEC: return "critsec";
case S_BACKGND: return "backgnd";
case S_BOOST: return "boost ";
default: return "????? ";
}
}
/*** traverseProcs - Traverse process tree depth-first to produce image
*
*/
traverseProcs (p,nest,tail,cnt,func)
dproc_t *p;
int nest;
line_t **tail; /* Screen image */
int *cnt; /* Number of rows in image */
int (*func)(dproc_t *, int, line_t **, int *); /* Report function */
{
dproc_t *x;
AssertDP(p);
AssertNotNull(cnt);
AssertNotNull(tail);
AssertLI(*tail);
for (x=p->dp_pChild; x != NULL; x=x->dp_pSibl) {
AssertDP(x);
(*func)(x,nest,tail,cnt); /* Do report */
traverseProcs(x,nest+1,tail,cnt,func); /* do child processes */
}
}
/*** imageProcSub - recursively image child processes of given process
*
*/
imageProcSub (p,nest,tail,cnt)
dproc_t *p; /* Process to image */
int nest; /* nesting count */
line_t **tail; /* Screen image */
int *cnt; /* Number of rows in image */
{
dthrd_t *t;
char buf[NEST_WIDTH+1];
char lbuf[MAX_SCREEN_WIDTH];
AssertDP(p);
AssertNotNull(tail);
AssertLI(*tail);
AssertNotNull(cnt);
if (nest <= NEST_WIDTH) {
memset(buf,' ',NEST_WIDTH); /* blank buffer */
buf[NEST_WIDTH] = '\0'; /* set terminator */
buf[nest-1] = nest + '0'; /* set nest level */
}
else
fmt (buf, "%*d", NEST_WIDTH, nest);
/* "Nest pid sgid Name" */
fmt (lbuf, "%*s %04x %02x %s\0",
NEST_WIDTH, buf, p->dp_pid, p->dp_sgid, p->dp_pLib->dl_pName);
appendLine (tail, cnt, lbuf);
for (t=p->dp_pThrd; t != NULL; t=t->dt_next) { /* image threads */
AssertDT(t);
/* "Nest pid sgid Name" */
/* " tid pri state name" */
fmt (lbuf," %02x %04x %7s %08lx\0",
t->dt_tid, t->dt_pri, stateToString(t->dt_state), t->dt_blkID);
appendLine (tail, cnt, lbuf);
}
}
/*** imageProc - Produce screen image of process and thread data
*
*/
imageProc (head, cnt)
line_t **head; /* Start of screen image */
int *cnt; /* Number of rows in image */
{
dproc_t *p;
line_t *tail;
AssertNotNull(cnt);
AssertNotNull(head);
if ((p = findProc(0)) == NULL)
Fatal("imageProcs: could not find process 0");
tail = allocLine("proc head"); /* header node */
*head = tail;
*cnt = 0; /* do not count header line */
traverseProcs (p,1,&tail,cnt,imageProcSub);
}
/*** imageLibSub - image library data for given process
*
*/
imageLibSub (p,nest,tail,cnt)
dproc_t *p;
int nest;
line_t **tail; /* Screen image */
int *cnt; /* Number of rows in image */
{
char lbuf[MAX_SCREEN_WIDTH];
AssertDP(p);
AssertNotNull(tail);
AssertLI(*tail);
AssertNotNull(cnt);
/* "PID SGID Name" */
fmt (lbuf, "%04x %02x %s\0",
p->dp_pid,
p->dp_sgid,
p->dp_pLib->dl_pName
);
appendLine (tail, cnt, lbuf);
imageMTE(p->dp_pLib,0,tail,cnt); /* Image MTEs recursively */
}
/*** imageMTE - recusively image library modules
*
*/
imageMTE (l,nest,tail,cnt)
dlib_t *l;
int nest;
line_t **tail; /* Screen image */
int *cnt; /* Number of rows in image */
{
dlib_t *pL;
dmte_t *m;
int fSeen;
char indent[MAX_SCREEN_WIDTH];
char lbuf[MAX_SCREEN_WIDTH];
char mark[2]; /* room for asterisk (*) plus 0-byte terminator */
AssertDL(l);
AssertNotNull(tail);
AssertLI(*tail);
AssertNotNull(cnt);
memset (indent, ' ', nest*LIB_INDENT);
indent[nest*LIB_INDENT] = '\0'; /* terminate indent */
for (m=l->dl_pMTE; m != NULL; m=m->dmt_next) {
AssertDE(m);
pL = m->dmt_pLib;
AssertDL(pL);
fSeen = pL->dl_fSeen;
if (fSeen)
strcpy (mark,"*");
else
mark[0] = '\0';
/* "PID SGID Name" */
/* " Library List" */
fmt (lbuf, " %s%s%s",
indent,
pL->dl_pName,
mark
);
appendLine (tail, cnt, lbuf);
if (!fSeen) {
pL->dl_fSeen = TRUE; /* mark library as seen */
imageMTE(pL,nest+1,tail,cnt); /* Image MTEs recursively */
}
}
}
/*** imageLib - Produce screen image of library data
*
*/
imageLib (head, cnt)
line_t **head; /* Start of screen image */
int *cnt; /* Number of rows in image */
{
dproc_t *p;
line_t *tail;
AssertNotNull(cnt);
AssertNotNull(head);
if ((p = findProc(0)) == NULL)
Fatal("imageLibs: could not find process 0");
tail = allocLine("lib head"); /* header node */
*head = tail;
*cnt = 0; /* do not count header line */
resetLibs(); /* Clear traversal flags */
traverseProcs (p,1,&tail,cnt,imageLibSub);
}
/*** imageMem - Produce screen image of Shared Memory Data
*
*/
imageMem (head, cnt)
line_t **head; /* Start of screen image */
int *cnt; /* Number of rows in image */
{
line_t *tail;
char lbuf[MAX_SCREEN_WIDTH];
dmem_t *m;
AssertNotNull(cnt);
AssertNotNull(head);
tail = allocLine("mem head"); /* header node */
*head = tail;
*cnt = 0; /* do not count header line */
AssertDM(pMemHead);
AssertDM(pMemHead->dm_link);
for (m=pMemHead->dm_link; m != NULL; m=m->dm_link) {
AssertDM(m);
/* "hndl sel ref name"*/
fmt (lbuf, "%04x %04x %04x %s",
m->dm_hSM, m->dm_sSM, m->dm_ref, m->dm_pName);
appendLine (&tail, cnt, lbuf);
}
}
typedef struct MPWPSZ {
int w;
char * psz;
} MPWPSZ;
MPWPSZ mpwpsz[] = { /*123456789*/
{SYSSEM_WAITING, "waiters" },
{SYSSEM_MUXWAITING, "muxwait" },
{SYSSEM_OWNER_DIED, "proc died"},
{SYSSEM_EXCLUSIVE, "exclusive"},
{SYSSEM_NAME_CLEANUP, "cleanup" },
{SYSSEM_THREAD_OWNER_DIED, "thrd died"},
{SYSSEM_EXITLIST_OWNER, "exit ownr"}
};
/*** MapSSFlagToName - return name for SysSem flag
*
*/
char *MapSSFlagToName (pw)
int *pw;
{
int i;
char *psz = (char *)NULL;
for (i=0; i<(sizeof(mpwpsz)/sizeof(MPWPSZ)); i++)
if (*pw & mpwpsz[i].w) {
*pw &= !mpwpsz[i].w; /* clear flag */
psz = mpwpsz[i].psz; /* flag name */
break;
}
if (psz == NULL)
psz = " ";
return psz;
}
/*** imageSem - Produce screen image of Semaphore Data
*
*/
imageSem (head, cnt)
line_t **head; /* Start of screen image */
int *cnt; /* Number of rows in image */
{
line_t *tail;
char lbuf[MAX_SCREEN_WIDTH];
dsem_t *s;
dproc_t *p;
dthrd_t *t;
word w;
char *psz;
AssertNotNull(cnt);
AssertNotNull(head);
tail = allocLine("sem head"); /* header node */
*head = tail;
*cnt = 0; /* do not count header line */
AssertDS(pSemHead);
AssertDS(pSemHead->ds_link);
for (s=pSemHead->ds_link; s != NULL; s=s->ds_link) {
AssertDS(s);
if (s->ds_slot == 0) /* no owner */
memset (lbuf,' ', SEM_INDENT);
else {
if ((p = findSlot(s->ds_slot, &t)) == NULL)
Fatal("imageSem: unknown thread slot");
/* 123456789 123456789 12 */
/* "module pid tid " */
fmt (lbuf, "%8s %04x %02x ",
p->dp_pLib->dl_pName, p->dp_pid, t->dt_tid);
}
/* " status ref want hndl name" */
w = s->ds_flag;
psz = MapSSFlagToName(&w);
fmt (&(lbuf[SEM_INDENT]), "%9s %04x %04x %04x %s",
psz, s->ds_ref, s->ds_want, s->ds_index, s->ds_pName);
appendLine (&tail, cnt, lbuf);
if (w != NULL) {
memset(lbuf,' ',SEM_INDENT); /* blank front of buffer */
do {
psz = MapSSFlagToName(&w); /* get next flag name */
fmt(&(lbuf[SEM_INDENT]), "%9s", psz);
appendLine(&tail, cnt, lbuf);
} while (w != 0);
}
}
}