Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [C++] Nome della funzione

    Ciao
    c'e' un modo per sapere, runtime, il nome della funzione che si sta attualmente eseguendo?

    grazie
    ciao
    #exclude <windows.h>
    Powered by Gentoo GNU/Linux
    Cos'e' l'impossibile se non una questione di tempo?
    Riesumare i post vecchi non e' sbagliato, e' una risposta o la continuazione di un thread. Meglio riesumare che creare di nuovi :]

  2. #2
    Utente di HTML.it L'avatar di Zalex
    Registrato dal
    Aug 2001
    Messaggi
    357
    non credo.....ma acosa ti serve ?:master:

  3. #3
    mi sarebbe piaciuto fare una funzione per il debug, che quando viene chiamata dave delle informazioni, tra cui il nome della funzione da cui e' chiamata.

    Giusto per chiarire un po le cose.

    vabe, grazie lo stesso
    ciao
    #exclude <windows.h>
    Powered by Gentoo GNU/Linux
    Cos'e' l'impossibile se non una questione di tempo?
    Riesumare i post vecchi non e' sbagliato, e' una risposta o la continuazione di un thread. Meglio riesumare che creare di nuovi :]

  4. #4
    Fosse cosi semplice fare un debugger... prima di tutto devi scriverti una disassembly engine che, leggendo il binario, 'tenta di indovinare' ( e tanto più buona sarà tanto meglio lo farà) quali sono le istruzioni del programma. Comunque in Unix per le funzioni se conosci bene il formato ELF e' una passaggiata scrivere un piccolo tool che legga un binario e ne mappi tutti i simboli (nomi delle funzioni incluso).

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

  5. #5
    ma o non voglio fare un debugger!
    ce ne sono gia tanti e di molto belli...
    io voglio fare un funzione, hc erichiamata durante un programma, viene utilizzata per il debug.

    Sai, come le funzioni dump usare in ASM, che scrivono lo stack, e cose varie. Solo che io ci scrivo cose relative al C++, certamente (o per ora) non lo stack e i registri ^_^

    Comunque era una curiosita'

    Non ci sono macro che indicano il nome vero?

    Vabe
    ciao
    #exclude <windows.h>
    Powered by Gentoo GNU/Linux
    Cos'e' l'impossibile se non una questione di tempo?
    Riesumare i post vecchi non e' sbagliato, e' una risposta o la continuazione di un thread. Meglio riesumare che creare di nuovi :]

  6. #6
    non c'è ma è facilmente creabile. na cosa tipo:

    codice:
    /* inizio code */
    
    #define NOMEFUNZ
    #define NUMLINE
    #define DESCRIPTION
    
    #define SETFLAGS(FUNZ, LINE, DES)      \
            {                              \
                #undef NOMEFUNZ            \
                #undef NUMLINE             \
                #undef DESCRIPTION         \
                #define NOMEFUNZ (FUNZ)    \
                #define NUMLINE  (LINE)    \
                #define DESCRIPTION (DES)  \
             }
    
    
    int funzione(void);
    
    int funzione()
    {
       SETFLAGS("funzione", 123, "shittyfunction");
       /* .... */
    }
    Altro da aggiungere?

    Questo è codice C in C++ si potrebbe (e si dovrebbe) fare diversamente. Cmq sarebbe più semplice lavorare sullo stack che sul text del programma...

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

  7. #7
    Eccoti un code completo. Questo forse può esserti d'aiuto:

    codice:
    debug.h
    
    #ifndef _MY_DEBUG_
    #define _MY_DEBUG_
    
    #define   FUNNAME
    #define   NUMLINE
    #define   NUMARGS
    #define   RETTYPE
    
    
    #define   SETVAL(NAME, LINE, ARGS, TYPE) \
              {                              \
                                             \
                   #undef FUNNAME            \
                   #undef NUMLINE            \
                   #undef NUMARGS            \
                   #undef RETTYPE            \
                                             \
                   #define FUNNAME (NAME)    \
                   #define NUMLINE (LINE)    \
                   #define NUMARGS (ARGS)    \
                   #define RETTYPE (TYPE)    \
                                             \
               }
    
    
    #define    PRINTDEBUG()                             \
               {                                        \
                                                        \
                   printf("function name: %s\n"         \
                          "number of lines: %lu\n"      \
                          "number of arguments: %lu\n"  \
                          "type of return: %s\n",       \
                          (FUNNAME),                    \
                          (NUMLINE),                    \ 
                          (NUMARGS),                    \
                          (RETTYPE));                   \
                                                        \
                }
    
    #endif
    Come si usa lo capisce anche mio nonno.

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

  8. #8
    Questa versione per C non mi piace molto. Meglio questa per C++:

    codice:
    #include <iostream>
    
    #ifdef __GNUC__
    
    extern "C"
    {
    
    	#include <sys/types.h>
    }
    
    #else
    
    typedef unsigned long u_long;
    #define __attribute__(x)
    
    #endif
    
    
    class Debugging
    {
    
    	public:
    
    	Debugging(const char*, const u_long, const u_long, const char*) throw();
    	~Debugging(void) throw();
    
    	void debug(void) throw() __attribute__((noreturn, weak));
    
    	private:
    
    	// evita la copia
    
    	Debugging(const Debugging&);
    	Debugging operator=(Debugging);
    
    	char *name;
    	u_long numline;
    	u_long numargs;
    	char *valret;
    
    };
    
    
    Debugging::Debugging(const char *n, const u_long nu, const u_long ar, const char*val) throw() :
    numline(nu),	numargs(ar)
    {
    	u_long	namecounter(0), valcounter(0);
    
    	while (n[namecounter++]);
    
    	while (val[valcounter++]);
    
    	name = new char[namecounter + 1];
    
    	valret = new char[valcounter + 1];
    
    	for (int i(0); i != namecounter; ++i)
    		name[i] = n[i];
    
    	for (int i(0); i != valcounter; ++i)
    		valret[i] = val[i];
    
    	name[namecounter] = '\0';
    	valret[valcounter] = '\0';
    
    }
    
    
    Debugging::~Debugging() throw()
    {
    
    	delete[] name;
    	delete[] valret;
    
    }
    
    
    void Debugging::debug() throw()
    {
    
    	std::cout << "nome della funzione:\t" << name << std::endl;
    	std::cout << "numero di argomenti:\t" << numline << std::endl;
    	std::cout << "numero di linee:\t" << numargs << std::endl;
    	std::cout << "valore di ritorno:\t" << valret << std::endl;
    
    	_exit(1);
    
    }
    Come avrai ben capito gli stub del preprocessore servono per farla funzionare bene anche su altri ambienti fuori da gcc.

    si usa cosi:

    codice:
    int main()
    {
    
    	Debugging var("main", 7, 0, "int");
    	int a = 10;
    
    	// ammesso che a == 10 indica un errore
    	if (a == 10)
    		var.debug();
    
    }

    Ok?

    bye
    There are 10 kinds of people in the world: who knows the binary numeration and who not

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.