Skip to content

Commit e50094f

Browse files
committed
Release udbserver 0.1.0 version
1 parent c6354bb commit e50094f

20 files changed

+189
-47
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/target
2+
/build
3+
dist
4+
udbserver.egg-info
25
Cargo.lock
36

47
*.so

Cargo.toml

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
name = "udbserver"
33
version = "0.1.0"
44
authors = ["Bet4 <[email protected]>"]
5-
edition = "2018"
5+
description = "Provide Unicorn emulator with a debug server"
6+
license = "MIT"
7+
edition = "2021"
8+
readme = "README.md"
9+
repository = "http://github.com/bet4it/udbserver"
10+
categories = ["emulators"]
11+
keywords = ["gdb", "debugging", "emulator"]
612

713
[lib]
8-
crate-type = ["lib", "cdylib"]
14+
crate-type = ["lib"]
15+
16+
[features]
17+
capi = []
918

1019
[dependencies]
1120
gdbstub = "0.6"
12-
unicorn-engine = { git = "https://github.com/unicorn-engine/unicorn", branch = "dev", features = ["use_system_unicorn"] }
21+
unicorn-engine = { version = "2.0.0-rc7", features = ["use_system_unicorn"] }
22+
23+
[package.metadata.capi.header]
24+
subdirectory = false
25+
generation = false

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Bet4
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

