def get_updated_lib_asm_path_list(): # get an asm path list for those libraries loaded at this point. loaded_asm_path_list = m_info.get_loaded_lib_asm_path() if m_debug.Debug: m_debug.dbg_print("loaded_lib_asm_path_list:", loaded_asm_path_list) # asm_path_list is a sorted list that contains asm paths asm_path_list = m_datastore.mgdb_rdata.get_mirbin_cache_asm_list() if m_debug.Debug: m_debug.dbg_print("return asm_path_list:", asm_path_list) # get the diff between currently loaded asm file and whats loaded by m_datastore.update_gdb_runtime_data diff_asm_path_list = list(set(loaded_asm_path_list) - set(asm_path_list)) if m_debug.Debug: m_debug.dbg_print("diff_asm_path_list:", diff_asm_path_list) if len(diff_asm_path_list) == 0 and len(asm_path_list) == 0: gdb_print( "The libraries that the symbol searched had not been loaded yet") gdb_print( "Retry this after running Maple breakpoint and Maple stack commands" ) return None for asm_path in diff_asm_path_list: #gdb_print("New Maple symbols found, loading them from", asm_path) m_datastore.mgdb_rdata.create_mirbin_info_cache(asm_path) asm_path_list.append(asm_path) return asm_path_list
def display_src_file_lines(filename, line_num): exist = m_datastore.mgdb_rdata.in_src_lines(filename) if exist: total = m_datastore.mgdb_rdata.get_src_lines(filename) else: with open(filename) as fp: total = len(fp.readlines()) m_datastore.mgdb_rdata.add_src_lines(filename, total) try: count = int(m_set.msettings["linecount"]) if count < 5: count = default_src_display_line_count if count != 0 else total << 1 except: count = default_src_display_line_count line_num = line_num if line_num < total else total half = (count - 1) >> 1 s_line = line_num - half if line_num > half + 1 else 1 half = count - half - 1 e_line = line_num + half if line_num + half <= total else total gdb_print("src file: %s%s%s line: %d" % (MColors.BT_SRC, filename, MColors.ENDC, line_num)) if m_util.Highlighted: buf =m_util.shell_cmd('highlight -O xterm256 --force -s %s %s | sed -n %d,%dp |' ' nl -ba -s" " -w8 -v%d | sed "s/^ \\( *%d \\)/=>\\1/"' \ % (m_util.HighlightStyle, filename, s_line, e_line, s_line, line_num)) else: buf = m_util.shell_cmd('sed -n %d,%dp %s | nl -ba -s" " -w8 -v%d | sed "s/^ \\( *%d \\)/=>\\1/"' \ % (s_line, e_line, filename, s_line, line_num)) m_util.gdb_write(buf) new_line_num = line_num + count if line_num + count <= total else total return new_line_num
def debug(self): if not self.mbp_object: return gdb_print(" ======= Maple breakpoint mbp table: ===========") self.mbp_object.display_mbp_table() gdb_print(" ======= Maple breakpoint addr_sym_table: ===========") self.mbp_object.display_mbp_addr_sym_table()
def help_one_command(self, cmd_name): version = str(maple_debugger_version_major) + '.' + str( maple_debugger_version_minor) gdb_print("Maple Version " + version) if not cmd_name in maple_commands_detail_info: return gdb_print(maple_commands_detail_info[cmd_name])
def usage(self): gdb_print( "mnexti: Steps one Maple instruction, but proceeds through subroutine calls" ) gdb_print( "mnexti [num]: Steps num Maple instructions, but proceeds through subroutine calls" )
def mlocal_func(self, args, from_tty): mode = 'local' s = args.split() if len(s) == 1 and s[0] == '-stack': mode = 'stack' elif len(s) == 1 and s[0] == '-s': mode = 'stack' frame = m_frame.get_selected_frame() if not frame: gdb_print("no valid frame found") return data = m_datastore.get_stack_frame_data(frame) if not data: gdb_print("no valid frame data found") return func_argus_locals = data['func_argus_locals'] func_header_name = data['frame_func_header_info']['func_header_name'] if m_debug.Debug: m_debug.dbg_print("func_argus_locals ======= ") m_debug.dbg_print("func_argus_locals=", func_argus_locals) m_debug.dbg_print("func_header_name=", func_header_name) if mode == 'local': self.display_locals(func_argus_locals, func_header_name) else: self.display_stack(func_argus_locals, func_header_name) return
def display_mir_file_lines(self, filename, line_num, mir_tuple): """ params: filename: string. asm file full path line_num: int. line number of this asm file mir_tuple: (maple_symbol_block_start_line, block_start_offset, block_end_offset) in mir.mpl file """ exist = m_datastore.mgdb_rdata.in_mirblock_lines( filename, mir_tuple[0]) if exist: total = m_datastore.mgdb_rdata.get_mirblock_lines( filename, mir_tuple[0]) else: with open(filename) as fp: offset = mir_tuple[1] fp.seek(offset) total = 0 while offset < mir_tuple[2]: line = fp.readline() offset += len(line) total += 1 m_datastore.mgdb_rdata.add_mirblock_lines(filename, mir_tuple[0], total) try: count = int(m_set.msettings["linecount"]) if count < 9: count = default_mir_display_line_count if count != 0 else total << 1 except: count = default_mir_display_line_count if line_num < mir_tuple[0]: line_num = mir_tuple[0] if line_num > mir_tuple[0] + total: line_num = mir_tuple[0] + total - 1 curr_num = line_num - mir_tuple[0] + 1 half = (count - 1) >> 1 s_line = curr_num - half if curr_num > half + 1 else 1 half = count - half - 1 e_line = curr_num + half if curr_num + half <= total else total gdb_print("asm file: %s%s%s line: %d" % (MColors.BT_SRC, filename, MColors.ENDC, line_num)) ss_line = mir_tuple[0] + s_line - 1 if m_util.Highlighted: buf =m_util.shell_cmd('dd skip=%d bs=1 count=%d if=%s 2> /dev/null |' ' highlight -O xterm256 --config-file=%s -s %s | sed -n %d,%dp |' ' nl -ba -s" " -w12 -v%d | sed "s/^ \\( *%d \\)/=>\\1/"' \ % (mir_tuple[1], mir_tuple[2] - mir_tuple[1], filename, mpl_lang, \ m_util.HighlightStyle, s_line, e_line, ss_line, line_num)) else: buf =m_util.shell_cmd('dd skip=%d bs=1 count=%d if=%s 2> /dev/null |' ' sed -n %d,%dp | nl -ba -s" " -w12 -v%d | sed "s/^ \\( *%d \\)/=>\\1/"' \ % (mir_tuple[1], mir_tuple[2] - mir_tuple[1], filename, \ s_line, e_line, ss_line, line_num)) m_util.gdb_write(buf) self.prev_mir_file_line = line_num self.frame_data['frame_func_src_info'][ 'mirmpl_line'] = line_num + count if line_num + count < mir_tuple[ 0] + total else mir_tuple[0] + total - 1
def display_array_char_values(self, addr, type_size, item_num): if item_num > 32: item_list = [i for i in range(24)] item_list = item_list + [i for i in range(item_num - 8, item_num)] else: item_list = [i for i in range(item_num)] steps = 0 show_snip = True buf = 'String Value: "' + MColors.MP_STR_V for i in item_list: obj_addr = addr + 4 + type_size * i # class reference is a pointer, 8 bytes cmd = 'x/1hx ' + hex(obj_addr) if m_debug.Debug: m_debug.dbg_print("cmd=", cmd) try: buffer = m_util.gdb_exec_to_str(cmd) except: buf = ' {}'.format('no-data') gdb_print(buf) steps += 1 return steps += 1 v = buffer.split(':')[1].strip() buf = buf + int(v, 16).to_bytes( 2, byteorder='big').decode("utf-16-be") if item_num > 32 and steps >= 24 and show_snip == True: buf = buf + '...' show_snip = False gdb_print(buf + MColors.ENDC + '"') return
def main(): ver = gdb.VERSION.split('.') if int(ver[0]) * 100 + int(ver[1]) < 801: m_util.gdb_print( \ "Current GDB version is %s.\n" "Maple debugger requires GDB version 8.1 and later.\n" "Warning: Maple debugger is not enabled due to unsupported GDB version." % gdb.VERSION) return # Register commands for Maple debugger MapleBacktrace() MapleBreakpointCmd() MapleStepiCmd() MapleSourcePathCmd() MapleListCmd() MapleUpCmd() MapleDownCmd() MapleLocalCmd() MaplePrintCmd() MapleNextiCmd() MapleSetCmd() MapleTypeCmd() MapleHelpCmd() MapleSymbolCmd() MapleFinishCmd() MapleStepCmd() MapleNextCmd() init_alert() init_gdb()
def usage(self): gdb_print( " msrcpath :\n" " msrcpath -show or msrcpath: Displays all the source code search paths\n" " msrcpath -add <path>: Adds one path to the top of the list\n" " msrcpath -del <path>: Deletes specified path from the list\n" " msrcpath -help: Prints this message")
def usage(self): gdb_print( " msi : Steps into next Maple instruction\n" " msi [n]: Steps into next n Maple instructions\n" " msi -abs [n]: Steps into specified index n of Maple instruction\n" " msi -c: Shows current Maple opcode count\n" " msi help: Displays usage")
def print_gdb_frame(frame, index): """ print one gdb native backtrace frame """ s = m_util.gdb_exec_to_str('bt') bt = s.split('#' + str(index)) bt = bt[1].split('#')[0][:-1] bt = '#' + str(index) + bt gdb_print(bt)
def show_current_opcode_cnt(self): current_opcode_cnt = self.get_current_opcode_cnt() tobject = m_info.get_current_thread() if not current_opcode_cnt: gdb_print("Thread %d: current opcode count not found" % (tobject.num)) else: gdb_print("Thread %d: current opcode count is %s" % (tobject.num, current_opcode_cnt)) return
def usage(self): gdb_print(" mbreak <symbol>: Sets a new Maple breakpoint at symbol\n" " mbreak -set <symbol>: An alias for 'mbreak <symbol>'\n" " mbreak -enable <symbol|index>: Enables an existing Maple breakpoint at symbol\n" " mbreak -disable <symbol|index>: Disables an existing Maple breakpoint at symbol\n" " mbreak -clear <symbol|index>: Deletes an existing Maple breakpoint at symbol\n" " mbreak -clearall : Deletes all existing Maple breakpoints\n" " mbreak -listall : Lists all existing Maple breakpoints\n" " mbreak -ignore <symbol | index> <count> : Sets ignore count for specified Maple breakpoints\n" " mbreak : Displays usage and syntax")
def init_alert(): m_util.gdb_print( "\n" "Before using the Maple debugger, please setup two sets of search paths:\n" "1, Search path for the user's program and library source code.\n" " This is required to display source code of your application and library\n" " Use the 'msrcpath' command to show/add/del source code search paths\n" "2, Search path of the generated assembly files of user's program and library.\n" " This is required by many of the Maple debugger commands\n" " Use the 'mset' command to show/add/del library asm file search paths\n" )
def usage(self): gdb_print( " mlist : Lists source code associated with current Maple frame\n" " mlist -asm : Lists assembly instructions associated with current Maple frame\n" " mlist . | mlist -asm:. | mlist -mir:. : Lists code located by the filename and line number of current Maple frame\n" " mlist line-num : Lists current source code file at line of [line-num]\n" " mlist filename:line-num : Lists specified source code file at line of [line-num]\n" " mlist +|-[num]: Lists current source code file offsetting from previous listed line, offset can be + or -\n" " mlist -asm:+|-[num]: Lists current assembly instructions offsetting from previous listed line. offset can be + or -)\n" " mlist -mir : Lists Maple IR associated with current Maple frame\n" " mlist -mir:+|-[num]: Lists current Maple IR offsetting from previous listed line. offset can be + or -" )
def display_array_primitive_values(self, addr, full_syntax, type_size): buffer = 'Object Type: {}'.format(MColors.MP_FSYNTAX + full_syntax + MColors.ENDC) gdb_print(buffer) item_num = self.get_array_length(addr) buffer = 'Level 1 {}: length={}'.format( MColors.MP_FSYNTAX + full_syntax + MColors.ENDC, item_num) gdb_print(buffer) if item_num > 10: item_list = [ 0, 1, 2, 3, 4, 5, 6, item_num - 3, item_num - 2, item_num - 1 ] else: item_list = [i for i in range(item_num)] steps = 0 show_snip = True for i in item_list: obj_addr = addr + 4 + type_size * i # class reference is a pointer, 8 bytes if type_size == 8: cmd = 'x/1gx ' + hex(obj_addr) elif type_size == 4: cmd = 'x/1xw ' + hex(obj_addr) elif type_size == 2: cmd = 'x/1xh ' + hex(obj_addr) elif type_size == 1: cmd = 'x/1xb ' + hex(obj_addr) else: return if m_debug.Debug: m_debug.dbg_print("cmd=", cmd) try: buffer = m_util.gdb_exec_to_str(cmd) except: buf = ' [{}] {}'.format(i, 'no-data') gdb_print(buf) steps += 1 continue steps += 1 v = buffer.split(':')[1].strip() v = hex(int(v, 16)) #remove leading 0s. e.g. 0x000123 to 0x123 if full_syntax == 'array C[]': # display unicode character v = v + ", '" + int(v, 16).to_bytes( 2, byteorder='big').decode("utf-16-be") + "'" buf = ' [{}] {}'.format(i, v) gdb_print(buf) if item_num > 10 and steps > 6 and show_snip == True: gdb_print(" ...") show_snip = False return
def add_path(self, path): # we add path into the top of the list global maple_source_path_list if path in maple_source_path_list: maple_source_path_list.remove(path) if not os.path.exists(os.path.expandvars(os.path.expanduser(path))): buffer = "%s specified but not found, please verify: " % (path) gdb_print(buffer) else: maple_source_path_list = [ os.path.expandvars(os.path.expanduser(path)) ] + maple_source_path_list return
def usage(self): gdb_print("mset:\n" " -To set Maple debugger environment:\n" " syntax: mset <name> <value>\n" " example: mset verbose on\n" " -To add/del items in list:\n" " syntax: mset -add <key name of list> <list item value>\n" " example: mset -add maple_lib_asm_path ~/gitee/maple_engine/maple_build/out/x864\n" " mset -del maple_lib_asm_path ~/gitee/maple_engine/maple_build/out/x864\n" " -To display current settings:\n" " syntax: mset -show\n" " -To load user defined configuration file in json format:\n" " syntax: mset -lconfig <configuration json file>")
def mdown_func(self, args, from_tty): steps = 1 s = str(args).split() if len(s) is 1: try: steps = int(s[0]) except: pass elif len(s) > 1: self.usage() return selected_frame = m_frame.get_selected_frame() newest_frame = m_frame.get_newest_frame() if not selected_frame or not newest_frame: gdb_print('Unable to locate Maple frame') return # walk through from innermost frame to selected frame # to get the level number from newest frame to selected frame. index = 0 frame = newest_frame while frame != selected_frame and frame: frame = m_frame.get_next_older_frame(frame) index += 1 if not frame: gdb_print('No valid frames found') return prev_maple_frame = frame prev_maple_frame_index = index while frame and steps > 0: frame = m_frame.get_next_newer_frame(frame) if frame: m_util.gdb_exec_to_null('down-silently') index -= 1 else: frame = prev_maple_frame m_util.gdb_exec_to_null('up-silently ' + str(index - prev_maple_frame_index)) index = prev_maple_frame_index break if m_frame.is_maple_frame(frame): prev_maple_frame = frame prev_maple_frame_index = index steps -= 1 print_maple_frame(frame, index, MBT_FORMAT_SRC) m_datastore.mgdb_rdata.update_frame_change_counter()
def mni_display_last_opcodes(): # to display the latest instructions will be executed after this command frame = m_frame.get_selected_frame() if not frame: return ds = m_datastore.get_stack_frame_data(frame) if not ds: return if m_debug.Debug: m_debug.dbg_print("retrieved ds=", ds) if m_set.msettings['stack'] == 'on': m_util.gdb_exec('mlocal -stack') asm_path = ds['frame_func_header_info']['asm_path'] asm_line = ds['frame_func_src_info']['asm_line'] asm_offset = ds['frame_func_src_info']['asm_offset'] gdb_print("asm file: %s%s%s" % (MColors.BT_SRC, asm_path, MColors.ENDC)) f = open(asm_path, 'r') f.seek(asm_offset) line = f.readline() gdb_print(str(asm_line) + " : " + line.rstrip()) line = f.readline() gdb_print(str(asm_line+1) + " : " + line.rstrip()) line = f.readline() gdb_print(str(asm_line+2) + " : " + line.rstrip()) f.close()
def display_symbol_detail(symbol_name, symbol_asm_path): if m_debug.Debug: m_debug.dbg_print("symbol_asm_path=", symbol_asm_path, "symbol_name=", symbol_name) if not symbol_name or not symbol_asm_path: return data = m_datastore.mgdb_rdata.get_one_label_mirbin_info_cache( symbol_asm_path, symbol_name) d = m_asm.lookup_src_file_info(symbol_asm_path, data[0], data[1], data[2], "0000") if not d: return short_src_file_name, short_src_file_line = m_asm.lookup_next_src_file_info(symbol_asm_path,\ d["asm_line"], d['asm_offset']) file_full_path = None for source_path in m_list.maple_source_path_list: file_full_path = m_list.find_one_file(short_src_file_name, source_path) if not file_full_path: continue else: break gdb_print("assembly file : " + symbol_asm_path) if not file_full_path: gdb_print("source : unknown") else: gdb_print("source : " + file_full_path) gdb_print("demangled name: " + m_symbol.get_demangled_maple_symbol( m_util.color_symbol(MColors.SP_SNAME, symbol_name[:-12])))
def mstepi_update_and_display(self, frame): # this will update the Maple gdb runtime metadata store. m_datastore.mgdb_rdata.update_gdb_runtime_data() m_datastore.mgdb_rdata.update_frame_change_counter() ds = m_datastore.get_stack_frame_data(frame) if not ds: return if m_debug.Debug: m_debug.dbg_print("retrieved ds=", ds) if m_set.msettings['stack'] == 'on': m_util.gdb_exec('mlocal -stack') asm_path = ds['frame_func_header_info']['asm_path'] asm_line = ds['frame_func_src_info']['asm_line'] asm_offset = ds['frame_func_src_info']['asm_offset'] gdb_print("asm file: %s%s%s" % (MColors.BT_SRC, asm_path, MColors.ENDC)) f = open(asm_path, 'r') f.seek(asm_offset) gdb_print("=> %d : %s" % (asm_line, f.readline().rstrip())) gdb_print(" %d : %s" % (asm_line + 1, f.readline().rstrip())) gdb_print(" %d : %s" % (asm_line + 2, f.readline().rstrip())) f.close() m_util.gdb_exec('display')
def search_display_symbol_list(sym_regexp, indent, print_prefix, asm_path_list): """ search symbols using symb_regexp, print matching symbol name with given prefix. params: sym_regexp: string. regular expression for the symbol to be searched print_prefix: string. the prefix for print. asm_path_list: search with the asm paths in asm_path_list indent: int. number of space characters returns: count: number of matched symbol last_matchimg_symbol_name: string, or None if count = 0 last_matching_symbol_path: string, or None if count = 0 """ # create a re.recompile pattern reg = r"(%s)+" % sym_regexp pattern = re.compile(reg) count = 0 last_matching_symbol_name = None last_matching_symbol_path = None asm_path = None for asm_path in asm_path_list: # asm_symbol_list is a sorted list contains all the symbols from asm_path asm_symbol_list = m_datastore.mgdb_rdata.get_mirbin_cache_symbol_list( asm_path) if m_debug.Debug: m_debug.dbg_print("asm=", asm_path, ", len(asm_symbol_list)=", len(asm_symbol_list)) for symbol_name in asm_symbol_list: m = pattern.search(symbol_name) if not m: continue count += 1 indent_str = ' ' * indent if print_prefix: buffer = '{}#{} {}: {}'.format( indent_str, count, print_prefix, m_util.color_symbol(MColors.SP_SNAME, symbol_name[:-12])) else: buffer = '{}#{} {}'.format( indent_str, count, m_util.color_symbol(MColors.TP_MNAME, symbol_name[:-12])) gdb_print(buffer) last_matching_symbol_name = symbol_name last_matching_symbol_path = asm_path return count, last_matching_symbol_name, last_matching_symbol_path
def get_initialized_maple_func_addrs(): """ get an initialized Maple frame func header address returns: func header address in string format of xxxxxx:yyyyyy: format where xxxxxx means (header - lib_base_addr), yyyyyy means (pc - header - header_size) """ header = get_initialized_maple_func_attr('func.header') pc = get_initialized_maple_func_attr('func.pc') if m_debug.Debug: m_debug.dbg_print("header=", header, "pc=", pc) if not header or not pc: return None try: header = int(header, 16) except: return None try: pc = int(pc, 16) except: return None lib_addr = get_lib_addr_from_proc_mapping(header) if m_debug.Debug: m_debug.dbg_print("header=", header, "lib_addr=",lib_addr, "pc=", pc) if not lib_addr: return None try: buf = m_util.gdb_exec_to_str('x/1xw ' + str(header)) except: return None if not buf: return None header_size = int(buf.split(':')[1],16) if (header < lib_addr): gdb_print ("Warning: The header address is lower than lib_addr.") return None if (pc < header): gdb_print ("Warning: The pc address is lower than the header address.") return None xxxxxx = header - lib_addr yyyyyy = pc - header - header_size return hex(xxxxxx) + ":" + "{:04x}".format(yyyyyy)+":"
def mprint_func(self, args, from_tty): s = str(args) try: val = gdb.parse_and_eval(s) s = hex(int(val)) except: pass x = s.split() if len(x) == 1: # address or a variable # if address: x[0] is list 0x123456 # if variable: x[0] must not be a number character if not x[0][0].isdigit(): # x[0] is a variable self.mprint_func_dync(x[0]) return else: try: addr = int(x[0], 16) except: self.usage() return else: self.usage() return class_name, full_syntax, type_size = self.get_class_name_syntax(addr) if not full_syntax: return # if it is a array of class obj, or just non-array regular class obj if 'array' in full_syntax and class_name[0] == 'L' \ or 'class' in full_syntax and class_name[0] == 'L' : class_list = self.get_class_list(class_name, full_syntax, type_size) if not class_list or len(class_list) is 0: gdb_print("no class data found") return self.display_class_data(class_list, addr) # if it is an array of primitive type elif 'array' in full_syntax and class_name in m_symbol.java_primitive_type_dict: ### array is a object that takes 12 bytes per java standard self.display_array_primitive_values(addr + 12, full_syntax, type_size) return
def listall_breakpoint(self): if not self.mbp_object: return gdb_print ("list all Maple breakpoints") # sort the dict with the index, so that we can display in index order blist = [{k:v} for k, v in sorted(m_breakpoint.mbp_table.items(), key=(lambda x:x[1]['index']))] bp_info = self.mbp_object.bp_info if self.mbp_object.bp_info else "in maple::maple_invoke_method()" bp_addr = hex(self.mbp_object.bp_addr) if self.mbp_object.bp_addr else "pending address" for v in blist: key = [*v][0] state = MColors.BP_STATE_RD + 'disabled' + MColors.ENDC if v[key]['disabled'] else MColors.BP_STATE_GR + 'enabled' + MColors.ENDC gdb_print('#%i %s %s %s %d %s %d at %s %s' % \ (v[key]['index'], m_util.color_symbol(MColors.BP_SYMBOL, key), state, MColors.BP_ATTR + 'hit_count' + MColors.ENDC, v[key]['hit_count'],\ MColors.BP_ATTR + 'ignore_count' + MColors.ENDC, v[key]['ignore_count'],\ MColors.BP_ADDR + bp_addr + MColors.ENDC, bp_info))
def update_gdb_runtime_data_dync(self): addr_offset, so_path, asm_path, func_header, mmpl_path = m_frame.get_newest_maple_frame_func_lib_info(True) if m_debug.Debug: m_debug.dbg_print("addr_offset=", addr_offset) m_debug.dbg_print("so_path=", so_path) m_debug.dbg_print("asm_path=", asm_path) m_debug.dbg_print("func_header=", func_header) m_debug.dbg_print("mmpl_path=", mmpl_path) if not addr_offset or not so_path or not asm_path or not func_header or not mmpl_path: return if not self.mirbin_info_cache_has_key(asm_path): gdb_print("create Maple symbol cache using file: " + asm_path) self.create_mirbin_info_cache(asm_path) '''
def trace_maple_debugger(frame, event, arg, mtrace_data=[0]): """implements the trace function of Maple debugger The trace output includes the elapsed time of each target function for performance profiling params: frame: the frame to be traced event: event which occurs, both "call" and "return" are needed arg: Return value for "return" and None for "call" returns: trace_maple_debugger itself """ if event == "call": co = frame.f_code filename = co.co_filename.split('/')[-1] if filename.startswith("m_") and filename not in mexcluded: funcname, lineno, caller = co.co_name, frame.f_lineno, frame.f_back callsite = caller.f_code.co_filename.split('/')[-1] + ':' + str( caller.f_lineno) if caller else "None" if co.co_argcount == 0: argstr = 'arg#0' else: argstr = 'arg#%d, %s' % (co.co_argcount, ', '.join( str(frame.f_locals[v]) for v in co.co_varnames[:co.co_argcount])) argstr = (argstr[:mtrclimit] + '...' if len(argstr) > mtrclimit \ else argstr).replace('\n', '\\n').replace('\033', '\\033') mtrace_data[0] += 1 gdb_print("+ " * mtrace_data[0] + "==> %s:%d, %s, @(%s), {%s}" % \ (filename, lineno, funcname, callsite, argstr), gdb.STDERR) mtrace_data.append(time.time()) elif event == "return" and mtrace_data[0] > 0: co = frame.f_code filename = co.co_filename.split('/')[-1] if filename.startswith("m_") and filename not in mexcluded: etime = int((time.time() - mtrace_data.pop()) * 1000000) funcname, lineno, retstr = co.co_name, frame.f_lineno, str(arg) retstr = (retstr[:mtrclimit] + '...' if len(retstr) > mtrclimit \ else retstr).replace('\n', '\\n').replace('\033', '\\033') gdb_print("- " * mtrace_data[0] + "<-- %s:%d, %s, %sus, ret: %s" % \ (filename, lineno, funcname, etime, retstr), gdb.STDERR) mtrace_data[0] -= 1 return trace_maple_debugger
def display_class_list(self, class_list): if not class_list or len(class_list) is 0: return level = len(class_list) field_count = 1 for i in range(level): buffer = ' level {} {}: '.format( i + 1, MColors.TP_CNAME + class_list[i]['class_name'] + MColors.ENDC) gdb_print(buffer) slist = sorted(class_list[i]['obj_class']['fields'], key=lambda x: x['offset']) for v in slist: buffer =' #{0:d},off={1:2d},len={2:2d},"{3:<16s}"'.format(field_count, v['offset']\ ,v['length'],v['name']) gdb_print(buffer) field_count += 1