Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arm Linux中终止线程的汇编代码_Linux_Assembly_Arm_System Calls_Kill - Fatal编程技术网

Arm Linux中终止线程的汇编代码

Arm Linux中终止线程的汇编代码,linux,assembly,arm,system-calls,kill,Linux,Assembly,Arm,System Calls,Kill,我想编写短Arm Linux汇编代码,终止调用它的线程 我无法调用函数(3)只有系统调用 我想用gettid(2)获取我的tid,然后将tkill(2)发送给这个tid mov %r7, $0xe0 /* gettid is syscall #0xe0 */ swi $0 /* invoke syscall */ /*r0 contains current tid*/ mov %r1, $9 /* SIGKILL signal */ mov

我想编写短Arm Linux汇编代码,终止调用它的线程

我无法调用函数(3)只有系统调用

我想用
gettid(2)
获取我的tid,然后将
tkill(2)
发送给这个tid

mov     %r7, $0xe0   /* gettid is syscall #0xe0 */
swi     $0          /* invoke syscall */
/*r0 contains current tid*/

mov     %r1, $9 /* SIGKILL signal */
mov     %r7, $0xee   /* tkill is syscall #0xee */
swi     $0          /* invoke syscall */

上面的代码有效吗?简单快捷的方法是什么?

首先使用符号测试C版本,而不是使用
musl libc
硬编码值-您可以检索并形成其git存储库

pthread\u exit.c
-调用
\u getId
系统调用:

#include <stdio.h>
#include <signal.h>

#include "syscall.h.in"
#include "syscall_arch.h"

int main(int argc, char** argv) {
  long pid =  __syscall0(__NR_gettid); 
  printf("pid: %ld\n", pid);
//__syscall2(__NR_tkill, pid, SIGKILL); 
  printf("Kilroy should not be here.\n");
}

gcc -o pthread_exit pthread_exit.c

./pthread_exit
pid: 14494
Kilroy should not be here.
./pthread_exit
pid: 14495
Kilroy should not be here.
./pthread_exit
pid: 14496
Kilroy should not be here.
查看
syscall\u arch.h

static inline long __syscall0(long n)
{
    register long r7 __ASM____R7__ = n;
    register long r0 __asm__("r0");
    __asm_syscall(R7_OPERAND);
}

static inline long __syscall2(long n, long a, long b)
{
    register long r7 __ASM____R7__ = n;
    register long r0 __asm__("r0") = a;
    register long r1 __asm__("r1") = b;
    __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1));
}
正如您已经计算出的,所需系统调用的号码应该位于
r7
,参数位于
r0
r5
-这里只需要
r0
r1

因此,是的,它是有效的:
pthread_exit.s

        .syntax unified
        .arch armv7-a
        .fpu vfpv3-d16
        .text
        .align  2
        .global main
        .thumb
        .thumb_func
        .type   main, %function
main:
        mov     %r7, $0xe0   /* gettid is syscall #0xe0 */
        swi     $0          /* invoke syscall */
        /*r0 contains current tid*/
        mov     %r1, $9 /* SIGKILL signal */
        mov     %r7, $0xee   /* tkill is syscall #0xee */
        swi     $0          /* invoke syscall */
       .end
使用qemu用户:

qemu-arm pthread_exit
Killed
Cortex-A7
系统上:

 ./pthread_exit
Killed

不确定这是正确的方式-请参阅。

tkill
不会终止线程。@n.'代词m。那么,用汇编杀死线程的方法是什么呢?没有办法杀死不想死亡的威胁。您链接的是使当前线程(或所有线程)退出,而不仅仅是另一个线程,而不是当前线程。我会假设
tgkill
是强制终止另一个线程的正确方法,尽管通常情况下,您会让其他线程运行在看到共享变量中的某些值后选择退出的代码。我明白了,感谢这种精度。从另一方面讲,我可能更愿意使用
tgkill
,哪个已经过时了,你能解释一下吗?如果我希望我的线程自杀,那么
gettid
tkill
是正确的方法,但是我想杀死另一个线程
tkill
不是正确的方法吗?为什么会这样?@yfr2493azzrggacom:这不是我的意思,我只是想知道是否有一种不那么残忍的方式。我想它也可以在另一个线程上运行。@Frant,问题是
tkill
kill所有进程。不仅仅是当前线程,你还有其他想法吗?
 ./pthread_exit
Killed