-
Notifications
You must be signed in to change notification settings - Fork 392
/
omrthreadinspect.c
383 lines (336 loc) · 10.7 KB
/
omrthreadinspect.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
/*******************************************************************************
* Copyright (c) 1991, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
/**
* @file
* @ingroup Thread
*/
/**
* This file contains thread routines which are compiled twice -- once for in-process, and
* once for out-of-process uses (e.g. debug extensions).
* The APIs in this file are only used for inspecting threads -- not for modifying them
*/
#if defined(LINUX)
/* Allowing the use of pthread_attr_getstack in omrthread_get_stack_range */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <features.h>
#elif defined(OSX)
#include <pthread.h>
#define _XOPEN_SOURCE
#endif /* defined(OSX) */
#include "threaddef.h"
#include "omrthreadinspect.h"
#define READU(field) ((uintptr_t)(field))
#define READP(field) (field)
static omrthread_library_t get_default_library(void);
/**
* Initialize a omrthread_monitor_walk_state_t structure used to walk the monitor pools.
*
* @param[in] walkState This is a pointer to a omrthread_monitor_walk_state_t. When this call returns it
* will be ready to use on the first call to omrthread_monitor_walk
* @return void
*
* @see omrthread_monitor_walk
*
*/
void
omrthread_monitor_init_walk(omrthread_monitor_walk_state_t *walkState)
{
omrthread_library_t lib = get_default_library();
ASSERT(lib);
ASSERT(lib->monitor_pool);
ASSERT(lib->monitor_pool->entries);
walkState->pool = lib->monitor_pool;
walkState->monitorIndex = 0;
walkState->lockTaken = FALSE;
}
/**
* Walk all active monitors.
*
* NOTE: Do *NOT* factor out the calls to MACRO_SELF().
* e.g. omrthread_t self = MACRO_SELF();
* They must be made within the calls to GLOBAL_(UN)LOCK so that
* when this file is compiled for "out of process" (i.e. in dbgext),
* these GLOBAL_* macros become nops, so no call is made to MACRO_SELF(),
* and therefore there's no required reference to 'default_library'.
*
* @param[in] walkState This is a pointer to a omrthread_monitor_walk_state_t. It should be initialized using omrthread_monitor_init_walk
* before calling omrthread_monitor_walk to start a new walk. (thread lib will be globally locked on the first call)
* @return a pointer to a monitor, or NULL if all monitors walked (thread lib will be globally unlocked when NULL is returned).
*
* @note As this is currently implemented, this must be called to walk ALL monitors. It can't
* be used to look for a specific monitor and then quit because the GLOBAL_LOCK be taken when
* the walk starts and exited when the walk ends. If the walk is not completed the lock will not
* be released.
*
* @see omrthread_monitor_walk_no_locking
*
*/
omrthread_monitor_t
omrthread_monitor_walk(omrthread_monitor_walk_state_t *walkState)
{
omrthread_monitor_t monitor = NULL;
if (FALSE == walkState->lockTaken) {
/* Take the lock if we are starting the walk */
GLOBAL_LOCK(MACRO_SELF(), CALLER_MONITOR_WALK);
walkState->lockTaken = TRUE;
}
monitor = omrthread_monitor_walk_no_locking(walkState);
if (NULL == monitor) {
/* Release the lock if we have finished the walk */
walkState->lockTaken = FALSE;
GLOBAL_UNLOCK(MACRO_SELF());
}
return monitor;
}
/**
* Walk all active monitors without locking the global lock.
*
* The caller MUST own the global lock.
*
* @param[in] walkState This is a pointer to a omrthread_monitor_walk_state_t. It should be initialized
* using omrthread_monitor_init_walk before calling omrthread_monitor_walk_no_locking to start a new walk.
* @return a pointer to a monitor, or NULL if all monitors walked
*
* @note Because the caller has explicit ownership of the global lock, the caller does not have to walk all monitors.
*
* @see omrthread_monitor_walk
*
*/
omrthread_monitor_t
omrthread_monitor_walk_no_locking(omrthread_monitor_walk_state_t *walkState)
{
omrthread_monitor_t monitor = NULL;
if (walkState->monitorIndex >= MONITOR_POOL_SIZE) {
if (NULL == (walkState->pool = walkState->pool->next)) {
/* we've walked all the monitors, final monitor was in use */
return NULL;
}
walkState->monitorIndex = 0;
}
monitor = &(walkState->pool->entries[walkState->monitorIndex]);
while (FREE_TAG == monitor->count) {
walkState->monitorIndex++;
if (walkState->monitorIndex >= MONITOR_POOL_SIZE) {
if (NULL == (walkState->pool = walkState->pool->next)) {
/* we've walked all the monitors, final monitor was free */
return NULL;
}
walkState->monitorIndex = 0;
}
monitor = &(walkState->pool->entries[walkState->monitorIndex]);
}
walkState->monitorIndex++;
return monitor;
}
/**
* Get a thread's thread local storage (TLS) value.
*
* @param[in] a thread
* @param[in] key key to have TLS value returned (value returned by omrthread_tls_alloc)
* @return pointer to location of TLS or NULL on failure.
*
*/
void *
omrthread_tls_get(omrthread_t thread, omrthread_tls_key_t key)
{
return (void *)READU(thread->tls[key - 1]);
}
/**
* Return a thread's scheduling priority.
*
* @param[in] thread (non-NULL)
* @return scheduling priority
* @see omrthread_create, omrthread_set_priority
*
*/
uintptr_t
omrthread_get_priority(omrthread_t thread)
{
ASSERT(thread);
return READU(thread->priority);
}
/**
* Return a thread's flags.
*
* @param[in] thread (non-NULL)
* @param[in] blocker if non-NULL, will be set to the monitor on which the thread is blocked (if any)
* @return flags
*
*/
uintptr_t
omrthread_get_flags(omrthread_t thread, omrthread_monitor_t *blocker)
{
uintptr_t flags;
ASSERT(thread);
OMROSMUTEX_ENTER(thread->mutex);
if (blocker) {
*blocker = READP(thread->monitor);
}
flags = READU(thread->flags);
OMROSMUTEX_EXIT(thread->mutex);
return flags;
}
/**
* Return a thread's state.
* The returned data is not validated in any way, and may appear inconsistent if,
* for example, the thread owning the blocker exits the blocker monitor.
*
* @param[in] thread (non-NULL)
* @param[out] state
* @return void
*/
void
omrthread_get_state(omrthread_t thread, omrthread_state_t *const state)
{
if (!thread) {
return;
}
if (!state) {
return;
}
OMROSMUTEX_ENTER(thread->mutex);
state->flags = READU(thread->flags);
state->blocker = READP(thread->monitor);
if (state->blocker) {
state->owner = READP(state->blocker->owner);
state->count = READU(state->blocker->count);
} else {
state->owner = 0;
state->count = 0;
}
OMROSMUTEX_EXIT(thread->mutex);
}
/**
* Return a thread's OS id
*
* @param[in] thread (non-NULL)
* @return OS id
* @see omrthread_get_ras_tid
*/
uintptr_t
omrthread_get_osId(omrthread_t thread)
{
ASSERT(thread);
return READU(thread->tid);
}
/**
* Return a monitor's name.
*
* @param[in] monitor (non-NULL)
* @return pointer to the monitor's name (may be NULL)
*
* @see omrthread_monitor_init_with_name
*
*/
char *
omrthread_monitor_get_name(omrthread_monitor_t monitor)
{
ASSERT(monitor);
return READP(monitor->name);
}
/**
* Get native stack address range and size (VMDESIGN 2038)
* @param[in] thread Current native thread
* @param[in] stackStart A pointer to the starting of native thread stack
* @param[in] stackEnd A pointer to the end of native thread stack
* @return success or failure
* @retval J9THREAD_SUCCESS success
* @retval J9THREAD_ERR_INVALID_THREAD for NULL thread argument
* @retval J9THREAD_ERR_GETATTR_NP for pthread_getattr_np failure. thread->errno set
* @retval J9THREAD_ERR_GETSTACK for stack range retrieval failure. thread->errno set
* @retval J9THREAD_ERR_UNSUPPORTED_PLAT for unsupported platform
*/
uintptr_t
omrthread_get_stack_range(omrthread_t thread, void **stackStart, void **stackEnd)
{
#if defined(LINUX) || defined(AIXPPC)
pthread_attr_t attr;
OSTHREAD osTid = thread->handle;
uintptr_t rc = 0;
size_t stackSize;
if (!thread) {
return J9THREAD_ERR_INVALID_THREAD;
}
/* Retrieve the pthread_attr_t from the thread */
if ((rc = pthread_getattr_np(osTid, &attr)) != 0) {
thread->os_errno = rc;
return (J9THREAD_ERR_GETATTR_NP | J9THREAD_ERR_OS_ERRNO_SET);
}
/* Retrieve base stack address and stack size from pthread_attr_t */
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
if ((rc = pthread_attr_getstack(&attr, stackStart, &stackSize)) != 0) {
thread->os_errno = rc;
return (J9THREAD_ERR_GETSTACK | J9THREAD_ERR_OS_ERRNO_SET);
}
#else /* (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) */
if ((rc = pthread_attr_getstackaddr(&attr, stackStart)) != 0) {
thread->os_errno = rc;
return (J9THREAD_ERR_GETSTACK | J9THREAD_ERR_OS_ERRNO_SET);
}
if ((rc = pthread_attr_getstacksize(&attr, &stackSize)) != 0) {
thread->os_errno = rc;
return (J9THREAD_ERR_GETSTACK | J9THREAD_ERR_OS_ERRNO_SET);
}
#endif /* (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) */
pthread_attr_destroy(&attr);
/* On Linux, native stack grows from high to low memory */
*stackEnd = (void *)((uintptr_t)*stackStart + stackSize);
return J9THREAD_SUCCESS;
#elif defined(OSX) /* defined(LINUX) || defined(AIXPPC) */
OSTHREAD osTid = thread->handle;
size_t stackSize = 0;
*stackStart = pthread_get_stackaddr_np(osTid);
stackSize = pthread_get_stacksize_np(osTid);
*stackEnd = (void *)((uintptr_t)*stackStart + stackSize);
return J9THREAD_SUCCESS;
#else /* defined(OSX) */
return J9THREAD_ERR_UNSUPPORTED_PLAT;
#endif /* defined(LINUX) || defined(AIXPPC) */
}
#if (defined(OMR_THR_JLM))
/*
* Return a monitor's tracing information.
*
* @param[in] monitor (non-NULL)
* @return pointer to the monitor's tracing information (may be NULL)
*
*/
J9ThreadMonitorTracing *
omrthread_monitor_get_tracing(omrthread_monitor_t monitor)
{
ASSERT(monitor);
return READP(monitor->tracing);
}
#endif /* OMR_THR_JLM */
/**
* Return the default threading library.
*
* @return pointer to the default threading library
*
*/
static omrthread_library_t
get_default_library(void)
{
return GLOBAL_DATA(default_library);
}