1 | /* $Id: tstSSM-2.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Saved State Manager Testcase: Extract the content of a saved state.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2022 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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <VBox/vmm/ssm.h>
|
---|
33 |
|
---|
34 | #include <VBox/log.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/getopt.h>
|
---|
37 | #include <iprt/errcore.h>
|
---|
38 | #include <iprt/file.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 |
|
---|
43 | static RTEXITCODE extractUnit(const char *pszFilename, const char *pszUnitname, const char *pszOutputFilename)
|
---|
44 | {
|
---|
45 | PSSMHANDLE pSSM;
|
---|
46 | int rc = SSMR3Open(pszFilename, NULL /*pStreamOps*/, NULL /*pvStreamOps*/, 0, &pSSM);
|
---|
47 | RTEXITCODE rcExit = RTEXITCODE_FAILURE;
|
---|
48 | if (RT_SUCCESS(rc))
|
---|
49 | {
|
---|
50 | RTFILE hFile;
|
---|
51 | rc = RTFileOpen(&hFile, pszOutputFilename, RTFILE_O_DENY_NONE | RTFILE_O_WRITE | RTFILE_O_CREATE);
|
---|
52 | if (RT_SUCCESS(rc))
|
---|
53 | {
|
---|
54 | uint32_t version = 0;
|
---|
55 | rc = SSMR3Seek(pSSM, pszUnitname, 0 /* iInstance */, &version);
|
---|
56 | size_t cbUnit = 0;
|
---|
57 | if (RT_SUCCESS(rc))
|
---|
58 | {
|
---|
59 | for (;;)
|
---|
60 | {
|
---|
61 | uint8_t u8;
|
---|
62 | rc = SSMR3GetU8(pSSM, &u8);
|
---|
63 | if (RT_FAILURE(rc))
|
---|
64 | break;
|
---|
65 | size_t cbWritten;
|
---|
66 | rc = RTFileWrite(hFile, &u8, sizeof(u8), &cbWritten);
|
---|
67 | cbUnit++;
|
---|
68 | }
|
---|
69 | RTPrintf("Unit size %zu bytes, version %d\n", cbUnit, version);
|
---|
70 | }
|
---|
71 | else
|
---|
72 | RTPrintf("Cannot find unit '%s' (%Rrc)\n", pszUnitname, rc);
|
---|
73 | RTFileClose(hFile);
|
---|
74 | }
|
---|
75 | else
|
---|
76 | RTPrintf("Cannot open output file '%s' (%Rrc)\n", pszOutputFilename, rc);
|
---|
77 | SSMR3Close(pSSM);
|
---|
78 | }
|
---|
79 | else
|
---|
80 | RTPrintf("Cannot open SSM file '%s' (%Rrc)\n", pszFilename, rc);
|
---|
81 | return rcExit;
|
---|
82 | }
|
---|
83 |
|
---|
84 | int main(int argc, char **argv)
|
---|
85 | {
|
---|
86 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
87 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
88 |
|
---|
89 | if (argc != 4)
|
---|
90 | {
|
---|
91 | RTPrintf("Usage: %s <SSM filename> <SSM unitname> <outfile>\n", RTPathFilename(argv[0]));
|
---|
92 | /* don't fail by default */
|
---|
93 | return RTEXITCODE_SUCCESS;
|
---|
94 | }
|
---|
95 | return extractUnit(argv[1], argv[2], argv[3]);
|
---|
96 | }
|
---|