1 | /* $Id: RTKrnlModInfo.cpp 86300 2020-09-26 09:59:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Utility for getting information about loaded kernel modules.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2020 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 | #include <iprt/krnlmod.h>
|
---|
32 |
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/errcore.h>
|
---|
35 | #include <iprt/getopt.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/message.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Handles loading a kernel module by name.
|
---|
46 | *
|
---|
47 | * @returns Process status code.
|
---|
48 | * @param pszName THe module name to load.
|
---|
49 | */
|
---|
50 | static RTEXITCODE rtKrnlModInfoHandleLoad(const char *pszName)
|
---|
51 | {
|
---|
52 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
53 | int rc = RTKrnlModLoadByName(pszName);
|
---|
54 | if (RT_SUCCESS(rc))
|
---|
55 | RTPrintf("Kernel module '%s' loaded successfully\n", pszName);
|
---|
56 | else
|
---|
57 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc loading kernel module '%s'", rc, pszName);
|
---|
58 |
|
---|
59 | return rcExit;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Handles unloading a kernel module by name.
|
---|
65 | *
|
---|
66 | * @returns Process status code.
|
---|
67 | * @param pszName THe module name to load.
|
---|
68 | */
|
---|
69 | static RTEXITCODE rtKrnlModInfoHandleUnload(const char *pszName)
|
---|
70 | {
|
---|
71 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
72 | int rc = RTKrnlModUnloadByName(pszName);
|
---|
73 | if (RT_SUCCESS(rc))
|
---|
74 | RTPrintf("Kernel module '%s' unloaded successfully\n", pszName);
|
---|
75 | else
|
---|
76 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc unloading kernel module '%s'", rc, pszName);
|
---|
77 |
|
---|
78 | return rcExit;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Handles listing all loaded kernel modules.
|
---|
84 | *
|
---|
85 | * @returns Process status code.
|
---|
86 | */
|
---|
87 | static RTEXITCODE rtKrnlModInfoHandleList(void)
|
---|
88 | {
|
---|
89 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
90 | uint32_t cKrnlMods = RTKrnlModLoadedGetCount();
|
---|
91 | if (cKrnlMods)
|
---|
92 | {
|
---|
93 | PRTKRNLMODINFO pahKrnlModInfo = (PRTKRNLMODINFO)RTMemAllocZ(cKrnlMods * sizeof(RTKRNLMODINFO));
|
---|
94 | if (pahKrnlModInfo)
|
---|
95 | {
|
---|
96 | int rc = RTKrnlModLoadedQueryInfoAll(pahKrnlModInfo, cKrnlMods, &cKrnlMods);
|
---|
97 | if (RT_SUCCESS(rc))
|
---|
98 | {
|
---|
99 | RTPrintf("Index Load address Size Ref count Name \n");
|
---|
100 | for (unsigned i = 0; i < cKrnlMods; i++)
|
---|
101 | {
|
---|
102 | RTKRNLMODINFO hKrnlModInfo = pahKrnlModInfo[i];
|
---|
103 | RTPrintf("%5u %#-18RHv %-10u %-10u %s\n", i,
|
---|
104 | RTKrnlModInfoGetLoadAddr(hKrnlModInfo),
|
---|
105 | RTKrnlModInfoGetSize(hKrnlModInfo),
|
---|
106 | RTKrnlModInfoGetRefCnt(hKrnlModInfo),
|
---|
107 | RTKrnlModInfoGetName(hKrnlModInfo));
|
---|
108 | RTKrnlModInfoRelease(hKrnlModInfo);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else
|
---|
112 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc querying kernel modules", rc);
|
---|
113 |
|
---|
114 | RTMemFree(pahKrnlModInfo);
|
---|
115 | }
|
---|
116 | else
|
---|
117 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc allocating memory", VERR_NO_MEMORY);
|
---|
118 | }
|
---|
119 |
|
---|
120 | return rcExit;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | int main(int argc, char **argv)
|
---|
125 | {
|
---|
126 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
127 | if (RT_FAILURE(rc))
|
---|
128 | return RTMsgInitFailure(rc);
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Parse arguments.
|
---|
132 | */
|
---|
133 | static const RTGETOPTDEF s_aOptions[] =
|
---|
134 | {
|
---|
135 | { "--load", 'l', RTGETOPT_REQ_STRING },
|
---|
136 | { "--unload", 'u', RTGETOPT_REQ_STRING },
|
---|
137 | { "--show-loaded", 's', RTGETOPT_REQ_NOTHING },
|
---|
138 | { "--help", 'h', RTGETOPT_REQ_NOTHING }
|
---|
139 | };
|
---|
140 |
|
---|
141 | RTGETOPTUNION ValueUnion;
|
---|
142 | RTGETOPTSTATE GetState;
|
---|
143 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
144 | while ((rc = RTGetOpt(&GetState, &ValueUnion)))
|
---|
145 | {
|
---|
146 | switch (rc)
|
---|
147 | {
|
---|
148 | case 'l':
|
---|
149 | return rtKrnlModInfoHandleLoad(ValueUnion.psz);
|
---|
150 | case 'u':
|
---|
151 | return rtKrnlModInfoHandleUnload(ValueUnion.psz);
|
---|
152 | case 's':
|
---|
153 | return rtKrnlModInfoHandleList();
|
---|
154 | case 'h':
|
---|
155 | RTPrintf("Usage: %s [options]\n"
|
---|
156 | "\n"
|
---|
157 | "Options:\n"
|
---|
158 | " -l, --load <module name>\n"
|
---|
159 | " Tries to load the given kernel module.\n"
|
---|
160 | " -s, --show-loaded\n"
|
---|
161 | " Lists all loaded kernel modules.\n"
|
---|
162 | , RTPathFilename(argv[0]));
|
---|
163 | return RTEXITCODE_SUCCESS;
|
---|
164 |
|
---|
165 | case 'V':
|
---|
166 | RTPrintf("$Revision: 86300 $\n");
|
---|
167 | return RTEXITCODE_SUCCESS;
|
---|
168 |
|
---|
169 | default:
|
---|
170 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* No arguments means listing all loaded kernel modules. */
|
---|
175 | return rtKrnlModInfoHandleList();
|
---|
176 | }
|
---|
177 |
|
---|