source: anotherchoice/tags/jsp-1.4.4-full-UTF8/windev/cmdwatch/cmdwatch.cpp@ 26

Last change on this file since 26 was 26, checked in by ykominami, 10 years ago

initial

File size: 13.3 KB
Line 
1/*
2 * TOPPERS/JSP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
5 *
6 * Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 *
9 * 上記著作権者
10は,以下の (1)〜(4) の条件か,Free Software Foundation
11 * によってå…
12¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
13 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
14 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
15å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
16 * 利用と呼ぶ)することを無償で許諾する.
17 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
18 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
19 * スコード中に含まれていること.
20 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
21 * 用できる形で再é…
22å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
23å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
24 * 者
25マニュアルなど)に,上記の著作権表示,この利用条件および下記
26 * の無保証規定を掲載すること.
27 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
28 * 用できない形で再é…
29å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
30 * と.
31 * (a) 再é…
32å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
33マニュアルなど)に,上記の著
34 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
35 * (b) 再é…
36å¸ƒã®å½¢æ…
37‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
38 * 報告すること.
39 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
40 * 害からも,上記著作権者
41およびTOPPERSプロジェクトをå…
42è²¬ã™ã‚‹ã“と.
43 *
44 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
45お
46 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
47 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
48 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
49 *
50 * @(#) $Id: cmdwatch.cpp,v 1.3 2003/06/30 15:51:47 takayuki Exp $
51 */
52
53// cmdwatch.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
54//
55
56#include <stdio.h>
57#include <stdarg.h>
58
59#include <windows.h>
60#include <objbase.h>
61#include <unknwn.h>
62#include <ocidl.h>
63
64#include <conio.h>
65
66#pragma warning(disable:4200) //非標準の拡張機能 : 長さ0のé…
67åˆ—を持つ構造体
68
69#define TMAX_LOGINFO 8
70
71typedef unsigned int BITMASK;
72typedef struct {
73 unsigned int logtype;
74 unsigned int logtim;
75 unsigned int valid;
76 unsigned int bufsz;
77 unsigned char buf[];
78 } DBIFLOG;
79
80#define DBIFLOG_BUFMARGIN ((unsigned int)(&((DBIFLOG *)0)->buf))
81
82/*
83 * ログ情
84報の重要度の定義
85 */
86#define LOG_EMERG 0 /* シャットダウンに値するエラー */
87#define LOG_ALERT 1
88#define LOG_CRIT 2
89#define LOG_ERR 3 /* システムエラー */
90#define LOG_WARNING 4 /* 警告メッセージ */
91#define LOG_NOTICE 5
92#define LOG_INFO 6
93#define LOG_DEBUG 7 /* デバッグ用メッセージ */
94
95/*
96 * ログ情
97報の重要度のビットマップを作るためのマクロ
98 */
99#define LOG_MASK(prio) (1 << (prio))
100#define LOG_UPTO(prio) ((1 << ((prio) + 1)) - 1)
101
102/*
103 * ログ情
104報の種別の定義
105 */
106#define LOG_TYP_INTERRUPT 0x01 /* 割込みハンドラ */
107#define LOG_TYP_ISR 0x02 /* 割込みサービスハンドラ */
108#define LOG_TYP_TIMERHDR 0x03 /* タイムイベントハンドラ */
109#define LOG_TYP_CPUEXC 0x04 /* CPU例外ハンドラ */
110#define LOG_TYP_TSKEXC 0x05 /* タスク例外処理ルーチン */
111#define LOG_TYP_TSKSTAT 0x06 /* タスク状æ…
112‹å¤‰åŒ– */
113#define LOG_TYP_DISPATCH 0x07 /* ディスパッチャ */
114#define LOG_TYP_SVC 0x08 /* サービスコール */
115#define LOG_TYP_COMMENT 0x09 /* コメント(文字列のみのログ) */
116#define LOG_ENTER 0x00 /* å…
117¥å£ï¼é–‹å§‹ */
118#define LOG_LEAVE 0x80 /* 出口/終了 */
119
120
121
122static FILE * destination = NULL;
123
124static const char * apiname[] =
125{ "cre_tsk","del_tsk","act_tsk","can_act","sta_tsk","ext_tsk","exd_tsk","ter_tsk","chg_pri","get_pri","ref_tsk","ref_tst","slp_tsk","tslp_tsk","wup_tsk","can_wup","rel_wai","sus_tsk","rsm_tsk","frsm_tsk","dly_tsk","","def_tex","ras_tex","dis_tex","ena_tex","sns_tex","ref_tex","cre_sem","del_sem","sig_sem","","wai_sem","pol_sem","twai_sem","ref_sem","cre_flg","del_flg","set_flg","clr_flg","wai_flg","pol_flg","twai_flg","ref_flg","cre_dtq","del_dtq","","","snd_dtq","psnd_dtq","tsnd_dtq","fsnd_dtq","rcv_dtq","prcv_dtq","trcv_dtq","ref_dtq","cre_mbx","del_mbx","snd_mbx","","rcv_mbx","prcv_mbx","trcv_mbx","ref_mbx","cre_mpf","del_mpf","rel_mpf","","get_mpf","pget_mpf","tget_mpf","ref_mpf","set_tim","get_tim","cre_cyc","del_cyc","sta_cyc","stp_cyc","ref_cyc","","rot_rdq","get_tid","","","loc_cpu","unl_cpu","dis_dsp","ena_dsp","sns_ctx","sns_loc","sns_dsp","sns_dpn","ref_sys","","","","def_inh","cre_isr","del_isr","ref_isr","dis_int","ena_int","chg_ixx","get_ixx","def_svc","def_exc","ref_cfg","ref_ver","iact_tsk","iwup_tsk","irel_wai","iras_tex","isig_sem","iset_flg","ipsnd_dtq","ifsnd_dtq","irot_rdq","iget_tid","iloc_cpu","iunl_cpu","isig_tim","","","","cre_mtx","del_mtx","unl_mtx","","loc_mtx","ploc_mtx","tloc_mtx","ref_mtx","cre_mbf","del_mbf","","","snd_mbf","psnd_mbf","tsnd_mbf" };
126
127
128
129void Printf(const char * format, ... )
130{
131 va_list vl;
132 char buffer[1024];
133 int i;
134
135 va_start(vl,format);
136 i = ::vsprintf(buffer, format, vl);
137 if(destination != NULL)
138 fwrite(buffer, i, 1, destination);
139 fwrite(buffer, i, 1, stdout);
140}
141
142const char * get_apiname(int fncd)
143{ return apiname[-5 - fncd]; };
144
145void print_log(DBIFLOG * log)
146{
147 int i;
148
149 Printf("%10d ms : ",log->logtim);
150 switch(log->logtype)
151 {
152 case LOG_TYP_SVC:
153 Printf("[SVC|ENT] %s (",get_apiname(*(int*)log->buf));
154
155 for(i=0;i < *((int*)log->buf+1);i++)
156 {
157 if(i!=0)
158 Printf(", ");
159 Printf("0x%08x",*((int*)log->buf+2+i));
160 }
161 Printf(")\n");
162 break;
163
164 case LOG_TYP_SVC|LOG_LEAVE:
165 Printf("[SVC|LEA] %s",get_apiname(*(int*)log->buf));
166 if(*((int*)log->buf+1) > 0)
167 Printf(" (ercd = 0x%08x)",*((int*)log->buf+2));
168 Printf("\n");
169 break;
170
171 case LOG_TYP_TSKSTAT:
172 Printf("[TSKSTAT] Tsk:%d ",*(int*)log->buf);
173 switch(*((int*)log->buf+1))
174 {
175 case 0x01:
176 case 0x02:
177 Printf("<Ready>\n",*(int*)log->buf);
178 break;
179 case 0x04:
180 case 0x0c:
181 if(*((int*)log->buf+1) == 0x04)
182 Printf("<Waiting : ");
183 else
184 Printf("<Wait-Suspend : ");
185
186 switch(*((int*)log->buf+2))
187 {
188 case 0x1:
189 Printf("Sleep>\n");
190 break;
191 case 0x2:
192 Printf("Delay>\n");
193 break;
194 case 0x4:
195 Printf("Semaphore (id=%d)>\n",*((int*)log->buf+3));
196 break;
197 case 0x8:
198 Printf("Eventflag (id=%d)>\n",*((int*)log->buf+3));
199 break;
200 case 0x10:
201 Printf("Dataqueue[send] (id=%d)>\n",*((int*)log->buf+3));
202 break;
203 case 0x20:
204 Printf("Dataqueue[receive] (id=%d)>\n",*((int*)log->buf+3));
205 break;
206 case 0x40:
207 Printf("Mailbox (id=%d)>\n",*((int*)log->buf+3));
208 break;
209 case 0x80:
210 Printf("Mutex (id=%d)>\n",*((int*)log->buf+3));
211 break;
212 case 0x100:
213 Printf("MessageBuffer[send] (id=%d)>\n",*((int*)log->buf+3));
214 break;
215 case 0x200:
216 Printf("MessageBuffer[receive] (id=%d)>\n",*((int*)log->buf+3));
217 break;
218 case 0x400:
219 Printf("Rendezvous[call] (id=%d)>\n",*((int*)log->buf+3));
220 break;
221 case 0x800:
222 Printf("Rendezvous[accept] (id=%d)>\n",*((int*)log->buf+3));
223 break;
224 case 0x1000:
225 Printf("Rendezvous[completion] (id=%d)>\n",*((int*)log->buf+3));
226 break;
227 case 0x2000:
228 Printf("Fixed Memorypool (id=%d)>\n",*((int*)log->buf+3));
229 break;
230 case 0x4000:
231 Printf("Variable Memorypool (id=%d)>\n",*((int*)log->buf+3));
232 break;
233 default:
234 Printf(" complex [%04x]>\n", *((int*)log->buf+2));
235 break;
236 }
237 break;
238
239 case 0x08:
240 Printf("<Suspended>\n");
241 break;
242 case 0x10:
243 Printf("<Dormant>\n");
244 break;
245 default:
246 Printf("<Unknown : %d>",*(int*)log->buf);
247 break;
248 }
249 break;
250
251 case LOG_TYP_TSKEXC:
252 Printf("[TEX|ENT] Tsk:%d\n",*(int*)log->buf);
253 break;
254
255 case LOG_TYP_TSKEXC | LOG_LEAVE:
256 Printf("[TEX|LEA] Tsk:%d\n",*(int*)log->buf);
257 break;
258
259 case LOG_TYP_DISPATCH:
260 if(*((int*)log->buf+1) == 0)
261 Printf("[DSP|ENT] Tsk:%d Task-context\n",*(int*)log->buf);
262 else
263 Printf("[DSP|ENT] Tsk:%d Nontask-context\n",*(int*)log->buf);
264 break;
265
266 case LOG_TYP_DISPATCH | LOG_LEAVE:
267 Printf("[DSP|LEA] Tsk:%d\n",*(int*)log->buf);
268 break;
269
270 case LOG_TYP_COMMENT:
271 *((char *)log->buf + log->bufsz -1) = '\x0';
272 Printf("[COMMENT](%d) : <%s>\n",*(int *)log->buf,log->buf+sizeof(int));
273 break;
274
275 case LOG_TYP_INTERRUPT:
276 Printf("[INT|ENT] %d\n",*(int*)log->buf);
277 break;
278
279 case LOG_TYP_INTERRUPT | LOG_LEAVE:
280 Printf("[INT|LEA] %d\n",*(int*)log->buf);
281 break;
282
283 case LOG_TYP_CPUEXC:
284 Printf("[CEX|ENT] Tsk:%d\n",*(int*)log->buf);
285 break;
286
287 case LOG_TYP_CPUEXC | LOG_LEAVE:
288 Printf("[CEX|LEA] Tsk:%d\n",*(int*)log->buf);
289 break;
290
291 case LOG_TYP_TIMERHDR:
292 case LOG_TYP_TIMERHDR | LOG_LEAVE:
293 if((log->logtype & LOG_LEAVE) != 0)
294 Printf("[INT|LEA]");
295 else
296 Printf("[INT|ENT]");
297
298 if( *(int*)log->buf == 0x8d )
299 Printf(" cyclic ");
300 else
301 Printf(" OBJ(%02x) ", *(int*)log->buf);
302
303 Printf(" ID:%d EXINF:0x%08x\n",*((int*)log->buf+1), *((int*)log->buf+2));
304 break;
305
306 default:
307 Printf("[UNKNOWN] : 0x%x\n",log->logtype);
308 };
309}
310
311
312
313
314const CLSID CLSID_KernelLog = {0x4BD327FC,0x9E4A,0x4A5D,{0x95,0x03,0x27,0xC9,0x79,0xA8,0xE8,0x02}};
315
316MIDL_INTERFACE("395F900A-AC7E-4A78-9BC1-EE5EF76254FF")
317IKernelLog : public IUnknown
318{
319public:
320};
321
322
323struct __declspec(uuid("35E35399-55ED-45FC-8F0B-4A1BC6CEA3F0")) _IKernelLogEvents;
324
325class IKernelLogEvents : public IUnknown
326{
327public:
328 STDMETHOD(QueryInterface)(REFIID, void **);
329 STDMETHOD_(ULONG,AddRef)();
330 STDMETHOD_(ULONG,Release)();
331 virtual HRESULT STDMETHODCALLTYPE OnKernelStart(void);
332 virtual HRESULT STDMETHODCALLTYPE OnKernelExit(void);
333 virtual HRESULT STDMETHODCALLTYPE OnLogEvent(long sz, byte __RPC_FAR data[]);
334
335 IKernelLogEvents(void) { RefCount = 0; };
336// virtual ~IKernelLogEvents(void) { if(destination != NULL) fclose(destination); };
337
338 LONG RefCount;
339};
340
341HRESULT IKernelLogEvents::QueryInterface(REFIID riid, void ** ppvObject)
342{
343 if( riid == __uuidof(_IKernelLogEvents) || riid == __uuidof(IUnknown) )
344 {
345 *ppvObject = this;
346 ::InterlockedIncrement(&RefCount);
347 return S_OK;
348 }
349
350 return E_NOINTERFACE;
351}
352
353ULONG IKernelLogEvents::AddRef(void)
354{ return ::InterlockedIncrement(&RefCount); }
355
356ULONG IKernelLogEvents::Release(void)
357{
358 if( ::InterlockedDecrement(&RefCount) == 0 )
359 {
360 delete this;
361 return 0;
362 }
363 return RefCount;
364}
365
366HRESULT IKernelLogEvents::OnKernelExit(void)
367{
368 Printf("OnKernelExit at \n");
369
370 if(destination != NULL)
371 {
372 fclose(destination);
373 destination = 0;
374 }
375 return S_OK;
376}
377
378HRESULT IKernelLogEvents::OnKernelStart(void)
379{
380 char buffer[256];
381 SYSTEMTIME systim;
382
383 if(destination != NULL)
384 fclose(destination);
385
386 ::GetLocalTime(&systim);
387 ::wsprintf(buffer, "kernel-log-%04d%02d%02d-%02d%02d%02d.txt", systim.wYear, systim.wMonth, systim.wDay, systim.wHour, systim.wMinute, systim.wSecond);
388 destination = fopen(buffer, "wt");
389
390 Printf("OnKernelStart\n");
391 return S_OK;
392}
393
394HRESULT IKernelLogEvents::OnLogEvent(long sz, byte data [])
395{
396 print_log((DBIFLOG *)data);
397 return S_OK;
398}
399
400
401 //コネクションポイントへの接続
402bool Advise(IUnknown * container, REFIID iid, IUnknown * sink, DWORD * cookie, IConnectionPoint ** pcp = NULL)
403{
404 IConnectionPointContainer * cpc;
405 IConnectionPoint * cp;
406
407 HRESULT result;
408
409 if(pcp == NULL)
410 pcp = &cp;
411
412 container->QueryInterface(IID_IConnectionPointContainer, (void **)&cpc);
413 if(cpc == 0)
414 return false;
415
416 cpc->FindConnectionPoint(iid, pcp);
417 cpc->Release();
418 if(*pcp == 0)
419 return false;
420
421 if(FAILED(result = (*pcp)->Advise(sink, cookie)))
422 return false;
423
424 if(pcp == &cp)
425 cp->Release();
426 return true;
427}
428
429DWORD tmain_threadid;
430
431BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
432{
433 switch(dwCtrlType)
434 {
435 case CTRL_C_EVENT:
436 ::PostThreadMessage(tmain_threadid,WM_QUIT,0,0);
437 break;
438 default:
439 return FALSE;
440 }
441
442 return TRUE;
443}
444
445int main(int argc, char * argv[])
446{
447 IKernelLog * pKernelLog;
448 IKernelLogEvents * psKernelLogEvents;
449 IConnectionPoint * cp;
450 DWORD cookie;
451 MSG msg;
452
453 IKernelLogEvents a;
454
455 ::CoInitialize(NULL);
456
457 destination = 0;
458 tmain_threadid = ::GetCurrentThreadId();
459
460 if(!FAILED(::CoCreateInstance(CLSID_KernelLog, NULL, CLSCTX_ALL, __uuidof(IKernelLog), (void **)&pKernelLog)))
461 {
462 cp = NULL;
463 psKernelLogEvents = new IKernelLogEvents;
464
465 if(Advise(pKernelLog, __uuidof(_IKernelLogEvents), psKernelLogEvents, &cookie, &cp))
466 {
467 ::SetConsoleCtrlHandler(HandlerRoutine, TRUE);
468 while(::GetMessage(&msg, NULL, 0, 0) != 0)
469 { ::DispatchMessage(&msg); }
470 ::SetConsoleCtrlHandler(HandlerRoutine, FALSE);
471
472 cp->Unadvise(cookie);
473 }
474
475 delete psKernelLogEvents;
476
477 if(pKernelLog != NULL)
478 pKernelLog->Release();
479 }
480
481 if(destination != NULL)
482 {
483 Printf("\n\nSuspended by Ctrl-C event occursion.\n");
484 fclose(destination);
485 }
486
487 ::CoUninitialize();
488 return 0;
489}
490
Note: See TracBrowser for help on using the repository browser.