linux strace 查看程序异常问题总结

1,死锁问题

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;

void* thread_func1(void* arg) {
    pthread_mutex_lock(&lock1);
    printf("Thread pid[%lx]: Locked lock1\n",pthread_self());
    sleep(1); // 模拟一些工作
    pthread_mutex_lock(&lock2);
    printf("Thread 1 pid[%lx]: Locked lock2\n",pthread_self());
    pthread_mutex_unlock(&lock2);
    pthread_mutex_unlock(&lock1);
    return NULL;
}

void* thread_func2(void* arg) {
    pthread_mutex_lock(&lock2);
    printf("Thread pid[%lx]: Locked lock2\n",pthread_self());
    sleep(1); // 模拟一些工作
    pthread_mutex_lock(&lock1);
    printf("Thread pid[%lx]: Locked lock1\n",pthread_self());
    pthread_mutex_unlock(&lock1);
    pthread_mutex_unlock(&lock2);
    return NULL;
}

int main() {
    pthread_t t1, t2;

    pthread_create(&t1, NULL, thread_func1, NULL);
    pthread_create(&t2, NULL, thread_func2, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    pthread_mutex_destroy(&lock1);
    pthread_mutex_destroy(&lock2);

    return 0;
}

运行

lark@ubuntu:~/test$ ./pthread1

Thread pid[7fc61fba2700]: Locked lock2

Thread pid[7fc6203a3700]: Locked lock1
lark@ubuntu:~$ ps -axf | grep pthread1

2769 pts/0 Sl+ 0:00 | \_ ./pthread1

2835 pts/2 S+ 0:00 \_ grep --color=auto pthread1
lark@ubuntu:~$ sudo strace -p 2769

[sudo] password for lark:

strace: Process 2769 attached

futex(0x7fc6203a39d0, FUTEX_WAIT, 2770, NULL

可以看到是锁在等待。

2,文件操作失败:

#include<stdio.h>
#include<unistd.h>
#include <string.h>
#include<errno.h>
 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
	#if 0
	int fd = open("/etc/shadow",O_RDONLY);
	if(fd < 0)
	{
		printf("fd[%d], errno[%d]\r\n", fd,errno);
		return -1;
	}
	#endif
	FILE* fp = fopen("/etc/shadow","r");
	if(NULL == fp)
	{
		printf("errno[%s]\r\n",strerror(errno));
		return -1;
	}	
	return 0;
}

lark@ubuntu:~/test$ gcc strace.c

lark@ubuntu:~/test$ strace ./a.out

execve("./a.out", ["./a.out"], 0x7ffdc7708a50 /* 25 vars */) = 0

brk(NULL) = 0x559038b11000

arch_prctl(0x3001 /* ARCH_??? */, 0x7ffe2657c8f0) = -1 EINVAL (Invalid argument)

access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

fstat(3, {st_mode=S_IFREG|0644, st_size=86377, ...}) = 0

mmap(NULL, 86377, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6ac1965000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300A\2\0\0\0\0\0"..., 832) = 832

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32

pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\7\2C\n\357_\243\335\2449\206V>\237\374\304"..., 68, 880) = 68

fstat(3, {st_mode=S_IFREG|0755, st_size=2029592, ...}) = 0

mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6ac1963000

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32

pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\7\2C\n\357_\243\335\2449\206V>\237\374\304"..., 68, 880) = 68

mmap(NULL, 2037344, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f6ac1771000

mmap(0x7f6ac1793000, 1540096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x22000) = 0x7f6ac1793000

mmap(0x7f6ac190b000, 319488, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19a000) = 0x7f6ac190b000

mmap(0x7f6ac1959000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f6ac1959000

mmap(0x7f6ac195f000, 13920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f6ac195f000

close(3) = 0

arch_prctl(ARCH_SET_FS, 0x7f6ac1964540) = 0

mprotect(0x7f6ac1959000, 16384, PROT_READ) = 0

mprotect(0x55903745a000, 4096, PROT_READ) = 0

mprotect(0x7f6ac19a8000, 4096, PROT_READ) = 0

munmap(0x7f6ac1965000, 86377) = 0

brk(NULL) = 0x559038b11000

brk(0x559038b32000) = 0x559038b32000

openat(AT_FDCWD, "/etc/shadow", O_RDONLY) = -1 EACCES (Permission denied)

fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x1), ...}) = 0

write(1, "errno[Permission denied]\r\n", 26errno[Permission denied]

) = 26

exit_group(-1) = ?

+++ exited with 255 +++

可以看到openat函数异常退出-1,原因为Permission denied。

相关推荐
乙己4073 小时前
计算机网络——网络层
运维·服务器·计算机网络
飞行的俊哥3 小时前
Linux 内核学习 3b - 和copilot 讨论pci设备的物理地址在内核空间和用户空间映射到虚拟地址的区别
linux·驱动开发·copilot
hunter2062065 小时前
ubuntu向一个pc主机通过web发送数据,pc端通过工具直接查看收到的数据
linux·前端·ubuntu
qzhqbb5 小时前
web服务器 网站部署的架构
服务器·前端·架构
不会飞的小龙人6 小时前
Docker Compose创建镜像服务
linux·运维·docker·容器·镜像
不会飞的小龙人6 小时前
Docker基础安装与使用
linux·运维·docker·容器
白粥行7 小时前
linux-ubuntu学习笔记碎记
linux·ubuntu
果果开发ggdoc.cn7 小时前
WordPress免费证书插件
服务器·https·ssl
jerry-898 小时前
通过配置核查,CentOS操作系统当前无多余的、过期的账户;但CentOS操作系统存在共享账户r***t
linux
小歆8848 小时前
100%全国产化时钟服务器、全国产化校时服务器、全国产化授时服务器
运维·服务器