tstSSM-2.cpp revision 0888c638f6ea789c5bd594c5e81ef3b75e2fcf0b
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync/* $Id$ */
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync/** @file
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * Saved State Manager Testcase: Extract the content of a saved state.
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync */
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync/*
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * Copyright (C) 2015 Oracle Corporation
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync *
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * available from http://www.virtualbox.org. This file is free software;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * you can redistribute it and/or modify it under the terms of the GNU
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * General Public License (GPL) as published by the Free Software
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync */
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync/*******************************************************************************
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync* Header Files *
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync*******************************************************************************/
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <VBox/vmm/ssm.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <VBox/log.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <iprt/getopt.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <iprt/err.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <iprt/file.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <iprt/path.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync#include <iprt/stream.h>
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsyncstatic RTEXITCODE extractUnit(const char *pszFilename, const char *pszUnitname, const char *pszOutputFilename)
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync{
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync PSSMHANDLE pSSM;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync int rc = SSMR3Open(pszFilename, 0, &pSSM);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTEXITCODE rcExit = RTEXITCODE_FAILURE;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync if (RT_SUCCESS(rc))
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync {
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTFILE hFile;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync rc = RTFileOpen(&hFile, pszOutputFilename, RTFILE_O_DENY_NONE | RTFILE_O_WRITE | RTFILE_O_CREATE);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync if (RT_SUCCESS(rc))
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync {
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync uint32_t version = 0;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync rc = SSMR3Seek(pSSM, pszUnitname, 0 /* iInstance */, &version);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync size_t cbUnit = 0;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync if (RT_SUCCESS(rc))
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync {
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync for (;;)
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync {
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync uint8_t u8;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync rc = SSMR3GetU8(pSSM, &u8);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync if (RT_FAILURE(rc))
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync break;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync size_t cbWritten;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync rc = RTFileWrite(hFile, &u8, sizeof(u8), &cbWritten);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync cbUnit++;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync }
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTPrintf("Unit size %zu bytes, version %d\n", cbUnit, version);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync }
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync else
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTPrintf("Cannot find unit '%s' (%Rrc)\n", pszUnitname, rc);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTFileClose(hFile);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync }
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync else
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTPrintf("Cannot open output file '%s' (%Rrc)\n", pszOutputFilename, rc);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync SSMR3Close(pSSM);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync }
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync else
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTPrintf("Cannot open SSM file '%s' (%Rrc)\n", pszFilename, rc);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync return rcExit;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync}
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsyncint main(int argc, char **argv)
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync{
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync if (argc != 4)
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync {
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync RTPrintf("Usage: %s <SSM filename> <SSM unitname> <outfile>\n", RTPathFilename(argv[0]));
0888c638f6ea789c5bd594c5e81ef3b75e2fcf0bvboxsync /* don't fail by default */
0888c638f6ea789c5bd594c5e81ef3b75e2fcf0bvboxsync return RTEXITCODE_SUCCESS;
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync }
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync return extractUnit(argv[1], argv[2], argv[3]);
40de92f85ffb44d3078fea55380a471a7e84ca01vboxsync}