keystone icon indicating copy to clipboard operation
keystone copied to clipboard

"Cannot find a symbol" is very unhelpful

Open lunixbochs opened this issue 9 years ago • 13 comments

I'm using Keystone as part of a compiler and I'm having trouble finding where the missing symbol exists in my 7000 lines of assembly. I'd definitely appreciate a way to print the symbol name in this case.

lunixbochs avatar Jul 23 '16 11:07 lunixbochs

Can you give one example?

aquynh avatar Jul 23 '16 11:07 aquynh

from keystone import *
ks = Ks(KS_ARCH_X86, KS_MODE_32)
ks.asm('mov eax, _sym')

lunixbochs avatar Jul 23 '16 11:07 lunixbochs

At the output you have "count" value, which indicates the number of statements successfully compiled. That can tell you where the problem is.

aquynh avatar Jul 23 '16 11:07 aquynh

This is not always useful, however. I am thinking about adding a new API to pass in symbol value at run time.

aquynh avatar Jul 23 '16 11:07 aquynh

Ah, found the underlying problem (.align 3 eats my symbol). Will post a new issue on that.

lunixbochs avatar Jul 23 '16 11:07 lunixbochs

At the output you have "count" value

The Python API doesn't expose this because an exception is thrown.

        status = _ks.ks_asm(self._ksh, string, addr, byref(encode), byref(encode_size), byref(stat_count))
        if (status != 0):
            errno = _ks.ks_errno(self._ksh)
            raise KsError(errno)

An easy solution would be to put the count on the error (maybe using a different error type).

errno = _ks.ks_errno(self._ksh)
raise KsError(errno, stat_count.value)

Then later reference e.count.

lunixbochs avatar Jul 26 '16 13:07 lunixbochs

Can you send a pull req?

aquynh avatar Jul 26 '16 13:07 aquynh

I just tested this, the count doesn't actually show the failed symbol.

mov eax, 1; mov eax, 2; jmp L2 will output a count of 3 mov eax, 1; mov eax, 2; jmp L2; mov eax, 3 will output a count of 4

lunixbochs avatar Jul 26 '16 13:07 lunixbochs

That is how it works internally: Keystone reports the number of statements successfully parsed. In the second case it returns 4, as expected.

The docs on this param should be improved.

aquynh avatar Jul 26 '16 13:07 aquynh

Was responding to this:

At the output you have "count" value, which indicates the number of statements successfully compiled. That can tell you where the problem is.

lunixbochs avatar Jul 26 '16 14:07 lunixbochs

as above, count tells you how many statements was successfully compiled. it may be useful when the broken statement is the last one, but as in the second case you pointed out above, that is not always helpful.

aquynh avatar Jul 26 '16 16:07 aquynh

fixed this issue by extending KsError() to return count via a new method. see commits https://github.com/keystone-engine/keystone/commit/961c2c869d4c2a78bb1c9f292fcf699a5f350602 & sample code for this at https://github.com/keystone-engine/keystone/commit/3aadf88ff3f2174e14cbaead80ec48f340837100

aquynh avatar Jul 26 '16 16:07 aquynh

Worth noting that the count doesn't always tell you where the problem is.

I had mis-typed "0x200" as "x200" on line 2 of my 40 line assembly code, and it said it failed on line 40, (presumably because it was looking for a symbol x200....)

S4lt5 avatar Jan 04 '23 21:01 S4lt5