Bug 9065 - can't use typeid
Summary: can't use typeid
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.1
: P3 enhancement
Target Milestone: 7.7
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-06-17 21:28 UTC by grover
Modified: 2013-05-24 14:36 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description grover 2005-06-17 21:28:01 UTC
[Converted from Gnats 1960]

When I try expressions like 

print typeid(*this).name()

I get the error
No symbol "typeid" in current context.

AFAIK, typeid is a C++ operator, not a local or global symbol.  It would be very helpful if it were supported.

Release:
GNU gdb Red Hat Linux (6.1post-1.20040607.43rh)
Comment 1 grover 2005-06-20 11:36:25 UTC
From: "Grover, Alan" <grover@kuchera.com>
To: <gdb-gnats@sources.redhat.com>
Cc:  
Subject: Re: c++/1960: can't use typeid
Date: Mon, 20 Jun 2005 07:36:25 -0400

 The problem came about trying to debug a statement of the form:
 
 printf("Class id is %s\n", typeid(*this));
 
 which works on most classes.=A0 One one class the printf seems to =
 complete (ie, the output appears on the console), but the process gets a =
 SIGSEGV and terminates at this point.=A0 Any ideas for debugging a =
 problem like that would be appreciated.
 
 Alan
Comment 2 Tom Tromey 2012-02-23 21:54:43 UTC
I think this can easily be done for classes with vtables.
For other types it does not appear to be easy; it may not
even be possible.
Comment 3 Tom Tromey 2013-02-22 19:35:49 UTC
(In reply to comment #2)
> I think this can easily be done for classes with vtables.
> For other types it does not appear to be easy; it may not
> even be possible.

We can be optimistic and try to find the symbol.
For a type named T, we can just look up the symbol "typeinfo for T".
E.g., from libstdc++

[...]
0000003be82eec80 V typeinfo for signed char
0000003be82eee60 V typeinfo for bool
0000003be82eece0 V typeinfo for char
0000003be82ee7a0 V typeinfo for double
[...]
Comment 4 Tom Tromey 2013-02-25 16:46:03 UTC
I have an initial patch for this.
Comment 5 Sourceware Commits 2013-04-15 17:36:15 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	tromey@sourceware.org	2013-04-15 17:36:14

Modified files:
	gdb            : ChangeLog NEWS breakpoint.c c-exp.y cp-abi.c 
	                 cp-abi.h eval.c expprint.c gnu-v3-abi.c parse.c 
	                 std-operator.def 
	gdb/testsuite  : ChangeLog 
Added files:
	gdb/testsuite/gdb.cp: typeid.cc typeid.exp 

Log message:
	PR c++/9065:
	* NEWS: Update.
	* breakpoint.c (watchpoint_exp_is_const): Add OP_TYPEID.
	* c-exp.y (TYPEID): New token.
	(exp): Add new TYPEID productions.
	(ident_tokens): Add "typeid".
	* cp-abi.c (cplus_typeid, cplus_typeid_type): New functions.
	* cp-abi.h (cplus_typeid, cplus_typeid_type): Declare.
	(struct cp_abi_ops) <get_typeid, get_typeid_type>: New fields.
	* eval.c (evaluate_subexp_standard) <OP_TYPEID>: New case.
	* expprint.c (dump_subexp_body_standard) <OP_TYPEID>: New
	case.
	* gnu-v3-abi.c (std_type_info_gdbarch_data): New global.
	(build_std_type_info_type, gnuv3_get_typeid_type)
	(gnuv3_get_typeid): New functions.
	(init_gnuv3_ops): Initialize std_type_info_gdbarch_data.  Set
	new fields on ABI object.
	* parse.c (operator_length_standard) <OP_TYPEID>: New case.
	* std-operator.def (OP_TYPEID): New.
	gdb/testsuite
	* gdb.cp/typeid.cc: New file.
	* gdb.cp/typeid.exp: New file.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.15412&r2=1.15413
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/NEWS.diff?cvsroot=src&r1=1.588&r2=1.589
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/breakpoint.c.diff?cvsroot=src&r1=1.754&r2=1.755
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/c-exp.y.diff?cvsroot=src&r1=1.116&r2=1.117
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/cp-abi.c.diff?cvsroot=src&r1=1.40&r2=1.41
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/cp-abi.h.diff?cvsroot=src&r1=1.25&r2=1.26
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/eval.c.diff?cvsroot=src&r1=1.181&r2=1.182
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/expprint.c.diff?cvsroot=src&r1=1.67&r2=1.68
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/gnu-v3-abi.c.diff?cvsroot=src&r1=1.82&r2=1.83
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/parse.c.diff?cvsroot=src&r1=1.139&r2=1.140
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/std-operator.def.diff?cvsroot=src&r1=1.10&r2=1.11
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.3624&r2=1.3625
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.cp/typeid.cc.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.cp/typeid.exp.diff?cvsroot=src&r1=NONE&r2=1.1
Comment 6 Tom Tromey 2013-04-15 18:15:53 UTC
Fix checked in.