view/listings.py

Go to the documentation of this file.
00001 # Copyright (c) 2006 Cristian L. Vlasceanu
00002 #
00003 # Permission is hereby granted, free of charge, to any person
00004 # obtaining a copy of this software and associated documentation
00005 # files (the "Software"), to deal in the Software without
00006 # restriction, including without limitation the rights to use,
00007 # copy, modify, merge, publish, distribute, sublicense, and/or
00008 # sell copies of the Software, and to permit persons to whom
00009 # the Software is furnished to do so, subject to the following
00010 # conditions:
00011 # 
00012 # The above copyright notice and this permission notice shall be
00013 # included in all copies or substantial portions of the Software.
00014 # 
00015 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
00016 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
00017 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00018 # AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00019 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00020 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00021 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
00022 # OR OTHER DEALINGS IN THE SOFTWARE.
00023 # 
00024 # NOTE: The above applies solely to Python source code provided 
00025 # herein, and it DOES NOT COVER the Zero Debugger Engine and plug-ins.
00026 #
00027 import os.path
00028 import base
00029 import gtk
00030 from disasm import Disasm
00031 from source import Source
00032 
00033 
00034 class Listings(base.Composite):
00035         """
00036         Display a collection of source code, disassembly, and mixed listing
00037         """
00038         def __init__(self, book):
00039                 base.Composite.__init__(self)
00040                 self.__current_view = None
00041                 assert book
00042                 self.__book = book
00043 
00044 
00045         def add(self, v, filename):
00046                 super(Listings, self).add(v)
00047                 w = v.widget()
00048                 book = self.__book
00049                 shortFileName = os.path.basename(filename)
00050                 try:
00051                         book.append_page(w)
00052                         book.set_tab_label_text(w, shortFileName)
00053                 except:
00054                         #Gtk 2.0
00055                         book.append_page(w, gtk.Label(shortFileName))
00056                 w.show_all()
00057                 book.set_current_page(book.page_num(w))
00058 
00059 
00060         def show(self, thread, addr, sym):
00061                 if sym:
00062                         filename = sym.filename()
00063                         line = sym.line()
00064                         self.__show(thread, addr, sym, filename, line)
00065                         try:
00066                                 self.__show(thread, addr, sym, filename, line)
00067                         except:
00068                                 #force fallback to disassembly:
00069                                 line = 0
00070                                 #filename = sym.table().filename()
00071                                 self.__show(thread, addr, sym, filename, line)
00072                         
00073         
00074         def __show(self, thread, addr, sym, filename, line):
00075                 assert(sym)
00076                 view = None
00077                 #find a view that matches the current filename
00078                 for v in self._Composite__views:
00079                         if v.filename() == filename:
00080                                 view = v
00081                                 book = self.__book
00082                                 book.set_current_page(book.page_num(v.widget()))
00083                                 break
00084                 #not found, add a new view
00085                 if not view:
00086                         w = self.__book
00087                         if line:
00088                                 view = Source(w)
00089                                 view.read(thread, addr, sym)
00090                         else:
00091                                 view = Disasm(w)
00092                         self.add(view, filename)
00093                 assert view
00094                 view.show(thread, addr, sym)
00095                 self.__current_view = view
00096 
00097 
00098         def show_frame(self, stackView, thread, frame):
00099                 self.show(thread, frame.program_count(), frame.function())
00100         
00101         
00102         def update(self, event):
00103                 w = self.__book.get_nth_page(0)
00104                 if w and w.get_name() == "placeholder":
00105                         self.__book.remove_page(0)
00106                 thread = event.thread()
00107                 pc = thread.program_count()
00108                 sym = thread.symbols().lookup(pc)
00109                 self.show(thread, pc, sym)
00110 
00111 
00112         def clear(self):
00113                 try:
00114                         while self.__book.get_n_pages():
00115                                 self.__book.remove_page(0)
00116                 except:
00117                         while len(self.__book.get_children()):
00118                                 self.__book.remove_page(0)
00119                 w = gtk.Frame()
00120                 w.set_name("placeholder")
00121                 w.set_shadow_type(gtk.SHADOW_ETCHED_IN)
00122                 w.show()
00123                 self.__book.add(w)
00124                 self.__book.set_tab_label_text(w, "main.cpp")
00125                 self._Composite__views = []