gdb调试工具的基本原理和使用

基础概念

GDB(GNU Debugger)是GNU项目下的一个强大的调试工具,用于调试各种程序,特别是那些用C、C++和其他编程语言(如go)编写的程序。

DB常用于以下几种场景:

  • 调试崩溃:当程序崩溃时,可以使用GDB来检查崩溃点,获取调用栈信息,找出导致崩溃的原因。这个时候就需要事先生成了一个coredump文件
  • 调试逻辑错误:通过设置断点和单步执行,可以逐步检查程序的逻辑,找出逻辑错误。
  • 性能调优:通过分析程序的执行路径和时间消耗,可以进行性能调优。

ELF: Linux可执行文件的格式,ELF中包含多个段,有一些是gdb调试所需要的,比如.debug_*,里面存了行号之类的

阅读更多

RSA算法中的公钥和私钥

定义

公钥私钥是非对称加密中的概念。

密码学中,密钥(key)是指某个用来完成加密解密、完整性验证等密码学应用的秘密信息。在对称密码学(或称密钥密码学)中,加密和解密用的是同一个钥匙,因此钥匙需要保密。而在公钥密码学(或称非对称密码学)中,加密和解密用的钥匙不同:通常一个是公开的,称为公钥;另一个保密,称为私钥

私钥可以有多种表示形式,常见的有 PEM(Privacy Enhanced Mail)和 DER(Distinguished Encoding Rules)等。PEM 格式是一种基于 Base64 编码的表示形式,通常使用 -----BEGIN PRIVATE KEY----- 和 -----END PRIVATE KEY----- 包围。

原理

阅读更多

性能优化工具-strace

概述

使用 strace 查看系统调用统计可以帮助你分析一个进程的行为,特别是它在运行时进行的系统调用

原理

strace 使用 ptrace 系统调用追踪目标进程

当被跟踪的进程

阅读更多

性能优化工具-iostat

常用命令

  • -x 扩展信息

  • -m 使用MB单位

  • 命令后跟数字x y 每x秒刷新, 显示y次

    #每隔 2 秒显示一次详细的设备统计信息,共显示 5 次。

    iostat -x 2 5
  • -c 显示cpu相关的信息

  • -p 指定特定设备信息,比如-p sda只展示sda下的\

指标

基本指标

  • Device: 设备名称,例如 sda 表示第一个SCSI磁盘设备。
阅读更多

性能优化工具-perf

概述

perf 是一个强大的 Linux 性能分析工具,它可以用来收集和分析性能数据。

perf的命令有很多,下文将就其中常用的几条进行阐述


usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

The most commonly used perf commands are:
annotate Read perf.data (created by perf record) and display annotated code
archive Create archive with object files with build-ids found in perf.data file
bench General framework for benchmark suites
buildid-cache Manage build-id cache.
buildid-list List the buildids in a perf.data file
c2c Shared Data C2C/HITM Analyzer.
config Get and set variables in a configuration file.
daemon Run record sessions on background
data Data file related processing
diff Read perf.data files and display the differential profile
evlist List the event names in a perf.data file
ftrace simple wrapper for kernel's ftrace functionality
inject Filter to augment the events stream with additional information
iostat Show I/O performance metrics
kallsyms Searches running kernel for symbols
kvm Tool to trace/measure kvm guest os
list List all symbolic event types
mem Profile memory accesses
record Run a command and record its profile into perf.data
report Read perf.data (created by perf record) and display the profile
script Read perf.data (created by perf record) and display trace output
stat Run a command and gather performance counter statistics
test Runs sanity tests.
top System profiling tool.
version display the version of perf binary
probe Define new dynamic tracepoints
trace strace inspired tool
kmem Tool to trace/measure kernel memory properties
kwork Tool to trace/measure kernel work properties (latencies)
lock Analyze lock events
sched Tool to trace/measure scheduler properties (latencies)
timechart Tool to visualize total system behavior during a workload

See 'perf help COMMAND' for more information on a specific command.

安装

阅读更多

linux二进制文件加载源码分析

linux二进制加载

结构

linux_binfmt 用来定义二进制格式的结构,代表某种 特定的格式,比如elf

struct linux_binfmt {
struct list_head lh; // 链表头部
struct module *module; // 模块
int (*load_binary)(struct linux_binprm *); // 加载函数
int (*load_shlib)(struct file *);
#ifdef CONFIG_COREDUMP
int (*core_dump)(struct coredump_params *cprm); // coredump处理函数,如elf会有定义
unsigned long min_coredump; /* minimal dump size */
#endif
} __randomize_layout;

linux_binprm 用来存放可执行 文件的各种参数,代表可执行文件的运行时结构

阅读更多

golang上下文切换测试

引入

golang是一门高性能的编程语言,基于其轻量,高效的的Goroutine的设计,可以实现很小的协程切换开销。
本文将解释一下基本的协程相关的原理,并实际做一个小的测试,去得到其实际的切换开销

原理

协程

协程(Coroutine)是一种更轻量级的并发编程方式,他是在用户态实现的,相比于线程进程更加轻量,拥有更小的上下文切换开销,栈空间等等。
协程主要分为两种类型:

阅读更多

Enum Class Improvements for C++17, C++20 and C++23(C++17、C++20 和 C++23 的枚举类改进)

Enum Class Improvements for C++17, C++20 and C++23(C++17、C++20 和 C++23 的枚举类改进)

The evolution of the C++ language continues to bring powerful features that enhance code safety, readability, and maintainability. Among these improvements, we got changes and additions to enum class functionalities across C++17, C++20, and C++23. In this blog post, we’ll explore these advancements, focusing on initialization improvements in C++17, the introduction of the using enum keyword in C++20, and the std::to_underlying utility in C++23.

文章转载自: https://www.cppstories.com/2024/enum-improvements/

alt text
C++ 语言的演变继续带来强大的功能,可增强代码安全性、可读性和可维护性。在这些改进中,我们对 C++17、C++20 和 C++23 中的枚举类功能进行了更改和添加。在这篇博文中,我们将探讨这些改进,重点介绍 C++17 中的初始化改进、C++20 中 using enum 关键字的引入以及 C++23 中的 std::to_underlying 实用程序。

Enum Class Recap Enum 类回顾

阅读更多

使用psmisc fuser 关闭指定端口或者路径的进程

使用psmisc fuser 关闭指定端口或者路径的进程

fuser 是一个非常有用的工具,可以用来查看哪些进程正在使用特定的文件、目录、或者端口。你也可以用它来查找和终止访问特定二进制文件的进程。

查询关闭特定端口的进程

格式如下,如启动了一个端口为8080的tcp服务进程

sudo fuser 8080/tcp
阅读更多