//------------------------------------------------------------------------- // callback for enumerating imports // ea: import address // name: import name (NULL if imported by ordinal) // ord: import ordinal (0 for imports by name) // param: user parameter passed to enum_import_names() // return: 1-ok, 0-stop enumeration static int idaapi py_import_enum_cb( ea_t ea, const char *name, uval_t ord, void *param) { // If no name, try to get the name associated with the 'ea'. It may be coming from IDS char name_buf[MAXSTR]; if ( name == NULL ) name = get_true_name(BADADDR, ea, name_buf, sizeof(name_buf)); PYW_GIL_CHECK_LOCKED_SCOPE(); ref_t py_name; if ( name == NULL ) py_name = borref_t(Py_None); else py_name = newref_t(PyString_FromString(name)); newref_t py_ord(Py_BuildValue(PY_FMT64, pyul_t(ord))); newref_t py_ea(Py_BuildValue(PY_FMT64, pyul_t(ea))); newref_t py_result( PyObject_CallFunctionObjArgs( (PyObject *)param, py_ea.o, py_name.o, py_ord.o, NULL)); return py_result != NULL && PyObject_IsTrue(py_result.o) ? 1 : 0; }
// !=NULL means variable size datatype static asize_t idaapi s_calc_item_size( // This function is used to determine // size of the (possible) item at 'ea' void *ud, // user-defined data ea_t ea, // address of the item asize_t maxsize) // maximal size of the item { PYW_GIL_GET; // Returns: 0-no such item can be created/displayed // this callback is required only for varsize datatypes py_custom_data_type_t *_this = (py_custom_data_type_t *)ud; newref_t py_result( PyObject_CallMethod( _this->py_self, (char *)S_CALC_ITEM_SIZE, PY_FMT64 PY_FMT64, pyul_t(ea), pyul_t(maxsize))); if ( PyW_ShowCbErr(S_CALC_ITEM_SIZE) || py_result == NULL ) return 0; uint64 num = 0; PyW_GetNumber(py_result.o, &num); return asize_t(num); }
virtual int idaapi execute() { PYW_GIL_GET; newref_t py_result(PyObject_CallFunctionObjArgs(py_callable.o, NULL)); int ret = py_result == NULL || !PyInt_Check(py_result.o) ? -1 : PyInt_AsLong(py_result.o); // if the requesting thread decided not to wait for the request to // complete, we have to self-destroy, nobody else will do it if ( (code & MFF_NOWAIT) != 0 ) delete this; return ret; }
static int idaapi callback(void *ud) { PYW_GIL_GET; py_timer_ctx_t *ctx = (py_timer_ctx_t *)ud; newref_t py_result(PyObject_CallFunctionObjArgs(ctx->pycallback, NULL)); int ret = py_result == NULL ? -1 : PyLong_AsLong(py_result.o); // Timer has been unregistered? if ( ret == -1 ) { // Free the context Py_DECREF(ctx->pycallback); delete ctx; } return ret; };
virtual bool idaapi run() { PYW_GIL_GET; // Get callable ref_t py_callable = py_callables.at(py_callable_idx); bool reschedule; newref_t py_result(PyObject_CallFunctionObjArgs(py_callable.o, NULL)); reschedule = py_result != NULL && PyObject_IsTrue(py_result.o); // No rescheduling? Then advance to the next callable if ( !reschedule ) ++py_callable_idx; // Reschedule this C callback only if there are more callables return py_callable_idx < py_callables.size(); }
static bool idaapi s_print( // convert to colored string void *ud, // user-defined data qstring *out, // output buffer. may be NULL const void *value, // value to print. may not be NULL asize_t size, // size of value in bytes ea_t current_ea, // current address (BADADDR if unknown) int operand_num, // current operand number int dtid) // custom data type id { PYW_GIL_GET; // Build a string from the buffer newref_t py_value(PyString_FromStringAndSize( (const char *)value, Py_ssize_t(size))); if ( py_value == NULL ) return false; py_custom_data_format_t *_this = (py_custom_data_format_t *) ud; newref_t py_result(PyObject_CallMethod( _this->py_self, (char *)S_PRINTF, "O" PY_FMT64 "ii", py_value.o, pyul_t(current_ea), operand_num, dtid)); // Error while calling the function? if ( PyW_ShowCbErr(S_PRINTF) || py_result == NULL ) return false; bool ok = false; if ( PyString_Check(py_result.o) ) { Py_ssize_t len; char *buf; if ( out != NULL && PyString_AsStringAndSize(py_result.o, &buf, &len) != -1 ) { out->qclear(); out->append(buf, len); } ok = true; } return ok; }
// may create data? NULL means always may static bool idaapi s_may_create_at( void *ud, // user-defined data ea_t ea, // address of the future item size_t nbytes) // size of the future item { py_custom_data_type_t *_this = (py_custom_data_type_t *)ud; PYW_GIL_GET; newref_t py_result( PyObject_CallMethod( _this->py_self, (char *)S_MAY_CREATE_AT, PY_FMT64 PY_FMT64, pyul_t(ea), pyul_t(nbytes))); PyW_ShowCbErr(S_MAY_CREATE_AT); return py_result != NULL && PyObject_IsTrue(py_result.o); }
void EmacsPythonEvalCommand::executeCommandImpl() { try { Py::Module module( "__main__" ); Py::Dict dict( module.getDict() ); runPythonStringInsideTryExcept( python_command ); Py::Object py_result( dict[ "__bemacs_eval_tmp__" ] ); if( !failed() ) commandSucceeded( convertPyObjectToEmacsExpression( py_result ) ); } catch( Py::Exception &e ) { e.clear(); } }
//--------------------------------------------------------------------------- static int idaapi py_visit_patched_bytes_cb( ea_t ea, qoff64_t fpos, uint64 o, uint64 v, void *ud) { PYW_GIL_CHECK_LOCKED_SCOPE(); newref_t py_result( PyObject_CallFunction( (PyObject *)ud, PY_BV_EA "LKK", bvea_t(ea), fpos, o, v)); PyW_ShowCbErr("visit_patched_bytes"); return (py_result != NULL && PyInt_Check(py_result.o)) ? PyInt_AsLong(py_result.o) : 0; }
static void idaapi s_analyze( // analyze custom data format occurrence void *ud, // user-defined data ea_t current_ea, // current address (BADADDR if unknown) int operand_num) // current operand number // this callback can be used to create // xrefs from the current item. // this callback may be missing. { PYW_GIL_GET; py_custom_data_format_t *_this = (py_custom_data_format_t *) ud; newref_t py_result( PyObject_CallMethod( _this->py_self, (char *)S_ANALYZE, PY_FMT64 "i", pyul_t(current_ea), operand_num)); PyW_ShowCbErr(S_ANALYZE); }
void EmacsPythonCallCommand::executeCommandImpl() { try { Py::Module module( "__main__" ); Py::Dict dict( module.getDict() ); // // create the tuple of arguments // Py::Tuple args_tuple( num_args ); for( int arg=0; arg < num_args; arg++ ) { args_tuple[ arg ] = convertEmacsExpressionToPyObject( python_args[arg] ); } dict[ "__bemacs_call_args__" ] = args_tuple; EmacsString command( FormatString ( "__bemacs_eval_tmp__ = apply( %s, __bemacs_call_args__ )\n" ) << python_function ); runPythonStringInsideTryExcept( command ); if( !failed() ) { Py::Object py_result( dict[ "__bemacs_eval_tmp__" ] ); commandSucceeded( convertPyObjectToEmacsExpression( py_result ) ); } } catch( Py::Exception &e ) { e.clear(); } }
generic_info context::get_info(cl_uint param_name) const { switch ((cl_context_info)param_name) { case CL_CONTEXT_REFERENCE_COUNT: return pyopencl_get_int_info(cl_uint, Context, PYOPENCL_CL_CASTABLE_THIS, param_name); case CL_CONTEXT_DEVICES: return pyopencl_get_opaque_array_info(device, Context, PYOPENCL_CL_CASTABLE_THIS, param_name); case CL_CONTEXT_PROPERTIES: { auto result = pyopencl_get_vec_info( cl_context_properties, Context, PYOPENCL_CL_CASTABLE_THIS, param_name); pyopencl_buf<generic_info> py_result(result.len() / 2); size_t i = 0; for (;i < py_result.len();i++) { cl_context_properties key = result[i * 2]; if (key == 0) break; cl_context_properties value = result[i * 2 + 1]; generic_info &info = py_result[i]; info.dontfree = 0; info.opaque_class = CLASS_NONE; switch (key) { case CL_CONTEXT_PLATFORM: info.opaque_class = CLASS_PLATFORM; info.type = "void *"; info.value = new platform( reinterpret_cast<cl_platform_id>(value)); break; #if defined(PYOPENCL_GL_SHARING_VERSION) && (PYOPENCL_GL_SHARING_VERSION >= 1) #if defined(__APPLE__) && defined(HAVE_GL) case CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE: #else case CL_GL_CONTEXT_KHR: case CL_EGL_DISPLAY_KHR: case CL_GLX_DISPLAY_KHR: case CL_WGL_HDC_KHR: case CL_CGL_SHAREGROUP_KHR: #endif info.type = "intptr_t *"; info.value = (void*)value; // we do not own this object info.dontfree = 1; break; #endif default: throw clerror("Context.get_info", CL_INVALID_VALUE, "unknown context_property key encountered"); } } py_result.resize(i); return pyopencl_convert_array_info(generic_info, py_result); } #if PYOPENCL_CL_VERSION >= 0x1010 case CL_CONTEXT_NUM_DEVICES: return pyopencl_get_int_info(cl_uint, Context, PYOPENCL_CL_CASTABLE_THIS, param_name); #endif default: throw clerror("Context.get_info", CL_INVALID_VALUE); } }
static bool idaapi s_scan( // convert from uncolored string void *ud, // user-defined data bytevec_t *value, // output buffer. may be NULL const char *input, // input string. may not be NULL ea_t current_ea, // current address (BADADDR if unknown) int operand_num, // current operand number (-1 if unknown) qstring *errstr) // buffer for error message { PYW_GIL_GET; py_custom_data_format_t *_this = (py_custom_data_format_t *) ud; newref_t py_result( PyObject_CallMethod( _this->py_self, (char *)S_SCAN, "s" PY_FMT64, input, pyul_t(current_ea), operand_num)); // Error while calling the function? if ( PyW_ShowCbErr(S_SCAN) || py_result == NULL) return false; bool ok = false; do { // We expect a tuple(bool, string|None) if ( !PyTuple_Check(py_result.o) || PyTuple_Size(py_result.o) != 2 ) break; borref_t py_bool(PyTuple_GetItem(py_result.o, 0)); borref_t py_val(PyTuple_GetItem(py_result.o, 1)); // Get return code from Python ok = PyObject_IsTrue(py_bool.o); // We expect None or the value (depending on probe) if ( ok ) { // Probe-only? Then okay, no need to extract the 'value' if ( value == NULL ) break; Py_ssize_t len; char *buf; if ( PyString_AsStringAndSize(py_val.o, &buf, &len) != -1 ) { value->qclear(); value->append(buf, len); } } // An error occured? else { // Make sure the user returned (False, String) if ( py_bool.o != Py_False || !PyString_Check(py_val.o) ) { *errstr = "Invalid return value returned from the Python callback!"; break; } // Get the error message *errstr = PyString_AsString(py_val.o); } } while ( false ); return ok; }