Skip to content

Commit

Permalink
uftrace: Introduce pre-commit and apply the changes
Browse files Browse the repository at this point in the history
This patch introduces pre-commit[1] to maintain the code quality in a
automated way.  It helps linting the code with some useful hooks
predefined in .pre-commit-config.yaml.

This patch also apples the changes by pre-commit such as removing all
the trailing whitespaces and so on.

  $ python3 -m pip install pre-commit

  $ pre-commit install
  pre-commit installed at .git/hooks/pre-commit

  $ pre-commit run -a
  Trim Trailing Whitespace.................................................Failed
  - hook id: trailing-whitespace
  - exit code: 1
  - files were modified by this hook
      ...
  Fix End of Files.........................................................Failed
  - hook id: end-of-file-fixer
  - exit code: 1
  - files were modified by this hook
      ...

After running this, it will show the result as follows.

  $ pre-commit run -a
  Check Yaml...............................................................Passed
  Fix End of Files.........................................................Passed
  Trim Trailing Whitespace.................................................Passed
  isort....................................................................Passed

[1] https://pre-commit.com
Signed-off-by: Honggyu Kim <[email protected]>
  • Loading branch information
honggyukim committed Jan 2, 2022
1 parent b417739 commit b93c415
Show file tree
Hide file tree
Showing 52 changed files with 124 additions and 129 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Please note that the position of the "else if" line.

For python programs (for tests or scripts), use 4 spaces to indent.

[1] https://www.kernel.org/doc/Documentation/process/coding-style.rst
[1] https://www.kernel.org/doc/Documentation/process/coding-style.rst


Include subject word in message header
Expand All @@ -38,11 +38,11 @@ developers more easily distinguish patches from other subject.

$ git log --oneline --graph
* fef4226 Merge branch 'misc-fix'
|\
|\
| * 54a4ef0 test: Fix to be able to call runtest.py directly
| * 6bbe4a0 graph: Skip kernel functions outside of user
| * a76c7cb kernel: Use real address for filter match
|/
|/
...


Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ config: $(srcdir)/configure
$(LIBMCOUNT_UTILS_OBJS): $(objdir)/libmcount/%.op: $(srcdir)/utils/%.c $(LIBMCOUNT_DEPS)
$(QUIET_CC_FPIC)$(CC) $(LIB_CFLAGS) -c -o $@ $<

$(objdir)/libmcount/mcount.op: $(objdir)/version.h
$(objdir)/libmcount/mcount.op: $(objdir)/version.h

$(LIBMCOUNT_OBJS): $(objdir)/%.op: $(srcdir)/%.c $(LIBMCOUNT_DEPS)
$(QUIET_CC_FPIC)$(CC) $(LIB_CFLAGS) -c -o $@ $<
Expand Down
4 changes: 2 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ uftrace v0.9.4
* tui change
add 's' key to sort column in tui report mode by Hyoungjong
use --report option to start tui with report mode

* other changes
task level analysis for graph and info command
add Korean documentation
Expand Down Expand Up @@ -379,4 +379,4 @@ uftrace v0.6.1

uftrace v0.6
============
* project open!
* project open!
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ example, function graph of function 'main' looks like below:
=============== BACKTRACE ===============
backtrace #0: hit 1, time 25.024 us
[0] main (0x40066b)

========== FUNCTION CALL GRAPH ==========
25.024 us : (1) main
2.706 us : +-(1) atoi
: |
: |
19.600 us : +-(1) fib
16.683 us : (2) fib
12.773 us : (4) fib
Expand All @@ -229,7 +229,7 @@ The `info` command shows system and program information when recorded.
# ==================
# program version : uftrace v0.8.1
# recorded on : Tue May 24 11:21:59 2016
# cmdline : uftrace record tests/t-abc
# cmdline : uftrace record tests/t-abc
# cpu info : Intel(R) Core(TM) i7-3930K CPU @ 3.20GHz
# number of cpus : 12 / 12 (online / possible)
# memory info : 20.1 / 23.5 GB (free / total)
Expand Down
1 change: 0 additions & 1 deletion arch/aarch64/cpuinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ int arch_fill_cpuinfo_model(int fd)
fclose(fp);
return ret;
}