-16
This file was deleted.

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# udbserver - Unicorn Emulator Debug Server
2+
3+
When you do emulation with [Unicorn Engine](https://www.unicorn-engine.org/), do you want to inspect the inner state during every step?
4+
5+
`udbserver` is a plugin for Unicorn, provides a debug server which implements [GDB Remote Serial Protocol](https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html). You can connect it by a `GDB` client and do debugging as what you do on real program.
6+
7+
`udbserver` can be used as a crate by Rust program, but it also provides a C library and bindings for other languages. You can use it inside most Unicorn based projects!
8+
9+
## Features
10+
11+
* [x] Registers
12+
* [x] Memory
13+
* [x] Single Step
14+
* [x] Breakpoint
15+
* [x] Watchpoint
16+
* [ ] Ctrl-C interrupt
17+
18+
## Architectures support
19+
20+
* i386
21+
* x86\_64
22+
* ARM
23+
* AArch64
24+
* MIPS
25+
* PowerPC
26+
27+
# Usage
28+
29+
## API
30+
31+
`udbserver` only provides one API:
32+
33+
```c
34+
void udbserver(void* handle, uint16_t port, uint64_t start_addr);
35+
```
36+
37+
The `handle` should be the raw handle of a Unicorn instance, `port` is the port to be listened, `start_addr` is the address which when Unicorn runs at the debug server will start and wait to be connected. if `start_addr` is provided with `0`, the debug server will start instantly.
38+
39+
You can call this API inside a Unicorn hook, so you can integrate `udbserver` inside other Unicorn based project easily.
40+
41+
## Used in Rust
42+
43+
You can use `udbserver` as a crate in `Rust`.
44+
45+
You can check the [example](examples/server.rs) on how to use it.
46+
47+
And you can try it by:
48+
49+
```sh
50+
$ cargo run --example server
51+
```
52+
53+
Then you can connect it with a `GDB` client.
54+
55+
## Installation
56+
57+
`udbserver` provides a C-compatible set of library, header and pkg-config files, which help you to use it with other languages.
58+
59+
To build and install it you need to use [cargo-c](https://crates.io/crates/cargo-c):
60+
61+
```sh
62+
$ cargo install cargo-c
63+
$ mkdir build
64+
$ cargo cinstall --release --prefix=/usr --destdir build
65+
$ sudo cp -a build/* /
66+
```
67+
68+
## Language bindings
69+
70+
After install the `udbserver` library, you can use `udbserver` in other languages.
71+
72+
You could check the examples on how to use `udbserver` by different languages:
73+
74+
* [C](bindings/c)
75+
* [Go](bindings/go)
76+
* [Java](bindings/java)
77+
* [Python](bindings/python)
File renamed without changes.

bindings/c/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Use udbserver in C
2+
3+
Check the [example](example.c) on how to use it.
4+
5+
```sh
6+
$ gcc example.c -lunicorn -ludbserver -o example
7+
$ ./example
8+
```

bindings/c/example.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <unicorn/unicorn.h>
2-
#include "udbserver.h"
2+
#include <udbserver.h>
33

44
int ADDRESS = 0x1000;
55
const unsigned char ARM_CODE[64] = {0x0f, 0x00, 0xa0, 0xe1, 0x14, 0x00, 0x80, 0xe2, 0x00, 0x10, 0x90, 0xe5, 0x14, 0x10, 0x81, 0xe2, 0x00, 0x10, 0x80, 0xe5, 0xfb, 0xff, 0xff, 0xea};

bindings/go/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Use udbserver in Go
2+
3+
Check the [example](example/main.go) on how to use it.
4+
5+
```sh
6+
$ go run ./example
7+
```

bindings/go/example/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"
7-
udbserver "sample.com/udbserver/go/udbserver"
7+
udbserver "github.com/bet4it/udbserver/bindings/go/udbserver"
88
)
99

1010
func run() error {

bindings/go/go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module sample.com/udbserver/go
1+
module github.com/bet4it/udbserver/bindings/go
22

3-
go 1.16
3+
go 1.18
44

5-
require github.com/unicorn-engine/unicorn v0.0.0-20210516133931-668c43c94d5b // indirect
5+
require github.com/unicorn-engine/unicorn v0.0.0-20220417144812-185a6fec9eaa

bindings/go/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/unicorn-engine/unicorn v0.0.0-20210516133931-668c43c94d5b h1:2EG7u1+P+rTXwIO37ciA9Te7WcpdJRzmb7jLo/c1vt8=
2-
github.com/unicorn-engine/unicorn v0.0.0-20210516133931-668c43c94d5b/go.mod h1:vm0xtY46O4X0t1J6Ob+syPhvL38XAAidXGXmTSlcMZM=
1+
github.com/unicorn-engine/unicorn v0.0.0-20220417144812-185a6fec9eaa h1:xm6nL6HwVdIwdXyOZBSAmBBhwJuiqd2ca2dwSyTMLoU=
2+
github.com/unicorn-engine/unicorn v0.0.0-20220417144812-185a6fec9eaa/go.mod h1:mcHBrigWSHlMZYol9QOFnK7sbltIt/OaKP5CQBZsC+4=

bindings/go/udbserver/udbserver.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
)
88

99
/*
10-
#cgo CFLAGS: -O3 -Wall -Werror -I../../../include
11-
#cgo LDFLAGS: -L../../../target/release -ludbserver
12-
#cgo linux LDFLAGS: -L../../../target/release -ludbserver -lrt
13-
#include "udbserver.h"
10+
#cgo CFLAGS: -O3 -Wall -Werror
11+
#cgo LDFLAGS: -ludbserver
12+
#include <udbserver.h>
1413
*/
1514
import "C"
1615

bindings/java/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Use udbserver in Java
2+
3+
Remember to [install Unicorn java bindings](https://github.com/unicorn-engine/unicorn/tree/master/bindings/java) before use it.
4+
5+
Check the [example](Example.java) on how to use it.
6+
7+
```sh
8+
$ sudo make install
9+
$ make example
10+
```

bindings/python/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Use udbserver in Python
2+
3+
Check the [example](example.py) on how to use it.
4+
5+
```sh
6+
$ sudo python3 setup.py install
7+
$ python3 example.py
8+
```
9+
10+
If you encounter any errors when use it, try to compile and install the latest Unicorn library and Unicorn python bindings by yourself.

bindings/python/setup.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#!/usr/bin/env python3
22
# encoding: utf-8
33

4-
from distutils.core import setup, Extension
4+
from setuptools import setup, Extension
55

66

77
rust_module = Extension('udbserver',
88
sources=['udbserver.c'],
99
libraries=['udbserver'],
10-
include_dirs=['../../include'],
11-
library_dirs=['../../target/release'],
1210
)
1311

1412
setup (name = 'udbserver',
1513
version = '0.1',
16-
author = "Bet4",
17-
description = """Udbserver""",
14+
author = 'Bet4',
15+
author_email = '[email protected]',
16+
description = 'Python bindings of udbserver',
17+
url = 'https://github.com/bet4it/udbserver',
18+
license='MIT License',
19+
classifiers=[
20+
'Intended Audience :: Developers',
21+
'License :: OSI Approved :: MIT License',
22+
'Programming Language :: Python :: 3',
23+
'Topic :: Software Development :: Debuggers',
24+
],
1825
ext_modules = [rust_module],
1926
py_modules = [],
2027
)

bindings/python/udbserver.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <Python.h>
33

4-
#include "udbserver.h"
4+
#include <udbserver.h>
55

66
static PyObject* _udbserver(PyObject *self, PyObject *args) {
77
PyObject *uc;
@@ -28,7 +28,7 @@ static PyMethodDef udbserver_methods[] = {
2828
static struct PyModuleDef udbserver_definition = {
2929
PyModuleDef_HEAD_INIT,
3030
"udbserver",
31-
"Unicorn debugger server.",
31+
"Unicorn emulator debug server.",
3232
-1,
3333
udbserver_methods
3434
};

src/capi.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use std::ffi::c_void;
55
use std::ptr::null_mut;
66
use unicorn_engine::Unicorn;
77

8-
pub type uc_handle = *mut c_void;
9-
pub type uc_hook = *mut c_void;
8+
type uc_handle = *mut c_void;
109

1110
static mut HANDLE: uc_handle = null_mut();
1211
static mut UNICORN: Option<&mut Unicorn<()>> = None;

src/emu.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::arch;
2-
use crate::capi::uc_hook;
32
use crate::reg::Register;
43
use crate::DynResult;
54

@@ -9,12 +8,15 @@ use gdbstub::target::ext::breakpoints::WatchKind;
98
use gdbstub::target::{TargetError, TargetResult};
109
use std::collections::HashMap;
1110
use std::convert::TryFrom;
11+
use std::ffi::c_void;
1212
use unicorn_engine::unicorn_const::{uc_error, HookType, MemType, Mode, Query};
1313
use unicorn_engine::Unicorn;
1414

15+
type Hook = *mut c_void;
16+
1517
struct EmuState {
1618
step_state: bool,
17-
step_hook: Option<uc_hook>,
19+
step_hook: Option<Hook>,
1820
watch_addr: Option<u64>,
1921
}
2022

@@ -75,11 +77,11 @@ fn mem_hook(uc: &mut Unicorn<()>, _mem_type: MemType, addr: u64, _size: usize, _
7577
pub struct Emu<'a> {
7678
uc: &'a mut Unicorn<'static, ()>,
7779
reg: Register,
78-
bp_sw_hooks: HashMap<u64, uc_hook>,
79-
bp_hw_hooks: HashMap<u64, uc_hook>,
80-
wp_r_hooks: HashMap<u64, HashMap<u64, uc_hook>>,
81-
wp_w_hooks: HashMap<u64, HashMap<u64, uc_hook>>,
82-
wp_rw_hooks: HashMap<u64, HashMap<u64, uc_hook>>,
80+
bp_sw_hooks: HashMap<u64, Hook>,
81+
bp_hw_hooks: HashMap<u64, Hook>,
82+
wp_r_hooks: HashMap<u64, HashMap<u64, Hook>>,
83+
wp_w_hooks: HashMap<u64, HashMap<u64, Hook>>,
84+
wp_rw_hooks: HashMap<u64, HashMap<u64, Hook>>,
8385
}
8486

8587
impl<'a> Emu<'a> {

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
mod arch;
1+
#[cfg(feature = "capi")]
22
mod capi;
3+
4+
mod arch;
35
mod emu;
46
mod reg;
57

0 commit comments

Comments
 (0)