@@ -48,7 +48,6 @@ extern "C"
48
48
// Typically these will be threads used by the communication stack to communicate with GDB or other important system
49
49
// threads.
50
50
static const char * g_threadNamesToIgnore[] = {
51
- " rtx_idle"
52
51
};
53
52
54
53
@@ -88,6 +87,17 @@ static const char* g_threadNamesToIgnore[] = {
88
87
89
88
90
89
90
+ // Structure used to store a 'suspended' thread's original priority settings (current and base) so that they can be
91
+ // later restored.
92
+ struct ThreadPriority
93
+ {
94
+ int8_t priority;
95
+ int8_t basePriority;
96
+ };
97
+
98
+
99
+
100
+
91
101
// Globals that describe the ThreadDebug singleton.
92
102
static DebugCommInterface* g_pComm;
93
103
static bool g_breakInSetup;
@@ -98,15 +108,17 @@ static volatile osThreadId_t g_haltedThreadId;
98
108
// The list of threads which were suspended upon entry into the debugger. These are the threads that will be resumed
99
109
// upon exit from the debugger.
100
110
static osThreadId_t g_suspendedThreads[MAXIMUM_ACTIVE_THREADS];
101
- // The state of each thread before being suspended .
102
- static uint8_t g_threadStates [MAXIMUM_ACTIVE_THREADS];
111
+ // The priorities (current and base) of each thread modified to suspend application threads when halting in debugger .
112
+ static ThreadPriority g_threadPriorities [MAXIMUM_ACTIVE_THREADS];
103
113
// The number of active threads that were placed in g_suspendedThreads. Some of those entries may be NULL if they were
104
114
// important enough to not be suspended.
105
115
static uint32_t g_threadCount;
106
116
// The current index into the g_suspendedThreads array being returned from Platform_RtosGetFirstThread.
107
117
static uint32_t g_threadIndex;
108
118
// Buffer to be used for storing extra thread info.
109
119
static char g_threadExtraInfo[64 ];
120
+ // The ID of the rtx_idle thread to be skipped when providing list of threads to GDB.
121
+ static osThreadId_t g_idleThread;
110
122
111
123
// This flag is set to a non-zero value if the DebugMon handler is to re-enable DWT watchpoints and FPB breakpoints
112
124
// after being disabled by the HardFault handler when a debug event is encounted in handler mode.
@@ -185,6 +197,7 @@ static void wakeMriMainToDebugCurrentThread();
185
197
static void stopSingleStepping ();
186
198
static void recordAndSwitchFaultHandlersToDebugger ();
187
199
static void skipNullThreadIds ();
200
+ static bool isNullOrIdleThread (osThreadId_t threadId);
188
201
static const char * getThreadStateName (uint8_t threadState);
189
202
static bool isDebugThreadActive ();
190
203
static void setFaultDetectedFlag ();
@@ -236,7 +249,7 @@ static __NO_RETURN void mriMain(void *pv)
236
249
{
237
250
// Run the code which suspends, resumes, etc the other threads at highest priority so that it doesn't context
238
251
// switch to one of the other threads. Switch to normal priority when running mriDebugException() though.
239
- osThreadSetPriority (osThreadGetId (), osPriorityRealtime7 );
252
+ osThreadSetPriority (osThreadGetId (), osPriorityISR );
240
253
241
254
while (1 ) {
242
255
int waitResult = osThreadFlagsWait (MRI_THREAD_DEBUG_EVENT_FLAG, osFlagsWaitAny, osWaitForever);
@@ -250,32 +263,42 @@ static __NO_RETURN void mriMain(void *pv)
250
263
251
264
osThreadSetPriority (osThreadGetId (), osPriorityNormal);
252
265
mriDebugException (&mriCortexMState.context );
253
- osThreadSetPriority (osThreadGetId (), osPriorityRealtime7 );
266
+ osThreadSetPriority (osThreadGetId (), osPriorityISR );
254
267
255
268
if (Platform_IsSingleStepping ()) {
256
269
mriThreadSingleStepThreadId = g_haltedThreadId;
257
270
switchRtxHandlersToDebugStubsForSingleStepping ();
258
- osThreadResume (mriThreadSingleStepThreadId);
259
- } else {
260
- resumeApplicationThreads ();
261
271
}
272
+ resumeApplicationThreads ();
262
273
g_haltedThreadId = 0 ;
263
274
}
264
275
}
265
276
266
277
static void suspendAllApplicationThreads ()
267
278
{
279
+ // Suspend application threads by setting their priorities to the lowest setting, osPriorityIdle.
280
+ // Bump rtx_idle thread up one priority level so that it will run instead of the 'suspended' application threads if
281
+ // the debug related threads go idle.
282
+ g_idleThread = 0 ;
268
283
g_threadCount = osThreadGetCount ();
269
284
ASSERT ( g_threadCount <= ARRAY_SIZE (g_suspendedThreads) );
270
285
osThreadEnumerate (g_suspendedThreads, ARRAY_SIZE (g_suspendedThreads));
271
286
for (uint32_t i = 0 ; i < g_threadCount ; i++) {
272
287
osThreadId_t thread = g_suspendedThreads[i];
288
+ osPriority_t newPriority = osPriorityIdle;
289
+ const char * pThreadName = osThreadGetName (thread);
290
+ if (strcmp (pThreadName, " rtx_idle" ) == 0 ) {
291
+ newPriority = (osPriority_t)(osPriorityIdle + 1 );
292
+ g_idleThread = thread;
293
+ }
294
+
273
295
if (isThreadToIgnore (thread)) {
274
296
g_suspendedThreads[i] = 0 ;
275
297
} else {
276
298
osRtxThread_t* pThread = (osRtxThread_t*)thread;
277
- g_threadStates[i] = pThread->state ;
278
- osThreadSuspend (thread);
299
+ g_threadPriorities[i].basePriority = pThread->priority_base ;
300
+ g_threadPriorities[i].priority = pThread->priority ;
301
+ osThreadSetPriority (thread, newPriority);
279
302
}
280
303
}
281
304
}
@@ -301,7 +324,9 @@ static void resumeApplicationThreads()
301
324
for (uint32_t i = 0 ; i < g_threadCount ; i++) {
302
325
osThreadId_t thread = g_suspendedThreads[i];
303
326
if (thread != 0 ) {
304
- osThreadResume (thread);
327
+ osThreadSetPriority (thread, (osPriority_t)g_threadPriorities[i].priority );
328
+ osRtxThread_t* pThread = (osRtxThread_t*)thread;
329
+ pThread->priority_base = g_threadPriorities[i].basePriority ;
305
330
}
306
331
}
307
332
}
@@ -549,20 +574,20 @@ uint32_t Platform_RtosGetNextThreadId(void)
549
574
550
575
static void skipNullThreadIds ()
551
576
{
552
- while (g_threadIndex < g_threadCount && g_suspendedThreads[g_threadIndex] == 0 )
577
+ while (g_threadIndex < g_threadCount && isNullOrIdleThread ( g_suspendedThreads[g_threadIndex]) )
553
578
g_threadIndex++;
554
579
}
555
580
581
+ static bool isNullOrIdleThread (osThreadId_t threadId)
582
+ {
583
+ return threadId == 0 || threadId == g_idleThread;
584
+ }
585
+
556
586
const char * Platform_RtosGetExtraThreadInfo (uint32_t threadId)
557
587
{
558
- const char * pState = " " ;
559
588
const char * pThreadName = osThreadGetName ((osThreadId)threadId);
560
- for (uint32_t i = 0 ; i < g_threadCount ; i++) {
561
- if ((uint32_t )g_suspendedThreads[i] == threadId) {
562
- pState = getThreadStateName (g_threadStates[i]);
563
- break ;
564
- }
565
- }
589
+ osRtxThread_t* pThread = (osRtxThread_t*)threadId;
590
+ const char * pState = getThreadStateName (pThread->state );
566
591
snprintf (g_threadExtraInfo, sizeof (g_threadExtraInfo), " \" %s\" %s" , pThreadName ? pThreadName : " " , pState);
567
592
return g_threadExtraInfo;
568
593
}
0 commit comments