Skip to main content

How to use gdb in Ubuntu Terminal

Here's a practical, minimal guide for using GDB in Ubuntu terminal — from launching to setting breakpoints and navigating your code. 🧰 Step 1: Compile with Debug Symbols Use -g and disable optimizations ( -O0 preferred for debugging): bash Copy Edit g++ -g -O0 -o myprog myprog.cpp 🚀 Step 2: Launch GDB bash Copy Edit gdb ./myprog You'll see a (gdb) prompt. 🎯 Step 3: Set Breakpoints You can set breakpoints in three main ways : ✅ a. At a function gdb Copy Edit (gdb) break main ✅ b. At a specific file and line gdb Copy Edit (gdb) break myprog.cpp:42 ✅ c. At a method in a class gdb Copy Edit (gdb) break MyClass::doSomething If the symbol is mangled (C++), GDB usually demangles automatically, especially if compiled with -g . ▶️ Step 4: Run the Program gdb Copy Edit (gdb) run 🧭 Step 5: Navigate Command Meaning next or n Step over current line step or s Step into function call finish Run until current function returns continue or c Continue until...

Built in OS system library in Unix like system for C/C++ programming : chatGpt Ans

Yes, there are several other functionalities that are built into the operating system (OS) or provided by system libraries, which can be used in C/C++ programs without relying on higher-level libraries like Boost or even the C++ Standard Library. Here are a few key examples:

1. File I/O (POSIX API)

  • System Functions: Instead of using C++'s <fstream> or C's <stdio.h> for file operations, you can directly use system calls like open, read, write, close, lseek, etc., which are part of the POSIX API.
  • Advantages: These system calls provide fine-grained control over file operations and are often more efficient for certain tasks, especially in systems programming.
c
#include <fcntl.h> #include <unistd.h> int fd = open("file.txt", O_RDONLY); if (fd != -1) { char buffer[100]; ssize_t bytesRead = read(fd, buffer, sizeof(buffer)); // Process the buffer close(fd); }

2. Memory Management (POSIX API)

  • System Functions: Instead of using C++'s new/delete or C's malloc/free, you can directly use mmap, munmap, brk, and sbrk for memory management.
  • Advantages: These allow you to allocate and manage memory with more control, such as mapping files directly into memory or controlling the growth of the heap.
c
#include <sys/mman.h> void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (memory != MAP_FAILED) { // Use memory munmap(memory, size); }

3. Networking (POSIX Sockets)

  • System Functions: Instead of using C++ networking libraries or higher-level abstractions, you can use POSIX socket APIs like socket, bind, listen, accept, connect, recv, and send.
  • Advantages: These are lower-level functions that give you full control over network communication, and are widely used in network programming.
c
#include <sys/socket.h> #include <netinet/in.h> int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd != -1) { struct sockaddr_in server_addr = {0}; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = INADDR_ANY; bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); listen(sockfd, 5); int clientfd = accept(sockfd, NULL, NULL); if (clientfd != -1) { char buffer[256]; recv(clientfd, buffer, sizeof(buffer), 0); // Process data close(clientfd); } close(sockfd); }

4. Time Management (POSIX API)

  • System Functions: Instead of using C++ <chrono> or C’s <time.h>, you can use clock_gettime, gettimeofday, nanosleep, and sleep for time management.
  • Advantages: These provide high-resolution timing and are crucial in real-time systems.
c
#include <time.h> struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); printf("Seconds: %ld, Nanoseconds: %ld\n", ts.tv_sec, ts.tv_nsec);

5. Inter-Process Communication (IPC)

  • System Functions: Instead of using higher-level libraries, you can use built-in OS mechanisms like pipes, message queues, semaphores, and shared memory for IPC.
  • Advantages: These are fundamental for process communication in systems programming, allowing for direct and efficient data exchange.
c
#include <sys/ipc.h> #include <sys/shm.h> int shmid = shmget(IPC_PRIVATE, 1024, IPC_CREAT | 0666); char* shared_memory = (char*)shmat(shmid, NULL, 0); if (shared_memory != (char*)-1) { // Use shared memory shmdt(shared_memory); }

