475173a2a597463114a02b98b5ace0c99d6ff967
[charm.git] / src / conv-core / debug-conv.c
1 /*
2 Converse-level debugger support
3
4 Collected from convcore.c, conv-ccs.c, register.c by
5 Orion Sky Lawlor, olawlor@acm.org, 4/10/2001
6  */
7 #include <stdio.h> /*for sscanf*/
8 #include <string.h> /*for strcmp*/
9 #include "converse.h"
10 #include "conv-trace.h"
11 #include "queueing.h"
12 #include "conv-ccs.h"
13 #include <errno.h>
14
15 CpvStaticDeclare(int, freezeModeFlag);
16 CpvStaticDeclare(int, continueFlag);
17 CpvStaticDeclare(int, stepFlag);
18 CpvDeclare(void *, debugQueue);
19
20 /***************************************************
21   The CCS interface to the debugger
22 */
23
24 #include <string.h>
25
26 #include "pup_c.h"
27 void * (*CpdDebugGetAllocationTree)(int *);
28 void (*CpdDebug_pupAllocationPoint)(pup_er p, void *data);
29 void (*CpdDebug_deleteAllocationPoint)(void *ptr);
30 void * (*CpdDebug_MergeAllocationTree)(void *data, void **remoteData, int numRemote);
31 CpvDeclare(int, CpdDebugCallAllocationTree_Index);
32
33 static void CpdDebugReturnAllocationTree(void *tree) {
34   pup_er sizer = pup_new_sizer();
35   char *buf;
36   pup_er packer;
37   int i;
38   CpdDebug_pupAllocationPoint(sizer, tree);
39   buf = (char *)malloc(pup_size(sizer));
40   packer = pup_new_toMem(buf);
41   CpdDebug_pupAllocationPoint(packer, tree);
42   /*CmiPrintf("size=%d tree:",pup_size(sizer));
43   for (i=0;i<100;++i) CmiPrintf(" %02x",((unsigned char*)buf)[i]);
44   CmiPrintf("\n");*/
45   CcsSendReply(pup_size(sizer),buf);
46   pup_destroy(sizer);
47   pup_destroy(packer);
48   free(buf);
49 }
50
51 static void CpdDebugCallAllocationTree(char *msg)
52 {
53   int numNodes;
54   int forPE;
55   void *tree;
56   sscanf(msg+CmiMsgHeaderSizeBytes, "%d", &forPE);
57   if (forPE == -1 && CmiMyPe()==0) {
58     CmiSetHandler(msg, CpvAccess(CpdDebugCallAllocationTree_Index));
59     CmiSyncBroadcast(CmiMsgHeaderSizeBytes+sizeof(int), msg);
60   }
61   tree = CpdDebugGetAllocationTree(&numNodes);
62   if (forPE == CmiMyPe()) CpdDebugReturnAllocationTree(tree);
63   else if (forPE == -1) CmiReduceStruct(tree, CpdDebug_pupAllocationPoint, CpdDebug_MergeAllocationTree,
64                                 CpdDebugReturnAllocationTree, CpdDebug_deleteAllocationPoint);
65   else CmiAbort("Received allocationTree request for another PE!");
66   CmiFree(msg);
67 }
68
69 static void CpdDebugHandler(char *msg)
70 {
71     char name[128];
72     sscanf(msg+CmiMsgHeaderSizeBytes, "%s", name);
73
74     if (strcmp(name, "freeze") == 0) {
75       CpdFreeze();
76     }
77     else if (strcmp(name, "unfreeze") == 0) {
78       CpdUnFreeze();
79     }
80     else if (strncmp(name, "step", strlen("step")) == 0){
81       CmiPrintf("step received\n");
82       CpvAccess(stepFlag) = 1;
83       CpdUnFreeze();
84     }
85     else if (strncmp(name, "continue", strlen("continue")) == 0){
86       CmiPrintf("continue received\n");
87       CpvAccess(continueFlag) = 1;
88       CpdUnFreeze();
89     }
90 #if 0
91     else if (strncmp(name, "setBreakPoint", strlen("setBreakPoint")) == 0){
92       CmiPrintf("setBreakPoint received\n");
93       temp = strstr(name, "#");
94       temp++;
95       setBreakPoints(temp);
96     }
97 #endif
98     else{
99       CmiPrintf("bad debugger command:%s received,len=%ld\n",name,strlen(name));
100     }
101 }
102
103
104 /*
105  Start the freeze-- call will not return until unfrozen
106  via a CCS request.
107  */
108 void CpdFreeze(void)
109 {
110   if (CpvAccess(freezeModeFlag)) return; /*Already frozen*/
111   CpvAccess(freezeModeFlag) = 1;
112   CpdFreezeModeScheduler();
113 }
114
115 void CpdUnFreeze(void)
116 {
117   CpvAccess(freezeModeFlag) = 0;
118 }
119
120 /* Special scheduler-type loop only executed while in
121 freeze mode-- only executes CCS requests.
122 */
123 void CcsServerCheck(void);
124 extern int _isCcsHandlerIdx(int idx);
125
126 void CpdFreezeModeScheduler(void)
127 {
128 #if CMK_CCS_AVAILABLE
129     void *msg;
130     void *debugQ=CpvAccess(debugQueue);
131     CsdSchedulerState_t state;
132     CsdSchedulerState_new(&state);
133
134     /* While frozen, queue up messages */
135     while (CpvAccess(freezeModeFlag)) {
136 #if NODE_0_IS_CONVHOST
137       CcsServerCheck(); /*Make sure we can get CCS messages*/
138 #endif
139       msg = CsdNextMessage(&state);
140
141       if (msg!=NULL) {
142           int hIdx=CmiGetHandler(msg);
143           if(_isCcsHandlerIdx(hIdx))
144           /*A CCS request-- handle it immediately*/
145           {
146             CmiHandleMessage(msg);
147           }
148           else
149           /*An ordinary message-- queue it up*/
150             CdsFifo_Enqueue(debugQ, msg);
151       } else CmiNotifyIdle();
152     }
153     /* Before leaving freeze mode, execute the messages 
154        in the order they would have executed before.*/
155     while (!CdsFifo_Empty(debugQ))
156     {
157         char *queuedMsg = (char *)CdsFifo_Dequeue(debugQ);
158         CmiHandleMessage(queuedMsg);
159     }
160 #endif
161 }
162
163
164 void CpdInit(void)
165 {
166   CpvInitialize(int, freezeModeFlag);
167   CpvAccess(freezeModeFlag) = 0;
168
169   CpvInitialize(void *, debugQueue);
170   CpvAccess(debugQueue) = CdsFifo_Create();
171
172   CcsRegisterHandler("ccs_debug", (CmiHandler)CpdDebugHandler);
173   CcsRegisterHandler("ccs_debug_allocationTree", (CmiHandler)CpdDebugCallAllocationTree);
174   CpvInitialize(int, CpdDebugCallAllocationTree_Index);
175   CpvAccess(CpdDebugCallAllocationTree_Index) = CmiRegisterHandler((CmiHandler)CpdDebugCallAllocationTree);
176   
177 #if 0
178   CpdInitializeObjectTable();
179   CpdInitializeHandlerArray();
180   CpdInitializeBreakPoints();
181
182   /* To allow start in freeze state: */
183   msgListCleanup();
184   msgListCache();
185 #endif
186   
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204