This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / handles / hps.c
1 #include <config.h>
2 #include <glib.h>
3
4 #include <mono/io-layer/io-layer.h>
5
6 /* We're digging into handle internals here... */
7 #include <mono/io-layer/handles-private.h>
8 #include <mono/io-layer/wapi-private.h>
9
10 static const guchar *unused_details (struct _WapiHandleShared *handle);
11 static const guchar *file_details (struct _WapiHandleShared *handle);
12 static const guchar *console_details (struct _WapiHandleShared *handle);
13 static const guchar *thread_details (struct _WapiHandleShared *handle);
14 static const guchar *sem_details (struct _WapiHandleShared *handle);
15 static const guchar *mutex_details (struct _WapiHandleShared *handle);
16 static const guchar *event_details (struct _WapiHandleShared *handle);
17 static const guchar *socket_details (struct _WapiHandleShared *handle);
18 static const guchar *find_details (struct _WapiHandleShared *handle);
19 static const guchar *process_details (struct _WapiHandleShared *handle);
20 static const guchar *pipe_details (struct _WapiHandleShared *handle);
21
22 /* This depends on the ordering of the enum WapiHandleType in
23  * io-layer/wapi-private.h
24  */
25 static const char *typename[]={
26         "Unused",
27         "File",
28         "Console",
29         "Thread",
30         "Sem",
31         "Mutex",
32         "Event",
33         "Socket",
34         "Find",
35         "Process",
36         "Pipe",
37         "Error!!"
38 };
39
40 /* So does this... */
41 static const guchar * (*details[])(struct _WapiHandleShared *)=
42 {
43         unused_details,
44         file_details,
45         console_details,
46         thread_details,
47         sem_details,
48         mutex_details,
49         event_details,
50         socket_details,
51         find_details,
52         process_details,
53         pipe_details,
54         unused_details,
55 };
56
57 int main (int argc, char **argv)
58 {
59         guint32 handle_idx;
60         gboolean success;
61         
62         _wapi_shared_data=g_new0 (struct _WapiHandleShared_list *, 1);
63         _wapi_shared_scratch=g_new0 (struct _WapiHandleScratch, 1);
64         
65         success=_wapi_shm_attach (&_wapi_shared_data[0],
66                                   &_wapi_shared_scratch);
67         if(success==FALSE) {
68                 g_error ("Failed to attach shared memory!");
69                 exit (-1);
70         }
71         
72         /* Make sure index 0 is actually unused */
73         for(handle_idx=0; handle_idx<_wapi_shared_data[0]->num_segments * _WAPI_HANDLES_PER_SEGMENT; handle_idx++) {
74                 guint32 segment, idx;
75                 struct _WapiHandleShared *shared;
76
77                 _wapi_handle_segment (GUINT_TO_POINTER (handle_idx), &segment,
78                                       &idx);
79                 _wapi_handle_ensure_mapped (segment);
80                 
81                 shared=&_wapi_shared_data[segment]->handles[idx];
82                 
83                 if(shared->type!=WAPI_HANDLE_UNUSED) {
84                         g_print ("%6x [%7s] %4u %s (%s)\n", handle_idx,
85                                  typename[shared->type], shared->ref,
86                                  shared->signalled?"Sg":"Un",
87                                  details[shared->type](shared));
88                 }
89         }
90         
91         exit (0);
92 }
93
94 static const guchar *unused_details (struct _WapiHandleShared *handle)
95 {
96         return("unused details");
97 }
98
99 static const guchar *file_details (struct _WapiHandleShared *handle)
100 {
101         static guchar buf[80];
102         guchar *name;
103         struct _WapiHandle_file *file=&handle->u.file;
104         
105         name=_wapi_handle_scratch_lookup (file->filename);
106         
107         g_snprintf (buf, sizeof(buf),
108                     "[%20s] acc: %c%c%c, shr: %c%c%c, attrs: %5u",
109                     name==NULL?(guchar *)"":name,
110                     file->fileaccess&GENERIC_READ?'R':'.',
111                     file->fileaccess&GENERIC_WRITE?'W':'.',
112                     file->fileaccess&GENERIC_EXECUTE?'X':'.',
113                     file->sharemode&FILE_SHARE_READ?'R':'.',
114                     file->sharemode&FILE_SHARE_WRITE?'W':'.',
115                     file->sharemode&FILE_SHARE_DELETE?'D':'.',
116                     file->attrs);
117
118         if(name!=NULL) {
119                 g_free (name);
120         }
121         
122         return(buf);
123 }
124
125 static const guchar *console_details (struct _WapiHandleShared *handle)
126 {
127         return(file_details (handle));
128 }
129
130 static const guchar *thread_details (struct _WapiHandleShared *handle)
131 {
132         static guchar buf[80];
133         struct _WapiHandle_thread *thr=&handle->u.thread;
134
135         g_snprintf (buf, sizeof(buf),
136                     "proc: %p, state: %d, exit: %u",
137                     thr->process_handle, thr->state, thr->exitstatus);
138         
139         return(buf);
140 }
141
142 static const guchar *sem_details (struct _WapiHandleShared *handle)
143 {
144         static guchar buf[80];
145         struct _WapiHandle_sem *sem=&handle->u.sem;
146         
147         g_snprintf (buf, sizeof(buf), "val: %5u, max: %5d",
148                     sem->val, sem->max);
149         
150         return(buf);
151 }
152
153 static const guchar *mutex_details (struct _WapiHandleShared *handle)
154 {
155         static guchar buf[80];
156         guchar *name;
157         struct _WapiHandle_mutex *mut=&handle->u.mutex;
158         
159         name=_wapi_handle_scratch_lookup (mut->sharedns.name);
160         
161         g_snprintf (buf, sizeof(buf), "[%20s] own: %5d:%5ld, count: %5u",
162                     name==NULL?(guchar *)"":name, mut->pid, mut->tid,
163                     mut->recursion);
164
165         if(name!=NULL) {
166                 g_free (name);
167         }
168         
169         return(buf);
170 }
171
172 static const guchar *event_details (struct _WapiHandleShared *handle)
173 {
174         static guchar buf[80];
175         struct _WapiHandle_event *event=&handle->u.event;
176
177         g_snprintf (buf, sizeof(buf), "manual: %s",
178                     event->manual?"TRUE":"FALSE");
179         
180         return(buf);
181 }
182
183 static const guchar *socket_details (struct _WapiHandleShared *handle)
184 {
185         /* Nothing to see here */
186         return("");
187 }
188
189 static const guchar *find_details (struct _WapiHandleShared *handle)
190 {
191         /* Nothing to see here either */
192         return("");
193 }
194
195 static const guchar *process_details (struct _WapiHandleShared *handle)
196 {
197         static guchar buf[80];
198         guchar *name;
199         struct _WapiHandle_process *proc=&handle->u.process;
200         
201         name=_wapi_handle_scratch_lookup (proc->proc_name);
202         
203         g_snprintf (buf, sizeof(buf), "[%20s] pid: %5u",
204                     name==NULL?(guchar *)"":name, proc->id);
205
206         if(name!=NULL) {
207                 g_free (name);
208         }
209         
210         return(buf);
211 }
212
213 static const guchar *pipe_details (struct _WapiHandleShared *handle)
214 {
215         return(file_details (handle));
216 }