public static TyDbFile Read(Stream s) { TyDbFile ret = new TyDbFile(); reader_s = s; uint magic = ReadUInt(s); if (magic != 0x42445954) { throw new Exception("Invalid magic number"); } uint version = ReadUInt(s); if (version != 0x00000001) { throw new Exception("Invalid version"); } int funcs_offset = ReadInt(s); int lines_offset = ReadInt(s); int varargs_offset = ReadInt(s); reader_strings_offset = ReadInt(s); int locs_offset = ReadInt(s); int func_count = ReadInt(s); ret.CompiledFileName = ReadString(s); s.Seek((long)funcs_offset, SeekOrigin.Begin); for (int i = 0; i < func_count; i++) { long f_offset = s.Position; libtysila.tydb.Function f = new libtysila.tydb.Function(); f.MetadataFileName = ReadString(s); f.MetadataToken = ReadUInt(s); f.MangledName = ReadString(s); f.TextOffset = ReadUInt(s); int line_start = ReadInt(s); int line_count = ReadInt(s); int var_start = ReadInt(s); int var_count = ReadInt(s); int arg_start = ReadInt(s); int arg_count = ReadInt(s); for (int j = 0; j < line_count; j++) { s.Seek((long)(lines_offset + line_start + j * 8), SeekOrigin.Begin); libtysila.tydb.Line l = new libtysila.tydb.Line(); l.ILOffset = ReadInt(s); l.CompiledOffset = ReadInt(s); f.Lines.Add(l); if (!f.compiled_to_il.ContainsKey(l.CompiledOffset)) { f.compiled_to_il[l.CompiledOffset] = l.ILOffset; } } for (int j = 0; j < var_count; j++) { s.Seek((long)(varargs_offset + var_start + j * 8), SeekOrigin.Begin); libtysila.tydb.VarArg v = new libtysila.tydb.VarArg(); v.Name = ReadString(s); v.Location = ReadLocation(s, locs_offset, ReadInt(s)); f.Vars.Add(v); } for (int j = 0; j < arg_count; j++) { s.Seek((long)(varargs_offset + arg_start + j * 8), SeekOrigin.Begin); libtysila.tydb.VarArg a = new libtysila.tydb.VarArg(); a.Name = ReadString(s); a.Location = ReadLocation(s, locs_offset, ReadInt(s)); f.Args.Add(a); } ret.Functions.Add(f); s.Seek(f_offset + 40, SeekOrigin.Begin); } return(ret); }
private static void main_loop() { Console.WriteLine("Synchronizing with target..."); comm.gdb_send_message("?"); await.state s = await.get_state(); Console.WriteLine(s.ToString()); bool cont = true; string[] prev_cmd = null; while (cont) { Console.Write("(tydb) "); string orig_cmd_line = Console.ReadLine(); string[] cmd_line = orig_cmd_line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (cmd_line.Length == 0) { if (prev_cmd != null) { cmd_line = prev_cmd; } else { continue; } } string cmd = cmd_line[0]; if ((cmd == "q") || (cmd == "quit") || (cmd == "exit")) { cont = false; } else if ((cmd == "c") || (cmd == "cont") || (cmd == "continue")) { comm.gdb_send_message("c"); s = await.get_state(); Console.WriteLine(s.ToString()); } else if ((cmd == "si") || (cmd == "stepi")) { comm.gdb_send_message("s"); s = await.get_state(); Console.WriteLine(s.ToString()); } else if ((cmd == "s") || (cmd == "step")) { do { comm.gdb_send_message("s"); s = await.get_state(); } while (s.source == null); Console.WriteLine(s.ToString()); } else if ((cmd == "r") || (cmd == "regs") || (cmd == "registers")) { regs.dump_regs(); } else if ((cmd == "bp") || (cmd == "break") || (cmd == "breakpoint")) { string addr = cmd_line[1]; if (addr.StartsWith("0x")) { addr = addr.Substring(2); } ulong uaddr = ulong.Parse(addr, System.Globalization.NumberStyles.HexNumber); comm.gdb_send_message("Z0," + uaddr.ToString("X16") + ",1"); if (comm.gdb_read_message() == "OK") { Console.WriteLine("Breakpoint set"); } else { Console.WriteLine("Error setting breakpoint"); } } else if ((cmd == "x") || (cmd == "?")) { //ulong addr = ulong.Parse(cmd_line[1], System.Globalization.NumberStyles.HexNumber); //obj obj = obj.get_obj(addr); if (s.ty_f != null) { libtysila.tydb.VarArg va = s.ty_f.GetVarArg(cmd_line[1]); if (va != null) { if (va.Location.Type == libtysila.tydb.Location.LocationType.Register) { cmd_line[1] = va.Location.RegisterName; } else if (va.Location.Type == libtysila.tydb.Location.LocationType.ContentsOfLocation) { if (va.Location.ContentsOf.Type == libtysila.tydb.Location.LocationType.Register) { ulong?val = await.get_register(s, Program.arch.get_reg(va.Location.ContentsOf.RegisterName).id); if (val.HasValue) { ulong mem_loc = val.Value; if (va.Location.Offset >= 0) { mem_loc += (ulong)va.Location.Offset; } else { mem_loc -= (ulong)(-va.Location.Offset); } cmd_line[1] = "*" + mem_loc.ToString("x" + (Program.arch.address_size * 2).ToString()); } } } } } dbgarch.register r = arch.get_reg(cmd_line[1]); if (r != null) { ulong?val = await.get_register(new await.state(), r.id); Console.WriteLine(r.name + " = " + (val.HasValue ? val.Value.ToString("x" + (arch.data_size * 2).ToString()) : "unknown")); } else { obj obj = var.get_var(cmd_line[1]); if (obj == null) { Console.WriteLine("Syntax Error"); } else { Console.WriteLine(obj.addr.ToString("x" + (arch.address_size * 2).ToString()) + " = " + obj.ToString()); } } } else if (cmd == "set") { if (orig_cmd_line == "set") { foreach (KeyValuePair <string, obj> kvp in var.vars) { Console.WriteLine(kvp.Key + " = " + kvp.Value.ToString()); } continue; } int eq_pos = orig_cmd_line.IndexOf('='); if (eq_pos == -1) { Console.WriteLine("Syntax Error"); continue; } string var_n = orig_cmd_line.Substring(4, eq_pos - 4).Trim(); string arg = orig_cmd_line.Substring(eq_pos + 1).Trim(); if (var_n.Contains(" ") || arg.Contains(" ")) { Console.WriteLine("Syntax Error"); continue; } obj o = var.get_var(arg); if (o == null) { Console.WriteLine("Syntax Error"); } else { var.vars[var_n] = o; } } else { Console.WriteLine("Unrecognized command: " + cmd); prev_cmd = null; continue; } prev_cmd = cmd_line; } }
private void write(List<byte> varargs, List<byte> locations, Dictionary<string, int> location_cache, libtysila.tydb.VarArg var) { byte[] va_buf = new byte[8]; write(va_buf, 0, var.Name); write(va_buf, 4, write(locations, location_cache, var.Location)); varargs.AddRange(va_buf); }