How to printf a std::string_view in gdb?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Just trying to debug something, and:



(gdb) 
Thread 1 "SciTE" hit Breakpoint 2, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
key="LS_COLORS",
val="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.a"...) at ./../src/PropSetFile.cxx:91
91 void PropSetFile::Set(std::string_view key, std::string_view val) {

(gdb) p key
$2 = "LS_COLORS"
(gdb) ptype key
type = std::string_view


Ok, so if I just say p key, then I get its contents printed.



But I want to do a dprintf, meaning a printf:



(gdb) printf "'%s'n", key
'Value can't be converted to integer.
(gdb) printf "'%s'n", key.c_str()
Can't take address of "key" which isn't an lvalue.
(gdb) printf "'%s'n", *(char **)key
Invalid cast.
(gdb) printf "'%s'n", (char *)key
Invalid cast.
(gdb) printf "'%s'n", std::string(key).c_str()
Cannot look up value of a typedef `std::__cxx11::string'.


So, how could I print this variable in a gdb printf/dprintf command?










share|improve this question





























    1















    Just trying to debug something, and:



    (gdb) 
    Thread 1 "SciTE" hit Breakpoint 2, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
    key="LS_COLORS",
    val="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.a"...) at ./../src/PropSetFile.cxx:91
    91 void PropSetFile::Set(std::string_view key, std::string_view val) {

    (gdb) p key
    $2 = "LS_COLORS"
    (gdb) ptype key
    type = std::string_view


    Ok, so if I just say p key, then I get its contents printed.



    But I want to do a dprintf, meaning a printf:



    (gdb) printf "'%s'n", key
    'Value can't be converted to integer.
    (gdb) printf "'%s'n", key.c_str()
    Can't take address of "key" which isn't an lvalue.
    (gdb) printf "'%s'n", *(char **)key
    Invalid cast.
    (gdb) printf "'%s'n", (char *)key
    Invalid cast.
    (gdb) printf "'%s'n", std::string(key).c_str()
    Cannot look up value of a typedef `std::__cxx11::string'.


    So, how could I print this variable in a gdb printf/dprintf command?










    share|improve this question

























      1












      1








      1








      Just trying to debug something, and:



      (gdb) 
      Thread 1 "SciTE" hit Breakpoint 2, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
      key="LS_COLORS",
      val="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.a"...) at ./../src/PropSetFile.cxx:91
      91 void PropSetFile::Set(std::string_view key, std::string_view val) {

      (gdb) p key
      $2 = "LS_COLORS"
      (gdb) ptype key
      type = std::string_view


      Ok, so if I just say p key, then I get its contents printed.



      But I want to do a dprintf, meaning a printf:



      (gdb) printf "'%s'n", key
      'Value can't be converted to integer.
      (gdb) printf "'%s'n", key.c_str()
      Can't take address of "key" which isn't an lvalue.
      (gdb) printf "'%s'n", *(char **)key
      Invalid cast.
      (gdb) printf "'%s'n", (char *)key
      Invalid cast.
      (gdb) printf "'%s'n", std::string(key).c_str()
      Cannot look up value of a typedef `std::__cxx11::string'.


      So, how could I print this variable in a gdb printf/dprintf command?










      share|improve this question














      Just trying to debug something, and:



      (gdb) 
      Thread 1 "SciTE" hit Breakpoint 2, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
      key="LS_COLORS",
      val="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.a"...) at ./../src/PropSetFile.cxx:91
      91 void PropSetFile::Set(std::string_view key, std::string_view val) {

      (gdb) p key
      $2 = "LS_COLORS"
      (gdb) ptype key
      type = std::string_view


      Ok, so if I just say p key, then I get its contents printed.



      But I want to do a dprintf, meaning a printf:



      (gdb) printf "'%s'n", key
      'Value can't be converted to integer.
      (gdb) printf "'%s'n", key.c_str()
      Can't take address of "key" which isn't an lvalue.
      (gdb) printf "'%s'n", *(char **)key
      Invalid cast.
      (gdb) printf "'%s'n", (char *)key
      Invalid cast.
      (gdb) printf "'%s'n", std::string(key).c_str()
      Cannot look up value of a typedef `std::__cxx11::string'.


      So, how could I print this variable in a gdb printf/dprintf command?







      debugging gdb printf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 4:31









      sdaausdaau

      20.4k28154207




      20.4k28154207
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Here is a workaround using gdb python:



          (gdb) break PropSetFile::Set
          Breakpoint 4 at 0x555555667700: file ./../src/PropSetFile.cxx, line 91.
          (gdb) commands
          Type commands for breakpoint(s) 4, one per line.
          End with a line saying just "end".
          >python print( "key {} val {}".format(gdb.parse_and_eval("key"), gdb.parse_and_eval("val")) )
          >c
          >end
          (gdb)


          ... but the output is way too verbose, which is why I want dprintf in the first place:



          (gdb) c
          Continuing.
          Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
          key="XDG_MENU_PREFIX", val="gnome-") at ./../src/PropSetFile.cxx:91
          91 void PropSetFile::Set(std::string_view key, std::string_view val) {
          key "XDG_MENU_PREFIX" val "gnome-"

          Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
          key="KIGITHUB", val="https://github.com/KiCad") at ./../src/PropSetFile.cxx:91
          91 void PropSetFile::Set(std::string_view key, std::string_view val) {
          key "KIGITHUB" val "https://github.com/KiCad"
          ...


          ... so I guess this question is still open...






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016379%2fhow-to-printf-a-stdstring-view-in-gdb%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Here is a workaround using gdb python:



            (gdb) break PropSetFile::Set
            Breakpoint 4 at 0x555555667700: file ./../src/PropSetFile.cxx, line 91.
            (gdb) commands
            Type commands for breakpoint(s) 4, one per line.
            End with a line saying just "end".
            >python print( "key {} val {}".format(gdb.parse_and_eval("key"), gdb.parse_and_eval("val")) )
            >c
            >end
            (gdb)


            ... but the output is way too verbose, which is why I want dprintf in the first place:



            (gdb) c
            Continuing.
            Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
            key="XDG_MENU_PREFIX", val="gnome-") at ./../src/PropSetFile.cxx:91
            91 void PropSetFile::Set(std::string_view key, std::string_view val) {
            key "XDG_MENU_PREFIX" val "gnome-"

            Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
            key="KIGITHUB", val="https://github.com/KiCad") at ./../src/PropSetFile.cxx:91
            91 void PropSetFile::Set(std::string_view key, std::string_view val) {
            key "KIGITHUB" val "https://github.com/KiCad"
            ...


            ... so I guess this question is still open...






            share|improve this answer




























              0














              Here is a workaround using gdb python:



              (gdb) break PropSetFile::Set
              Breakpoint 4 at 0x555555667700: file ./../src/PropSetFile.cxx, line 91.
              (gdb) commands
              Type commands for breakpoint(s) 4, one per line.
              End with a line saying just "end".
              >python print( "key {} val {}".format(gdb.parse_and_eval("key"), gdb.parse_and_eval("val")) )
              >c
              >end
              (gdb)


              ... but the output is way too verbose, which is why I want dprintf in the first place:



              (gdb) c
              Continuing.
              Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
              key="XDG_MENU_PREFIX", val="gnome-") at ./../src/PropSetFile.cxx:91
              91 void PropSetFile::Set(std::string_view key, std::string_view val) {
              key "XDG_MENU_PREFIX" val "gnome-"

              Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
              key="KIGITHUB", val="https://github.com/KiCad") at ./../src/PropSetFile.cxx:91
              91 void PropSetFile::Set(std::string_view key, std::string_view val) {
              key "KIGITHUB" val "https://github.com/KiCad"
              ...


              ... so I guess this question is still open...






              share|improve this answer


























                0












                0








                0







                Here is a workaround using gdb python:



                (gdb) break PropSetFile::Set
                Breakpoint 4 at 0x555555667700: file ./../src/PropSetFile.cxx, line 91.
                (gdb) commands
                Type commands for breakpoint(s) 4, one per line.
                End with a line saying just "end".
                >python print( "key {} val {}".format(gdb.parse_and_eval("key"), gdb.parse_and_eval("val")) )
                >c
                >end
                (gdb)


                ... but the output is way too verbose, which is why I want dprintf in the first place:



                (gdb) c
                Continuing.
                Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
                key="XDG_MENU_PREFIX", val="gnome-") at ./../src/PropSetFile.cxx:91
                91 void PropSetFile::Set(std::string_view key, std::string_view val) {
                key "XDG_MENU_PREFIX" val "gnome-"

                Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
                key="KIGITHUB", val="https://github.com/KiCad") at ./../src/PropSetFile.cxx:91
                91 void PropSetFile::Set(std::string_view key, std::string_view val) {
                key "KIGITHUB" val "https://github.com/KiCad"
                ...


                ... so I guess this question is still open...






                share|improve this answer













                Here is a workaround using gdb python:



                (gdb) break PropSetFile::Set
                Breakpoint 4 at 0x555555667700: file ./../src/PropSetFile.cxx, line 91.
                (gdb) commands
                Type commands for breakpoint(s) 4, one per line.
                End with a line saying just "end".
                >python print( "key {} val {}".format(gdb.parse_and_eval("key"), gdb.parse_and_eval("val")) )
                >c
                >end
                (gdb)


                ... but the output is way too verbose, which is why I want dprintf in the first place:



                (gdb) c
                Continuing.
                Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
                key="XDG_MENU_PREFIX", val="gnome-") at ./../src/PropSetFile.cxx:91
                91 void PropSetFile::Set(std::string_view key, std::string_view val) {
                key "XDG_MENU_PREFIX" val "gnome-"

                Thread 1 "SciTE" hit Breakpoint 4, PropSetFile::Set (this=this@entry=0x7fffffffbea0,
                key="KIGITHUB", val="https://github.com/KiCad") at ./../src/PropSetFile.cxx:91
                91 void PropSetFile::Set(std::string_view key, std::string_view val) {
                key "KIGITHUB" val "https://github.com/KiCad"
                ...


                ... so I guess this question is still open...







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 4:50









                sdaausdaau

                20.4k28154207




                20.4k28154207
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016379%2fhow-to-printf-a-stdstring-view-in-gdb%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    Npm cannot find a required file even through it is in the searched directory