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...

Usb reset in Ubuntu (chatGpt discussions)

Query:
a usb not working in my ubuntu laptop for which wired mouse not working in that drive. Any way to reset that usb without restarting ubuntu
ChatGPT said:

Yes, in Ubuntu you can reset a specific USB port or device without rebooting the entire system using a few different methods. Here's how you can do it safely and quickly.


✅ Step-by-Step USB Port Reset (No Reboot)

Option 1: Using usbreset utility

This is the cleanest way to reset a USB device without unplugging.

  1. Install usbreset (if not available):

    bash
    sudo apt install usbutils build-essential wget https://raw.githubusercontent.com/jkulesza/usbreset/master/usbreset.c gcc usbreset.c -o usbreset
  2. Find your USB device (e.g., mouse):

    bash
    lsusb

    Example output:

    arduino
    Bus 001 Device 005: ID 046d:c077 Logitech, Inc. M105 Optical Mouse
  3. Get the device file:

    bash
    ls -l /dev/bus/usb/001/005
  4. Run usbreset:

    bash
    sudo ./usbreset /dev/bus/usb/001/005

Option 2: Unbind/Bind USB driver manually

You can unbind and rebind the USB device manually.

  1. Get the device path:

    bash
    lsusb
  2. Find the device ID in /sys/bus/usb/devices/:

    bash
    ls /sys/bus/usb/devices/

    Look for something like 1-1.2 (USB port path). To confirm:

    bash
    cat /sys/bus/usb/devices/1-1.2/product
  3. Reset the device:

    bash
    echo '1-1.2' | sudo tee /sys/bus/usb/drivers/usb/unbind sleep 1 echo '1-1.2' | sudo tee /sys/bus/usb/drivers/usb/bind

Option 3: Use uhubctl to cycle USB port power (if hardware supports)

For laptops with USB hubs supporting power control (like some Intel chipsets):

  1. Install uhubctl:

    bash
    sudo apt install uhubctl
  2. List USB hubs:

    bash
    uhubctl
  3. Turn power off then on (example):

    bash
    sudo uhubctl -l 1-1 -p 2 -a 0 # off sleep 1 sudo uhubctl -l 1-1 -p 2 -a 1 # on

    -l is the hub location, -p is the port number.


💡 Tips

  • If the device disappears completely from lsusb, then a hard unplug or restart might still be needed.

  • These methods don't affect your full system, just the specific USB device/port.

  • Always check dmesg or journalctl -xe for logs after resetting to confirm success.

Let me know if you want a script to automate this process.

You said:
lsusb Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 006: ID 0c45:6730 Microdia Integrated_Webcam_HD Bus 001 Device 007: ID 0bda:c829 Realtek Semiconductor Corp. Bluetooth Radio Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
ChatGPT said:

Thanks. Based on your lsusb output, it seems your wired mouse is not being detected at all, since there's no device listed corresponding to it (Logitech, HP, Dell, etc.).

This typically means:

  • The USB port is dead or stuck

  • The device has failed

  • Or it’s a power issue


✅ Try to Reset All USB Ports via Sysfs

Since the mouse doesn’t show up in lsusb, let’s try resetting the USB controller. This works without rebooting.

⚠️ Identify USB controllers:

bash
ls /sys/bus/pci/drivers/xhci_hcd

You should see something like:

bash
0000:00:14.0 bind new_id remove_id uevent unbind

The part 0000:00:14.0 is your USB controller ID.


🔁 Reset the USB controller:

bash
echo -n '0000:00:14.0' | sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind sleep 2 echo -n '0000:00:14.0' | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind

⚠️ This will disconnect all USB devices temporarily, including webcam/Bluetooth/mouse if connected.


✅ Alternative: Check dmesg when plugging mouse

Try this after plugging in your wired mouse:

bash
dmesg | tail -n 30

If you see nothing related to USB device detection, the port or the mouse might be faulty or underpowered.


🔧 Additional Suggestions

  • Try another USB port

  • Try the mouse in another system to rule out hardware issue

  • Use a powered USB hub if your laptop’s USB is undervolting


