Revision: 974 Author: teawater Date: Sun Mar 18 09:08:01 2012 Log: Hotcode can show line info now. http://code.google.com/p/kgtp/source/detail?r=974 Modified: /branches/hotcode/hotcode.py ======================================= --- /branches/hotcode/hotcode.py Mon Mar 12 22:23:19 2012 +++ /branches/hotcode/hotcode.py Sun Mar 18 09:08:01 2012 @@ -91,55 +91,69 @@ return ret #0 inferior_id 1 dir_name 2 kernel_list 3 user_list +#list 0 function_list 1 file_list 2 line_list task_list = {} no_task = False -kernel_hotcode_list = {} - -def task_list_add_function(is_user, pid, name): +kernel_hotcode_list = ({},{},{}) + +#info[0] line_num, info[1] file_name, info[2] function_name +def add_info_to_code_list(info, code_list): + #function_list + if info[2] in code_list[0]: + code_list[0][info[2]] += 1 + else: + code_list[0][info[2]] = 1 + #file_list + if info[1] in code_list[1]: + code_list[1][info[1]] += 1 + else: + code_list[1][info[1]] = 1 + #line_list + line = info[1]+":"+info[0] + if line in code_list[2]: + code_list[2][line] += 1 + else: + code_list[2][line] = 1 + +def task_list_add_line(is_user, pid, info): if no_task: - if name in kernel_hotcode_list: - kernel_hotcode_list[name] += 1 - else: - kernel_hotcode_list[name] = 1 + #function_list + add_info_to_code_list (info, kernel_hotcode_list) else: if is_user: - if name in task_list[pid][3]: - task_list[pid][3][name] += 1 - else: - task_list[pid][3][name] = 1 + add_info_to_code_list (info, task_list[pid][3]) else: - if name in task_list[pid][2]: - task_list[pid][2][name] += 1 - else: - task_list[pid][2][name] = 1 + add_info_to_code_list (info, task_list[pid][2]) show_line_number = 20 -def hotcode_show(): - if no_task: - print "Kernel hotcode:" + +def hotcode_show_code_list(string, code_list): + if len(code_list) > 0: + print "\t", string i = 1 - for c in dict_sort(kernel_hotcode_list): - print c[0], "\t\t", c[1] + for c in dict_sort(code_list): + print "\t", c[0], "\t\t", c[1] i += 1 if i > show_line_number: break + print + +def hotcode_show(): + if no_task: + hotcode_show_code_list("Hotest function", kernel_hotcode_list[0]) + hotcode_show_code_list("Hotest file", kernel_hotcode_list[1]) + hotcode_show_code_list("Hotest line", kernel_hotcode_list[2]) else: for pid in task_list: print "task", str(pid), task_list[pid][1] print "Kernel hotcode:" - i = 1 - for c in dict_sort(task_list[pid][2]): - print c[0], "\t\t", c[1] - i += 1 - if i > show_line_number: - break + hotcode_show_code_list("Hotest function", task_list[pid][2][0]) + hotcode_show_code_list("Hotest file", task_list[pid][2][1]) + hotcode_show_code_list("Hotest line", task_list[pid][2][2]) print "User hotcode:" - i = 1 - for c in dict_sort(task_list[pid][3]): - print c[0], "\t\t", c[1] - i += 1 - if i > show_line_number: - break + hotcode_show_code_list("Hotest function", task_list[pid][3][0]) + hotcode_show_code_list("Hotest file", task_list[pid][3][1]) + hotcode_show_code_list("Hotest line", task_list[pid][3][2]) print gdb.execute("set target-async on", True, False) @@ -232,7 +246,7 @@ gdb.execute("add-symbol-file "+filename+" ("+addr+"+"+text_offset+")") gdb.execute("file "+user_dir) gdb.execute("inferior 1") - task_list[pid] = (fid, user_dir, {}, {}) + task_list[pid] = (fid, user_dir, ({},{},{}), ({},{},{})) def get_ignore_str(function): ret = "" @@ -428,8 +442,8 @@ tempfile.write("commands\n") tempfile.write("collect $no_self_trace\n") tempfile.write("collect $pc_ip0\n") + tempfile.write("end\n") else: - tempfile.write("trace handle_irq\n") pid_str = "" for pid in task_list: if pid_str != "": @@ -448,13 +462,22 @@ if pid_str != "": cond_str += "&&" cond_str += " ((regs->cs & 3) == 3)" + tempfile.write("trace handle_irq\n") tempfile.write("condition $bpnum "+pid_str+cond_str+"\n") tempfile.write("commands\n") tempfile.write("collect regs->ip\n") if trace_user and trace_kernel: tempfile.write("collect regs->cs\n") tempfile.write("collect $current_task_pid\n") -tempfile.write("end\n") + tempfile.write("end\n") + tempfile.write("trace smp_apic_timer_interrupt\n") + tempfile.write("condition $bpnum "+pid_str+cond_str+"\n") + tempfile.write("commands\n") + tempfile.write("collect regs->ip\n") + if trace_user and trace_kernel: + tempfile.write("collect regs->cs\n") + tempfile.write("collect $current_task_pid\n") + tempfile.write("end\n") tempfile.close() tempfile = open(tempfilename, "r") print "Tracepoint command:" @@ -472,33 +495,42 @@ #Connect to pipe gdb.execute("target tfile /sys/kernel/debug/gtpframe_pipe") -def get_function_from_sym(sym): +def get_line_from_sym(sym): sym = sym.rstrip(os.linesep) - sym_end = sym.find(" in section") - function = "" - if sym_end > 0: - function = sym[0:sym_end] - function_list = function.split(' + ') - function_list_len = len(function_list) - if function_list_len >= 1: - function = function_list[0] - filename_offset = sym.find(" of ") - if filename_offset > 0: - function += sym[filename_offset:] - else: - function = "" - if function == "": - function = "Unknown address" - return function + + #Get line_num and file_name + begin = sym.find("Line ") + end = sym.find("\" starts at address") + line_num = "Unknow" + file_name = "Unknow" + if begin >= 0 and end > 0 and begin + len("Line ") < end: + line = sym[begin + len("Line "):end] + line = line.split(" of \"") + if len(line) == 2: + line_num = line[0] + file_name = line[1] + sym = sym[end:] + + #Get function_name + begin = sym.find("<") + end = sym.find(">") + if begin >= 0 and end > 0 and begin + 1 < end: + function_name = sym[begin + 1:end] + end = function_name.rfind("+") + if end > 0: + function_name = function_name[:end] + else: + function_name = "Unknow" + return (line_num, file_name, function_name) if no_task: while 1: try: gdb.execute("tfind 0", False, True) cpu_id = long(gdb.parse_and_eval("$cpu_id")); - sym = gdb.execute("info symbol ($pc_ip"+str(cpu_id)+" - 1)", True, True) - function = get_function_from_sym(sym) - task_list_add_function(False, 0, function) + sym = gdb.execute("info line *($pc_ip"+str(cpu_id)+" - 1)", True, True) + line = get_line_from_sym(sym) + task_list_add_line(False, 0, line) except gdb.error, x: print("Drop one entry because", x) except gdb.MemoryError, x: @@ -522,12 +554,12 @@ else: ip = long(gdb.parse_and_eval("regs->ip - 1")) gdb.execute("inferior "+str(task_list[pid][0]), False, True) - sym = gdb.execute("info symbol "+str(ip), True, True) + sym = gdb.execute("info line *"+str(ip), True, True) gdb.execute("inferior 1", False, True) else: - sym = gdb.execute("info symbol (regs->ip - 1)", True, True) - function = get_function_from_sym(sym) - task_list_add_function(is_user, pid, function) + sym = gdb.execute("info line *(regs->ip - 1)", True, True) + line = get_line_from_sym(sym) + task_list_add_line(is_user, pid, line) except gdb.error, x: print("Drop one entry because", x) except gdb.MemoryError, x: