Sorry for the delay, here is the source code with license attached.
I think it would fit the best in the InstructionAPI manual.

On Wed, Sep 23, 2015 at 6:56 PM, Bill Williams <b...@cs.wisc.edu> wrote:

> On 09/22/2015 05:46 PM, Alin Mîndroc wrote:
>
> Hi,
>
> No problem, a comment specifying the license placed at the beginning of
> the code would be enough?
>
> License and that it's copyrighted by you, 2015 in a comment is sufficient.
>
> Alin
> On Sep 21, 2015 6:04 PM, "Bill Williams" <b...@cs.wisc.edu> wrote:
>
>> Alin--
>>
>> Thanks much for the patches. I'll work out where to drop the example in
>> and let you know.
>>
>> Do you mind adding license/copyright info to your code sample? (Anything
>> that allows us to distribute it with the manual should be fine.)
>>
>> --bw
>>
>> On 09/20/2015 07:11 AM, Alin Mîndroc wrote:
>>
>> Hi everyone,
>>
>> I have attached some code patches to the documentation, as following:
>>
>>   CodeSource.tex: Added underscore in front of the names of the members
>> of the "Hint" struct, as they are named in the source code
>>   Iterating.tex: Changed "findModule" to "findModuleByName"; also, quotes
>> around "foo" came out as bogus unicode chars, so I replaced them
>>   LineInformation.tex: Fixed link for code sample which pointed to wrong
>> section
>>   Symtab.C, Symtab.h: Added capital "O" for "getNumberofSymbols" and
>> "getNumberofRegions", as they are named in the documentation
>>
>> Also, an example that I have missed in your documentation was how to
>> parse a file and get the assembly instructions for each function, a
>> compilable piece of code together with build guidelines, like the sample in
>> the manual for parseAPI which shows how to generate a CFG.
>> So I have also attached a code sample with this functionality, which can
>> be built exactly like in the ParseAPI manual, section 3.1. I am not sure
>> where to place it in the manual, so I have attached only the source code.
>>
>> Regards,
>> Alin
>>
>>
>> _______________________________________________
>> Dyninst-api mailing 
>> listdyninst-...@cs.wisc.eduhttps://lists.cs.wisc.edu/mailman/listinfo/dyninst-api
>>
>>
>>
>
/*
 * Copyright (C) 2015 Alin Mindroc
 * (mindroc dot alin at gmail dot com)
 *
 * This is a sample program that shows how to use InstructionAPI in order to
 * print the assembly code and functions in a provided binary.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */

#include <iostream>
#include "CodeObject.h"
#include "InstructionDecoder.h"

using namespace std;
using namespace Dyninst;
using namespace ParseAPI;
using namespace InstructionAPI;

int main(int argc, char **argv){
	if(argc != 2){
		printf("Usage: %s <binary path>\n", argv[0]);
		return -1;
	}

	char *binaryPath = argv[1];

	SymtabCodeSource *sts;
	CodeObject *co;
	Instruction::Ptr instr;

	SymtabAPI::Symtab *symTab;
	std::string binaryPathStr(binaryPath);
	bool isParsable = SymtabAPI::Symtab::openFile(symTab, binaryPathStr);

	if(isParsable == false){
		const char *error = "error: file can not be parsed";
		cout << error;
		return - 1;
	}

	sts = new SymtabCodeSource(binaryPath);
	co = new CodeObject(sts);

	//parse the binary given as a command line arg
	co->parse();

	//get list of all functions in the binary
	const CodeObject::funclist &all = co->funcs();
	if(all.size() == 0){
		const char *error = "error: no functions in file";
		cout << error;
		return - 1;
	}

	auto fit = all.begin();
	Function *f = *fit;

	//create an Instruction decoder which will convert the binary opcodes to strings
	InstructionDecoder decoder(f->isrc()->getPtrToInstruction(f->addr()),
			InstructionDecoder::maxInstructionLength,
			f->region()->getArch());

	for(;fit != all.end(); ++fit){
		Function *f = *fit;

		//get address of entry point for current function
		Address crtAddr = f->addr();
		int instr_count = 0;

		instr = decoder.decode((unsigned char *)f->isrc()->getPtrToInstruction(crtAddr));
		auto fbl = f->blocks().end();
		fbl--;
		Block *b = *fbl;
		Address lastAddr = b->last();

		//if current function has zero instructions, don't output it
		if(crtAddr == lastAddr)
			continue;

		cout << "\n\n\"" << f->name() << "\" :";
		while(crtAddr < lastAddr){
			//decode current instruction
			instr = decoder.decode((unsigned char *)f->isrc()->getPtrToInstruction(crtAddr));

			cout << "\n" << hex << crtAddr;
			cout << ": \"" << instr->format() << "\"";

			//go to the address of the next instruction
			crtAddr += instr->size();
			instr_count++;
		}
	}

	return 0;
}

_______________________________________________
Dyninst-api mailing list
Dyninst-api@cs.wisc.edu
https://lists.cs.wisc.edu/mailman/listinfo/dyninst-api

Reply via email to