6. Threading and Synchronization (POSIX Threads)

  • System Functions: Instead of using std::thread and C++ synchronization primitives, you can use POSIX threads (pthread_create, pthread_join, pthread_mutex_lock, etc.) and synchronization mechanisms (pthread_mutex, pthread_cond, etc.).
  • Advantages: POSIX threads (pthreads) are well-supported across Unix-like systems and offer a portable and efficient way to implement multithreading.
c
#include <pthread.h> void* thread_function(void* arg) { // Do some work return NULL; } pthread_t thread; pthread_create(&thread, NULL, thread_function, NULL); pthread_join(thread, NULL);

7. Process Management

  • System Functions: Instead of relying on high-level libraries for process management, you can use fork, exec, wait, waitpid, and kill to manage processes.
  • Advantages: These are essential for creating and managing processes, especially in Unix-based systems.
c
#include <unistd.h> pid_t pid = fork(); if (pid == 0) { // Child process execl("/bin/ls", "ls", NULL); } else if (pid > 0) { // Parent process wait(NULL); }

Conclusion

Using built-in OS system functions can lead to more efficient and lower-level control over various aspects of a program. These functions are typically faster and offer more flexibility than higher-level abstractions. However, they may require more detailed knowledge of the underlying system and more careful resource management.

Choosing between system-level functions and higher-level libraries like Boost or the C++ Standard Library depends on the specific needs of your project. If you need portability, ease of use, and safety, higher-level libraries may be more appropriate. If you need maximum performance, control, and efficiency, system functions might be the better choice.

Comments

Popular posts from this blog

Ubuntu Notes: Set up autossh as systemd useful-reminder commands

 sudo vim /etc/systemd/system/permanentAutossh.service ======================================== [Unit] Description=Keeps a tunnel to myOciFreeNat open After=network-online.target [Service] User=<username> Environment="AUTOSSH_GATETIME=0" ExecStart=/usr/bin/autossh -M 0 -q -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -L 4322:127.0.0.1:5432 -i /<ssh_private_key_file_absolute_path> <username>@<ip_address> -p 22 [Install] WantedBy=multi-user.target ======================================== sudo systemctl daemon-reload sudo systemctl enable permanentAutossh sudo systemctl start permanentAutossh ================== sudo systemctl daemon-reload sudo systemctl restart permanentAutossh sudo systemctl status permanentAutossh ====================== sudo systemctl stop permanentAutossh sudo systemctl disable permanentAutossh sudo rm /etc/systemd/system/permanentAutossh.service

syslog cpp programming: learning using chatGcp

  Introduction to Syslog C++ August 27, 2024 This conversation may reflect the link creator’s personalized data, which isn’t shared and can meaningfully change how the model responds. You said: I am new to syslog in cpp programming n just starting. Can you teach me ChatGPT said: ChatGPT Sure! syslog is a standard for logging system messages in Unix-like operating systems. It provides a way to send log messages to the system log, which can then be collected and analyzed by various tools. Here's a basic guide to get you started with syslog in C++: 1. Include the Necessary Header To use syslog , you need to include the syslog.h header: cpp Copy code # include <syslog.h> 2. Open the Connection to syslog Before sending log messages, you need to open a connection to the syslog system. This is typically done at the start of your program: cpp Copy code openlog ( "your_program_name" , LOG_PID | LOG_CONS, LOG_USER); "your_program_name" : A string representing ...

Ubuntu - C++ Notes: Install & update boost c++ library from git (may contain some error in process mentioned below)

 Boost C++ Library Installation from git in ubuntu: 1. create a directory named boost in your desired location and go to that directory mkdir boost;cd boost; 2. clone boost recursively from git using command git clone --recursive https://github.com/boostorg/boost.git 3. cd boost; sudo ./bootstrap.sh; 4. sudo ./b2; 5. sudo ./b2 install; DONE! --------------------------- Boost C++ Library Update Verson from git in Ubuntu: 1. go to gitDownloads/boost directory cd gitDownloads/boost; 2. git pull; 3. git submodule update --recursive; 4. cd boost; sudo ./bootstrap.sh; 5. sudo ./b2; 6. sudo ./b2 install; DONE!