首页
社区
课程
招聘
[原创]发一个c++ hook库, 主要用来测试和调试
发表于: 2013-10-13 08:33 10631

[原创]发一个c++ hook库, 主要用来测试和调试

2013-10-13 08:33
10631
class C {
  int val_;  
public:
  C(int val) : val_(val) {}
  void Print() {
    std::cout << val_ << std::endl;
  }
};

void RunPrint(C &a) {
  a.Print();
}

int main() {
  C a(10), b(20);
  TRACER_TRACE_WITH(C::Print, (tracer::CallStackRecorder)) print;
  print.Before().connect([&b] (bool&, C *&self) {          // 注册一个会在C::Print调用之前被调用的回调
    self = &b;                          // 将任意对象对C::Print的调用转移到对象b上
  });
  RunPrint(a);                          // 输出20
  assert(print.GetCallStack(0).IsCalledBy("RunPrint") == true);  // 查找调用栈中是否有指定函数名
  assert(print.GetCallStack(0).IsCalledBy(main) == true);      // 查找调用栈中是否有指定函数指针
}


https://github.com/QingYun/tracer

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 5
支持
分享
最新回复 (9)
雪    币: 2362
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
lanbda都用上了 先进啊
已star   有空玩玩
2013-10-13 09:51
0
雪    币: 2362
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
https://github.com/QingYun/tracer/blob/master/tracer/hook_impl.cpp#L23
这里进程就没必要比较了吧
其实如果是hook第三方进程,挂起所有线程出问题的概率比全部不挂起还要高,感脚怎么着都不完美,真是一种蛋蛋的忧伤啊   原来有一丝心痛叫做无奈
2013-10-13 10:39
0
雪    币: 2362
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
还有,我看你的代码,是每hook一处地方就提交一次
https://github.com/QingYun/tracer/blob/master/tracer/trace.hpp#L105
这样的话,挂起线程安装钩子那出问题的几率又增加了很多啊,而且性能还稍有影响
2013-10-13 10:46
0
雪    币: 2362
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
boost和c++11用得很熟练啊,膜拜一下
2013-10-13 10:47
0
雪    币: 40
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
6
没考虑性能, 因为这个库没打算用在发布版本中
2013-10-13 10:56
0
雪    币: 25
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
原来有一丝心..
感觉好熟悉
2013-10-13 17:54
0
雪    币: 80
活跃值: (72)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
Singleton:
#ifndef __UTILITY_SINGLETON_HPP__
#define __UTILITY_SINGLETON_HPP__
#include <boost/scoped_ptr.hpp>
#include <boost/thread/once.hpp>
#include <boost/noncopyable.hpp>
namespace core {
	template <class T>
	class Singleton : private boost::noncopyable
	{
	public:
		static T& GetInstance()
		{
			boost::call_once(init, flag_);
			return *t_;
		}
		static void init() // never throws
		{
			static delete_helper<T> helper;
			helper.ptr = t_ = new T();
		}

		static T* GetInstancePtr()
		{
			boost::call_once(init, flag_);
			return t_;
		};
	protected:
		~Singleton() {}
		Singleton() {}

	private:
		template<class M>
		struct delete_helper 
		{
			delete_helper()
			{
				ptr = NULL;
			}
			~delete_helper()
			{
				delete ptr;
			}
			M* ptr;
		};
		static T* t_;
		static boost::once_flag flag_;
	};

	template<class T> T * Singleton<T>::t_ = NULL;
	template<class T> boost::once_flag Singleton<T>::flag_ = BOOST_ONCE_INIT;

}// namespace core
#endif//__UTILITY_SINGLETON_HPP__
2013-10-14 17:00
0
雪    币: 80
活跃值: (72)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
感觉有点复杂。这是俺的实现。
boost::function<BOOL(HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED> ReadFileAPI = 
					 sCoreHook.HookAPI<BOOL(HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED)>("Kernel32.dll", "ReadFile", boost::bind(&Handler::ReadFileHandler, this, _1, _2, _3, _4, _5));
2013-10-14 17:06
0
雪    币: 40
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
10
呵呵 我那个复杂的是库里的类型推导, 用户实际用的接口上是不用自己手敲一遍函数签名的
2013-10-16 20:49
0
游客
登录 | 注册 方可回帖
返回
//