| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- // Copyright (C) 2012 Y_Less
- //
- // Permission is hereby granted, free of charge, to any person obtaining a
- // copy of this software and associated documentation files (the "Software"),
- // to deal in the Software without restriction, including without limitation
- // the rights to use, copy, modify, merge, publish, distribute, sublicense,
- // and/or sell copies of the Software, and to permit persons to whom the
- // Software is furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- // DEALINGS IN THE SOFTWARE.
- #if defined FRAME_INFO_INC
- #endinput
- #endif
- #define FRAME_INFO_INC
- #include "amx_header"
- #include "amx_base"
- #include "opcode"
- stock GetCurrentFrame() {
- #emit lctrl 5
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- return 0; // make compiler happy
- }
- stock GetFramePreviousFrame(frm_addr) {
- #emit lref.s.pri frm_addr
- #emit retn
- return 0; // make compiler happy
- }
- stock GetFrameReturn(frm_addr) {
- #emit load.s.pri frm_addr
- #emit add.c 4
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- return 0; // make compiler happy
- }
- stock SetFramePreviousFrame(frm_addr, addr) {
- #emit load.s.pri frm_addr
- #emit push.pri
- #emit load.s.alt addr
- #emit sref.s.alt 0xfffffffc
- #emit stack 4
- #emit retn
- }
- stock SetFrameReturn(frm_addr, addr) {
- #emit load.s.pri frm_addr
- #emit add.c 4
- #emit push.pri
- #emit load.s.alt addr
- #emit sref.s.alt 0xfffffffc
- #emit stack 4
- #emit retn
- }
- stock SetFrameParameterSize(frm_addr, size) {
- #emit load.s.pri frm_addr
- #emit add.c 8
- #emit push.pri
- #emit load.s.pri size
- #emit sref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- }
- stock SetFrameParameterCount(frm_addr, count) {
- SetFrameParameterSize(frm_addr, count << 2);
- }
- stock GetFrameTotalSize(frm_addr) {
- return GetFrameLocalSize(frm_addr) + GetFrameHeaderSize(frm_addr) + GetFrameParameterSize(frm_addr);
- }
- stock GetFrameTotalCount(frm_addr) {
- return GetFrameTotalSize(frm_addr) / 4;
- }
- stock GetFrameNextFrame(frm_addr) {
- // this function always works because it is at the top of the stack
- // run back through the stack
- new cur_frm = GetCurrentFrame();
- while (cur_frm != 0) {
- new prev_frm = GetFramePreviousFrame(cur_frm);
- if (prev_frm == frm_addr) {
- break;
- }
- cur_frm = prev_frm;
- }
- return cur_frm;
- }
- stock GetFrameLocalSize(frm_addr) {
- // run back through the stack
- new next_frm = GetFrameNextFrame(frm_addr);
- // find the size of local variables in the selected frame
- new frm_bottom = next_frm + GetFrameHeaderSize(next_frm) + GetFrameParameterSize(next_frm);
- return frm_addr - frm_bottom;
- }
- stock GetFrameLocalCount(frm_addr) {
- return GetFrameLocalSize(frm_addr) / 4;
- }
- stock GetFrameHeaderSize(frm_addr) {
- #pragma unused frm_addr
- return 12;
- }
- stock GetFrameHeaderCount(frm_addr) {
- return GetFrameHeaderSize(frm_addr) / 4;
- }
- stock GetFrameParameterSize(frm_addr) {
- #emit load.s.pri frm_addr
- #emit add.c 8
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- return 0; // make compiler happy
- }
- stock GetFrameParameterCount(frm_addr) {
- return GetFrameParameterSize(frm_addr) / 4;
- }
- stock GetFrameParameter(frm_addr, param, idx = -1) {
- if (idx == -1) {
- #emit load.s.pri param
- #emit shl.c.pri 2
- #emit load.s.alt frm_addr
- #emit add
- #emit add.c 12
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- } else {
- #emit load.s.pri param
- #emit shl.c.pri 2
- #emit load.s.alt frm_addr
- #emit add
- #emit add.c 12
- #emit push.pri
- #emit lref.s.alt 0xfffffffc
- #emit load.s.pri idx
- #emit lidx
- #emit stack 4
- #emit retn
- }
- return 0; // make compiler happy
- }
- stock GetFrameVariable(frm_addr, param, idx = -1) {
- if (idx == -1) {
- #emit load.s.pri param
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- } else {
- #emit load.s.pri param
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit lref.s.alt 0xfffffffc
- #emit load.s.pri idx
- #emit lidx
- #emit stack 4
- #emit retn
- }
- return 0; // make compiler happy
- }
- stock GetFrameLocal(frm_addr, param) {
- if (param < 0) {
- // probably in correct format for negative offsets
- #emit load.s.pri param
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- } else {
- #emit load.s.pri param
- #emit shl.c.pri 2
- #emit const.alt 0xfffffffc
- #emit sub.alt
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- }
- return 0; // make compiler happy
- }
- stock SetFrameParameter(frm_addr, param, value, idx = -1) {
- if (idx == -1) {
- #emit load.s.pri param
- #emit shl.c.pri 2
- #emit load.s.alt frm_addr
- #emit add
- #emit add.c 12
- #emit push.pri
- #emit load.s.pri value
- #emit sref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- } else {
- #emit load.s.pri param
- #emit shl.c.pri 2
- #emit load.s.alt frm_addr
- #emit add
- #emit add.c 12
- #emit push.pri
- #emit lref.s.alt 0xfffffffc
- #emit load.s.pri idx
- #emit idxaddr
- #emit load.s.alt value
- #emit xchg
- #emit stor.i
- #emit stack 4
- #emit retn
- }
- return 0; // make compiler happy
- }
- stock SetFrameVariable(frm_addr, param, value, idx = -1) {
- if (idx == -1) {
- #emit load.s.pri param
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit load.s.pri value
- #emit sref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- } else {
- #emit load.s.pri param
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit lref.s.alt 0xfffffffc
- #emit load.s.pri idx
- #emit idxaddr
- #emit load.s.alt value
- #emit xchg
- #emit stor.i
- #emit stack 4
- #emit retn
- }
- return 0; // make compiler happy
- }
- stock SetFrameLocal(frm_addr, param, value) {
- if (param < 0) {
- // probably in correct format for negative offsets
- #emit load.s.pri param
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit load.s.pri value
- #emit sref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- } else {
- #emit load.s.pri param
- #emit shl.c.pri 2
- #emit const.alt 0xfffffffc
- #emit sub.alt
- #emit load.s.alt frm_addr
- #emit add
- #emit push.pri
- #emit load.s.pri value
- #emit sref.s.pri 0xfffffffc
- #emit stack 4
- #emit retn
- }
- return 0; // make compiler happy
- }
- static stock GetCallerFrame() {
- #emit lctrl 5
- #emit push.pri
- #emit lref.s.pri 0xfffffffc
- #emit push.pri
- #emit lref.s.pri 0xfffffff8
- #emit stack 8
- #emit retn
- return 0; // make compiler happy
- }
- stock GetCurrentFramePreviousFrame() {
- return GetFramePreviousFrame(GetCallerFrame());
- }
- stock GetCurrentFrameReturn() {
- return GetFrameReturn(GetCallerFrame());
- }
- stock GetCurrentFrameTotalSize() {
- return GetFrameTotalSize(GetCallerFrame());
- }
- stock GetCurrentFrameTotalCount() {
- return GetFrameTotalCount(GetCallerFrame());
- }
- stock GetCurrentFrameLocalSize() {
- return GetFrameLocalSize(GetCallerFrame());
- }
- stock GetCurrentFrameLocalCount() {
- return GetFrameLocalCount(GetCallerFrame());
- }
- stock GetCurrentFrameHeaderSize() {
- return GetFrameHeaderSize(GetCallerFrame());
- }
- stock GetCurrentFrameHeaderCount() {
- return GetFrameHeaderCount(GetCallerFrame());
- }
- stock GetCurrentFrameParameterSize() {
- return GetFrameParameterSize(GetCallerFrame());
- }
- stock GetCurrentFrameParameter(param, idx = -1) {
- return GetFrameParameter(GetCallerFrame(), param, idx);
- }
- stock GetCurrentFrameLocal(param) {
- return GetFrameLocal(GetCallerFrame(), param);
- }
- stock GetCurrentFrameParameterCount() {
- return GetFrameParameterCount(GetCallerFrame());
- }
- stock SetCurrentFrameReturn(addr) {
- SetFrameReturn(GetCallerFrame(), addr);
- }
- stock SetCurrentFramePreviousFrame(addr) {
- SetFramePreviousFrame(GetCallerFrame(), addr);
- }
- stock SetCurrentParameterSize(size) {
- SetFrameParameterSize(GetCallerFrame(), size);
- }
- stock SetCurrentParameterCount(count) {
- SetFrameParameterCount(GetCallerFrame(), count);
- }
- stock GetFrameFunction(frm_addr) {
- // try to determine the start address of the function this frame is for
- new prev_frm = GetFramePreviousFrame(frm_addr);
- new amxhdr[AMX_HDR];
- GetAmxHeader(amxhdr);
- if (prev_frm == 0) {
- // public entry point
- new call_addr = GetFrameReturn(GetFrameNextFrame(frm_addr));
- // find the closest public/main
- new highest_found = 0;
- new defsize = amxhdr[AMX_HDR_DEFSIZE];
- new num_publics = (amxhdr[AMX_HDR_NATIVES] - amxhdr[AMX_HDR_PUBLICS]) / defsize;
- new off = amxhdr[AMX_HDR_PUBLICS] - amxhdr[AMX_HDR_DAT];
- for (new i = 0; i != num_publics; ++i) {
- new addr = ReadAmxMemory(off);
- off += defsize;
- if (highest_found < addr < call_addr) {
- highest_found = addr;
- }
- }
- if (highest_found < amxhdr[AMX_HDR_CIP] < call_addr) {
- highest_found = amxhdr[AMX_HDR_CIP];
- }
- // return the best found match
- return highest_found;
- } else {
- // called from inside the script
- new ret_addr = (GetFrameReturn(frm_addr) - 8) + (amxhdr[AMX_HDR_COD] - amxhdr[AMX_HDR_DAT]);
- new Opcode:opcode = UnrelocateOpcode(Opcode:ReadAmxMemory(ret_addr));
- if (opcode == OP_CALL) {
- // standard function call, get the call address
- return ReadAmxMemory(ret_addr + 4) - amxhdr[AMX_HDR_COD] - GetAmxBaseAddress();
- } else if (opcode == OP_SCTRL) {
- // modified code to call a function by pointer
- opcode = UnrelocateOpcode(Opcode:ReadAmxMemory(ret_addr - 8));
- if (opcode == OP_LOAD_PRI) {
- return ReadAmxMemory(ReadAmxMemory(ret_addr - 4));
- } else if (opcode == OP_LOAD_S_PRI) {
- return GetFrameVariable(prev_frm, ReadAmxMemory(ret_addr - 4));
- } else if (opcode == OP_CONST_PRI) {
- return ReadAmxMemory(ret_addr - 4);
- }
- }
- // guess!
- new end = amxhdr[AMX_HDR_COD] - amxhdr[AMX_HDR_DAT] + 8; // "halt"
- ret_addr = GetFrameReturn(GetFrameNextFrame(frm_addr)) + end;
- opcode = RelocateOpcode(OP_RETN);
- new Opcode:proc = RelocateOpcode(OP_PROC);
- while (ret_addr >= end) {
- if (Opcode:ReadAmxMemory(ret_addr) == proc) {
- if (ret_addr == end || Opcode:ReadAmxMemory(ret_addr - 4) == opcode) {
- // found a retn/proc pair.
- return ret_addr;
- }
- }
- ret_addr -= 4;
- }
- }
- // give up...
- return 0;
- }
- stock GetCurrentFrameFunction() {
- // this function gets its own caller - pointless but here for completeness
- return GetFrameFunction(GetCallerFrame());
- }
|