Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

A quick way to translate physical addresses into virtual ones

June 13, 2022
Noah Sanci
Related topics:
Linux
Related products:
Red Hat Enterprise Linux

Share:

    Recently, I have been working on enabling cooperation between SystemTap (a kernel profiling tool) and gprof (a tool that makes graphs from program profiles). This exercise has given me insight into meaningful topics only briefly touched upon at my university, such as kernel space, user space, and virtual memory. But these concepts are fundamental to the proper and safe execution of programs on any modern operating system. The trade off is some address translation when viewing memory from kernel space versus user space. In this article, you'll see how that translation can be handled.

    A quick dive into user and kernel address spaces

    User space and kernel space are kept separate in all modern OSes. This separation is an essential layer of security between information essential to the operation of the machine and the applications on that machine.

    As the term suggests, user process address space is the area where user-run processes are stored and store information. Text pages are a portion of the process address space that stores the program’s code.

    Consider a simple calculator program, of the sort installed on most OSes. If you ran this calculator program with the expression 5+5 and a web browser at the same time without any preventative measures, the browser might be able to access the mathematical expression located in the calculator’s address space and change it to a different expression, such as 6+6. Common sense dictates that this should not occur except under very unique circumstances. The operating system provides the program separation that the user desires by giving each process its own isolated address space.

    The kernel address space is the realm of the kernel code and information that may be useful to other programs, such as user input/output and network data. Kernel space information is generally inaccessible except through functions known as system calls.

    How virtual memory works

    Virtual memory is a simple convenience in the form of an abstraction. Without virtual memory, applications would need to manage their physical memory space, coordinating with every other process running on the computer. Virtual memory leaves that management to the kernel by creating maps that allow translation between virtual and physical memory. Applications use virtual memory, and the kernel’s memory management unit (MMU) uses physical memory. The MMU is the mapping hardware that translates virtual memory addresses to physical memory addresses.

    To understand how virtual memory works, suppose there are two running programs: a calculator with the expression 5+5 and an internet browser. The calculator may have the expression 5+5 stored in the address 0xdeadbeef, and the browser could store any of its data, such as an IP address, in 0xdeadbeef as well. Why doesn't one piece of information overwrite the other? The addresses where the programs are storing their data are located in virtual memory, and they refer to different locations in physical memory. Although the virtual addresses are the same, the OS handles the physical addresses, which are completely different.

    The OS may also take a page from a process and write it to disk to make space for another process page, resulting in two operations at the same physical address.

    SystemTap and gprof cooperation

    I have been working on leveraging the data collected by SystemTap and outputting it into a gprof-readable format. SystemTap profiles programs that use shared libraries. The problem is that SystemTap can load those libraries anywhere in virtual memory. It must relativize their addresses and process them later.

    umodaddr(), shown in the listing below, was created for the purpose of this translation:

    function umodaddr:long (address:long)
    %{ /* pragma:vma */
     long vm_start = -1;
      stap_find_vma_map_info(current, STAP_ARG_address,
                             &vm_start, NULL, NULL, NULL,NULL);
      if(vm_start == -1)
        STAP_ERROR("umodaddr 0x%llx unknown\n ", STAP_ARG_address);
      STAP_RETURN(STAP_ARG_address - vm_start);
    %}
    
    

    This function takes the virtual address of the called function, found in STAP_ARG_address, and returns its offset from the beginning of the virtual memory. This function becomes a tool to ensure that SystemTap outputs addresses relative to the beginning of their virtual memory, enabling compatibility with gprof.

    Conclusion

    With this translation complete, a SystemTap script can use these translated addresses to create a file that can be interpreted by gprof. This file will be of the gmon.out format, so it will internally contain a histogram. A script can then automatically invoke gprof on all of these files, giving us the ability to leverage gprof's functionality to inspect a process's activity.

    Understanding how virtual memory works made it possible to combine the extremely low-level capabilities of SystemTap with the informative functionality of gprof. This will allow users to gain insight into where precisely a program spends time and contribute to solving time-sensitive issues.

    Last updated: June 15, 2022

    Recent Posts

    • How to change the meaning of python and python3 on RHEL

    • vLLM or llama.cpp: Choosing the right LLM inference engine for your use case

    • How to implement and monitor circuit breakers in OpenShift Service Mesh 3

    • Analysis of OpenShift node-system-admin-client lifespan

    • What's New in OpenShift GitOps 1.18

    What’s up next?

    Linux offers a multitude of commands, which can be overwhelming. The Linux Commands cheat sheet provides Linux commands commonly needed by developers, with explanations and screenshots.

    This cheat sheet includes a range of basic to advanced commands, screenshots of command output, and tips from cat to tar -xf somefile.tar.gz.

    Get the free cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue