Home > Net >  Using gdb to print a time_t variable
Using gdb to print a time_t variable

Time:10-09

I want to print some information from gdb but don't see how. I am used to p/s p/x formats. But don't know what to do in the case below.

#include<iostream>
#include<climits>
#include <stdio.h>
#include <time.h>
#include <stdint.h>

using namespace std;
int main()
{
        time_t dataFrom = 1234560;
        cout << "dataFrom = " << asctime(gmtime(&dataFrom)) << "\n";

        return 0;
}

How do i print dataFrom using gdb correctly ? I have read about informing gdb about a structure if it was user defined. But what to do with something inbuilt such as time_t ?

badri@badri-All-Series:~/progs$ g   --std=c  11 testgdb.cpp -g
badri@badri-All-Series:~/progs$ gdb ./a.out 
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3 : GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) b main
Breakpoint 1 at 0x903: file testgdb.cpp, line 9.
(gdb) r
Starting program: /home/badri/progs/a.out 

Breakpoint 1, main () at testgdb.cpp:9
9   {
(gdb) n
10      time_t dataFrom = 1234560;
(gdb) p dataFrom
$1 = 93824992233952
(gdb) p *dataFrom
$2 = 1447122753
(gdb) p/s dataFrom
$3 = 93824992233952

CodePudding user response:

Yes, as mentioned by n. 1.8e9-where's-my-share m, the execution is stopped at the line 9 and the line 10 is yet to be executed. So, a simple next or n command in gdb will run the line 10 and then if you print the value in dataFrom you can observe the proper value.

GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
...
Reading symbols from ./a.out...
(gdb) b main
Breakpoint 1 at 0x11e9: file testgdb.cpp, line 9.
(gdb) r
Starting program: /home/user/path/a.out

Breakpoint 1, main () at testgdb.cpp:9
9       {
(gdb) n
10              time_t dataFrom = 1234560;
(gdb) p dataFrom
$1 = 0
(gdb) n
11              cout << "dataFrom = " << asctime(gmtime(&dataFrom)) << "\n";
(gdb) p dataFrom
$2 = 1234560
(gdb)
  • Related