1 | /* $Id: RTThreadGetNativeState-linux.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTThreadGetNativeState, linux implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
32 | #include <iprt/thread.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/errcore.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 |
|
---|
40 | #include "internal/thread.h"
|
---|
41 |
|
---|
42 | #include <unistd.h>
|
---|
43 | #include <sys/fcntl.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | RTDECL(RTTHREADNATIVESTATE) RTThreadGetNativeState(RTTHREAD hThread)
|
---|
47 | {
|
---|
48 | RTTHREADNATIVESTATE enmRet = RTTHREADNATIVESTATE_INVALID;
|
---|
49 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
50 | if (pThread)
|
---|
51 | {
|
---|
52 | enmRet = RTTHREADNATIVESTATE_UNKNOWN;
|
---|
53 |
|
---|
54 | char szName[512];
|
---|
55 | RTStrPrintf(szName, sizeof(szName), "/proc/self/task/%u/stat", pThread->tid);
|
---|
56 | int fd = open(szName, O_RDONLY, 0);
|
---|
57 | if (fd >= 0)
|
---|
58 | {
|
---|
59 | ssize_t cch = read(fd, szName, sizeof(szName) - 1);
|
---|
60 | close(fd);
|
---|
61 | if (cch > 0)
|
---|
62 | {
|
---|
63 | szName[cch] = '\0';
|
---|
64 |
|
---|
65 | /* skip the pid, the (comm name) and stop at the status char. */
|
---|
66 | const char *psz = szName;
|
---|
67 | while ( *psz
|
---|
68 | && ( *psz != ')'
|
---|
69 | || !RT_C_IS_SPACE(psz[1])
|
---|
70 | || !RT_C_IS_ALPHA(psz[2])
|
---|
71 | || !RT_C_IS_SPACE(psz[3])
|
---|
72 | )
|
---|
73 | )
|
---|
74 | psz++;
|
---|
75 | if (*psz == ')')
|
---|
76 | {
|
---|
77 | switch (psz[2])
|
---|
78 | {
|
---|
79 | case 'R': /* running */
|
---|
80 | enmRet = RTTHREADNATIVESTATE_RUNNING;
|
---|
81 | break;
|
---|
82 |
|
---|
83 | case 'S': /* sleeping */
|
---|
84 | case 'D': /* disk sleeping */
|
---|
85 | enmRet = RTTHREADNATIVESTATE_BLOCKED;
|
---|
86 | break;
|
---|
87 |
|
---|
88 | case 'T': /* stopped or tracking stop */
|
---|
89 | enmRet = RTTHREADNATIVESTATE_SUSPENDED;
|
---|
90 | break;
|
---|
91 |
|
---|
92 | case 'Z': /* zombie */
|
---|
93 | case 'X': /* dead */
|
---|
94 | enmRet = RTTHREADNATIVESTATE_TERMINATED;
|
---|
95 | break;
|
---|
96 |
|
---|
97 | default:
|
---|
98 | AssertMsgFailed(("state=%c\n", psz[2]));
|
---|
99 | enmRet = RTTHREADNATIVESTATE_UNKNOWN;
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | else
|
---|
104 | AssertMsgFailed(("stat='%s'\n", szName));
|
---|
105 | }
|
---|
106 | }
|
---|
107 | rtThreadRelease(pThread);
|
---|
108 | }
|
---|
109 | return enmRet;
|
---|
110 | }
|
---|
111 |
|
---|