【发布时间】:2021-05-22 05:52:47
【问题描述】:
我正在编写一个用于跟踪 riscv 指令上的跳转(JAL/JALR) 指令的工具。我正在使用https://github.com/illumine/disk-benchmark/blob/master/src/disk-benchmark.c 磁盘基准测试我的工具。该工具在不使用虚拟函数和 pthread 时跟踪二进制文件上的跳转,但在使用 pthread 时难以跟踪。这是riscv上二进制文件的objdump:
0000000000011d8e <main>:
11d8e: de010113 addi sp,sp,-544
11d92: 20113c23 sd ra,536(sp)
11d96: 20813823 sd s0,528(sp)
11d9a: 1400 addi s0,sp,544
11d9c: 87aa mv a5,a0
11d9e: ecb43023 sd a1,-320(s0)
11da2: ecf42623 sw a5,-308(s0)
11da6: fe043023 sd zero,-32(s0)
11daa: fc043c23 sd zero,-40(s0)
11dae: ed040793 addi a5,s0,-304
11db2: 853e mv a0,a5
11db4: 8aeff0ef jal ra,10e62 <init_thread_work>
11db8: ecc42703 lw a4,-308(s0)
11dbc: ed040793 addi a5,s0,-304
11dc0: ec043603 ld a2,-320(s0)
11dc4: 85ba mv a1,a4
11dc6: 853e mv a0,a5
11dc8: e60ff0ef jal ra,11428 <parse_cli_arguments>
11dcc: 87aa mv a5,a0
11dce: c385 beqz a5,11dee <main+0x60>
11dd0: 67d5 lui a5,0x15
11dd2: 1287b783 ld a5,296(a5) # 15128 <stderr@@GLIBC_2.26>
11dd6: 86be mv a3,a5
11dd8: 02000613 li a2,32
11ddc: 4585 li a1,1
11dde: 67d1 lui a5,0x14
11de0: b7078513 addi a0,a5,-1168 # 13b70 <__stat+0x4c0>
11de4: f0dfe0ef jal ra,10cf0 <fwrite@plt>
11de8: 4505 li a0,1
11dea: e17fe0ef jal ra,10c00 <exit@plt>
11dee: f4842783 lw a5,-184(s0)
11df2: 12078163 beqz a5,11f14 <main+0x186>
11df6: ee042783 lw a5,-288(s0)
11dfa: 45a1 li a1,8
11dfc: 853e mv a0,a5
11dfe: e43fe0ef jal ra,10c40 <calloc@plt>
11e02: 87aa mv a5,a0
11e04: fef43023 sd a5,-32(s0)
11e08: ee042783 lw a5,-288(s0)
11e0c: 0e000593 li a1,224
11e10: 853e mv a0,a5
11e12: e2ffe0ef jal ra,10c40 <calloc@plt>
11e16: 87aa mv a5,a0
11e18: fcf43c23 sd a5,-40(s0)
11e1c: fe042623 sw zero,-20(s0)
11e20: a841 j 11eb0 <main+0x122>
11e22: fec42703 lw a4,-20(s0)
11e26: 87ba mv a5,a4
11e28: 078e slli a5,a5,0x3
11e2a: 8f99 sub a5,a5,a4
11e2c: 0796 slli a5,a5,0x5
11e2e: 873e mv a4,a5
11e30: fd843783 ld a5,-40(s0)
11e34: 973e add a4,a4,a5
11e36: fec42683 lw a3,-20(s0)
11e3a: ed040793 addi a5,s0,-304
11e3e: 8636 mv a2,a3
11e40: 85ba mv a1,a4
11e42: 853e mv a0,a5
11e44: 92eff0ef jal ra,10f72 <copy_thread_work>
11e48: fec42783 lw a5,-20(s0)
11e4c: 078e slli a5,a5,0x3
11e4e: fe043703 ld a4,-32(s0)
11e52: 00f70533 add a0,a4,a5
11e56: fec42703 lw a4,-20(s0)
11e5a: 87ba mv a5,a4
11e5c: 078e slli a5,a5,0x3
11e5e: 8f99 sub a5,a5,a4
11e60: 0796 slli a5,a5,0x5
11e62: 873e mv a4,a5
11e64: fd843783 ld a5,-40(s0)
11e68: 97ba add a5,a5,a4
11e6a: 86be mv a3,a5
11e6c: 000117b7 lui a5,0x11
11e70: 76078613 addi a2,a5,1888 # 11760 <do_benchmark>
11e74: 4581 li a1,0
11e76: e0bfe0ef jal ra,10c80 <pthread_create@plt>
11e7a: 87aa mv a5,a0
11e7c: faf42a23 sw a5,-76(s0)
11e80: fb442783 lw a5,-76(s0)
11e84: 2781 sext.w a5,a5
11e86: c385 beqz a5,11ea6 <main+0x118>
11e88: 67d5 lui a5,0x15
11e8a: 1287b703 ld a4,296(a5) # 15128 <stderr@@GLIBC_2.26>
11e8e: fb442783 lw a5,-76(s0)
11e92: 863e mv a2,a5
11e94: 67d1 lui a5,0x14
11e96: b9878593 addi a1,a5,-1128 # 13b98 <__stat+0x4e8>
11e9a: 853a mv a0,a4
11e9c: e35fe0ef jal ra,10cd0 <fprintf@plt>
11ea0: 4505 li a0,1
11ea2: d5ffe0ef jal ra,10c00 <exit@plt>
11ea6: fec42783 lw a5,-20(s0)
11eaa: 2785 addiw a5,a5,1
11eac: fef42623 sw a5,-20(s0)
11eb0: ee042703 lw a4,-288(s0)
11eb4: fec42783 lw a5,-20(s0)
11eb8: 2781 sext.w a5,a5
11eba: f6e7c4e3 blt a5,a4,11e22 <main+0x94>
11ebe: fe042623 sw zero,-20(s0)
11ec2: a00d j 11ee4 <main+0x156>
11ec4: fec42783 lw a5,-20(s0)
11ec8: 078e slli a5,a5,0x3
11eca: fe043703 ld a4,-32(s0)
11ece: 97ba add a5,a5,a4
11ed0: 639c ld a5,0(a5)
11ed2: 4581 li a1,0
11ed4: 853e mv a0,a5
11ed6: e0bfe0ef jal ra,10ce0 <pthread_join@plt>
11eda: fec42783 lw a5,-20(s0)
11ede: 2785 addiw a5,a5,1
11ee0: fef42623 sw a5,-20(s0)
11ee4: ee042703 lw a4,-288(s0)
11ee8: fec42783 lw a5,-20(s0)
11eec: 2781 sext.w a5,a5
11eee: fce7cbe3 blt a5,a4,11ec4 <main+0x136>
11ef2: fd843503 ld a0,-40(s0)
11ef6: dbbfe0ef jal ra,10cb0 <free@plt>
11efa: fe043503 ld a0,-32(s0)
11efe: db3fe0ef jal ra,10cb0 <free@plt>
11f02: 4529 li a0,10
11f04: e3dfe0ef jal ra,10d40 <putchar@plt>
11f08: f4c42783 lw a5,-180(s0)
11f0c: 853e mv a0,a5
11f0e: d13fe0ef jal ra,10c20 <sleep@plt>
11f12: b5d5 j 11df6 <main+0x68>
11f14: 4501 li a0,0
11f16: d7bfe0ef jal ra,10c90 <time@plt>
11f1a: fca43823 sd a0,-48(s0)
11f1e: df3fe0ef jal ra,10d10 <clock@plt>
11f22: fca43423 sd a0,-56(s0)
11f26: ee042783 lw a5,-288(s0)
11f2a: 45a1 li a1,8
11f2c: 853e mv a0,a5
11f2e: d13fe0ef jal ra,10c40 <calloc@plt>
11f32: 87aa mv a5,a0
11f34: fef43023 sd a5,-32(s0)
11f38: ee042783 lw a5,-288(s0)
11f3c: 0e000593 li a1,224
11f40: 853e mv a0,a5
11f42: cfffe0ef jal ra,10c40 <calloc@plt>
11f46: 87aa mv a5,a0
11f48: fcf43c23 sd a5,-40(s0)
11f4c: fe042623 sw zero,-20(s0)
11f50: a841 j 11fe0 <main+0x252>
11f52: fec42703 lw a4,-20(s0)
11f56: 87ba mv a5,a4
11f58: 078e slli a5,a5,0x3
11f5a: 8f99 sub a5,a5,a4
11f5c: 0796 slli a5,a5,0x5
11f5e: 873e mv a4,a5
11f60: fd843783 ld a5,-40(s0)
11f64: 973e add a4,a4,a5
11f66: fec42683 lw a3,-20(s0)
11f6a: ed040793 addi a5,s0,-304
11f6e: 8636 mv a2,a3
11f70: 85ba mv a1,a4
11f72: 853e mv a0,a5
11f74: ffffe0ef jal ra,10f72 <copy_thread_work>
11f78: fec42783 lw a5,-20(s0)
11f7c: 078e slli a5,a5,0x3
11f7e: fe043703 ld a4,-32(s0)
11f82: 00f70533 add a0,a4,a5
11f86: fec42703 lw a4,-20(s0)
11f8a: 87ba mv a5,a4
11f8c: 078e slli a5,a5,0x3
11f8e: 8f99 sub a5,a5,a4
11f90: 0796 slli a5,a5,0x5
11f92: 873e mv a4,a5
11f94: fd843783 ld a5,-40(s0)
11f98: 97ba add a5,a5,a4
11f9a: 86be mv a3,a5
11f9c: 000117b7 lui a5,0x11
11fa0: 76078613 addi a2,a5,1888 # 11760 <do_benchmark>
11fa4: 4581 li a1,0
11fa6: cdbfe0ef jal ra,10c80 <pthread_create@plt>
11faa: 87aa mv a5,a0
11fac: faf42a23 sw a5,-76(s0)
11fb0: fb442783 lw a5,-76(s0)
11fb4: 2781 sext.w a5,a5
11fb6: c385 beqz a5,11fd6 <main+0x248>
11fb8: 67d5 lui a5,0x15
11fba: 1287b703 ld a4,296(a5) # 15128 <stderr@@GLIBC_2.26>
11fbe: fb442783 lw a5,-76(s0)
11fc2: 863e mv a2,a5
11fc4: 67d1 lui a5,0x14
11fc6: bc878593 addi a1,a5,-1080 # 13bc8 <__stat+0x518>
11fca: 853a mv a0,a4
11fcc: d05fe0ef jal ra,10cd0 <fprintf@plt>
11fd0: 4505 li a0,1
11fd2: c2ffe0ef jal ra,10c00 <exit@plt>
11fd6: fec42783 lw a5,-20(s0)
11fda: 2785 addiw a5,a5,1
11fdc: fef42623 sw a5,-20(s0)
11fe0: ee042703 lw a4,-288(s0)
11fe4: fec42783 lw a5,-20(s0)
11fe8: 2781 sext.w a5,a5
11fea: f6e7c4e3 blt a5,a4,11f52 <main+0x1c4>
11fee: 4581 li a1,0
11ff0: fd843503 ld a0,-40(s0)
11ff4: a38ff0ef jal ra,1122c <debug_thread_results_header>
11ff8: fe042623 sw zero,-20(s0)
11ffc: a00d j 1201e <main+0x290>
11ffe: fec42783 lw a5,-20(s0)
12002: 078e slli a5,a5,0x3
12004: fe043703 ld a4,-32(s0)
12008: 97ba add a5,a5,a4
1200a: 639c ld a5,0(a5)
1200c: 4581 li a1,0
1200e: 853e mv a0,a5
12010: cd1fe0ef jal ra,10ce0 <pthread_join@plt>
12014: fec42783 lw a5,-20(s0)
12018: 2785 addiw a5,a5,1
1201a: fef42623 sw a5,-20(s0)
1201e: ee042703 lw a4,-288(s0)
12022: fec42783 lw a5,-20(s0)
12026: 2781 sext.w a5,a5
12028: fce7cbe3 blt a5,a4,11ffe <main+0x270>
1202c: ee042683 lw a3,-288(s0)
12030: de040793 addi a5,s0,-544
12034: ed040713 addi a4,s0,-304
12038: fd843603 ld a2,-40(s0)
1203c: 85ba mv a1,a4
1203e: 853e mv a0,a5
12040: a0dff0ef jal ra,11a4c <calculate_summaries>
12044: ed040793 addi a5,s0,-304
12048: de040713 addi a4,s0,-544
1204c: 0e000693 li a3,224
12050: 8636 mv a2,a3
12052: 85ba mv a1,a4
12054: 853e mv a0,a5
12056: cfbfe0ef jal ra,10d50 <memcpy@plt>
1205a: 4585 li a1,1
1205c: fd843503 ld a0,-40(s0)
12060: 9ccff0ef jal ra,1122c <debug_thread_results_header>
12064: ed040793 addi a5,s0,-304
12068: 853e mv a0,a5
1206a: 852ff0ef jal ra,110bc <debug_thread_results>
1206e: 4501 li a0,0
12070: c21fe0ef jal ra,10c90 <time@plt>
12074: fca43023 sd a0,-64(s0)
12078: c99fe0ef jal ra,10d10 <clock@plt>
1207c: faa43c23 sd a0,-72(s0)
12080: f3c42783 lw a5,-196(s0)
12084: ef9d bnez a5,120c2 <main+0x334>
12086: fd043583 ld a1,-48(s0)
1208a: fc043503 ld a0,-64(s0)
1208e: ce3fe0ef jal ra,10d70 <difftime@plt>
12092: 22a506d3 fmv.d fa3,fa0
12096: fb843703 ld a4,-72(s0)
1209a: fc843783 ld a5,-56(s0)
1209e: 40f707b3 sub a5,a4,a5
120a2: d227f753 fcvt.d.l fa4,a5
120a6: 67d1 lui a5,0x14
120a8: c487b787 fld fa5,-952(a5) # 13c48 <__stat+0x598>
120ac: 1af777d3 fdiv.d fa5,fa4,fa5
120b0: e2078653 fmv.x.d a2,fa5
120b4: e20685d3 fmv.x.d a1,fa3
120b8: 67d1 lui a5,0x14
120ba: bf878513 addi a0,a5,-1032 # 13bf8 <__stat+0x548>
120be: b73fe0ef jal ra,10c30 <printf@plt>
120c2: f4442783 lw a5,-188(s0)
120c6: c399 beqz a5,120cc <main+0x33e>
120c8: ac4ff0ef jal ra,1138c <print_system_datetime>
120cc: fd843503 ld a0,-40(s0)
120d0: be1fe0ef jal ra,10cb0 <free@plt>
120d4: fe043503 ld a0,-32(s0)
120d8: bd9fe0ef jal ra,10cb0 <free@plt>
120dc: 4501 li a0,0
120de: c43fe0ef jal ra,10d20 <pthread_exit@plt>
main函数的代码如下:
int main(int argc, char *argv[]){
pthread_t *threads = NULL;
ThreadWork_t thread_work, *threads_work = NULL;
int rc, t;
time_t start_time, stop_time;
clock_t start_clock, stop_clock;
/* Initialize the work and parse user options */
init_thread_work(&thread_work);
if( parse_cli_arguments( &thread_work, argc, argv) ){
fprintf(stderr,"\nError while parsing arguments!\n");
exit(ER_EXIT);
}
//if work continously, just block in the loop waiting for user to interrupt with Cntr-C
if( thread_work.work_continously){
while(1){
threads = calloc( thread_work.threads, sizeof(pthread_t) );
threads_work = calloc( thread_work.threads, sizeof(ThreadWork_t) );
for(t=0; t<thread_work.threads; t++) {
copy_thread_work(&thread_work, &threads_work[t], t );
rc = pthread_create( &threads[t], NULL, do_benchmark, (void *) &threads_work[t] );
if (rc){
fprintf(stderr,"\nError return code %d from pthread_create()",rc);
exit(ER_EXIT);
}
}
for(t=0; t<thread_work.threads; t++)
pthread_join( threads[t], NULL);
free(threads_work);
free(threads);
printf("\n");
sleep(thread_work.work_continously_sleep_brake);
}
}
/* Start Wall and CPU clocks and fire threads */
start_time = time(NULL);
start_clock = clock();
threads = calloc( thread_work.threads, sizeof(pthread_t) );
threads_work = calloc( thread_work.threads, sizeof(ThreadWork_t) );
for(t=0; t<thread_work.threads; t++) {
copy_thread_work(&thread_work, &threads_work[t], t );
rc = pthread_create( &threads[t], NULL, do_benchmark, (void *) &threads_work[t] );
if (rc){
fprintf(stderr,"\nERROR return code %d from pthread_create()",rc);
exit(ER_EXIT);
}
}
我的问题是如何跟踪在 riscv 架构上有 pthread 和虚函数的跳转?
作为一个信息,它从main-->do_benchmark-->write_file 跳转。我对这个序列很感兴趣。
【问题讨论】:
-
XY problem?。如果您正在跟踪跳转指令,则不会显示线程创建,这是一个“要求内核在另一个位置生成额外的 CPU”的系统调用。我认为您可以通过在您试图实现的目标的解释中添加更多细节并从您显示的代码中删除不重要的细节来改进您的问题(请参阅minimal reproducible example)。
-
感谢您的友好解释。我正在尝试检测程序流以计算控制流图。我只对跳跃感兴趣,对树枝不感兴趣。
-
使用
pthreads时的问题是indirect jumps,因为间接跳转不使用JAL/JALR指令,而是使用pthreads 和其他库,这使得函数无法准确定位到哪个地址.
标签: c multithreading binary pthreads riscv