libTriton  version 0.9 build 1502
callbacks.cpp
Go to the documentation of this file.
1 /*
3 ** Copyright (C) - Triton
4 **
5 ** This program is under the terms of the Apache License 2.0.
6 */
7 
8 /* libTriton */
10 #include <triton/pythonObjects.hpp>
11 #include <triton/pythonUtils.hpp>
13 
14 #include <iostream>
15 #include <stdexcept>
16 #include <string>
17 
18 #include <pin.H>
19 
20 /* pintool */
21 #include "bindings.hpp"
22 #include "utils.hpp"
23 
24 
25 
26 
27 namespace tracer {
28  namespace pintool {
29  namespace callbacks {
30 
32  /* Check if there is a callback wich must be called at each instruction instrumented */
34 
35  /* Create the Instruction Python class */
36  PyObject* instClass = triton::bindings::python::PyInstruction(*inst);
37 
38  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
39  * Triton sends only one argument to the callback. This argument is the Instruction
40  * class and contains all information. */
41  PyObject* args = triton::bindings::python::xPyTuple_New(1);
42  PyTuple_SetItem(args, 0, instClass);
43  if (PyObject_CallObject(tracer::pintool::options::callbackAfter, args) == nullptr) {
44  PyErr_Print();
45  exit(1);
46  }
47 
48  Py_DECREF(args);
49  }
50  }
51 
52 
54  /* Check if there is a callback wich must be called at each instruction instrumented */
56 
57  /* Create the Instruction Python class */
58  PyObject* instClass = triton::bindings::python::PyInstruction(*inst);
59 
60  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
61  * Triton sends only one argument to the callback. This argument is the Instruction
62  * class and contains all information. */
63  PyObject* args = triton::bindings::python::xPyTuple_New(1);
64  PyTuple_SetItem(args, 0, instClass);
65  if (PyObject_CallObject(tracer::pintool::options::callbackBefore, args) == nullptr) {
66  PyErr_Print();
67  exit(1);
68  }
69 
70  Py_DECREF(args);
71  }
72  }
73 
74 
76  /* Check if there is a callback wich must be called at each instruction instrumented */
78 
79  /* Create the Instruction Python class */
80  PyObject* instClass = triton::bindings::python::PyInstruction(*inst);
81 
82  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
83  * Triton sends only one argument to the callback. This argument is the Instruction
84  * class and contains all information. */
85  PyObject* args = triton::bindings::python::xPyTuple_New(1);
86  PyTuple_SetItem(args, 0, instClass);
87  if (PyObject_CallObject(tracer::pintool::options::callbackBeforeIRProc, args) == nullptr) {
88  PyErr_Print();
89  exit(1);
90  }
91 
92  Py_DECREF(args);
93  }
94  }
95 
96 
97  void fini(void) {
98  /* Check if there is a callback wich must be called at the end of the execution */
100 
101  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
102  * There is no argument sent to the callback. */
103  PyObject* args = triton::bindings::python::xPyTuple_New(0);
104  if (PyObject_CallObject(tracer::pintool::options::callbackFini, args) == nullptr) {
105  PyErr_Print();
106  exit(1);
107  }
108 
109  Py_DECREF(args);
110  }
111  }
112 
113 
114  void routine(triton::uint32 threadId, PyObject* callback) {
115  PyObject* args = triton::bindings::python::xPyTuple_New(1);
116  PyTuple_SetItem(args, 0, triton::bindings::python::PyLong_FromUint32(threadId));
117  if (PyObject_CallObject(callback, args) == nullptr) {
118  PyErr_Print();
119  exit(1);
120  }
121  Py_DECREF(args);
122  }
123 
124 
125  void signals(triton::uint32 threadId, triton::sint32 sig) {
126  /* Check if there is a callback wich must be called when a signal occurs */
128 
129  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
130  * threadId and sig are sent to the callback. */
131  PyObject* args = triton::bindings::python::xPyTuple_New(2);
132  PyTuple_SetItem(args, 0, triton::bindings::python::PyLong_FromUint32(threadId));
133  PyTuple_SetItem(args, 1, triton::bindings::python::PyLong_FromUint32(sig));
134  if (PyObject_CallObject(tracer::pintool::options::callbackSignals, args) == nullptr) {
135  PyErr_Print();
136  exit(1);
137  }
138 
139  Py_DECREF(args);
140  }
141  }
142 
143 
145  /* Check if there is a callback wich must be called before the syscall processing */
147 
148  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
149  * threadId and Std are sent to the callback. */
150  PyObject* args = triton::bindings::python::xPyTuple_New(2);
151  PyTuple_SetItem(args, 0, triton::bindings::python::PyLong_FromUint32(threadId));
152  PyTuple_SetItem(args, 1, triton::bindings::python::PyLong_FromUint32(std));
153  if (PyObject_CallObject(tracer::pintool::options::callbackSyscallEntry, args) == nullptr) {
154  PyErr_Print();
155  exit(1);
156  }
157 
158  Py_DECREF(args);
159  }
160  }
161 
162 
164  /* Check if there is a callback wich must be called after the syscall processing */
166 
167  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
168  * threadId and Std are sent to the callback. */
169  PyObject* args = triton::bindings::python::xPyTuple_New(2);
170  PyTuple_SetItem(args, 0, triton::bindings::python::PyLong_FromUint32(threadId));
171  PyTuple_SetItem(args, 1, triton::bindings::python::PyLong_FromUint32(std));
172  if (PyObject_CallObject(tracer::pintool::options::callbackSyscallExit, args) == nullptr) {
173  PyErr_Print();
174  exit(1);
175  }
176 
177  Py_DECREF(args);
178  }
179  }
180 
181 
182  void imageLoad(std::string imagePath, triton::__uint imageBase, triton::__uint imageSize) {
183  /* Check if there is a callback wich must be called when an image is loaded */
185 
186  /* CallObject needs a tuple. The size of the tuple is the number of arguments.
187  * imagePath, imageBase and imageSize are sent to the callback. */
188  PyObject* args = triton::bindings::python::xPyTuple_New(3);
189  PyTuple_SetItem(args, 0, PyStr_FromString(imagePath.c_str()));
190  PyTuple_SetItem(args, 1, triton::bindings::python::PyLong_FromUint(imageBase));
191  PyTuple_SetItem(args, 2, triton::bindings::python::PyLong_FromUint(imageSize));
192  if (PyObject_CallObject(tracer::pintool::options::callbackImageLoad, args) == nullptr) {
193  PyErr_Print();
194  exit(1);
195  }
196 
197  Py_DECREF(args);
198  }
199  }
200 
201 
203  triton::__uint addr = inst->getAddress();
205 
207  return;
208 
212  }
213 
217  }
218  }
219 
220 
222  triton::__uint addr = inst->getAddress();
224 
226  return;
227 
231  }
232 
236  }
237  }
238 
239  };
240  };
241 };
242 
void update(bool flag)
Sets the state to flag.
Definition: trigger.cpp:39
This class is used when to represent an instruction.
Definition: instruction.hpp:48
TRITON_EXPORT triton::uint64 getAddress(void) const
Returns the address of the instruction.
void before(triton::arch::Instruction *inst)
Callback called before the instruction processing.
Definition: callbacks.cpp:53
void syscallExit(triton::uint32 threadId, triton::uint32 std)
Callback called after the syscall processing.
Definition: callbacks.cpp:163
void preProcessing(triton::arch::Instruction *inst, triton::uint32 threadId)
Pre processing configuration.
Definition: callbacks.cpp:202
void imageLoad(std::string imagePath, triton::__uint imageBase, triton::__uint imageSize)
Callback called when an image is loaded.
Definition: callbacks.cpp:182
void after(triton::arch::Instruction *inst)
Callback called after the instruction processing.
Definition: callbacks.cpp:31
void routine(triton::uint32 threadId, PyObject *callback)
Callback called before and after routine processing.
Definition: callbacks.cpp:114
void signals(triton::uint32 threadId, triton::sint32 sig)
Callback called when a signal occurs.
Definition: callbacks.cpp:125
void fini(void)
Callback called at the end of the execution.
Definition: callbacks.cpp:97
void syscallEntry(triton::uint32 threadId, triton::uint32 std)
Callback called before the syscall processing.
Definition: callbacks.cpp:144
void postProcessing(triton::arch::Instruction *inst, triton::uint32 threadId)
Post processing configuration.
Definition: callbacks.cpp:221
void beforeIRProc(triton::arch::Instruction *inst)
Callback called before the IR processing.
Definition: callbacks.cpp:75
triton::uint32 targetThreadId
TID focused during the JIT.
Definition: init.cpp:140
PyObject * callbackFini
Callback called at the end of the execution.
Definition: init.cpp:125
std::set< triton::__uint > startAnalysisFromOffset
Start analysis from an offset.
Definition: init.cpp:137
PyObject * callbackSyscallEntry
Callback called before the syscall processing.
Definition: init.cpp:128
PyObject * callbackBefore
Callback called before the instruction processing.
Definition: init.cpp:123
PyObject * callbackAfter
Callback called after the instruction processing.
Definition: init.cpp:122
std::set< triton::__uint > stopAnalysisFromAddress
Stop analysis from address.
Definition: init.cpp:138
PyObject * callbackSyscallExit
Callback called after the syscall processing.
Definition: init.cpp:129
std::set< triton::__uint > startAnalysisFromAddress
Start analysis from a symbol.
Definition: init.cpp:136
PyObject * callbackBeforeIRProc
Callback called before the IR processing.
Definition: init.cpp:124
PyObject * callbackImageLoad
Callback called when an image is loaded.
Definition: init.cpp:126
std::set< triton::__uint > stopAnalysisFromOffset
Stop analysis from an offset.
Definition: init.cpp:139
PyObject * callbackSignals
Callback called when a signal occurs.
Definition: init.cpp:127
Trigger analysisTrigger
Lock / Unlock InsertCall.
Definition: main.cpp:201
triton::__uint getInsOffset(triton::__uint address)
Returns the instruction offset from a given address.
Definition: utils.cpp:59
PyObject * PyLong_FromUint(triton::__uint value)
Returns a pyObject from a triton::__uint.
Definition: utils.cpp:231
PyObject * xPyTuple_New(Py_ssize_t len)
Creates a PyTuple and raises an exception if it fails.
PyObject * PyInstruction(void)
Creates the Instruction python class.
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition: utils.cpp:297
std::int32_t sint32
signed 32-bits
Definition: tritonTypes.hpp:52
std::uint32_t uint32
unisgned 32-bits
Definition: tritonTypes.hpp:31
unsigned long long __uint
unsigned long long if the arch is 64-bits.
Definition: tritonTypes.hpp:71
The Tracer namespace.
Definition: api.cpp:12