#include "defs.h"
#include "target.h"
#include "value.h"
#include "ui-out.h"
#include "gdb_string.h"
#include "disasm.h"
struct dis_line_entry
{
int line;
CORE_ADDR start_pc;
CORE_ADDR end_pc;
};
int gdb_disassemble_from_exec = -1;
static int
gdb_dis_asm_read_memory (bfd_vma memaddr, bfd_byte * myaddr,
unsigned int len, disassemble_info * info)
{
extern struct target_ops exec_ops;
int res;
errno = 0;
res = xfer_memory (memaddr, myaddr, len, 0, 0, &exec_ops);
if (res == len)
return 0;
else if (errno == 0)
return EIO;
else
return errno;
}
static int
compare_lines (const void *mle1p, const void *mle2p)
{
struct dis_line_entry *mle1, *mle2;
int val;
mle1 = (struct dis_line_entry *) mle1p;
mle2 = (struct dis_line_entry *) mle2p;
val = mle1->line - mle2->line;
if (val != 0)
return val;
return mle1->start_pc - mle2->start_pc;
}
static int
dump_insns (struct ui_out *uiout, disassemble_info * di,
CORE_ADDR low, CORE_ADDR high,
int how_many, struct ui_stream *stb)
{
int num_displayed = 0;
CORE_ADDR pc;
int unmapped;
char *filename = NULL;
char *name = NULL;
int offset;
int line;
for (pc = low; pc < high;)
{
QUIT;
if (how_many >= 0)
{
if (num_displayed >= how_many)
break;
else
num_displayed++;
}
ui_out_tuple_begin (uiout, NULL);
ui_out_field_core_addr (uiout, "address", pc);
if (!build_address_symbolic (pc, 0, &name, &offset, &filename,
&line, &unmapped))
{
ui_out_text (uiout, " <");
ui_out_field_string (uiout, "func-name", name);
ui_out_text (uiout, "+");
ui_out_field_int (uiout, "offset", offset);
ui_out_text (uiout, ">:\t");
}
if (filename != NULL)
xfree (filename);
if (name != NULL)
xfree (name);
ui_file_rewind (stb->stream);
pc += TARGET_PRINT_INSN (pc, di);
ui_out_field_stream (uiout, "inst", stb);
ui_file_rewind (stb->stream);
ui_out_tuple_end (uiout);
ui_out_text (uiout, "\n");
}
return num_displayed;
}
static void
do_mixed_source_and_assembly (struct ui_out *uiout,
struct disassemble_info *di, int nlines,
struct linetable_entry *le,
CORE_ADDR low, CORE_ADDR high,
struct symtab *symtab,
int how_many, struct ui_stream *stb)
{
int newlines = 0;
struct dis_line_entry *mle;
struct symtab_and_line sal;
int i;
int out_of_order = 0;
int next_line = 0;
CORE_ADDR pc;
int num_displayed = 0;
mle = (struct dis_line_entry *) alloca (nlines
* sizeof (struct dis_line_entry));
for (i = 0; i < nlines - 1 && le[i].pc < low; i++);
for (; i < nlines - 1 && le[i].pc < high; i++)
{
if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
continue;
if (le[i].line == 0)
continue;
mle[newlines].line = le[i].line;
if (le[i].line > le[i + 1].line)
out_of_order = 1;
mle[newlines].start_pc = le[i].pc;
mle[newlines].end_pc = le[i + 1].pc;
newlines++;
}
if (i == nlines - 1 && le[i].pc < high)
{
mle[newlines].line = le[i].line;
mle[newlines].start_pc = le[i].pc;
sal = find_pc_line (le[i].pc, 0);
mle[newlines].end_pc = sal.end;
newlines++;
}
if (out_of_order)
qsort (mle, newlines, sizeof (struct dis_line_entry), compare_lines);
ui_out_list_begin (uiout, "asm_insns");
for (i = 0; i < newlines; i++)
{
int close_list = 1;
if (mle[i].line >= next_line)
{
if (next_line != 0)
{
if (next_line == mle[i].line)
{
ui_out_tuple_begin (uiout, "src_and_asm_line");
print_source_lines (symtab, next_line, mle[i].line + 1, 0);
}
else
{
for (; next_line < mle[i].line; next_line++)
{
ui_out_tuple_begin (uiout, "src_and_asm_line");
print_source_lines (symtab, next_line, next_line + 1,
0);
ui_out_list_begin (uiout, "line_asm_insn");
ui_out_list_end (uiout);
ui_out_tuple_end (uiout);
}
ui_out_tuple_begin (uiout, "src_and_asm_line");
print_source_lines (symtab, next_line, mle[i].line + 1, 0);
}
}
else
{
ui_out_tuple_begin (uiout, "src_and_asm_line");
print_source_lines (symtab, mle[i].line, mle[i].line + 1, 0);
}
next_line = mle[i].line + 1;
ui_out_list_begin (uiout, "line_asm_insn");
if (i < (newlines - 1) && mle[i + 1].line <= mle[i].line)
close_list = 0;
}
num_displayed += dump_insns (uiout, di, mle[i].start_pc, mle[i].end_pc,
how_many, stb);
if (close_list)
{
ui_out_list_end (uiout);
ui_out_tuple_end (uiout);
ui_out_text (uiout, "\n");
close_list = 0;
}
if (how_many >= 0)
if (num_displayed >= how_many)
break;
}
ui_out_list_end (uiout);
}
static void
do_assembly_only (struct ui_out *uiout, disassemble_info * di,
CORE_ADDR low, CORE_ADDR high,
int how_many, struct ui_stream *stb)
{
int num_displayed = 0;
ui_out_list_begin (uiout, "asm_insns");
num_displayed = dump_insns (uiout, di, low, high, how_many, stb);
ui_out_list_end (uiout);
}
void
gdb_disassembly (struct ui_out *uiout,
char *file_string,
int line_num,
int mixed_source_and_assembly,
int how_many, CORE_ADDR low, CORE_ADDR high)
{
static disassemble_info di;
static int di_initialized;
static struct ui_stream *stb = NULL;
struct symtab *symtab = NULL;
struct linetable_entry *le = NULL;
int nlines = -1;
if (!di_initialized)
{
stb = ui_out_stream_new (uiout);
INIT_DISASSEMBLE_INFO_NO_ARCH (di, stb->stream,
(fprintf_ftype) fprintf_unfiltered);
di.flavour = bfd_target_unknown_flavour;
di.memory_error_func = dis_asm_memory_error;
di.print_address_func = dis_asm_print_address;
di_initialized = 1;
}
di.mach = TARGET_PRINT_INSN_INFO->mach;
if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
di.endian = BFD_ENDIAN_BIG;
else
di.endian = BFD_ENDIAN_LITTLE;
if (gdb_disassemble_from_exec == -1)
{
if (strcmp (target_shortname, "child") == 0
|| strcmp (target_shortname, "procfs") == 0
|| strcmp (target_shortname, "vxprocess") == 0
|| strstr (target_shortname, "-threads") != NULL)
gdb_disassemble_from_exec = 0;
else
gdb_disassemble_from_exec = 1;
}
if (gdb_disassemble_from_exec)
di.read_memory_func = gdb_dis_asm_read_memory;
else
di.read_memory_func = dis_asm_read_memory;
symtab = find_pc_symtab (low);
if (symtab != NULL && symtab->linetable != NULL)
{
le = symtab->linetable->item;
nlines = symtab->linetable->nitems;
}
if (!mixed_source_and_assembly || nlines <= 0
|| symtab == NULL || symtab->linetable == NULL)
do_assembly_only (uiout, &di, low, high, how_many, stb);
else if (mixed_source_and_assembly)
do_mixed_source_and_assembly (uiout, &di, nlines, le, low,
high, symtab, how_many, stb);
gdb_flush (gdb_stdout);
}