VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTKrnlModInfo.cpp@ 106877

Last change on this file since 106877 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: RTKrnlModInfo.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Utility for getting information about loaded kernel modules.
4 */
5
6/*
7 * Copyright (C) 2017-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/krnlmod.h>
42
43#include <iprt/assert.h>
44#include <iprt/errcore.h>
45#include <iprt/getopt.h>
46#include <iprt/initterm.h>
47#include <iprt/mem.h>
48#include <iprt/message.h>
49#include <iprt/path.h>
50#include <iprt/stream.h>
51#include <iprt/string.h>
52
53
54/**
55 * Handles loading a kernel module by name.
56 *
57 * @returns Process status code.
58 * @param pszName THe module name to load.
59 */
60static RTEXITCODE rtKrnlModInfoHandleLoad(const char *pszName)
61{
62 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
63 int rc = RTKrnlModLoadByName(pszName);
64 if (RT_SUCCESS(rc))
65 RTPrintf("Kernel module '%s' loaded successfully\n", pszName);
66 else
67 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc loading kernel module '%s'", rc, pszName);
68
69 return rcExit;
70}
71
72
73/**
74 * Handles unloading a kernel module by name.
75 *
76 * @returns Process status code.
77 * @param pszName THe module name to load.
78 */
79static RTEXITCODE rtKrnlModInfoHandleUnload(const char *pszName)
80{
81 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
82 int rc = RTKrnlModUnloadByName(pszName);
83 if (RT_SUCCESS(rc))
84 RTPrintf("Kernel module '%s' unloaded successfully\n", pszName);
85 else
86 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc unloading kernel module '%s'", rc, pszName);
87
88 return rcExit;
89}
90
91
92/**
93 * Handles listing all loaded kernel modules.
94 *
95 * @returns Process status code.
96 */
97static RTEXITCODE rtKrnlModInfoHandleList(void)
98{
99 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
100 uint32_t cKrnlMods = RTKrnlModLoadedGetCount();
101 if (cKrnlMods)
102 {
103 PRTKRNLMODINFO pahKrnlModInfo = (PRTKRNLMODINFO)RTMemAllocZ(cKrnlMods * sizeof(RTKRNLMODINFO));
104 if (pahKrnlModInfo)
105 {
106 int rc = RTKrnlModLoadedQueryInfoAll(pahKrnlModInfo, cKrnlMods, &cKrnlMods);
107 if (RT_SUCCESS(rc))
108 {
109 RTPrintf("Index Load address Size Ref count Name \n");
110 for (unsigned i = 0; i < cKrnlMods; i++)
111 {
112 RTKRNLMODINFO hKrnlModInfo = pahKrnlModInfo[i];
113 RTPrintf("%5u %#-18RHv %-10u %-10u %s\n", i,
114 RTKrnlModInfoGetLoadAddr(hKrnlModInfo),
115 RTKrnlModInfoGetSize(hKrnlModInfo),
116 RTKrnlModInfoGetRefCnt(hKrnlModInfo),
117 RTKrnlModInfoGetName(hKrnlModInfo));
118 RTKrnlModInfoRelease(hKrnlModInfo);
119 }
120 }
121 else
122 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc querying kernel modules", rc);
123
124 RTMemFree(pahKrnlModInfo);
125 }
126 else
127 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc allocating memory", VERR_NO_MEMORY);
128 }
129
130 return rcExit;
131}
132
133
134int main(int argc, char **argv)
135{
136 int rc = RTR3InitExe(argc, &argv, 0);
137 if (RT_FAILURE(rc))
138 return RTMsgInitFailure(rc);
139
140 /*
141 * Parse arguments.
142 */
143 static const RTGETOPTDEF s_aOptions[] =
144 {
145 { "--load", 'l', RTGETOPT_REQ_STRING },
146 { "--unload", 'u', RTGETOPT_REQ_STRING },
147 { "--show-loaded", 's', RTGETOPT_REQ_NOTHING },
148 { "--help", 'h', RTGETOPT_REQ_NOTHING }
149 };
150
151 RTGETOPTUNION ValueUnion;
152 RTGETOPTSTATE GetState;
153 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
154 while ((rc = RTGetOpt(&GetState, &ValueUnion)))
155 {
156 switch (rc)
157 {
158 case 'l':
159 return rtKrnlModInfoHandleLoad(ValueUnion.psz);
160 case 'u':
161 return rtKrnlModInfoHandleUnload(ValueUnion.psz);
162 case 's':
163 return rtKrnlModInfoHandleList();
164 case 'h':
165 RTPrintf("Usage: %s [options]\n"
166 "\n"
167 "Options:\n"
168 " -l, --load <module name>\n"
169 " Tries to load the given kernel module.\n"
170 " -s, --show-loaded\n"
171 " Lists all loaded kernel modules.\n"
172 , RTPathFilename(argv[0]));
173 return RTEXITCODE_SUCCESS;
174
175 case 'V':
176 RTPrintf("$Revision: 106061 $\n");
177 return RTEXITCODE_SUCCESS;
178
179 default:
180 return RTGetOptPrintError(rc, &ValueUnion);
181 }
182 }
183
184 /* No arguments means listing all loaded kernel modules. */
185 return rtKrnlModInfoHandleList();
186}
187
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette