VirtualBox

Ticket #10222: vboxdump.py

File vboxdump.py, 3.0 KB (added by Luiz Angelo Daros de Luca, 13 years ago)

Sample python script that uses the ReadPhysicalMemory to dump VM memory

Line 
1#!/usr/bin/python
2#
3# Copyright (C) 2012 Luiz Angelo Daros de Luca <luizluca@gmail.com>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17#
18#
19# This program dumps the VM memory to the standard output.
20# Generally, it will be used like this:
21#
22# vboxdump.py MyVM > MyVM.memdump
23#
24import os,sys
25from vboxapi import VirtualBoxManager
26
27def main(argv):
28 vboxman = VirtualBoxManager(None, None)
29
30 if len(sys.argv) < 2:
31 print >> sys.stderr, "Use: %s machine-id\n" % (sys.argv[0])
32 return
33
34 id=sys.argv[1]
35
36 mach = None
37 pagesize=4096
38 seek=0
39 try:
40 mach = vboxman.vbox.getMachine(id)
41 except:
42 mach = vboxman.vbox.findMachine(id)
43
44 if mach == None:
45 print >> sys.stderr, "Machine '%s' is unknown, use list command to find available machines" %(id)
46 return
47
48 try:
49 vb = vboxman.vbox
50 session = vboxman.mgr.getSessionObject(vb)
51 mach.lockMachine(session, vboxman.constants.LockType_Shared)
52 except Exception,e:
53 print >> sys.stderr, "Session to '%s' not open: %s" %(mach.name,str(e))
54 return
55 if session.state != vboxman.constants.SessionState_Locked:
56 print >> sys.stderr, "Session to '%s' in wrong state: %s" %(mach.name, session.state)
57 session.unlockMachine()
58 return
59 console=session.console
60 try:
61 # pause the machine
62 console.pause()
63 debugger=console.debugger
64 npages=(mach.memorySize*1024)/(pagesize/1024)
65 print >> sys.stderr, "Dumping \"%s\" memory (%lu Megabytes, %lu pages of %lu bytes)" % (mach.name , mach.memorySize, npages, pagesize)
66 #for seek in range(0, (mach.memorySize-1)*kbytes):
67 for seek in range(0, npages-1):
68 print >> sys.stderr, "page:%lu (pos:%lukb)" % (seek, seek*pagesize/1024)
69 try:
70 buf=debugger.readPhysicalMemory(seek*pagesize, pagesize)
71 except Exception, e:
72 # Reserved, like MMIO
73 if e.errno==-1618:
74 print >> sys.stderr, "skiping %lu reserved bytes at 0x%lx" % (pagesize, seek * pagesize)
75 buf="\x00"*1024
76 else:
77 raise
78 print buf,
79 del buf
80 except KeyboardInterrupt:
81 print >> sys.stderr, "Interrupted!\n"
82 except Exception, e:
83 print dir(e)
84 print e.errno
85 print >> sys.stderr, "Failed to read at 0x%lx(%lu bytes): %s" % (seek * pagesize, pagesize, e)
86 # Resume machine
87 console.resume()
88 session.unlockMachine()
89 vboxman.deinit()
90 del vboxman
91
92if __name__ == '__main__':
93 main(sys.argv)

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