| 
									
										
										
										
											2020-05-12 12:32:33 +02:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  | # GDB debugging support | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright 2012 Red Hat, Inc. and/or its affiliates | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Authors: | 
					
						
							|  |  |  | #  Avi Kivity <avi@redhat.com> | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2019-03-11 13:13:53 +01:00
										 |  |  | # This work is licensed under the terms of the GNU GPL, version 2 or | 
					
						
							|  |  |  | # later.  See the COPYING file in the top-level directory. | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # 'qemu mtree' -- display the memory hierarchy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import gdb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def isnull(ptr): | 
					
						
							|  |  |  |     return ptr == gdb.Value(0).cast(ptr.type) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def int128(p): | 
					
						
							| 
									
										
										
										
											2017-03-21 16:26:27 +00:00
										 |  |  |     '''Read an Int128 type to a python integer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QEMU can be built with native Int128 support so we need to detect | 
					
						
							|  |  |  |     if the value is a structure or the native type. | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     if p.type.code == gdb.TYPE_CODE_STRUCT: | 
					
						
							|  |  |  |         return int(p['lo']) + (int(p['hi']) << 64) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return int(("%s" % p), 16) | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MtreeCommand(gdb.Command): | 
					
						
							|  |  |  |     '''Display the memory tree hierarchy''' | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         gdb.Command.__init__(self, 'qemu mtree', gdb.COMMAND_DATA, | 
					
						
							|  |  |  |                              gdb.COMPLETE_NONE) | 
					
						
							|  |  |  |         self.queue = [] | 
					
						
							|  |  |  |     def invoke(self, arg, from_tty): | 
					
						
							|  |  |  |         self.seen = set() | 
					
						
							|  |  |  |         self.queue_root('address_space_memory') | 
					
						
							|  |  |  |         self.queue_root('address_space_io') | 
					
						
							|  |  |  |         self.process_queue() | 
					
						
							|  |  |  |     def queue_root(self, varname): | 
					
						
							|  |  |  |         ptr = gdb.parse_and_eval(varname)['root'] | 
					
						
							|  |  |  |         self.queue.append(ptr) | 
					
						
							|  |  |  |     def process_queue(self): | 
					
						
							|  |  |  |         while self.queue: | 
					
						
							|  |  |  |             ptr = self.queue.pop(0) | 
					
						
							| 
									
										
										
										
											2015-12-05 19:52:20 +08:00
										 |  |  |             if int(ptr) in self.seen: | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             self.print_item(ptr) | 
					
						
							|  |  |  |     def print_item(self, ptr, offset = gdb.Value(0), level = 0): | 
					
						
							| 
									
										
										
										
											2015-12-05 19:52:20 +08:00
										 |  |  |         self.seen.add(int(ptr)) | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  |         addr = ptr['addr'] | 
					
						
							|  |  |  |         addr += offset | 
					
						
							|  |  |  |         size = int128(ptr['size']) | 
					
						
							|  |  |  |         alias = ptr['alias'] | 
					
						
							|  |  |  |         klass = '' | 
					
						
							|  |  |  |         if not isnull(alias): | 
					
						
							|  |  |  |             klass = ' (alias)' | 
					
						
							|  |  |  |         elif not isnull(ptr['ops']): | 
					
						
							|  |  |  |             klass = ' (I/O)' | 
					
						
							|  |  |  |         elif bool(ptr['ram']): | 
					
						
							|  |  |  |             klass = ' (RAM)' | 
					
						
							|  |  |  |         gdb.write('%s%016x-%016x %s%s (@ %s)\n' | 
					
						
							|  |  |  |                   % ('  ' * level, | 
					
						
							| 
									
										
										
										
											2015-12-05 19:52:20 +08:00
										 |  |  |                      int(addr), | 
					
						
							|  |  |  |                      int(addr + (size - 1)), | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  |                      ptr['name'].string(), | 
					
						
							|  |  |  |                      klass, | 
					
						
							|  |  |  |                      ptr, | 
					
						
							|  |  |  |                      ), | 
					
						
							|  |  |  |                   gdb.STDOUT) | 
					
						
							|  |  |  |         if not isnull(alias): | 
					
						
							|  |  |  |             gdb.write('%s    alias: %s@%016x (@ %s)\n' % | 
					
						
							|  |  |  |                       ('  ' * level, | 
					
						
							|  |  |  |                        alias['name'].string(), | 
					
						
							| 
									
										
										
										
											2017-03-21 16:26:27 +00:00
										 |  |  |                        int(ptr['alias_offset']), | 
					
						
							| 
									
										
										
										
											2015-08-14 18:46:29 +01:00
										 |  |  |                        alias, | 
					
						
							|  |  |  |                        ), | 
					
						
							|  |  |  |                       gdb.STDOUT) | 
					
						
							|  |  |  |             self.queue.append(alias) | 
					
						
							|  |  |  |         subregion = ptr['subregions']['tqh_first'] | 
					
						
							|  |  |  |         level += 1 | 
					
						
							|  |  |  |         while not isnull(subregion): | 
					
						
							|  |  |  |             self.print_item(subregion, addr, level) | 
					
						
							|  |  |  |             subregion = subregion['subregions_link']['tqe_next'] |