search
HomeSystem TutorialLINUXSyscall system call Linux kernel tracing

Syscall system call Linux kernel tracing

Feb 12, 2024 pm 09:21 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

In Linux user space, we often need to call system calls. Let's take Linux version 2.6.37 as an example to track the implementation of the read system call. System call implementations may vary between versions of Linux.

Syscall system call Linux kernel tracing

In some applications, we can see the following definition:

scssCopy code
#define real_read(fd, buf, count ) (syscall(SYS_read, (fd), (buf), (count)))

Actually, what is actually called is the system function syscall(SYS_read), that is, the sys_read() function. In the Linux version 2.6.37, this function is implemented through several macro definitions.

Linux system call (SCI, system call interface) is actually a process of multi-channel aggregation and decomposition. The aggregation point is the 0x80 interrupt entry point (X86 system structure). That is to say, all system calls are aggregated from user space to the 0x80 interrupt point, and the specific system call number is saved at the same time. When the 0x80 interrupt handler is running, different system calls will be processed separately according to the system call number, that is, different kernel functions will be called for processing.

There are two ways to cause system calls:

(1) int $0×80, this is the only way to cause a system call in old Linux kernel versions.

(2) sysenter assembly instructions

In the Linux kernel, we can use the following macro definitions to make system calls.

SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
{
    struct file *file;
    ssize_t ret = -EBADF;
    int fput_needed;

    file = fget_light(fd, &fput_needed);
    if (file) {
        loff_t pos = file_pos_read(file);
        ret = vfs_read(file, buf, count, &pos);
        file_pos_write(file, pos);
        fput_light(file, fput_needed);
    }

    return ret;
}

The macro definition of SYSCALL_DEFINE3 is as follows:

#define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)

## means that the characters in the macro are directly replaced,
If name = read, then __NR_##name is replaced with __NR_read in the macro. NR##name is the system call number, ## refers to two macro expansions. That is, replace "name" with the actual system call name, and then expand __NR.... If name == ioctl, it is __NR_ioctl.

#ifdef CONFIG_FTRACE_SYSCALLS
#define SYSCALL_DEFINEx(x, sname, ...)                \
    static const char *types_##sname[] = {            \
        __SC_STR_TDECL##x(__VA_ARGS__)            \
    };                            \
    static const char *args_##sname[] = {            \
        __SC_STR_ADECL##x(__VA_ARGS__)            \
    };                            \
    SYSCALL_METADATA(sname, x);                \
    __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
#else
#define SYSCALL_DEFINEx(x, sname, ...)                \
    __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
#endif

Regardless of whether the CONFIG_FTRACE_SYSCALLS macro is defined or not, the following macro definition will eventually be executed:

__SYSCALL_DEFINEx(x, sname, VA_ARGS)

#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS

#define SYSCALL_DEFINE(name) static inline 
long SYSC_##name

#define __SYSCALL_DEFINEx(x, name, ...)                    \
    asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__));        \
    static inline long SYSC##name(__SC_DECL##x(__VA_ARGS__));    \
    asmlinkage long SyS##name(__SC_LONG##x(__VA_ARGS__))        \
    {                                \
        __SC_TEST##x(__VA_ARGS__);                \
        return (long) SYSC##name(__SC_CAST##x(__VA_ARGS__));    \
    }                                \
    SYSCALL_ALIAS(sys##name, SyS##name);                \
    static inline long SYSC##name(__SC_DECL##x(__VA_ARGS__))

#else /*
 CONFIG_HAVE_SYSCALL_WRAPPERS */

#define SYSCALL_DEFINE(name) asmlinkage 
long sys_##name
#define __SYSCALL_DEFINEx(x, name, ...)                    \
    asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))

#endif /*
 CONFIG_HAVE_SYSCALL_WRAPPERS */

The following types of macro definitions will eventually be called:

asmlinkage long sys##name(__SC_DECL##x(VA_ARGS))
That is the sys_read() system function we mentioned earlier.
asmlinkage tells the compiler to extract only the function's arguments from the stack. All system calls require this qualifier! This is similar to the macro definition mentioned in our previous article quagga.

That is, the following code in the macro definition:

struct file *file;
    ssize_t ret = -EBADF;
    int fput_needed;

    file = fget_light(fd, &fput_needed);
    if (file) {
        loff_t pos = file_pos_read(file);
        ret = vfs_read(file, buf, count, &pos);
        file_pos_write(file, pos);
        fput_light(file, fput_needed);
    }

    return ret;

Code analysis:

  • fget_light(): According to the index specified by fd, retrieve the corresponding file object from the current process descriptor (see Figure 3).
  • If the specified file object is not found, an error
  • is returned.
  • If the specified file object is found:
  • Call the file_pos_read() function to get the current position of the file read and written this time.
  • Call vfs_read() to perform a file reading operation, and this function ultimately calls the function pointed to by file->f_op.read(). The code is as follows:

if (file->f_op->read)
ret = file->f_op->read(file, buf, count, pos);

  • Call file_pos_write() to update the current read and write position of the file.
  • Call fput_light() to update the file's reference count.
  • Finally, the number of bytes of read data is returned.

At this point, the processing of the virtual file system layer is completed, and control is handed over to the ext2 file system layer.

The above is the detailed content of Syscall system call Linux kernel tracing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
Explain the architectural differences between Linux and Windows.Explain the architectural differences between Linux and Windows.May 06, 2025 am 12:01 AM

The main differences in architecture between Linux and Windows include: 1) Design philosophy and kernel structure: Linux uses a modular kernel, Windows uses a single kernel; 2) File system: Linux supports multiple file systems, Windows mainly uses NTFS; 3) Security: Linux is known for its permission management and open source features. Windows has a unique security mechanism but lags in repair; 4) Usage experience: Linux command line operation is more efficient, and Windows graphical interface is more intuitive.

What are some common security threats targeting Linux versus Windows?What are some common security threats targeting Linux versus Windows?May 05, 2025 am 12:03 AM

Linux and Windows systems face different security threats. Common Linux threats include Rootkit, DDoS attacks, exploits, and permission escalation; common Windows threats include malware, ransomware, phishing attacks, and zero-day attacks.

How does process management differ between Linux and Windows?How does process management differ between Linux and Windows?May 04, 2025 am 12:04 AM

The main difference between Linux and Windows in process management lies in the implementation and concept of tools and APIs. Linux is known for its flexibility and power, relying on kernel and command line tools; while Windows is known for its user-friendliness and integration, mainly managing processes through graphical interfaces and system services.

What are the typical use cases for Linux versus Windows?What are the typical use cases for Linux versus Windows?May 03, 2025 am 12:01 AM

Linuxisidealforcustomization,development,andservermanagement,whileWindowsexcelsineaseofuse,softwarecompatibility,andgaming.Linuxoffershighconfigurabilityfordevelopersandserversetups,whereasWindowsprovidesauser-friendlyinterfaceandbroadsoftwaresupport

What are the differences in user account management between Linux and Windows?What are the differences in user account management between Linux and Windows?May 02, 2025 am 12:02 AM

The main difference between Linux and Windows in user account management is the permission model and management tools. Linux uses Unix-based permissions models and command-line tools (such as useradd, usermod, userdel), while Windows uses its own security model and graphical user interface (GUI) management tools.

How does the command line environment of Linux make it more/less secure than Windows?How does the command line environment of Linux make it more/less secure than Windows?May 01, 2025 am 12:03 AM

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

How to Make a USB Drive Mount Automatically in LinuxHow to Make a USB Drive Mount Automatically in LinuxApr 30, 2025 am 10:04 AM

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Apr 30, 2025 am 09:57 AM

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.