1 change: 0 additions & 1 deletion arch/aarch64/dynamic.S
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,3 @@ ENTRY(dynamic_return)

br x16
END(dynamic_return)

2 changes: 1 addition & 1 deletion arch/arm/plthook.S
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ENTRY(plt_hooker)
add sp, sp, #8
ldr lr, [sp, #-4]
ldr pc, [sp, #-8] /* return */

1:
ldr r2, .L2
.LPIC0:
Expand Down
1 change: 0 additions & 1 deletion arch/i386/mcount-dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,3 @@ int mcount_patch_func(struct mcount_dynamic_info *mdi, struct sym *sym,
}
return result;
}

28 changes: 14 additions & 14 deletions arch/i386/mcount-support.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define PR_FMT "mcount"
#define PR_DOMAIN DBG_MCOUNT

// a max number that retrieves the stack to find the location of
// a max number that retrieves the stack to find the location of
// the real return address of the main function for i386.
#define MAX_SEARCH_STACK 5

Expand All @@ -22,7 +22,7 @@

static bool search_main_ret = false;

int mcount_get_register_arg(struct mcount_arg_context *ctx,
int mcount_get_register_arg(struct mcount_arg_context *ctx,
struct uftrace_arg_spec *spec)
{
struct mcount_regs *regs = ctx->regs;
Expand All @@ -33,7 +33,7 @@ int mcount_get_register_arg(struct mcount_arg_context *ctx,
reg_idx = spec->reg_idx;
break;
default:
return -1;
return -1;
}

switch (reg_idx) {
Expand Down Expand Up @@ -127,7 +127,7 @@ void mcount_arch_get_retval(struct mcount_arg_context *ctx,
/* type of return value cannot be FLOAT, so check format instead */
if (spec->fmt != ARG_FMT_FLOAT)
memcpy(ctx->val.v, ctx->retval, spec->size);
else if (spec->size == 4)
else if (spec->size == 4)
asm volatile ("fstps %0\n\tflds %0" : "=m" (ctx->val.v));
else if (spec->size == 8)
asm volatile ("fstpl %0\n\tfldl %0" : "=m" (ctx->val.v));
Expand Down Expand Up @@ -160,7 +160,7 @@ void mcount_restore_arch_context(struct mcount_arch_context *ctx)
}

/*
For 16-byte stack-alignment,
For 16-byte stack-alignment,
the main function stores the return address in its stack scope at prologue.
When the time comes for the main function to return,
1. restore the saved return address from stack.
Expand All @@ -178,24 +178,24 @@ void mcount_restore_arch_context(struct mcount_arch_context *ctx)
8048606: 83 ec 14 sub $0x14,%esp
8048609: e8 02 fe ff ff call 8048410 <mcount@plt>
... ...
... ...
8048645: 8b 4d fc mov -0x4(%ebp),%ecx
8048648: c9 leave
8048649: 8d 61 fc lea -0x4(%ecx),%esp
804864c: c3 ret
So, in this case. The return address we want to replace with
mcount_exit is in the stack scope of the main function.
Non a parent located.
So, in this case. The return address we want to replace with
mcount_exit is in the stack scope of the main function.
Non a parent located.
we search stack for that address.
we search stack for that address.
we will look for it.
we will find it, and we will replace it.
we will find it, and we will replace it.
GOOD LUCK!
*/
unsigned long *mcount_arch_parent_location(struct symtabs *symtabs,
unsigned long *parent_loc,
unsigned long *parent_loc,
unsigned long child_ip)
{
if (!search_main_ret) {
Expand All @@ -217,8 +217,8 @@ unsigned long *mcount_arch_parent_location(struct symtabs *symtabs,
child_sym = find_symtabs(symtabs, child_ip);
child_name = symbol_getname(child_sym, child_ip);

// Assuming that this happens only in main..
if (!(strcmp(find_main[0], parent_name) ||
// Assuming that this happens only in main..
if (!(strcmp(find_main[0], parent_name) ||
strcmp(find_main[1], child_name))) {
ret_addr = *parent_loc;
for (stack_index = 1; stack_index < MAX_SEARCH_STACK; stack_index++) {
Expand Down
2 changes: 1 addition & 1 deletion arch/i386/mcount.S
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GLOBAL(mcount)
sub $32, %esp
/* save registers */
movl %edx, 28(%esp)
movl %ecx, 24(%esp)
movl %ecx, 24(%esp)
movl %eax, 20(%esp)
movl $0, 16(%esp)
/* parent location */
Expand Down
2 changes: 1 addition & 1 deletion arch/i386/plthook.S
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ENTRY(plt_hooker)
/* child_idx */
movl 36(%esp), %eax
movl %eax, 4(%esp)

/* module_id */
movl 32(%esp), %eax
movl %eax, 8(%esp)
Expand Down
6 changes: 3 additions & 3 deletions arch/x86_64/mcount-dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int mcount_setup_trampoline(struct mcount_dynamic_info *mdi)
pr_err("failed to mmap trampoline for setup");
}

if (mprotect(PAGE_ADDR(mdi->text_addr),
if (mprotect(PAGE_ADDR(mdi->text_addr),
PAGE_LEN(mdi->text_addr, mdi->text_size),
PROT_READ | PROT_WRITE | PROT_EXEC)) {
pr_dbg("cannot setup trampoline due to protection: %m\n");
Expand Down Expand Up @@ -704,7 +704,7 @@ void mcount_arch_patch_branch(struct mcount_disasm_info *info,
struct mcount_orig_insn *orig)
{
/*
* The first entry in the table starts right after the out-of-line
* The first entry in the table starts right after the out-of-line
* execution buffer.
*/
uint64_t entry_offset = orig->insn_size;
Expand Down Expand Up @@ -736,7 +736,7 @@ void mcount_arch_patch_branch(struct mcount_disasm_info *info,
/* previously, all jcc32 are downgraded to jcc8 */
disp = entry_offset - (jcc_index + JCC8_INSN_SIZE);
if (disp > SCHAR_MAX) { /* should not happen */
pr_err("target is not in reach");
pr_err("target is not in reach");
}

/* patch jcc displacement to target correspending entry in the table */
Expand Down
8 changes: 4 additions & 4 deletions arch/x86_64/mcount-insn.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ static int x86_reg_index(int capstone_reg)
/*
* Handle relative conditional jumps and relative unconditional jumps.
*
* This function relocates jcc8 and jcc32 instructions by replacing them with a jcc8
* This function relocates jcc8 and jcc32 instructions by replacing them with a jcc8
* that has a null offset. The offset will be patched later when the code is saved
* in the of line execution buffer. The new jcc8 will bounce (if condition is met)
* on a trampoline that jumps to the target of the orginal instruction.
*
* The relocation of jmp8 and jmp32 is achieved by replacing them with an absolute
* The relocation of jmp8 and jmp32 is achieved by replacing them with an absolute
* indirect jump to the target.
*
*/
Expand Down Expand Up @@ -125,7 +125,7 @@ static int handle_rel_jmp(cs_insn *insn, uint8_t insns[],
memcpy(insns + JMP_INSN_SIZE, &target, sizeof(target));

info->modified = true;
/*
/*
* If this jump is the last insn in the prologue, we can ignore
* the one in patch_normal_func()
*/
Expand Down Expand Up @@ -163,7 +163,7 @@ static int handle_rel_jmp(cs_insn *insn, uint8_t insns[],
relocated_insn[OFS] = 0x00;

memcpy(insns, (void *)relocated_insn, JCC8_INSN_SIZE);

info->modified = true;
return JCC8_INSN_SIZE;
}
Expand Down
40 changes: 20 additions & 20 deletions doc/ko/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ uftrace 는 C/C++ 로 작성된 프로그램의 실행 흐름을 추적(trace)
기능
========

프로그램 실행 중 각 함수들과 그 실행 소요시간을 추적한다.
프로그램 실행 중 각 함수들과 그 실행 소요시간을 추적한다.
외부 라이브러리의 호출 또한 지원하지만, 보통 진입과 종료만 지원한다.
다른 (중첩된) 외부 라이브러리의 호출 그리고 (혹은) 라이브러리 호출에서
다른 (중첩된) 외부 라이브러리의 호출 그리고 (혹은) 라이브러리 호출에서
내부 함수의 호출을 추적하는 것도 가능하다.

함수 레벨의 구체적인 실행 흐름을 보여주고, 어떤 함수가 가장 많은 오버헤드를
Expand All @@ -40,7 +40,7 @@ uftrace 는 C/C++ 로 작성된 프로그램의 실행 흐름을 추적(trace)
uftrace 빌드 및 설치 방법
================================

리눅스 배포판에서, [misc/install-deps.sh](../../misc/install-deps.sh) 스크립트는
리눅스 배포판에서, [misc/install-deps.sh](../../misc/install-deps.sh) 스크립트는
추가적으로 필요한 소프트웨어를 설치해준다.
이는 고급 기능들을 위한 것이며 반드시 설치할 필요는 없지만
함께 설치하기를 적극 권장한다.
Expand Down Expand Up @@ -79,16 +79,16 @@ uftrace 명령어는 다음과 같은 하위 명령어들로 구성된다.
Try `uftrace --help' or `uftrace --usage' for more information.

만일 하위 명령어를 생략한다면, 기본적으로 record 와 replay 를 차례로 적용한 것과 동일한
`live` 명령어를 수행한다. (하지만 추적 정보를 파일로 저장하지 않는다)
`live` 명령어를 수행한다. (하지만 추적 정보를 파일로 저장하지 않는다)

record 명령어로 기록을 하려면, 각 함수마다 mcount 나 __cyg_profile_func_enter/exit 함수를
record 명령어로 기록을 하려면, 각 함수마다 mcount 나 __cyg_profile_func_enter/exit 함수를
호출하는 프로파일링 코드를 생성하기 위해 실행 파일을 `-pg`(혹은 `-finstrument-functions`) 옵션으로
컴파일해야 한다.

x86_64 와 AArch64(ARM64) 아키텍처에서 (재)컴파일 과정이 필요하지 않은 동적 추적 기능이
실험적으로 지원되고 있다. 또한 최근 컴파일러들 중
(여전히 사용자 프로그램을 재컴파일해야 하긴 하지만) 비슷한 방식으로
uftrace의 추적 과정에서 생기는 오버헤드를 줄이기 위한 옵션들을 제공하고 있다.
실험적으로 지원되고 있다. 또한 최근 컴파일러들 중
(여전히 사용자 프로그램을 재컴파일해야 하긴 하지만) 비슷한 방식으로
uftrace의 추적 과정에서 생기는 오버헤드를 줄이기 위한 옵션들을 제공하고 있다.
더 자세한 내용은 [dynamic tracing](uftrace-record.md#dynamic-tracing) 에서 확인해
볼 수 있다.

Expand All @@ -106,9 +106,9 @@ uftrace의 추적 과정에서 생기는 오버헤드를 줄이기 위한 옵션
4.962 us [ 1892] | } /* a */
5.769 us [ 1892] | } /* main */

더 상세한 분석을 하려면, record를 통해 우선 데이터를 기록하고
replay, report, graph, dump, info와 같은
분석 명령어를 여러 번 사용하는 것이 좋다.
더 상세한 분석을 하려면, record를 통해 우선 데이터를 기록하고
replay, report, graph, dump, info와 같은
분석 명령어를 여러 번 사용하는 것이 좋다.

$ uftrace record tests/t-abc

Expand All @@ -124,11 +124,11 @@ c 함수에서, 일반적인 시스템의 C 라이브러리 (glibc)에 내장된
사용자들은 함수들의 레코드/출력을 제한하기 위해 다양한 필터를 이용할 수 있다.
깊이 필터 (`-D` 옵션)은 주어진 호출 깊이보다 더 깊게 호출된 함수들을 생략하는 필터이다.
시간 필터 (`-t` 옵션)은 주어진 시간보다 더 작은 시간동안 실행된 함수들을 생략하는 필터이다.
그리고 함수 필터(`-F``-N` 옵션)은 주어진 함수의 하위 함수들을 보여주고/생략하는 필터이다.
그리고 함수 필터(`-F``-N` 옵션)은 주어진 함수의 하위 함수들을 보여주고/생략하는 필터이다.

`-k` 옵션으로 커널 함수들 또한 추적이 가능하다 (루트 권한 필요).
`-k` 옵션으로 커널 함수들 또한 추적이 가능하다 (루트 권한 필요).
보통 'hello world' 프로그램에 대한 출력 결과는 아래와 같다
(시스템 호출을 직접 호출하기 위해, 일반적인 printf()가 아닌
(시스템 호출을 직접 호출하기 위해, 일반적인 printf()가 아닌
stderr와 fprintf()를 사용하기로 한 것에 유의하라):

$ sudo uftrace -k tests/t-hello
Expand Down Expand Up @@ -172,7 +172,7 @@ fprintf()호출 내부에서 page fault 핸들러와 write syscall 핸들러가
19.600 us [22080] | } = 5; /* fib */
25.024 us [22080] | } /* main */

`report` 명령어는 어떤 함수가 그 자식을 포함해서
`report` 명령어는 어떤 함수가 그 자식을 포함해서
가장 오랫동안 실행되었는지, 총 시간을 알려준다.

$ uftrace report
Expand All @@ -186,18 +186,18 @@ fprintf()호출 내부에서 page fault 핸들러와 write syscall 핸들러가


`graph` 명령어는 주어진 함수의 호출 그래프를 보여준다.
위의 예제에서, main 함수의 호출 그래프는 아래와 같다:
위의 예제에서, main 함수의 호출 그래프는 아래와 같다:

$ uftrace graph main
# Function Call Graph for 'main' (session: 073f1e84aa8b09d3)
=============== BACKTRACE ===============
backtrace #0: hit 1, time 25.024 us
[0] main (0x40066b)

========== FUNCTION CALL GRAPH ==========
25.024 us : (1) main
2.706 us : +-(1) atoi
: |
: |
19.600 us : +-(1) fib
16.683 us : (2) fib
12.773 us : (4) fib
Expand All @@ -223,7 +223,7 @@ flame-graph 형식의 결과 또한 지원한다. 해당 데이터는 `uftrace d
# ==================
# program version : uftrace v0.8.1
# recorded on : Tue May 24 11:21:59 2016
# cmdline : uftrace record tests/t-abc
# cmdline : uftrace record tests/t-abc
# cpu info : Intel(R) Core(TM) i7-3930K CPU @ 3.20GHz
# number of cpus : 12 / 12 (online / possible)
# memory info : 20.1 / 23.5 GB (free / total)
Expand Down Expand Up @@ -263,4 +263,4 @@ flame-graph 형식의 결과 또한 지원한다. 해당 데이터는 `uftrace d

라이선스
=======
uftrace 는 GPL v2. 라이선스 하에 배포되며 자세한 내용은 [COPYING](../../COPYING) 파일에서 확인할 수 있다.
uftrace 는 GPL v2. 라이선스 하에 배포되며 자세한 내용은 [COPYING](../../COPYING) 파일에서 확인할 수 있다.
Loading

0 comments on commit b93c415

Please sign in to comment.