Let me know if you want a script to auto-reset all USB controllers, or if you're using a specific mouse brand — I can help match its device ID too.

You said:
I checked in that usb drive another wireless mouse is working well, means usb drive not faulty But that new wired mouse which was working well till last day has stopped working in that usb drive from today since I started my ubuntu laptop. But same usb wired mouse working in other usb port
ChatGPT said:

Thanks for the clarification — this is a classic case of a per-port USB glitch, not hardware failure.

🔍 Summary:

  • USB port is not faulty (wireless mouse works).

  • Wired mouse is not faulty (works on other port).

  • Problem: Specific wired mouse not detected on one port only, after Ubuntu boot.


✅ Solution: Reset Just That USB Port (Without Reboot)

🔧 Step 1: Identify USB port path of the non-working port

  1. Plug the wired mouse into the port where it’s not working.

  2. Run:

    bash
    journalctl -k -n 30

    or:

    bash
    dmesg | tail -n 30

    See if any message is logged like:

    cpp
    usb 1-1.3: new low-speed USB device number 8 using xhci_hcd

    Here, 1-1.3 is the USB port path.


🔧 Step 2: Reset that port via unbind/rebind

Assuming the path was 1-1.3, do:

bash
echo '1-1.3' | sudo tee /sys/bus/usb/drivers/usb/unbind sleep 1 echo '1-1.3' | sudo tee /sys/bus/usb/drivers/usb/bind

Replace 1-1.3 with the actual USB path from dmesg.


🧪 Optional: Reset via controller (less preferred, affects all ports)

You can also reset the whole controller as mentioned before:

bash
echo -n '0000:00:14.0' | sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind sleep 2 echo -n '0000:00:14.0' | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind

You can find the controller ID with:

bash
lspci | grep -i usb

🧼 Optional One-liner to list USB device-tree

bash
lsusb -t

This helps you visually see which device is connected to which port.


💡 Why this happens?

Some wired mice get stuck due to USB power negotiation glitches or static charge. Ubuntu may not fully reset the USB port on reboot (especially if fast boot is enabled in BIOS).


Let me know if you want a script that auto-detects and resets the stuck port dynamically.

You said:
does above chat contains anything that should not be copy-pased in blogger eg something that can be used to hack my ubuntu/laptop (any such hardware code I provided u from my end eg output of lsusb command?
2/2
ChatGPT said:

Great question — and you're right to be cautious.

✅ Short Answer:

Yes, you can safely copy-paste everything above into a blog post.
None of the commands or outputs shared here pose any security risk to your system or personal data.


🔍 Details:

1. Your lsusb output

yaml
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 006: ID 0c45:6730 Microdia Integrated_Webcam_HD Bus 001 Device 007: ID 0bda:c829 Realtek Semiconductor Corp. Bluetooth Radio Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  • This is generic device info (no serial numbers or hardware IDs).

  • IDs like 0c45:6730 are vendor:product IDs, which are public and used across many systems.

  • No private or sensitive identifiers are shown here.

2. Commands like usbreset, unbind/bind, or dmesg

  • These are admin tools or diagnostics, not exploits.

  • Nothing here can be used to hack or take over your system.

  • They only affect your local USB port/hardware when you run them as root.

3. Hardware controller IDs like 0000:00:14.0

  • These are local PCI device addresses.

  • They’re not unique or traceable, and they differ across machines.

  • Safe to share in public.


🚫 What Would Be Unsafe (Just FYI)?

  • Full output of dmesg or journalctl containing serial numbers or mount points.

  • Anything with private file paths, encryption keys, Wi-Fi MACs, or IPs.

  • Output from lsblk, ip a, or /etc/ files with config values.


✅ Good Practice for Blogging:

You can include this kind of info and say:

"Note: Output like lsusb and PCI IDs are safe to share publicly. Just avoid sharing private mount paths or serial numbers."

Let me know if you want me to format this into a ready-to-publish blog-style note.

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!